Bug 1787199 [wpt PR 35620] - Add tests for `VisibilityStateEntry`, a=testonly
[gecko.git] / dom / cache / ManagerId.cpp
blobe83e9163c5c1076efff1e425af588ff2d62fde51
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/. */
7 #include "mozilla/dom/cache/ManagerId.h"
9 #include "CacheCommon.h"
11 #include "mozilla/dom/quota/QuotaManager.h"
12 #include "nsIPrincipal.h"
13 #include "nsProxyRelease.h"
14 #include "mozilla/RefPtr.h"
15 #include "nsThreadUtils.h"
17 namespace mozilla::dom::cache {
19 using mozilla::dom::quota::QuotaManager;
21 // static
22 Result<SafeRefPtr<ManagerId>, nsresult> ManagerId::Create(
23 nsIPrincipal* aPrincipal) {
24 MOZ_ASSERT(NS_IsMainThread());
26 // QuotaManager::GetOriginFromPrincipal() has special logic for system
27 // and about: principals. We need to use the same modified origin in
28 // order to interpret calls from QM correctly.
29 QM_TRY_INSPECT(const auto& quotaOrigin,
30 QuotaManager::GetOriginFromPrincipal(aPrincipal));
32 return MakeSafeRefPtr<ManagerId>(aPrincipal, quotaOrigin, ConstructorGuard{});
35 already_AddRefed<nsIPrincipal> ManagerId::Principal() const {
36 MOZ_ASSERT(NS_IsMainThread());
37 nsCOMPtr<nsIPrincipal> ref = mPrincipal;
38 return ref.forget();
41 ManagerId::ManagerId(nsIPrincipal* aPrincipal, const nsACString& aQuotaOrigin,
42 ConstructorGuard)
43 : mPrincipal(aPrincipal), mQuotaOrigin(aQuotaOrigin) {
44 MOZ_DIAGNOSTIC_ASSERT(mPrincipal);
47 ManagerId::~ManagerId() {
48 // If we're already on the main thread, then default destruction is fine
49 if (NS_IsMainThread()) {
50 return;
53 // Otherwise we need to proxy to main thread to do the release
55 // The PBackground worker thread shouldn't be running after the main thread
56 // is stopped. So main thread is guaranteed to exist here.
57 NS_ReleaseOnMainThread("ManagerId::mPrincipal", mPrincipal.forget());
60 } // namespace mozilla::dom::cache