MediaChild.cpp (3179B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set sw=2 ts=8 et ft=cpp : */ 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 file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "MediaChild.h" 8 9 #include "MediaParent.h" 10 #include "mozilla/Logging.h" 11 #include "mozilla/MediaManager.h" 12 #include "mozilla/dom/ContentChild.h" 13 #include "nsQueryObject.h" 14 15 mozilla::LazyLogModule gMediaChildLog("MediaChild"); 16 #define LOG(args) MOZ_LOG(gMediaChildLog, mozilla::LogLevel::Debug, args) 17 18 namespace mozilla::media { 19 20 RefPtr<PrincipalKeyPromise> GetPrincipalKey( 21 const ipc::PrincipalInfo& aPrincipalInfo, bool aPersist) { 22 RefPtr<MediaManager> mgr = MediaManager::GetInstance(); 23 MOZ_ASSERT(mgr); 24 25 if (XRE_GetProcessType() == GeckoProcessType_Default) { 26 auto p = MakeRefPtr<PrincipalKeyPromise::Private>(__func__); 27 28 mgr->GetNonE10sParent()->RecvGetPrincipalKey( 29 aPrincipalInfo, aPersist, 30 [p](const nsACString& aKey) { p->Resolve(aKey, __func__); }); 31 return p; 32 } 33 return Child::Get() 34 ->SendGetPrincipalKey(aPrincipalInfo, aPersist) 35 ->Then(GetMainThreadSerialEventTarget(), __func__, 36 [](const Child::GetPrincipalKeyPromise::ResolveOrRejectValue& 37 aValue) { 38 if (aValue.IsReject() || aValue.ResolveValue().IsEmpty()) { 39 return PrincipalKeyPromise::CreateAndReject(NS_ERROR_FAILURE, 40 __func__); 41 } 42 return PrincipalKeyPromise::CreateAndResolve( 43 aValue.ResolveValue(), __func__); 44 }); 45 } 46 47 void SanitizeOriginKeys(const uint64_t& aSinceWhen, bool aOnlyPrivateBrowsing) { 48 LOG(("SanitizeOriginKeys since %" PRIu64 " %s", aSinceWhen, 49 (aOnlyPrivateBrowsing ? "in Private Browsing." : "."))); 50 51 if (XRE_GetProcessType() == GeckoProcessType_Default) { 52 // Avoid opening MediaManager in this case, since this is called by 53 // sanitize.js when cookies are cleared, which can happen on startup. 54 RefPtr<Parent<NonE10s>> tmpParent = new Parent<NonE10s>(); 55 tmpParent->RecvSanitizeOriginKeys(aSinceWhen, aOnlyPrivateBrowsing); 56 } else { 57 Child::Get()->SendSanitizeOriginKeys(aSinceWhen, aOnlyPrivateBrowsing); 58 } 59 } 60 61 static Child* sChild; 62 63 Child* Child::Get() { 64 MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Content); 65 MOZ_ASSERT(NS_IsMainThread()); 66 if (!sChild) { 67 sChild = static_cast<Child*>( 68 dom::ContentChild::GetSingleton()->SendPMediaConstructor()); 69 } 70 return sChild; 71 } 72 73 Child::Child() : mActorDestroyed(false) { 74 LOG(("media::Child: %p", this)); 75 MOZ_COUNT_CTOR(Child); 76 } 77 78 Child::~Child() { 79 LOG(("~media::Child: %p", this)); 80 sChild = nullptr; 81 MOZ_COUNT_DTOR(Child); 82 } 83 84 void Child::ActorDestroy(ActorDestroyReason aWhy) { mActorDestroyed = true; } 85 86 PMediaChild* AllocPMediaChild() { return new Child(); } 87 88 bool DeallocPMediaChild(media::PMediaChild* aActor) { 89 delete static_cast<Child*>(aActor); 90 return true; 91 } 92 93 } // namespace mozilla::media 94 95 #undef LOG