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"
|
|
|
|
|
2018-06-06 03:42:56 +03:00
|
|
|
#include "mozilla/HashFunctions.h"
|
2015-05-20 20:21:09 +03:00
|
|
|
#include "mozilla/Move.h"
|
2018-06-20 20:38:22 +03:00
|
|
|
#include "nsContentUtils.h"
|
2017-07-24 06:51:20 +03:00
|
|
|
#include "nsLayoutUtils.h"
|
2015-05-20 20:21:09 +03:00
|
|
|
#include "nsString.h"
|
2018-06-02 16:51:42 +03:00
|
|
|
#include "mozilla/dom/BlobURLProtocolHandler.h"
|
2016-12-01 17:12:42 +03:00
|
|
|
#include "mozilla/dom/File.h"
|
2018-01-27 00:08:59 +03:00
|
|
|
#include "mozilla/dom/ServiceWorkerManager.h"
|
2015-10-27 21:12:46 +03:00
|
|
|
#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
|
|
|
static Maybe<uint64_t>
|
2018-06-06 03:42:56 +03:00
|
|
|
BlobSerial(nsIURI* aURI)
|
2015-05-21 04:49:53 +03:00
|
|
|
{
|
|
|
|
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-08-04 21:22:00 +03:00
|
|
|
ImageCacheKey::ImageCacheKey(nsIURI* aURI,
|
2017-01-12 19:38:48 +03:00
|
|
|
const OriginAttributes& aAttrs,
|
2016-08-29 08:34:32 +03:00
|
|
|
nsIDocument* aDocument,
|
|
|
|
nsresult& aRv)
|
2018-06-06 03:42:56 +03:00
|
|
|
: mURI(aURI)
|
2016-08-04 21:22:00 +03:00
|
|
|
, mOriginAttributes(aAttrs)
|
2018-06-20 20:38:22 +03:00
|
|
|
, mControlledDocument(GetSpecialCaseDocumentToken(aDocument, aURI))
|
2018-06-06 03:42:56 +03:00
|
|
|
, mHash(0)
|
|
|
|
, mIsChrome(false)
|
2015-05-20 20:21:09 +03:00
|
|
|
{
|
2018-06-06 03:42:56 +03:00
|
|
|
if (SchemeIs("blob")) {
|
2015-05-21 04:49:53 +03:00
|
|
|
mBlobSerial = BlobSerial(mURI);
|
2018-06-06 03:42:56 +03:00
|
|
|
} else if (SchemeIs("chrome")) {
|
|
|
|
mIsChrome = true;
|
2015-05-21 04:49:53 +03:00
|
|
|
}
|
2015-05-20 20:21:09 +03:00
|
|
|
|
2018-06-06 03:42:56 +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-20 20:21:09 +03:00
|
|
|
|
2018-06-06 03:42:56 +03:00
|
|
|
nsPrintfCString ptr("%p", mControlledDocument);
|
|
|
|
nsAutoCString suffix;
|
|
|
|
mOriginAttributes.CreateSuffix(suffix);
|
|
|
|
|
|
|
|
if (mBlobSerial) {
|
|
|
|
aRv = mURI->GetRef(mBlobRef);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(aRv);
|
|
|
|
mHash = HashGeneric(*mBlobSerial, HashString(mBlobRef));
|
|
|
|
} else {
|
|
|
|
nsAutoCString spec;
|
|
|
|
aRv = mURI->GetSpec(spec);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(aRv);
|
|
|
|
mHash = HashString(spec);
|
2015-05-21 04:49:53 +03:00
|
|
|
}
|
2015-05-20 20:21:09 +03:00
|
|
|
|
2018-06-06 03:42:56 +03:00
|
|
|
mHash = AddToHash(mHash, HashString(suffix), HashString(ptr));
|
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)
|
2018-06-06 03:42:56 +03:00
|
|
|
, mBlobRef(aOther.mBlobRef)
|
2016-08-04 21:22: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)
|
2018-05-30 22:15:35 +03:00
|
|
|
: mURI(std::move(aOther.mURI))
|
|
|
|
, mBlobSerial(std::move(aOther.mBlobSerial))
|
2018-06-06 03:42:56 +03:00
|
|
|
, mBlobRef(std::move(aOther.mBlobRef))
|
2016-08-04 21:22: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-08-04 21:22: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 &&
|
2018-06-06 03:42:56 +03:00
|
|
|
mBlobRef == aOther.mBlobRef;
|
2015-05-21 04:49:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// For non-blob URIs, compare the URIs.
|
2018-06-06 03:42:56 +03:00
|
|
|
bool equals = false;
|
|
|
|
nsresult rv = mURI->Equals(aOther.mURI, &equals);
|
|
|
|
return NS_SUCCEEDED(rv) && equals;
|
2015-05-20 20:21:09 +03:00
|
|
|
}
|
|
|
|
|
2018-06-06 03:42:56 +03:00
|
|
|
bool
|
|
|
|
ImageCacheKey::SchemeIs(const char* aScheme)
|
2015-05-20 20:21:09 +03:00
|
|
|
{
|
2018-06-06 03:42:56 +03:00
|
|
|
bool matches = false;
|
|
|
|
return NS_SUCCEEDED(mURI->SchemeIs(aScheme, &matches)) && matches;
|
2015-10-27 21:12:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void*
|
2018-06-20 20:38:22 +03:00
|
|
|
ImageCacheKey::GetSpecialCaseDocumentToken(nsIDocument* aDocument, nsIURI* aURI)
|
2015-10-27 21:12:46 +03:00
|
|
|
{
|
2018-06-20 20:38:22 +03:00
|
|
|
// For controlled documents, we cast the pointer into a void* to avoid
|
|
|
|
// dereferencing it (since we only use it for comparisons).
|
2015-10-27 21:12:46 +03:00
|
|
|
void* pointer = nullptr;
|
|
|
|
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;
|
2017-12-20 18:53:18 +03:00
|
|
|
if (aDocument->GetController().isSome()) {
|
2016-04-11 12:00:03 +03:00
|
|
|
pointer = aDocument;
|
2015-10-27 21:12:46 +03:00
|
|
|
}
|
|
|
|
}
|
2018-06-20 20:38:22 +03:00
|
|
|
|
|
|
|
// If this document has been marked as tracker, let's use its address to make
|
|
|
|
// a unique cache key.
|
|
|
|
if (!pointer && aDocument &&
|
2018-07-10 11:32:34 +03:00
|
|
|
nsContentUtils::StorageDisabledByAntiTracking(nullptr,
|
|
|
|
aDocument->GetChannel(),
|
|
|
|
aURI)) {
|
2018-06-20 20:38:22 +03:00
|
|
|
pointer = aDocument;
|
|
|
|
}
|
|
|
|
|
2015-10-27 21:12:46 +03:00
|
|
|
return pointer;
|
2015-05-20 20:21:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|