Bug 1942006 - Upstream a variety of Servo-specific code from Servo's downstream fork...
[gecko.git] / image / ImageCacheKey.h
blobb4910a9f86a1a65ab7a5a4f5d0ce0f6014d09124
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 /**
7 * ImageCacheKey is the key type for the image cache (see imgLoader.h).
8 */
10 #ifndef mozilla_image_src_ImageCacheKey_h
11 #define mozilla_image_src_ImageCacheKey_h
13 #include "mozilla/BasePrincipal.h"
14 #include "mozilla/Maybe.h"
15 #include "mozilla/RefPtr.h"
16 #include "PLDHashTable.h"
17 #include "nsIDocShell.h"
19 class nsIURI;
21 namespace mozilla {
23 enum CORSMode : uint8_t;
25 namespace image {
27 /**
28 * An ImageLib cache entry key.
30 * We key the cache on the initial URI (before any redirects), with some
31 * canonicalization applied. See ComputeHash() for the details.
32 * Controlled documents do not share their cache entries with
33 * non-controlled documents, or other controlled documents.
35 class ImageCacheKey final {
36 public:
37 ImageCacheKey(nsIURI*, CORSMode, const OriginAttributes&, dom::Document*);
39 ImageCacheKey(const ImageCacheKey& aOther);
40 ImageCacheKey(ImageCacheKey&& aOther);
42 bool operator==(const ImageCacheKey& aOther) const;
43 PLDHashNumber Hash() const {
44 if (MOZ_UNLIKELY(mHash.isNothing())) {
45 EnsureHash();
47 return mHash.value();
50 /// A weak pointer to the URI.
51 nsIURI* URI() const { return mURI; }
53 CORSMode GetCORSMode() const { return mCORSMode; }
55 const OriginAttributes& OriginAttributesRef() const {
56 return mOriginAttributes;
59 const nsCString& IsolationKeyRef() const { return mIsolationKey; }
61 /// A token indicating which service worker controlled document this entry
62 /// belongs to, if any.
63 void* ControlledDocument() const { return mControlledDocument; }
65 private:
66 // For ServiceWorker we need to use the document as
67 // token for the key. All those exceptions are handled by this method.
68 static void* GetSpecialCaseDocumentToken(dom::Document* aDocument);
70 // For anti-tracking we need to use an isolation key. It can be the suffix of
71 // the PatitionedPrincipal (see StoragePrincipalHelper.h) or the top-level
72 // document's base domain. This is handled by this method.
73 static nsCString GetIsolationKey(dom::Document* aDocument, nsIURI* aURI);
75 // The AppType of the docshell an image is loaded in can influence whether the
76 // image is allowed to load. The specific AppType is fetched by this method.
77 static nsIDocShell::AppType GetAppType(dom::Document* aDocument);
79 void EnsureHash() const;
81 nsCOMPtr<nsIURI> mURI;
82 const OriginAttributes mOriginAttributes;
83 void* mControlledDocument;
84 nsCString mIsolationKey;
85 mutable Maybe<PLDHashNumber> mHash;
86 const CORSMode mCORSMode;
87 const nsIDocShell::AppType mAppType;
90 } // namespace image
91 } // namespace mozilla
93 #endif // mozilla_image_src_ImageCacheKey_h