Backed out 7 changesets (bug 1942424) for causing frequent crashes. a=backout
[gecko.git] / dom / media / mediacontrol / MediaControlKeySource.h
blobf05448ca898b30a4b87da0cd95bd68813ad65108
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/. */
5 #ifndef DOM_MEDIA_MEDIACONTROL_MEDIACONTROLKEYSOURCE_H_
6 #define DOM_MEDIA_MEDIACONTROL_MEDIACONTROLKEYSOURCE_H_
8 #include "mozilla/Maybe.h"
9 #include "mozilla/dom/MediaControllerBinding.h"
10 #include "mozilla/dom/MediaMetadata.h"
11 #include "mozilla/dom/MediaSession.h"
12 #include "mozilla/dom/MediaSessionBinding.h"
13 #include "nsISupportsImpl.h"
14 #include "nsTArray.h"
16 namespace mozilla::dom {
18 // This is used to store seek related properties from MediaSessionActionDetails.
19 // https://w3c.github.io/mediasession/#the-mediasessionactiondetails-dictionary
20 struct AbsoluteSeek {
21 double mSeekTime;
22 bool mFastSeek;
24 struct SeekDetails {
25 Maybe<AbsoluteSeek> mAbsolute;
26 Maybe<double> mRelativeSeekOffset;
28 SeekDetails() = default;
29 SeekDetails(double aSeekTime, bool aFastSeek)
30 : mAbsolute(Some(AbsoluteSeek{aSeekTime, aFastSeek})) {}
31 explicit SeekDetails(double aRelativeSeekOffset)
32 : mRelativeSeekOffset(Some(aRelativeSeekOffset)) {}
35 struct MediaControlAction {
36 MediaControlAction() = default;
37 explicit MediaControlAction(MediaControlKey aKey) : mKey(Some(aKey)) {}
38 MediaControlAction(MediaControlKey aKey, const SeekDetails& aDetails)
39 : mKey(Some(aKey)), mDetails(Some(aDetails)) {}
40 Maybe<MediaControlKey> mKey;
41 Maybe<SeekDetails> mDetails;
44 /**
45 * MediaControlKeyListener is a pure interface, which is used to monitor
46 * MediaControlKey, we can add it onto the MediaControlKeySource,
47 * and then everytime when the media key events occur, `OnActionPerformed` will
48 * be called so that we can do related handling.
50 class MediaControlKeyListener {
51 public:
52 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
53 MediaControlKeyListener() = default;
55 virtual void OnActionPerformed(const MediaControlAction& aAction) = 0;
57 protected:
58 virtual ~MediaControlKeyListener() = default;
61 /**
62 * MediaControlKeyHandler is used to operate media controller by corresponding
63 * received media control key events.
65 class MediaControlKeyHandler final : public MediaControlKeyListener {
66 public:
67 NS_INLINE_DECL_REFCOUNTING(MediaControlKeyHandler, override)
68 void OnActionPerformed(const MediaControlAction& aAction) override;
70 private:
71 virtual ~MediaControlKeyHandler() = default;
74 /**
75 * MediaControlKeySource is an abstract class which is used to implement
76 * transporting media control keys event to all its listeners when media keys
77 * event happens.
79 class MediaControlKeySource {
80 public:
81 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
82 MediaControlKeySource();
84 using MediaKeysArray = nsTArray<MediaControlKey>;
86 virtual void AddListener(MediaControlKeyListener* aListener);
87 virtual void RemoveListener(MediaControlKeyListener* aListener);
88 size_t GetListenersNum() const;
90 // Return true if the initialization of the source succeeds, and inherited
91 // sources should implement this method to handle the initialization fails.
92 virtual bool Open() = 0;
93 virtual void Close();
94 virtual bool IsOpened() const = 0;
96 /**
97 * All following `SetXXX()` functions are used to update the playback related
98 * properties change from a specific tab, which can represent the playback
99 * status for Firefox instance. Even if we have multiple tabs playing media at
100 * the same time, we would only update information from one of that tabs that
101 * would be done by `MediaControlService`.
103 virtual void SetPlaybackState(MediaSessionPlaybackState aState);
104 virtual MediaSessionPlaybackState GetPlaybackState() const;
106 // Override this method if the event source needs to handle the metadata.
107 virtual void SetMediaMetadata(const MediaMetadataBase& aMetadata) {}
109 // Set the supported media keys which the event source can use to determine
110 // what kinds of buttons should be shown on the UI.
111 virtual void SetSupportedMediaKeys(const MediaKeysArray& aSupportedKeys) = 0;
113 // Override these methods if the inherited key source want to know the change
114 // for following attributes. For example, GeckoView would use these methods
115 // to notify change to the embedded application.
116 virtual void SetEnableFullScreen(bool aIsEnabled) {};
117 virtual void SetEnablePictureInPictureMode(bool aIsEnabled) {};
118 virtual void SetPositionState(const Maybe<PositionState>& aState) {};
120 protected:
121 virtual ~MediaControlKeySource() = default;
122 nsTArray<RefPtr<MediaControlKeyListener>> mListeners;
123 MediaSessionPlaybackState mPlaybackState;
126 } // namespace mozilla::dom
128 #endif