author Morgan Reschenberg <>
Tue, 18 Jan 2022 23:31:21 +0000
changeset 605085 4c684d5d16fe3a85614e7cb56be8c0029dc98b12
parent 569652 af4737be6643caadf09c93886620bceedeef0452
permissions -rw-r--r--
Bug 1748878: Use LayoutDeviceInt types for points, rects, sizes in dev pixels r=Jamie,emilio a=graft Differential Revision:

/* -*- 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 */

#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;

struct PositionState {
  PositionState() = default;
  PositionState(double aDuration, double aPlaybackRate,
                double aLastReportedTime)
      : mDuration(aDuration),
        mLastReportedPlaybackPosition(aLastReportedTime) {}
  double mDuration;
  double mPlaybackRate;
  double mLastReportedPlaybackPosition;

class MediaSession final : public nsIDocumentActivity, public nsWrapperCache {
  // Ref counting and cycle collection

  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;

  // 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_,

  // This is used as is a hint for the user agent to determine whether the
  // browsing context is playing or paused.
  MediaSessionPlaybackState mDeclaredPlaybackState =

  Maybe<PositionState> mPositionState;
  RefPtr<Document> mDoc;
  SessionDocStatus mSessionDocState = SessionDocStatus::eInactive;

}  // namespace dom
}  // namespace mozilla

#endif  // mozilla_dom_MediaSession_h