1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_MediaSession_h
8 #define mozilla_dom_MediaSession_h
10 #include "js/TypeDecls.h"
11 #include "mozilla/Attributes.h"
12 #include "mozilla/dom/MediaSessionBinding.h"
13 #include "mozilla/EnumeratedArray.h"
14 #include "nsCycleCollectionParticipant.h"
15 #include "nsIDocumentActivity.h"
16 #include "nsWrapperCache.h"
18 class nsPIDOMWindowInner
;
28 // https://w3c.github.io/mediasession/#position-state
29 struct PositionState
{
30 PositionState() = default;
31 PositionState(double aDuration
, double aPlaybackRate
,
32 double aLastReportedTime
)
33 : mDuration(aDuration
),
34 mPlaybackRate(aPlaybackRate
),
35 mLastReportedPlaybackPosition(aLastReportedTime
) {}
38 double mLastReportedPlaybackPosition
;
41 class MediaSession final
: public nsIDocumentActivity
, public nsWrapperCache
{
43 // Ref counting and cycle collection
44 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
45 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(MediaSession
)
46 NS_DECL_NSIDOCUMENTACTIVITY
48 explicit MediaSession(nsPIDOMWindowInner
* aParent
);
51 nsPIDOMWindowInner
* GetParentObject() const;
53 JSObject
* WrapObject(JSContext
* aCx
,
54 JS::Handle
<JSObject
*> aGivenProto
) override
;
56 MediaMetadata
* GetMetadata() const;
58 void SetMetadata(MediaMetadata
* aMetadata
);
60 void SetPlaybackState(const MediaSessionPlaybackState
& aPlaybackState
);
62 MediaSessionPlaybackState
PlaybackState() const;
64 void SetActionHandler(MediaSessionAction aAction
,
65 MediaSessionActionHandler
* aHandler
);
67 void SetPositionState(const MediaPositionState
& aState
, ErrorResult
& aRv
);
69 bool IsSupportedAction(MediaSessionAction aAction
) const;
71 // Use this method to trigger media session action handler asynchronously.
72 void NotifyHandler(const MediaSessionActionDetails
& aDetails
);
76 // `MediaStatusManager` would determine which media session is an active media
77 // session and update it from the chrome process. This active session is not
78 // 100% equal to the active media session in the spec, which is a globally
79 // active media session *among all tabs*. The active session here is *among
80 // different windows but in same tab*, so each tab can have at most one
81 // active media session.
82 bool IsActive() const;
85 // When the document which media session belongs to is going to be destroyed,
86 // or is in the bfcache, then the session would be inactive. Otherwise, it's
87 // active all the time.
88 enum class SessionDocStatus
: bool {
92 void SetMediaSessionDocStatus(SessionDocStatus aState
);
94 // These methods are used to propagate media session's status to the chrome
96 void NotifyMediaSessionDocStatus(SessionDocStatus aState
);
97 void NotifyMediaSessionAttributes();
98 void NotifyPlaybackStateUpdated();
99 void NotifyMetadataUpdated();
100 void NotifyEnableSupportedAction(MediaSessionAction aAction
);
101 void NotifyDisableSupportedAction(MediaSessionAction aAction
);
102 void NotifyPositionStateChanged();
104 void DispatchNotifyHandler(const MediaSessionActionDetails
& aDetails
);
106 MediaSessionActionHandler
* GetActionHandler(MediaSessionAction aAction
) const;
108 ~MediaSession() = default;
110 nsCOMPtr
<nsPIDOMWindowInner
> mParent
;
112 RefPtr
<MediaMetadata
> mMediaMetadata
;
114 EnumeratedArray
<MediaSessionAction
, MediaSessionAction::EndGuard_
,
115 RefPtr
<MediaSessionActionHandler
>>
118 // This is used as is a hint for the user agent to determine whether the
119 // browsing context is playing or paused.
120 // https://w3c.github.io/mediasession/#declared-playback-state
121 MediaSessionPlaybackState mDeclaredPlaybackState
=
122 MediaSessionPlaybackState::None
;
124 Maybe
<PositionState
> mPositionState
;
125 RefPtr
<Document
> mDoc
;
126 SessionDocStatus mSessionDocState
= SessionDocStatus::eInactive
;
130 } // namespace mozilla
132 #endif // mozilla_dom_MediaSession_h