ImageCacheKey.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. * ImageCacheKey is the key type for the image cache (see imgLoader.h).
  7. */
  8. #ifndef mozilla_image_src_ImageCacheKey_h
  9. #define mozilla_image_src_ImageCacheKey_h
  10. #include "mozilla/BasePrincipal.h"
  11. #include "mozilla/Maybe.h"
  12. #include "mozilla/RefPtr.h"
  13. class nsIDocument;
  14. class nsIURI;
  15. namespace mozilla {
  16. namespace image {
  17. class ImageURL;
  18. /**
  19. * An ImageLib cache entry key.
  20. *
  21. * We key the cache on the initial URI (before any redirects), with some
  22. * canonicalization applied. See ComputeHash() for the details.
  23. * Controlled documents do not share their cache entries with
  24. * non-controlled documents, or other controlled documents.
  25. */
  26. class ImageCacheKey final
  27. {
  28. public:
  29. ImageCacheKey(nsIURI* aURI, const PrincipalOriginAttributes& aAttrs,
  30. nsIDocument* aDocument, nsresult& aRv);
  31. ImageCacheKey(ImageURL* aURI, const PrincipalOriginAttributes& aAttrs,
  32. nsIDocument* aDocument);
  33. ImageCacheKey(const ImageCacheKey& aOther);
  34. ImageCacheKey(ImageCacheKey&& aOther);
  35. bool operator==(const ImageCacheKey& aOther) const;
  36. uint32_t Hash() const { return mHash; }
  37. /// A weak pointer to the URI spec for this cache entry. For logging only.
  38. const char* Spec() const;
  39. /// Is this cache entry for a chrome image?
  40. bool IsChrome() const { return mIsChrome; }
  41. /// A token indicating which service worker controlled document this entry
  42. /// belongs to, if any.
  43. void* ControlledDocument() const { return mControlledDocument; }
  44. private:
  45. static uint32_t ComputeHash(ImageURL* aURI,
  46. const Maybe<uint64_t>& aBlobSerial,
  47. const PrincipalOriginAttributes& aAttrs,
  48. void* aControlledDocument);
  49. static void* GetControlledDocumentToken(nsIDocument* aDocument);
  50. RefPtr<ImageURL> mURI;
  51. Maybe<uint64_t> mBlobSerial;
  52. PrincipalOriginAttributes mOriginAttributes;
  53. void* mControlledDocument;
  54. uint32_t mHash;
  55. bool mIsChrome;
  56. };
  57. } // namespace image
  58. } // namespace mozilla
  59. #endif // mozilla_image_src_ImageCacheKey_h