scoped_cg_window_id.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/remote_cocoa/browser/scoped_cg_window_id.h"
  5. #include <map>
  6. #include "base/no_destructor.h"
  7. namespace remote_cocoa {
  8. namespace {
  9. using ScoperMap = std::map<uint32_t, ScopedCGWindowID*>;
  10. ScoperMap& GetMap() {
  11. static base::NoDestructor<ScoperMap> map;
  12. return *map.get();
  13. }
  14. } // namespace
  15. ScopedCGWindowID::ScopedCGWindowID(uint32_t cg_window_id,
  16. const viz::FrameSinkId& frame_sink_id)
  17. : cg_window_id_(cg_window_id),
  18. frame_sink_id_(frame_sink_id),
  19. weak_factory_(this) {
  20. DCHECK_EQ(GetMap().count(cg_window_id), 0u);
  21. GetMap()[cg_window_id] = this;
  22. }
  23. ScopedCGWindowID::~ScopedCGWindowID() {
  24. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  25. weak_factory_.InvalidateWeakPtrs();
  26. auto found = GetMap().find(cg_window_id_);
  27. DCHECK_EQ(found->second, this);
  28. GetMap().erase(found);
  29. for (auto& observer : observer_list_)
  30. observer.OnScopedCGWindowIDDestroyed(cg_window_id_);
  31. observer_list_.Clear();
  32. }
  33. void ScopedCGWindowID::AddObserver(Observer* observer) {
  34. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  35. observer_list_.AddObserver(observer);
  36. }
  37. void ScopedCGWindowID::RemoveObserver(Observer* observer) {
  38. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  39. observer_list_.RemoveObserver(observer);
  40. }
  41. void ScopedCGWindowID::OnMouseMoved(const gfx::PointF& location_in_window,
  42. const gfx::Size& window_size) {
  43. for (auto& observer : observer_list_) {
  44. observer.OnScopedCGWindowIDMouseMoved(cg_window_id_, location_in_window,
  45. window_size);
  46. }
  47. }
  48. // static
  49. base::WeakPtr<ScopedCGWindowID> ScopedCGWindowID::Get(uint32_t cg_window_id) {
  50. auto found = GetMap().find(cg_window_id);
  51. if (found == GetMap().end())
  52. return nullptr;
  53. DCHECK_CALLED_ON_VALID_THREAD(found->second->thread_checker_);
  54. return found->second->weak_factory_.GetWeakPtr();
  55. }
  56. } // namespace remote_cocoa