ImageCacheKey.h (2564B)
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/. */ 5 6 /** 7 * ImageCacheKey is the key type for the image cache (see imgLoader.h). 8 */ 9 10 #ifndef mozilla_image_src_ImageCacheKey_h 11 #define mozilla_image_src_ImageCacheKey_h 12 13 #include "mozilla/BasePrincipal.h" 14 #include "mozilla/Maybe.h" 15 #include "PLDHashTable.h" 16 #include "nsIDocShell.h" 17 18 class nsIURI; 19 20 namespace mozilla { 21 22 enum CORSMode : uint8_t; 23 24 namespace image { 25 26 /** 27 * An ImageLib cache entry key. 28 * 29 * We key the cache on the initial URI (before any redirects), with some 30 * canonicalization applied. See ComputeHash() for the details. 31 * Controlled documents do not share their cache entries with 32 * non-controlled documents, or other controlled documents. 33 */ 34 class ImageCacheKey final { 35 public: 36 ImageCacheKey(nsIURI*, CORSMode, dom::Document*); 37 38 ImageCacheKey(const ImageCacheKey& aOther); 39 ImageCacheKey(ImageCacheKey&& aOther); 40 41 bool operator==(const ImageCacheKey& aOther) const; 42 PLDHashNumber Hash() const { 43 if (MOZ_UNLIKELY(mHash.isNothing())) { 44 EnsureHash(); 45 } 46 return mHash.value(); 47 } 48 49 /// A weak pointer to the URI. 50 nsIURI* URI() const { return mURI; } 51 52 nsIPrincipal* PartitionPrincipal() const { return mPartitionPrincipal; } 53 nsIPrincipal* LoaderPrincipal() const { return mLoaderPrincipal; } 54 55 CORSMode GetCORSMode() const { return mCORSMode; } 56 57 /// A token indicating which service worker controlled document this entry 58 /// belongs to, if any. 59 void* ControlledDocument() const { return mControlledDocument; } 60 61 private: 62 // For ServiceWorker we need to use the document as 63 // token for the key. All those exceptions are handled by this method. 64 static void* GetSpecialCaseDocumentToken(dom::Document* aDocument); 65 66 // The AppType of the docshell an image is loaded in can influence whether the 67 // image is allowed to load. The specific AppType is fetched by this method. 68 static nsIDocShell::AppType GetAppType(dom::Document* aDocument); 69 70 void EnsureHash() const; 71 72 nsCOMPtr<nsIURI> mURI; 73 void* mControlledDocument; 74 nsCOMPtr<nsIPrincipal> mLoaderPrincipal; 75 nsCOMPtr<nsIPrincipal> mPartitionPrincipal; 76 mutable Maybe<PLDHashNumber> mHash; 77 const CORSMode mCORSMode; 78 const nsIDocShell::AppType mAppType; 79 }; 80 81 } // namespace image 82 } // namespace mozilla 83 84 #endif // mozilla_image_src_ImageCacheKey_h