summaryrefslogtreecommitdiffstats
path: root/dom/media/mediasession/MediaSession.h
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
commit0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d (patch)
treea31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /dom/media/mediasession/MediaSession.h
parentInitial commit. (diff)
downloadfirefox-esr-37a0381f8351b370577b65028ba1f6563ae23fdf.tar.xz
firefox-esr-37a0381f8351b370577b65028ba1f6563ae23fdf.zip
Adding upstream version 115.8.0esr.upstream/115.8.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/media/mediasession/MediaSession.h')
-rw-r--r--dom/media/mediasession/MediaSession.h132
1 files changed, 132 insertions, 0 deletions
diff --git a/dom/media/mediasession/MediaSession.h b/dom/media/mediasession/MediaSession.h
new file mode 100644
index 0000000000..db6864c842
--- /dev/null
+++ b/dom/media/mediasession/MediaSession.h
@@ -0,0 +1,132 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim:set ts=2 sw=2 sts=2 et cindent: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_dom_MediaSession_h
+#define mozilla_dom_MediaSession_h
+
+#include "js/TypeDecls.h"
+#include "mozilla/Attributes.h"
+#include "mozilla/dom/MediaSessionBinding.h"
+#include "mozilla/EnumeratedArray.h"
+#include "nsCycleCollectionParticipant.h"
+#include "nsIDocumentActivity.h"
+#include "nsWrapperCache.h"
+
+class nsPIDOMWindowInner;
+
+namespace mozilla {
+class ErrorResult;
+
+namespace dom {
+
+class Document;
+class MediaMetadata;
+
+// https://w3c.github.io/mediasession/#position-state
+struct PositionState {
+ PositionState() = default;
+ PositionState(double aDuration, double aPlaybackRate,
+ double aLastReportedTime)
+ : mDuration(aDuration),
+ mPlaybackRate(aPlaybackRate),
+ mLastReportedPlaybackPosition(aLastReportedTime) {}
+ double mDuration;
+ double mPlaybackRate;
+ double mLastReportedPlaybackPosition;
+};
+
+class MediaSession final : public nsIDocumentActivity, public nsWrapperCache {
+ public:
+ // Ref counting and cycle collection
+ NS_DECL_CYCLE_COLLECTING_ISUPPORTS
+ NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(MediaSession)
+ NS_DECL_NSIDOCUMENTACTIVITY
+
+ explicit MediaSession(nsPIDOMWindowInner* aParent);
+
+ // WebIDL methods
+ nsPIDOMWindowInner* GetParentObject() const;
+
+ JSObject* WrapObject(JSContext* aCx,
+ JS::Handle<JSObject*> aGivenProto) override;
+
+ MediaMetadata* GetMetadata() const;
+
+ void SetMetadata(MediaMetadata* aMetadata);
+
+ void SetPlaybackState(const MediaSessionPlaybackState& aPlaybackState);
+
+ MediaSessionPlaybackState PlaybackState() const;
+
+ void SetActionHandler(MediaSessionAction aAction,
+ MediaSessionActionHandler* aHandler);
+
+ void SetPositionState(const MediaPositionState& aState, ErrorResult& aRv);
+
+ bool IsSupportedAction(MediaSessionAction aAction) const;
+
+ // Use this method to trigger media session action handler asynchronously.
+ void NotifyHandler(const MediaSessionActionDetails& aDetails);
+
+ void Shutdown();
+
+ // `MediaStatusManager` would determine which media session is an active media
+ // session and update it from the chrome process. This active session is not
+ // 100% equal to the active media session in the spec, which is a globally
+ // active media session *among all tabs*. The active session here is *among
+ // different windows but in same tab*, so each tab can have at most one
+ // active media session.
+ bool IsActive() const;
+
+ private:
+ // When the document which media session belongs to is going to be destroyed,
+ // or is in the bfcache, then the session would be inactive. Otherwise, it's
+ // active all the time.
+ enum class SessionDocStatus : bool {
+ eInactive = false,
+ eActive = true,
+ };
+ void SetMediaSessionDocStatus(SessionDocStatus aState);
+
+ // These methods are used to propagate media session's status to the chrome
+ // process.
+ void NotifyMediaSessionDocStatus(SessionDocStatus aState);
+ void NotifyMediaSessionAttributes();
+ void NotifyPlaybackStateUpdated();
+ void NotifyMetadataUpdated();
+ void NotifyEnableSupportedAction(MediaSessionAction aAction);
+ void NotifyDisableSupportedAction(MediaSessionAction aAction);
+ void NotifyPositionStateChanged();
+
+ void DispatchNotifyHandler(const MediaSessionActionDetails& aDetails);
+
+ MediaSessionActionHandler* GetActionHandler(MediaSessionAction aAction) const;
+
+ ~MediaSession() = default;
+
+ nsCOMPtr<nsPIDOMWindowInner> mParent;
+
+ RefPtr<MediaMetadata> mMediaMetadata;
+
+ EnumeratedArray<MediaSessionAction, MediaSessionAction::EndGuard_,
+ RefPtr<MediaSessionActionHandler>>
+ mActionHandlers;
+
+ // This is used as is a hint for the user agent to determine whether the
+ // browsing context is playing or paused.
+ // https://w3c.github.io/mediasession/#declared-playback-state
+ MediaSessionPlaybackState mDeclaredPlaybackState =
+ MediaSessionPlaybackState::None;
+
+ Maybe<PositionState> mPositionState;
+ RefPtr<Document> mDoc;
+ SessionDocStatus mSessionDocState = SessionDocStatus::eInactive;
+};
+
+} // namespace dom
+} // namespace mozilla
+
+#endif // mozilla_dom_MediaSession_h