diff --git a/image/ImageCacheKey.cpp b/image/ImageCacheKey.cpp new file mode 100644 index 00000000000..96bdaec93f9 --- /dev/null +++ b/image/ImageCacheKey.cpp @@ -0,0 +1,70 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "ImageCacheKey.h" + +#include "mozilla/Move.h" +#include "File.h" +#include "ImageURL.h" +#include "nsHostObjectProtocolHandler.h" +#include "nsString.h" + +namespace mozilla { + +using namespace dom; + +namespace image { + +ImageCacheKey::ImageCacheKey(nsIURI* aURI) +{ + MOZ_ASSERT(NS_IsMainThread()); + MOZ_ASSERT(aURI); + + bool isChrome; + mIsChrome = NS_SUCCEEDED(aURI->SchemeIs("chrome", &isChrome)) && isChrome; + + aURI->GetSpec(mSpec); + mHash = ComputeHash(mSpec); +} + +ImageCacheKey::ImageCacheKey(ImageURL* aURI) +{ + MOZ_ASSERT(aURI); + + bool isChrome; + mIsChrome = NS_SUCCEEDED(aURI->SchemeIs("chrome", &isChrome)) && isChrome; + + aURI->GetSpec(mSpec); + mHash = ComputeHash(mSpec); +} + +ImageCacheKey::ImageCacheKey(const ImageCacheKey& aOther) + : mSpec(aOther.mSpec) + , mHash(aOther.mHash) + , mIsChrome(aOther.mIsChrome) +{ } + +ImageCacheKey::ImageCacheKey(ImageCacheKey&& aOther) + : mSpec(Move(aOther.mSpec)) + , mHash(aOther.mHash) + , mIsChrome(aOther.mIsChrome) +{ } + +bool +ImageCacheKey::operator==(const ImageCacheKey& aOther) const +{ + return mSpec == aOther.mSpec; +} + +/* static */ uint32_t +ImageCacheKey::ComputeHash(const nsACString& aSpec) +{ + // Since we frequently call Hash() several times in a row on the same + // ImageCacheKey, as an optimization we compute our hash once and store it. + return HashString(aSpec); +} + +} // namespace image +} // namespace mozilla diff --git a/image/ImageCacheKey.h b/image/ImageCacheKey.h new file mode 100644 index 00000000000..985f49d8fc7 --- /dev/null +++ b/image/ImageCacheKey.h @@ -0,0 +1,55 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/** + * ImageCacheKey is the key type for the image cache (see imgLoader.h). + */ + +#ifndef mozilla_image_src_ImageCacheKey_h +#define mozilla_image_src_ImageCacheKey_h + +class nsIURI; + +namespace mozilla { +namespace image { + +class ImageURL; + +/** + * An ImageLib cache entry key. + * + * We key the cache on the initial URI (before any redirects), with some + * canonicalization applied. See ComputeHash() for the details. + */ +class ImageCacheKey final +{ +public: + explicit ImageCacheKey(nsIURI* aURI); + explicit ImageCacheKey(ImageURL* aURI); + + ImageCacheKey(const ImageCacheKey& aOther); + ImageCacheKey(ImageCacheKey&& aOther); + + bool operator==(const ImageCacheKey& aOther) const; + uint32_t Hash() const { return mHash; } + + /// A weak pointer to the URI spec for this cache entry. For logging only. + const char* Spec() const { return mSpec.get(); } + + /// Is this cache entry for a chrome image? + bool IsChrome() const { return mIsChrome; } + +private: + static uint32_t ComputeHash(const nsACString& aSpec); + + nsCString mSpec; + uint32_t mHash; + bool mIsChrome; +}; + +} // namespace image +} // namespace mozilla + +#endif // mozilla_image_src_ImageCacheKey_h diff --git a/image/imgLoader.cpp b/image/imgLoader.cpp index d685798664f..28ee443e933 100644 --- a/image/imgLoader.cpp +++ b/image/imgLoader.cpp @@ -1100,60 +1100,6 @@ imgCacheExpirationTracker::NotifyExpired(imgCacheEntry* entry) } -/////////////////////////////////////////////////////////////////////////////// -// ImageCacheKey -/////////////////////////////////////////////////////////////////////////////// - -ImageCacheKey::ImageCacheKey(nsIURI* aURI) -{ - MOZ_ASSERT(NS_IsMainThread()); - MOZ_ASSERT(aURI); - - bool isChrome; - mIsChrome = NS_SUCCEEDED(aURI->SchemeIs("chrome", &isChrome)) && isChrome; - - aURI->GetSpec(mSpec); - mHash = ComputeHash(mSpec); -} - -ImageCacheKey::ImageCacheKey(ImageURL* aURI) -{ - MOZ_ASSERT(aURI); - - bool isChrome; - mIsChrome = NS_SUCCEEDED(aURI->SchemeIs("chrome", &isChrome)) && isChrome; - - aURI->GetSpec(mSpec); - mHash = ComputeHash(mSpec); -} - -ImageCacheKey::ImageCacheKey(const ImageCacheKey& aOther) - : mSpec(aOther.mSpec) - , mHash(aOther.mHash) - , mIsChrome(aOther.mIsChrome) -{ } - -ImageCacheKey::ImageCacheKey(ImageCacheKey&& aOther) - : mSpec(Move(aOther.mSpec)) - , mHash(aOther.mHash) - , mIsChrome(aOther.mIsChrome) -{ } - -bool -ImageCacheKey::operator==(const ImageCacheKey& aOther) const -{ - return mSpec == aOther.mSpec; -} - -/* static */ uint32_t -ImageCacheKey::ComputeHash(const nsACString& aSpec) -{ - // Since we frequently call Hash() several times in a row on the same - // ImageCacheKey, as an optimization we compute our hash once and store it. - return HashString(aSpec); -} - - /////////////////////////////////////////////////////////////////////////////// // imgLoader /////////////////////////////////////////////////////////////////////////////// diff --git a/image/imgLoader.h b/image/imgLoader.h index 8f13aaa6c96..776b32e6978 100644 --- a/image/imgLoader.h +++ b/image/imgLoader.h @@ -17,6 +17,7 @@ #include "nsRefPtrHashtable.h" #include "nsExpirationTracker.h" #include "nsAutoPtr.h" +#include "ImageCacheKey.h" #include "imgRequest.h" #include "nsIProgressEventSink.h" #include "nsIChannel.h" @@ -214,38 +215,6 @@ enum class AcceptedMimeTypes : uint8_t { IMAGES_AND_DOCUMENTS, }; -/** - * An ImageLib cache entry key. - * - * We key the cache on the initial URI (before any redirects), with some - * canonicalization applied. See ComputeHash() for the details. - */ -class ImageCacheKey final -{ -public: - explicit ImageCacheKey(nsIURI* aURI); - explicit ImageCacheKey(mozilla::image::ImageURL* aURI); - - ImageCacheKey(const ImageCacheKey& aOther); - ImageCacheKey(ImageCacheKey&& aOther); - - bool operator==(const ImageCacheKey& aOther) const; - uint32_t Hash() const { return mHash; } - - /// A weak pointer to the URI spec for this cache entry. For logging only. - const char* Spec() const { return mSpec.get(); } - - /// Is this cache entry for a chrome image? - bool IsChrome() const { return mIsChrome; } - -private: - static uint32_t ComputeHash(const nsACString& aSpec); - - nsCString mSpec; - uint32_t mHash; - bool mIsChrome; -}; - class imgLoader final : public imgILoader, public nsIContentSniffer, public imgICache, @@ -255,6 +224,7 @@ class imgLoader final : public imgILoader, virtual ~imgLoader(); public: + typedef mozilla::image::ImageCacheKey ImageCacheKey; typedef mozilla::image::ImageURL ImageURL; typedef nsRefPtrHashtable, imgCacheEntry> imgCacheTable; diff --git a/image/moz.build b/image/moz.build index 29b33714fc2..299fe09ddd2 100644 --- a/image/moz.build +++ b/image/moz.build @@ -34,6 +34,7 @@ XPIDL_SOURCES += [ XPIDL_MODULE = 'imglib2' EXPORTS += [ + 'ImageCacheKey.h', 'ImageLogging.h', 'ImageOps.h', 'ImageRegion.h', @@ -53,6 +54,7 @@ UNIFIED_SOURCES += [ 'FrameAnimator.cpp', 'FrozenImage.cpp', 'Image.cpp', + 'ImageCacheKey.cpp', 'ImageFactory.cpp', 'ImageMetadata.cpp', 'ImageOps.cpp',