1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66

content / browser / media / desktop_media_window_registry_aura.cc [blame]

// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/containers/id_map.h"
#include "base/no_destructor.h"
#include "content/browser/media/desktop_media_window_registry.h"
#include "ui/aura/window.h"
#include "ui/aura/window_observer.h"

namespace content {

class DesktopMediaWindowRegistryAura final : public DesktopMediaWindowRegistry,
                                             public aura::WindowObserver {
 public:
  static DesktopMediaWindowRegistryAura* GetInstance() {
    static base::NoDestructor<DesktopMediaWindowRegistryAura> instance;
    return instance.get();
  }

  DesktopMediaWindowRegistryAura() = default;

  DesktopMediaWindowRegistryAura(const DesktopMediaWindowRegistryAura&) =
      delete;
  DesktopMediaWindowRegistryAura& operator=(
      const DesktopMediaWindowRegistryAura&) = delete;

  DesktopMediaID::Id RegisterWindow(gfx::NativeWindow window) final {
    base::IDMap<aura::Window*>::const_iterator it(®istered_windows_);
    for (; !it.IsAtEnd(); it.Advance()) {
      if (it.GetCurrentValue() == window)
        return it.GetCurrentKey();
    }

    window->AddObserver(this);
    return registered_windows_.Add(window);
  }

  gfx::NativeWindow GetWindowById(DesktopMediaID::Id id) final {
    return registered_windows_.Lookup(id);
  }

 private:
  ~DesktopMediaWindowRegistryAura() final = default;

  // WindowObserver overrides.
  void OnWindowDestroying(aura::Window* window) final {
    base::IDMap<aura::Window*>::iterator it(®istered_windows_);
    for (; !it.IsAtEnd(); it.Advance()) {
      if (it.GetCurrentValue() == window) {
        registered_windows_.Remove(it.GetCurrentKey());
        return;
      }
    }
    NOTREACHED_IN_MIGRATION();
  }

  base::IDMap<aura::Window*> registered_windows_;
};

// static
DesktopMediaWindowRegistry* DesktopMediaWindowRegistry::GetInstance() {
  return DesktopMediaWindowRegistryAura::GetInstance();
}

}  // namespace content