ManagerId.cpp (1964B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 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 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "mozilla/dom/cache/ManagerId.h" 8 9 #include "CacheCommon.h" 10 #include "mozilla/dom/quota/PrincipalUtils.h" 11 #include "nsIPrincipal.h" 12 #include "nsProxyRelease.h" 13 #include "nsThreadUtils.h" 14 15 namespace mozilla::dom::cache { 16 17 using mozilla::dom::quota::QuotaManager; 18 19 // static 20 Result<SafeRefPtr<ManagerId>, nsresult> ManagerId::Create( 21 nsIPrincipal* aPrincipal) { 22 MOZ_ASSERT(NS_IsMainThread()); 23 24 // mozilla::dom::quota::GetOriginFromPrincipal() has special logic for system 25 // and about: principals. We need to use the same modified origin in 26 // order to interpret calls from QM correctly. 27 QM_TRY_INSPECT(const auto& quotaOrigin, 28 quota::GetOriginFromPrincipal(aPrincipal)); 29 30 return MakeSafeRefPtr<ManagerId>(aPrincipal, quotaOrigin, ConstructorGuard{}); 31 } 32 33 already_AddRefed<nsIPrincipal> ManagerId::Principal() const { 34 MOZ_ASSERT(NS_IsMainThread()); 35 nsCOMPtr<nsIPrincipal> ref = mPrincipal; 36 return ref.forget(); 37 } 38 39 ManagerId::ManagerId(nsIPrincipal* aPrincipal, const nsACString& aQuotaOrigin, 40 ConstructorGuard) 41 : mPrincipal(aPrincipal), mQuotaOrigin(aQuotaOrigin) { 42 MOZ_DIAGNOSTIC_ASSERT(mPrincipal); 43 } 44 45 ManagerId::~ManagerId() { 46 // If we're already on the main thread, then default destruction is fine 47 if (NS_IsMainThread()) { 48 return; 49 } 50 51 // Otherwise we need to proxy to main thread to do the release 52 53 // The PBackground worker thread shouldn't be running after the main thread 54 // is stopped. So main thread is guaranteed to exist here. 55 NS_ReleaseOnMainThread("ManagerId::mPrincipal", mPrincipal.forget()); 56 } 57 58 } // namespace mozilla::dom::cache