Bug 1624711 - Traverse and Unlink MediaSession::mActionHandlers r=alwu
authorNika Layzell <nika@thelayzells.com>
Tue, 24 Mar 2020 21:20:26 +0000
changeset 520283 dea5a9179f87dcf9347ea9fa5770ad2117bcddc0
parent 520282 0ee82dc106f20c4f207fa33f84e910566b29fa79
child 520284 ca14cd1c0e0b131b1dbd3045f6226c8e08bbab80
push id37246
push useropoprus@mozilla.com
push dateWed, 25 Mar 2020 03:40:33 +0000
treeherdermozilla-central@14b59d4adc95 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersalwu
bugs1624711
milestone76.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1624711 - Traverse and Unlink MediaSession::mActionHandlers r=alwu Differential Revision: https://phabricator.services.mozilla.com/D68091
dom/media/mediasession/MediaSession.cpp
dom/media/mediasession/MediaSession.h
--- a/dom/media/mediasession/MediaSession.cpp
+++ b/dom/media/mediasession/MediaSession.cpp
@@ -1,23 +1,25 @@
 /* -*- 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/. */
 
 #include "mozilla/dom/MediaSession.h"
+#include "mozilla/EnumeratedArrayCycleCollection.h"
 
 #include "MediaSessionUtils.h"
 
 namespace mozilla {
 namespace dom {
 
 // Only needed for refcounted objects.
-NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MediaSession, mParent, mMediaMetadata)
+NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MediaSession, mParent, mMediaMetadata,
+                                      mActionHandlers)
 NS_IMPL_CYCLE_COLLECTING_ADDREF(MediaSession)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(MediaSession)
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaSession)
   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
   NS_INTERFACE_MAP_ENTRY(nsISupports)
 NS_INTERFACE_MAP_END
 
 MediaSession::MediaSession(nsPIDOMWindowInner* aParent) : mParent(aParent) {
@@ -64,23 +66,24 @@ void MediaSession::SetPlaybackState(
 }
 
 MediaSessionPlaybackState MediaSession::PlaybackState() const {
   return mDeclaredPlaybackState;
 }
 
 void MediaSession::SetActionHandler(MediaSessionAction aAction,
                                     MediaSessionActionHandler* aHandler) {
-  size_t index = static_cast<size_t>(aAction);
-  mActionHandlers[index] = aHandler;
+  MOZ_ASSERT(size_t(aAction) < ArrayLength(mActionHandlers));
+  mActionHandlers[aAction] = aHandler;
 }
 
 MediaSessionActionHandler* MediaSession::GetActionHandler(
     MediaSessionAction aAction) const {
-  return mActionHandlers[static_cast<size_t>(aAction)];
+  MOZ_ASSERT(size_t(aAction) < ArrayLength(mActionHandlers));
+  return mActionHandlers[aAction];
 }
 
 void MediaSession::SetPositionState(const MediaPositionState& aState,
                                     ErrorResult& aRv) {
   // https://w3c.github.io/mediasession/#dom-mediasession-setpositionstate
   // If the state is an empty dictionary then clear the position state.
   if (!aState.IsAnyMemberPresent()) {
     mPositionState.reset();
@@ -160,19 +163,18 @@ void MediaSession::DispatchNotifyHandler
     MediaSessionActionDetails mDetails;
   };
 
   RefPtr<nsIRunnable> runnable = new Runnable(this, aDetails);
   NS_DispatchToMainThread(runnable);
 }
 
 bool MediaSession::IsSupportedAction(MediaSessionAction aAction) const {
-  size_t index = static_cast<size_t>(aAction);
-  MOZ_ASSERT(index < ACTIONS);
-  return mActionHandlers[index] != nullptr;
+  MOZ_ASSERT(size_t(aAction) < ArrayLength(mActionHandlers));
+  return mActionHandlers[aAction] != nullptr;
 }
 
 void MediaSession::Shutdown() {
   NotifyMediaSessionStatus(SessionStatus::eDestroyed);
 }
 
 void MediaSession::NotifyMediaSessionStatus(SessionStatus aState) {
   RefPtr<BrowsingContext> currentBC = GetParentObject()->GetBrowsingContext();
--- a/dom/media/mediasession/MediaSession.h
+++ b/dom/media/mediasession/MediaSession.h
@@ -8,16 +8,17 @@
 #define mozilla_dom_MediaSession_h
 
 #include "js/TypeDecls.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/dom/BindingDeclarations.h"
 #include "mozilla/dom/MediaMetadata.h"
 #include "mozilla/dom/MediaSessionBinding.h"
 #include "mozilla/ErrorResult.h"
+#include "mozilla/EnumeratedArray.h"
 #include "nsCycleCollectionParticipant.h"
 #include "nsWrapperCache.h"
 
 class nsPIDOMWindowInner;
 
 namespace mozilla {
 namespace dom {
 
@@ -83,18 +84,20 @@ class MediaSession final : public nsISup
 
   MediaSessionActionHandler* GetActionHandler(MediaSessionAction aAction) const;
 
   ~MediaSession() = default;
 
   nsCOMPtr<nsPIDOMWindowInner> mParent;
 
   RefPtr<MediaMetadata> mMediaMetadata;
-  static const size_t ACTIONS = MediaSessionActionValues::Count;
-  RefPtr<MediaSessionActionHandler> mActionHandlers[ACTIONS] = {nullptr};
+
+  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;