ContentMediaController.h (4953B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #ifndef DOM_MEDIA_MEDIACONTROL_CONTENTMEDIACONTROLLER_H_ 6 #define DOM_MEDIA_MEDIACONTROL_CONTENTMEDIACONTROLLER_H_ 7 8 #include "MediaControlKeySource.h" 9 #include "MediaStatusManager.h" 10 11 namespace mozilla::dom { 12 13 class BrowsingContext; 14 15 /** 16 * ContentMediaControlKeyReceiver is an interface which is used to receive media 17 * control key sent from the chrome process. 18 */ 19 class ContentMediaControlKeyReceiver { 20 public: 21 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING 22 23 // Return nullptr if the top level browsing context is no longer alive. 24 static ContentMediaControlKeyReceiver* Get(BrowsingContext* aBC); 25 26 // Use this method to handle the event from `ContentMediaAgent`. 27 virtual void HandleMediaKey(MediaControlKey aKey, 28 Maybe<SeekDetails> aDetails = Nothing()) = 0; 29 30 virtual bool IsPlaying() const = 0; 31 }; 32 33 /** 34 * ContentMediaAgent is an interface which we use to (1) propoagate media 35 * related information from the content process to the chrome process (2) act an 36 * event source to dispatch media control key to its listeners. 37 * 38 * If the media would like to know the media control key, then media MUST 39 * inherit from ContentMediaControlKeyReceiver, and register themselves to 40 * ContentMediaAgent. Whenever media control key delivers, ContentMediaAgent 41 * would notify all its receivers. In addition, whenever controlled media 42 * changes its playback status or audible state, they should update their status 43 * update via ContentMediaAgent. 44 */ 45 class ContentMediaAgent : public IMediaInfoUpdater { 46 public: 47 // Return nullptr if the top level browsing context is no longer alive. 48 static ContentMediaAgent* Get(BrowsingContext* aBC); 49 50 // IMediaInfoUpdater Methods 51 void NotifyMediaPlaybackChanged(uint64_t aBrowsingContextId, 52 MediaPlaybackState aState) override; 53 void NotifyMediaAudibleChanged(uint64_t aBrowsingContextId, 54 MediaAudibleState aState) override; 55 void SetIsInPictureInPictureMode(uint64_t aBrowsingContextId, 56 bool aIsInPictureInPictureMode) override; 57 void SetDeclaredPlaybackState(uint64_t aBrowsingContextId, 58 MediaSessionPlaybackState aState) override; 59 void NotifySessionCreated(uint64_t aBrowsingContextId) override; 60 void NotifySessionDestroyed(uint64_t aBrowsingContextId) override; 61 void UpdateMetadata(uint64_t aBrowsingContextId, 62 const Maybe<MediaMetadataBase>& aMetadata) override; 63 void EnableAction(uint64_t aBrowsingContextId, 64 MediaSessionAction aAction) override; 65 void DisableAction(uint64_t aBrowsingContextId, 66 MediaSessionAction aAction) override; 67 void NotifyMediaFullScreenState(uint64_t aBrowsingContextId, 68 bool aIsInFullScreen) override; 69 void UpdatePositionState(uint64_t aBrowsingContextId, 70 const Maybe<PositionState>& aState) override; 71 void UpdateGuessedPositionState(uint64_t aBrowsingContextId, 72 const nsID& aMediaId, 73 const Maybe<PositionState>& aState) override; 74 75 // Use these methods to register/unregister `ContentMediaControlKeyReceiver` 76 // in order to listen to media control key events. 77 virtual void AddReceiver(ContentMediaControlKeyReceiver* aReceiver) = 0; 78 virtual void RemoveReceiver(ContentMediaControlKeyReceiver* aReceiver) = 0; 79 }; 80 81 /** 82 * ContentMediaController exists in per inner window, which has a responsibility 83 * to update the content media state to MediaController (ContentMediaAgent) and 84 * delivers MediaControlKey to its receiver in order to control media in the 85 * content page (ContentMediaControlKeyReceiver). 86 */ 87 class ContentMediaController final : public ContentMediaAgent, 88 public ContentMediaControlKeyReceiver { 89 public: 90 NS_INLINE_DECL_REFCOUNTING(ContentMediaController, override) 91 92 explicit ContentMediaController(uint64_t aId); 93 // ContentMediaAgent methods 94 void AddReceiver(ContentMediaControlKeyReceiver* aListener) override; 95 void RemoveReceiver(ContentMediaControlKeyReceiver* aListener) override; 96 97 // ContentMediaControlKeyReceiver method 98 void HandleMediaKey(MediaControlKey aKey, 99 Maybe<SeekDetails> aDetails = Nothing()) override; 100 101 private: 102 ~ContentMediaController() = default; 103 104 // We don't need this method, so make it as private and simply return false. 105 virtual bool IsPlaying() const override { return false; } 106 107 void PauseOrStopMedia(); 108 109 nsTArray<RefPtr<ContentMediaControlKeyReceiver>> mReceivers; 110 }; 111 112 } // namespace mozilla::dom 113 114 #endif // DOM_MEDIA_MEDIACONTROL_CONTENTMEDIACONTROLLER_H_