Bug 1755931 [wpt PR 32880] - Rewrite geolocation-API/PositionOptions.https.html,...
[gecko.git] / image / ImageCacheKey.cpp
blob67819071bd36a66f1ed5bf1349129e1ee929ac41
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "ImageCacheKey.h"
8 #include <utility>
10 #include "mozilla/AntiTrackingUtils.h"
11 #include "mozilla/ContentBlocking.h"
12 #include "mozilla/HashFunctions.h"
13 #include "mozilla/StorageAccess.h"
14 #include "mozilla/StoragePrincipalHelper.h"
15 #include "mozilla/Unused.h"
16 #include "mozilla/dom/Document.h"
17 #include "mozilla/dom/File.h"
18 #include "mozilla/dom/ServiceWorkerManager.h"
19 #include "mozilla/StaticPrefs_privacy.h"
20 #include "nsContentUtils.h"
21 #include "nsHashKeys.h"
22 #include "nsLayoutUtils.h"
23 #include "nsPrintfCString.h"
24 #include "nsString.h"
26 namespace mozilla {
28 using namespace dom;
30 namespace image {
32 ImageCacheKey::ImageCacheKey(nsIURI* aURI, const OriginAttributes& aAttrs,
33 Document* aDocument)
34 : mURI(aURI),
35 mOriginAttributes(aAttrs),
36 mControlledDocument(GetSpecialCaseDocumentToken(aDocument)),
37 mIsolationKey(GetIsolationKey(aDocument, aURI)),
38 mIsChrome(false) {
39 if (mURI->SchemeIs("chrome")) {
40 mIsChrome = true;
44 ImageCacheKey::ImageCacheKey(const ImageCacheKey& aOther)
45 : mURI(aOther.mURI),
46 mOriginAttributes(aOther.mOriginAttributes),
47 mControlledDocument(aOther.mControlledDocument),
48 mIsolationKey(aOther.mIsolationKey),
49 mHash(aOther.mHash),
50 mIsChrome(aOther.mIsChrome) {}
52 ImageCacheKey::ImageCacheKey(ImageCacheKey&& aOther)
53 : mURI(std::move(aOther.mURI)),
54 mOriginAttributes(aOther.mOriginAttributes),
55 mControlledDocument(aOther.mControlledDocument),
56 mIsolationKey(aOther.mIsolationKey),
57 mHash(aOther.mHash),
58 mIsChrome(aOther.mIsChrome) {}
60 bool ImageCacheKey::operator==(const ImageCacheKey& aOther) const {
61 // Don't share the image cache between a controlled document and anything
62 // else.
63 if (mControlledDocument != aOther.mControlledDocument) {
64 return false;
66 // Don't share the image cache between two top-level documents of different
67 // base domains.
68 if (!mIsolationKey.Equals(aOther.mIsolationKey,
69 nsCaseInsensitiveCStringComparator)) {
70 return false;
72 // The origin attributes always have to match.
73 if (mOriginAttributes != aOther.mOriginAttributes) {
74 return false;
77 // For non-blob URIs, compare the URIs.
78 bool equals = false;
79 nsresult rv = mURI->Equals(aOther.mURI, &equals);
80 return NS_SUCCEEDED(rv) && equals;
83 void ImageCacheKey::EnsureHash() const {
84 MOZ_ASSERT(mHash.isNothing());
85 PLDHashNumber hash = 0;
87 // Since we frequently call Hash() several times in a row on the same
88 // ImageCacheKey, as an optimization we compute our hash once and store it.
90 nsPrintfCString ptr("%p", mControlledDocument);
91 nsAutoCString suffix;
92 mOriginAttributes.CreateSuffix(suffix);
94 nsAutoCString spec;
95 Unused << mURI->GetSpec(spec);
96 hash = HashString(spec);
98 hash = AddToHash(hash, HashString(suffix), HashString(mIsolationKey),
99 HashString(ptr));
100 mHash.emplace(hash);
103 /* static */
104 void* ImageCacheKey::GetSpecialCaseDocumentToken(Document* aDocument) {
105 // Cookie-averse documents can never have storage granted to them. Since they
106 // may not have inner windows, they would require special handling below, so
107 // just bail out early here.
108 if (!aDocument || aDocument->IsCookieAverse()) {
109 return nullptr;
112 // For controlled documents, we cast the pointer into a void* to avoid
113 // dereferencing it (since we only use it for comparisons).
114 RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
115 if (swm && aDocument->GetController().isSome()) {
116 return aDocument;
119 return nullptr;
122 /* static */
123 nsCString ImageCacheKey::GetIsolationKey(Document* aDocument, nsIURI* aURI) {
124 if (!aDocument || !aDocument->GetInnerWindow()) {
125 return ""_ns;
128 // Network-state isolation
129 if (StaticPrefs::privacy_partition_network_state()) {
130 OriginAttributes oa;
131 StoragePrincipalHelper::GetOriginAttributesForNetworkState(aDocument, oa);
133 nsAutoCString suffix;
134 oa.CreateSuffix(suffix);
136 return std::move(suffix);
139 // If the window is 3rd party resource, let's see if first-party storage
140 // access is granted for this image.
141 if (AntiTrackingUtils::IsThirdPartyWindow(aDocument->GetInnerWindow(),
142 nullptr)) {
143 uint32_t rejectedReason = 0;
144 Unused << rejectedReason;
145 return StorageDisabledByAntiTracking(aDocument, aURI, rejectedReason)
146 ? aDocument->GetBaseDomain()
147 : ""_ns;
150 // Another scenario is if this image is a 3rd party resource loaded by a
151 // first party context. In this case, we should check if the nsIChannel has
152 // been marked as tracking resource, but we don't have the channel yet at
153 // this point. The best approach here is to be conservative: if we are sure
154 // that the permission is granted, let's return 0. Otherwise, let's make a
155 // unique image cache per the top-level document eTLD+1.
156 if (!ContentBlocking::ApproximateAllowAccessForWithoutChannel(
157 aDocument->GetInnerWindow(), aURI)) {
158 // If we are here, the image is a 3rd-party resource loaded by a first-party
159 // context. We can just use the document's base domain as the key because it
160 // should be the same as the top-level document's base domain.
161 return aDocument
162 ->GetBaseDomain(); // because we don't have anything better!
165 return ""_ns;
168 } // namespace image
169 } // namespace mozilla