2015-05-20 10:21:09 -07:00
|
|
|
/* -*- 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 {
|
|
|
|
|
2015-05-20 18:49:53 -07:00
|
|
|
bool
|
|
|
|
URISchemeIs(ImageURL* aURI, const char* aScheme)
|
|
|
|
{
|
|
|
|
bool schemeMatches = false;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(aURI->SchemeIs(aScheme, &schemeMatches)))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return schemeMatches;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Maybe<uint64_t>
|
|
|
|
BlobSerial(ImageURL* aURI)
|
|
|
|
{
|
|
|
|
nsAutoCString spec;
|
|
|
|
aURI->GetSpec(spec);
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<BlobImpl> blob;
|
2015-05-20 18:49:53 -07:00
|
|
|
if (NS_SUCCEEDED(NS_GetBlobForBlobURISpec(spec, getter_AddRefs(blob))) &&
|
|
|
|
blob) {
|
|
|
|
return Some(blob->GetSerialNumber());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
2015-05-20 10:21:09 -07:00
|
|
|
ImageCacheKey::ImageCacheKey(nsIURI* aURI)
|
2015-05-20 10:21:11 -07:00
|
|
|
: mURI(new ImageURL(aURI))
|
2015-05-20 18:49:53 -07:00
|
|
|
, mIsChrome(URISchemeIs(mURI, "chrome"))
|
2015-05-20 10:21:09 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-05-20 18:49:53 -07:00
|
|
|
if (URISchemeIs(mURI, "blob")) {
|
|
|
|
mBlobSerial = BlobSerial(mURI);
|
|
|
|
}
|
2015-05-20 10:21:09 -07:00
|
|
|
|
2015-05-20 18:49:53 -07:00
|
|
|
mHash = ComputeHash(mURI, mBlobSerial);
|
2015-05-20 10:21:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ImageCacheKey::ImageCacheKey(ImageURL* aURI)
|
2015-05-20 10:21:11 -07:00
|
|
|
: mURI(aURI)
|
2015-05-20 18:49:53 -07:00
|
|
|
, mIsChrome(URISchemeIs(mURI, "chrome"))
|
2015-05-20 10:21:09 -07:00
|
|
|
{
|
2015-05-20 18:49:53 -07:00
|
|
|
MOZ_ASSERT(aURI);
|
2015-05-20 10:21:09 -07:00
|
|
|
|
2015-05-20 18:49:53 -07:00
|
|
|
if (URISchemeIs(mURI, "blob")) {
|
|
|
|
mBlobSerial = BlobSerial(mURI);
|
|
|
|
}
|
2015-05-20 10:21:09 -07:00
|
|
|
|
2015-05-20 18:49:53 -07:00
|
|
|
mHash = ComputeHash(mURI, mBlobSerial);
|
2015-05-20 10:21:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ImageCacheKey::ImageCacheKey(const ImageCacheKey& aOther)
|
2015-05-20 10:21:11 -07:00
|
|
|
: mURI(aOther.mURI)
|
2015-05-20 18:49:53 -07:00
|
|
|
, mBlobSerial(aOther.mBlobSerial)
|
2015-05-20 10:21:09 -07:00
|
|
|
, mHash(aOther.mHash)
|
|
|
|
, mIsChrome(aOther.mIsChrome)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
ImageCacheKey::ImageCacheKey(ImageCacheKey&& aOther)
|
2015-05-20 10:21:11 -07:00
|
|
|
: mURI(Move(aOther.mURI))
|
2015-05-20 18:49:53 -07:00
|
|
|
, mBlobSerial(Move(aOther.mBlobSerial))
|
2015-05-20 10:21:09 -07:00
|
|
|
, mHash(aOther.mHash)
|
|
|
|
, mIsChrome(aOther.mIsChrome)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
ImageCacheKey::operator==(const ImageCacheKey& aOther) const
|
|
|
|
{
|
2015-05-20 18:49:53 -07:00
|
|
|
if (mBlobSerial || aOther.mBlobSerial) {
|
2015-06-05 01:52:06 -07:00
|
|
|
// If at least one of us has a blob serial, just compare the blob serial and
|
|
|
|
// the ref portion of the URIs.
|
|
|
|
return mBlobSerial == aOther.mBlobSerial &&
|
|
|
|
mURI->HasSameRef(*aOther.mURI);
|
2015-05-20 18:49:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// For non-blob URIs, compare the URIs.
|
2015-05-20 10:21:11 -07:00
|
|
|
return *mURI == *aOther.mURI;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
|
|
|
ImageCacheKey::Spec() const
|
|
|
|
{
|
|
|
|
return mURI->Spec();
|
2015-05-20 10:21:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ uint32_t
|
2015-05-20 18:49:53 -07:00
|
|
|
ImageCacheKey::ComputeHash(ImageURL* aURI,
|
|
|
|
const Maybe<uint64_t>& aBlobSerial)
|
2015-05-20 10:21:09 -07:00
|
|
|
{
|
|
|
|
// 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.
|
2015-05-20 18:49:53 -07:00
|
|
|
|
|
|
|
if (aBlobSerial) {
|
|
|
|
// For blob URIs, we hash the serial number of the underlying blob, so that
|
2015-06-05 01:52:06 -07:00
|
|
|
// different blob URIs which point to the same blob share a cache entry. We
|
2015-09-30 17:00:52 -07:00
|
|
|
// also include the ref portion of the URI to support -moz-samplesize, which
|
|
|
|
// requires us to create different Image objects even if the source data is
|
|
|
|
// the same.
|
2015-06-05 01:52:06 -07:00
|
|
|
nsAutoCString ref;
|
|
|
|
aURI->GetRef(ref);
|
|
|
|
return HashGeneric(*aBlobSerial, HashString(ref));
|
2015-05-20 18:49:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// For non-blob URIs, we hash the URI spec.
|
2015-05-20 10:21:11 -07:00
|
|
|
nsAutoCString spec;
|
|
|
|
aURI->GetSpec(spec);
|
|
|
|
return HashString(spec);
|
2015-05-20 10:21:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|