| // Copyright 2019 The Chromium Authors. All rights reserved. |
| // 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 : public DesktopMediaWindowRegistry, |
| public aura::WindowObserver { |
| public: |
| static DesktopMediaWindowRegistryAura* GetInstance() { |
| static base::NoDestructor<DesktopMediaWindowRegistryAura> instance; |
| return instance.get(); |
| } |
| |
| DesktopMediaWindowRegistryAura() = default; |
| |
| 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(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(); |
| } |
| |
| base::IDMap<aura::Window*> registered_windows_; |
| |
| DISALLOW_COPY_AND_ASSIGN(DesktopMediaWindowRegistryAura); |
| }; |
| |
| // static |
| DesktopMediaWindowRegistry* DesktopMediaWindowRegistry::GetInstance() { |
| return DesktopMediaWindowRegistryAura::GetInstance(); |
| } |
| |
| } // namespace content |