scoped_cg_window_id.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #ifndef COMPONENTS_REMOTE_COCOA_BROWSER_SCOPED_CG_WINDOW_ID_H_
  5. #define COMPONENTS_REMOTE_COCOA_BROWSER_SCOPED_CG_WINDOW_ID_H_
  6. #include <stdint.h>
  7. #include "base/memory/weak_ptr.h"
  8. #include "base/observer_list.h"
  9. #include "base/threading/thread_checker.h"
  10. #include "components/remote_cocoa/browser/remote_cocoa_browser_export.h"
  11. #include "components/viz/common/surfaces/frame_sink_id.h"
  12. #include "ui/gfx/geometry/point_f.h"
  13. #include "ui/gfx/geometry/size.h"
  14. namespace remote_cocoa {
  15. // A registry of the CGWindowIDs for the remote cocoa windows created in any
  16. // process (the browser and also the renderer). This can be used to look up
  17. // the compositing frame sink id for efficient video capture. This class may
  18. // only be accessed on the UI thread.
  19. class REMOTE_COCOA_BROWSER_EXPORT ScopedCGWindowID final {
  20. public:
  21. class Observer : public base::CheckedObserver {
  22. public:
  23. // This is invoked in the ScopedCGWindowID destructor, after all of its
  24. // weak pointers have been destroyed.
  25. virtual void OnScopedCGWindowIDDestroyed(uint32_t cg_window_id) {}
  26. virtual void OnScopedCGWindowIDMouseMoved(
  27. uint32_t cg_window_id,
  28. const gfx::PointF& location_in_window_dips,
  29. const gfx::Size& window_size_dips) {}
  30. };
  31. ScopedCGWindowID(uint32_t cg_window_id,
  32. const viz::FrameSinkId& frame_sink_id);
  33. ~ScopedCGWindowID();
  34. ScopedCGWindowID(const ScopedCGWindowID&) = delete;
  35. ScopedCGWindowID& operator=(const ScopedCGWindowID&) = delete;
  36. // Add and remove an observer. This class will automatically remove all
  37. // observers after invalidating its weak pointers during its destruction.
  38. void AddObserver(Observer* observer);
  39. void RemoveObserver(Observer* observer);
  40. // Inform frame sink capturers of where the mouse is, so that they can draw
  41. // a cursor.
  42. void OnMouseMoved(const gfx::PointF& location_in_window_dips,
  43. const gfx::Size& window_size_dips);
  44. // Query the frame sink id for this window, which can be used for optimized
  45. // video capture.
  46. const viz::FrameSinkId& GetFrameSinkId() const { return frame_sink_id_; }
  47. // Look up this structure corresponding to a given CGWindowID. The returned
  48. // weak pointer may only be accessed on the UI thread, and will be invalidated
  49. // before it calls OnScopedCGWindowIDDestroying on its observers.
  50. static base::WeakPtr<ScopedCGWindowID> Get(uint32_t cg_window_id);
  51. private:
  52. THREAD_CHECKER(thread_checker_);
  53. const uint32_t cg_window_id_;
  54. const viz::FrameSinkId frame_sink_id_;
  55. base::ObserverList<Observer> observer_list_;
  56. base::WeakPtrFactory<ScopedCGWindowID> weak_factory_;
  57. };
  58. } // namespace remote_cocoa
  59. #endif // COMPONENTS_REMOTE_COCOA_BROWSER_SCOPED_CG_WINDOW_ID_H_