page_live_state_decorator.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2019 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_PERFORMANCE_MANAGER_PUBLIC_DECORATORS_PAGE_LIVE_STATE_DECORATOR_H_
  5. #define COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_DECORATORS_PAGE_LIVE_STATE_DECORATOR_H_
  6. #include "base/observer_list.h"
  7. #include "base/observer_list_types.h"
  8. #include "base/sequence_checker.h"
  9. #include "components/performance_manager/public/graph/graph.h"
  10. #include "components/performance_manager/public/graph/node_data_describer.h"
  11. #include "components/performance_manager/public/graph/page_node.h"
  12. namespace content {
  13. class WebContents;
  14. } // namespace content
  15. namespace performance_manager {
  16. class PageNode;
  17. class PageLiveStateObserver;
  18. // Used to record some live state information about the PageNode.
  19. // All the functions that take a WebContents* as a parameter should only be
  20. // called from the UI thread, the event will be forwarded to the corresponding
  21. // PageNode on the Performance Manager's sequence.
  22. class PageLiveStateDecorator
  23. : public GraphOwnedDefaultImpl,
  24. public NodeDataDescriberDefaultImpl {
  25. public:
  26. class Data;
  27. // This object should only be used via its static methods.
  28. PageLiveStateDecorator() = default;
  29. ~PageLiveStateDecorator() override = default;
  30. PageLiveStateDecorator(const PageLiveStateDecorator& other) = delete;
  31. PageLiveStateDecorator& operator=(const PageLiveStateDecorator&) = delete;
  32. // Must be called when the connected to USB device state changes.
  33. static void OnIsConnectedToUSBDeviceChanged(content::WebContents* contents,
  34. bool is_connected_to_usb_device);
  35. // Must be called when the connected to Bluetooth device state changes.
  36. static void OnIsConnectedToBluetoothDeviceChanged(
  37. content::WebContents* contents,
  38. bool is_connected_to_bluetooth_device);
  39. // Functions that should be called by a MediaStreamCaptureIndicator::Observer.
  40. static void OnIsCapturingVideoChanged(content::WebContents* contents,
  41. bool is_capturing_video);
  42. static void OnIsCapturingAudioChanged(content::WebContents* contents,
  43. bool is_capturing_audio);
  44. static void OnIsBeingMirroredChanged(content::WebContents* contents,
  45. bool is_being_mirrored);
  46. static void OnIsCapturingWindowChanged(content::WebContents* contents,
  47. bool is_capturing_window);
  48. static void OnIsCapturingDisplayChanged(content::WebContents* contents,
  49. bool is_capturing_display);
  50. // Set the auto discardable property. This indicates whether or not the page
  51. // can be discarded during an intervention.
  52. static void SetIsAutoDiscardable(content::WebContents* contents,
  53. bool is_auto_discardable);
  54. static void SetWasDiscarded(content::WebContents* contents,
  55. bool was_discarded);
  56. private:
  57. // GraphOwned implementation:
  58. void OnPassedToGraph(Graph* graph) override;
  59. void OnTakenFromGraph(Graph* graph) override;
  60. // NodeDataDescriber implementation:
  61. base::Value DescribePageNodeData(const PageNode* node) const override;
  62. };
  63. class PageLiveStateDecorator::Data {
  64. public:
  65. Data();
  66. virtual ~Data();
  67. Data(const Data& other) = delete;
  68. Data& operator=(const Data&) = delete;
  69. void AddObserver(PageLiveStateObserver* observer);
  70. void RemoveObserver(PageLiveStateObserver* observer);
  71. virtual bool IsConnectedToUSBDevice() const = 0;
  72. virtual bool IsConnectedToBluetoothDevice() const = 0;
  73. virtual bool IsCapturingVideo() const = 0;
  74. virtual bool IsCapturingAudio() const = 0;
  75. virtual bool IsBeingMirrored() const = 0;
  76. virtual bool IsCapturingWindow() const = 0;
  77. virtual bool IsCapturingDisplay() const = 0;
  78. virtual bool IsAutoDiscardable() const = 0;
  79. virtual bool WasDiscarded() const = 0;
  80. static const Data* FromPageNode(const PageNode* page_node);
  81. static Data* GetOrCreateForPageNode(const PageNode* page_node);
  82. virtual void SetIsConnectedToUSBDeviceForTesting(bool value) = 0;
  83. virtual void SetIsConnectedToBluetoothDeviceForTesting(bool value) = 0;
  84. virtual void SetIsCapturingVideoForTesting(bool value) = 0;
  85. virtual void SetIsCapturingAudioForTesting(bool value) = 0;
  86. virtual void SetIsBeingMirroredForTesting(bool value) = 0;
  87. virtual void SetIsCapturingWindowForTesting(bool value) = 0;
  88. virtual void SetIsCapturingDisplayForTesting(bool value) = 0;
  89. virtual void SetIsAutoDiscardableForTesting(bool value) = 0;
  90. virtual void SetWasDiscardedForTesting(bool value) = 0;
  91. protected:
  92. base::ObserverList<PageLiveStateObserver> observers_
  93. GUARDED_BY_CONTEXT(sequence_checker_);
  94. SEQUENCE_CHECKER(sequence_checker_);
  95. };
  96. class PageLiveStateObserver : public base::CheckedObserver {
  97. public:
  98. PageLiveStateObserver();
  99. ~PageLiveStateObserver() override;
  100. PageLiveStateObserver(const PageLiveStateObserver& other) = delete;
  101. PageLiveStateObserver& operator=(const PageLiveStateObserver&) = delete;
  102. virtual void OnIsConnectedToUSBDeviceChanged(const PageNode* page_node) = 0;
  103. virtual void OnIsConnectedToBluetoothDeviceChanged(
  104. const PageNode* page_node) = 0;
  105. virtual void OnIsCapturingVideoChanged(const PageNode* page_node) = 0;
  106. virtual void OnIsCapturingAudioChanged(const PageNode* page_node) = 0;
  107. virtual void OnIsBeingMirroredChanged(const PageNode* page_node) = 0;
  108. virtual void OnIsCapturingWindowChanged(const PageNode* page_node) = 0;
  109. virtual void OnIsCapturingDisplayChanged(const PageNode* page_node) = 0;
  110. virtual void OnIsAutoDiscardableChanged(const PageNode* page_node) = 0;
  111. virtual void OnWasDiscardedChanged(const PageNode* page_node) = 0;
  112. };
  113. } // namespace performance_manager
  114. #endif // COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_DECORATORS_PAGE_LIVE_STATE_DECORATOR_H_