From d2cf2fa5cca5f66c2a92455af5133b9256b9bca2 Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Fri, 26 Feb 2021 11:36:42 +0000 Subject: [PATCH] Bug 1693541 - Improve uses of nsBaseHashtable and descendants and avoid multiple subsequent lookups in dom/media. r=bryce Differential Revision: https://phabricator.services.mozilla.com/D106095 --- dom/media/gmp/GMPServiceChild.cpp | 21 ++++------ dom/media/gmp/GMPServiceParent.cpp | 8 +--- .../mediacontrol/MediaPlaybackStatus.cpp | 7 +--- dom/media/mediacontrol/MediaStatusManager.cpp | 39 +++++++++++-------- .../agnostic/eme/EMEDecoderModule.cpp | 6 +-- .../MediaSystemResourceManager.cpp | 4 +- 6 files changed, 38 insertions(+), 47 deletions(-) diff --git a/dom/media/gmp/GMPServiceChild.cpp b/dom/media/gmp/GMPServiceChild.cpp index ca3fdff07562..6260c1f90359 100644 --- a/dom/media/gmp/GMPServiceChild.cpp +++ b/dom/media/gmp/GMPServiceChild.cpp @@ -473,23 +473,16 @@ void GeckoMediaPluginServiceChild::RemoveShutdownBlockerIfNeeded() { already_AddRefed GMPServiceChild::GetBridgedGMPContentParent( ProcessId aOtherPid, ipc::Endpoint&& endpoint) { - RefPtr parent; - mContentParents.Get(aOtherPid, getter_AddRefs(parent)); + return do_AddRef(mContentParents.LookupOrInsertWith(aOtherPid, [&] { + MOZ_ASSERT(aOtherPid == endpoint.OtherPid()); - if (parent) { - return parent.forget(); - } + auto parent = MakeRefPtr(); - MOZ_ASSERT(aOtherPid == endpoint.OtherPid()); + DebugOnly ok = endpoint.Bind(parent); + MOZ_ASSERT(ok); - parent = new GMPContentParent(); - - DebugOnly ok = endpoint.Bind(parent); - MOZ_ASSERT(ok); - - mContentParents.InsertOrUpdate(aOtherPid, RefPtr{parent}); - - return parent.forget(); + return parent; + })); } void GMPServiceChild::RemoveGMPContentParent( diff --git a/dom/media/gmp/GMPServiceParent.cpp b/dom/media/gmp/GMPServiceParent.cpp index e35897352559..e7e7b6049ca6 100644 --- a/dom/media/gmp/GMPServiceParent.cpp +++ b/dom/media/gmp/GMPServiceParent.cpp @@ -1037,12 +1037,8 @@ nsresult ReadSalt(nsIFile* aPath, nsACString& aOutData) { already_AddRefed GeckoMediaPluginServiceParent::GetMemoryStorageFor( const nsACString& aNodeId) { - RefPtr s; - if (!mTempGMPStorage.Get(aNodeId, getter_AddRefs(s))) { - s = CreateGMPMemoryStorage(); - mTempGMPStorage.InsertOrUpdate(aNodeId, RefPtr{s}); - } - return s.forget(); + return do_AddRef(mTempGMPStorage.LookupOrInsertWith( + aNodeId, [] { return CreateGMPMemoryStorage(); })); } NS_IMETHODIMP diff --git a/dom/media/mediacontrol/MediaPlaybackStatus.cpp b/dom/media/mediacontrol/MediaPlaybackStatus.cpp index 01fc87d64d5f..95e5af862446 100644 --- a/dom/media/mediacontrol/MediaPlaybackStatus.cpp +++ b/dom/media/mediacontrol/MediaPlaybackStatus.cpp @@ -104,11 +104,8 @@ bool MediaPlaybackStatus::IsAnyMediaBeingControlled() const { MediaPlaybackStatus::ContextMediaInfo& MediaPlaybackStatus::GetNotNullContextInfo(uint64_t aContextId) { MOZ_ASSERT(NS_IsMainThread()); - if (!mContextInfoMap.Contains(aContextId)) { - mContextInfoMap.InsertOrUpdate(aContextId, - MakeUnique(aContextId)); - } - return *(mContextInfoMap.GetValue(aContextId)->get()); + return *mContextInfoMap.LookupOrInsertWith( + aContextId, [&] { return MakeUnique(aContextId); }); } Maybe MediaPlaybackStatus::GetAudioFocusOwnerContextId() const { diff --git a/dom/media/mediacontrol/MediaStatusManager.cpp b/dom/media/mediacontrol/MediaStatusManager.cpp index 5a868416294c..62d54604c8f3 100644 --- a/dom/media/mediacontrol/MediaStatusManager.cpp +++ b/dom/media/mediacontrol/MediaStatusManager.cpp @@ -62,28 +62,33 @@ void MediaStatusManager::NotifyMediaAudibleChanged(uint64_t aBrowsingContextId, } void MediaStatusManager::NotifySessionCreated(uint64_t aBrowsingContextId) { - if (mMediaSessionInfoMap.Contains(aBrowsingContextId)) { - return; - } + if (!mMediaSessionInfoMap.WithEntryHandle( + aBrowsingContextId, [&](auto&& entry) { + if (entry) return true; - LOG("Session %" PRIu64 " has been created", aBrowsingContextId); - mMediaSessionInfoMap.InsertOrUpdate(aBrowsingContextId, - MediaSessionInfo::EmptyInfo()); - if (IsSessionOwningAudioFocus(aBrowsingContextId)) { - SetActiveMediaSessionContextId(aBrowsingContextId); + LOG("Session %" PRIu64 " has been created", aBrowsingContextId); + entry.Insert(MediaSessionInfo::EmptyInfo()); + return false; + })) { + // This can't be done from within the WithEntryHandle functor, since it + // accesses mMediaSessionInfoMap. + if (IsSessionOwningAudioFocus(aBrowsingContextId)) { + SetActiveMediaSessionContextId(aBrowsingContextId); + } } } void MediaStatusManager::NotifySessionDestroyed(uint64_t aBrowsingContextId) { - if (!mMediaSessionInfoMap.Contains(aBrowsingContextId)) { - return; - } - LOG("Session %" PRIu64 " has been destroyed", aBrowsingContextId); - mMediaSessionInfoMap.Remove(aBrowsingContextId); - if (mActiveMediaSessionContextId && - *mActiveMediaSessionContextId == aBrowsingContextId) { - ClearActiveMediaSessionContextIdIfNeeded(); - } + mMediaSessionInfoMap.WithEntryHandle(aBrowsingContextId, [&](auto&& entry) { + if (entry) return; + + LOG("Session %" PRIu64 " has been destroyed", aBrowsingContextId); + entry.Remove(); + if (mActiveMediaSessionContextId && + *mActiveMediaSessionContextId == aBrowsingContextId) { + ClearActiveMediaSessionContextIdIfNeeded(); + } + }); } void MediaStatusManager::UpdateMetadata( diff --git a/dom/media/platforms/agnostic/eme/EMEDecoderModule.cpp b/dom/media/platforms/agnostic/eme/EMEDecoderModule.cpp index cf57f7ada7a3..ea7666305fe5 100644 --- a/dom/media/platforms/agnostic/eme/EMEDecoderModule.cpp +++ b/dom/media/platforms/agnostic/eme/EMEDecoderModule.cpp @@ -145,12 +145,12 @@ class EMEDecryptor : public MediaDataDecoder, return; } - mDecrypts.InsertOrUpdate(aSample, - MakeUnique()); + const auto& decrypt = mDecrypts.InsertOrUpdate( + aSample, MakeUnique()); mProxy->Decrypt(aSample) ->Then(mThread, __func__, this, &EMEDecryptor::Decrypted, &EMEDecryptor::Decrypted) - ->Track(*mDecrypts.Get(aSample)); + ->Track(*decrypt); } void Decrypted(const DecryptResult& aDecrypted) { diff --git a/dom/media/systemservices/MediaSystemResourceManager.cpp b/dom/media/systemservices/MediaSystemResourceManager.cpp index 771da1c8ffa7..49574be8eac5 100644 --- a/dom/media/systemservices/MediaSystemResourceManager.cpp +++ b/dom/media/systemservices/MediaSystemResourceManager.cpp @@ -122,7 +122,7 @@ bool MediaSystemResourceManager::IsIpcClosed() { return mChild ? true : false; } void MediaSystemResourceManager::Register(MediaSystemResourceClient* aClient) { ReentrantMonitorAutoEnter mon(mReentrantMonitor); MOZ_ASSERT(aClient); - MOZ_ASSERT(!mResourceClients.Get(aClient->mId)); + MOZ_ASSERT(!mResourceClients.Contains(aClient->mId)); mResourceClients.InsertOrUpdate(aClient->mId, aClient); } @@ -131,7 +131,7 @@ void MediaSystemResourceManager::Unregister( MediaSystemResourceClient* aClient) { ReentrantMonitorAutoEnter mon(mReentrantMonitor); MOZ_ASSERT(aClient); - MOZ_ASSERT(mResourceClients.Get(aClient->mId)); + MOZ_ASSERT(mResourceClients.Contains(aClient->mId)); MOZ_ASSERT(mResourceClients.Get(aClient->mId) == aClient); mResourceClients.Remove(aClient->mId);