2015-05-20 20:21:09 +03: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"
|
2015-10-27 21:12:46 +03:00
|
|
|
#include "mozilla/dom/workers/ServiceWorkerManager.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsPrintfCString.h"
|
2015-05-20 20:21:09 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace dom;
|
|
|
|
|
|
|
|
namespace image {
|
|
|
|
|
2015-05-21 04:49:53 +03: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-18 08:24:48 +03:00
|
|
|
RefPtr<BlobImpl> blob;
|
2015-05-21 04:49:53 +03:00
|
|
|
if (NS_SUCCEEDED(NS_GetBlobForBlobURISpec(spec, getter_AddRefs(blob))) &&
|
|
|
|
blob) {
|
|
|
|
return Some(blob->GetSerialNumber());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
2016-06-02 00:07:00 +03:00
|
|
|
ImageCacheKey::ImageCacheKey(nsIURI* aURI,
|
|
|
|
const PrincipalOriginAttributes& aAttrs,
|
|
|
|
nsIDocument* aDocument)
|
2015-05-20 20:21:11 +03:00
|
|
|
: mURI(new ImageURL(aURI))
|
2016-06-02 00:07:00 +03:00
|
|
|
, mOriginAttributes(aAttrs)
|
2015-10-27 21:12:46 +03:00
|
|
|
, mControlledDocument(GetControlledDocumentToken(aDocument))
|
2015-05-21 04:49:53 +03:00
|
|
|
, mIsChrome(URISchemeIs(mURI, "chrome"))
|
2015-05-20 20:21:09 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-05-21 04:49:53 +03:00
|
|
|
if (URISchemeIs(mURI, "blob")) {
|
|
|
|
mBlobSerial = BlobSerial(mURI);
|
|
|
|
}
|
2015-05-20 20:21:09 +03:00
|
|
|
|
2016-06-02 00:07:00 +03:00
|
|
|
mHash = ComputeHash(mURI, mBlobSerial, mOriginAttributes, mControlledDocument);
|
2015-05-20 20:21:09 +03:00
|
|
|
}
|
|
|
|
|
2016-06-02 00:07:00 +03:00
|
|
|
ImageCacheKey::ImageCacheKey(ImageURL* aURI,
|
|
|
|
const PrincipalOriginAttributes& aAttrs,
|
|
|
|
nsIDocument* aDocument)
|
2015-05-20 20:21:11 +03:00
|
|
|
: mURI(aURI)
|
2016-06-02 00:07:00 +03:00
|
|
|
, mOriginAttributes(aAttrs)
|
2015-10-27 21:12:46 +03:00
|
|
|
, mControlledDocument(GetControlledDocumentToken(aDocument))
|
2015-05-21 04:49:53 +03:00
|
|
|
, mIsChrome(URISchemeIs(mURI, "chrome"))
|
2015-05-20 20:21:09 +03:00
|
|
|
{
|
2015-05-21 04:49:53 +03:00
|
|
|
MOZ_ASSERT(aURI);
|
2015-05-20 20:21:09 +03:00
|
|
|
|
2015-05-21 04:49:53 +03:00
|
|
|
if (URISchemeIs(mURI, "blob")) {
|
|
|
|
mBlobSerial = BlobSerial(mURI);
|
|
|
|
}
|
2015-05-20 20:21:09 +03:00
|
|
|
|
2016-06-02 00:07:00 +03:00
|
|
|
mHash = ComputeHash(mURI, mBlobSerial, mOriginAttributes, mControlledDocument);
|
2015-05-20 20:21:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ImageCacheKey::ImageCacheKey(const ImageCacheKey& aOther)
|
2015-05-20 20:21:11 +03:00
|
|
|
: mURI(aOther.mURI)
|
2015-05-21 04:49:53 +03:00
|
|
|
, mBlobSerial(aOther.mBlobSerial)
|
2016-06-02 00:07:00 +03:00
|
|
|
, mOriginAttributes(aOther.mOriginAttributes)
|
2015-10-27 21:12:46 +03:00
|
|
|
, mControlledDocument(aOther.mControlledDocument)
|
2015-05-20 20:21:09 +03:00
|
|
|
, mHash(aOther.mHash)
|
|
|
|
, mIsChrome(aOther.mIsChrome)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
ImageCacheKey::ImageCacheKey(ImageCacheKey&& aOther)
|
2015-05-20 20:21:11 +03:00
|
|
|
: mURI(Move(aOther.mURI))
|
2015-05-21 04:49:53 +03:00
|
|
|
, mBlobSerial(Move(aOther.mBlobSerial))
|
2016-06-02 00:07:00 +03:00
|
|
|
, mOriginAttributes(aOther.mOriginAttributes)
|
2015-10-27 21:12:46 +03:00
|
|
|
, mControlledDocument(aOther.mControlledDocument)
|
2015-05-20 20:21:09 +03:00
|
|
|
, mHash(aOther.mHash)
|
|
|
|
, mIsChrome(aOther.mIsChrome)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
ImageCacheKey::operator==(const ImageCacheKey& aOther) const
|
|
|
|
{
|
2015-10-27 21:12:46 +03:00
|
|
|
// Don't share the image cache between a controlled document and anything else.
|
|
|
|
if (mControlledDocument != aOther.mControlledDocument) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-02 00:07:00 +03:00
|
|
|
// The origin attributes always have to match.
|
|
|
|
if (mOriginAttributes != aOther.mOriginAttributes) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-21 04:49:53 +03:00
|
|
|
if (mBlobSerial || aOther.mBlobSerial) {
|
2015-06-05 11:52:06 +03: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-21 04:49:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// For non-blob URIs, compare the URIs.
|
2015-05-20 20:21:11 +03:00
|
|
|
return *mURI == *aOther.mURI;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
|
|
|
ImageCacheKey::Spec() const
|
|
|
|
{
|
|
|
|
return mURI->Spec();
|
2015-05-20 20:21:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ uint32_t
|
2015-05-21 04:49:53 +03:00
|
|
|
ImageCacheKey::ComputeHash(ImageURL* aURI,
|
2015-10-27 21:12:46 +03:00
|
|
|
const Maybe<uint64_t>& aBlobSerial,
|
2016-06-02 00:07:00 +03:00
|
|
|
const PrincipalOriginAttributes& aAttrs,
|
2015-10-27 21:12:46 +03:00
|
|
|
void* aControlledDocument)
|
2015-05-20 20:21:09 +03: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-21 04:49:53 +03:00
|
|
|
|
2015-10-27 21:12:46 +03:00
|
|
|
nsPrintfCString ptr("%p", aControlledDocument);
|
2016-06-02 00:07:00 +03:00
|
|
|
nsAutoCString suffix;
|
|
|
|
aAttrs.CreateSuffix(suffix);
|
|
|
|
|
2015-05-21 04:49:53 +03:00
|
|
|
if (aBlobSerial) {
|
|
|
|
// For blob URIs, we hash the serial number of the underlying blob, so that
|
2015-06-05 11:52:06 +03:00
|
|
|
// different blob URIs which point to the same blob share a cache entry. We
|
2015-10-01 03:00:52 +03: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 11:52:06 +03:00
|
|
|
nsAutoCString ref;
|
|
|
|
aURI->GetRef(ref);
|
2016-06-02 00:07:00 +03:00
|
|
|
return HashGeneric(*aBlobSerial, HashString(ref + suffix + ptr));
|
2015-05-21 04:49:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// For non-blob URIs, we hash the URI spec.
|
2015-05-20 20:21:11 +03:00
|
|
|
nsAutoCString spec;
|
|
|
|
aURI->GetSpec(spec);
|
2016-06-02 00:07:00 +03:00
|
|
|
return HashString(spec + suffix + ptr);
|
2015-10-27 21:12:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void*
|
2016-04-11 12:00:03 +03:00
|
|
|
ImageCacheKey::GetControlledDocumentToken(nsIDocument* aDocument)
|
2015-10-27 21:12:46 +03:00
|
|
|
{
|
|
|
|
// For non-controlled documents, we just return null. For controlled
|
|
|
|
// documents, we cast the pointer into a void* to avoid dereferencing
|
|
|
|
// it (since we only use it for comparisons), and return it.
|
|
|
|
void* pointer = nullptr;
|
|
|
|
using dom::workers::ServiceWorkerManager;
|
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
2016-04-11 12:00:03 +03:00
|
|
|
if (aDocument && swm) {
|
2015-10-27 21:12:46 +03:00
|
|
|
ErrorResult rv;
|
2016-04-11 12:00:03 +03:00
|
|
|
if (swm->IsControlled(aDocument, rv)) {
|
|
|
|
pointer = aDocument;
|
2015-10-27 21:12:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return pointer;
|
2015-05-20 20:21:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|