1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "SharedScriptCache.h"
#include "ScriptLoadHandler.h" // ScriptLoadHandler
#include "ScriptLoader.h" // ScriptLoader
#include "mozilla/Maybe.h" // Maybe, Some, Nothing
#include "mozilla/Unused.h" // Unused
#include "mozilla/dom/ContentParent.h" // dom::ContentParent
#include "nsIMemoryReporter.h" // nsIMemoryReporter, MOZ_DEFINE_MALLOC_SIZE_OF, RegisterWeakMemoryReporter, UnregisterWeakMemoryReporter, MOZ_COLLECT_REPORT, KIND_HEAP, UNITS_BYTES
#include "nsIPrefBranch.h" // nsIPrefBranch, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID
#include "nsIPrefService.h" // NS_PREFSERVICE_CONTRACTID
#include "nsIPrincipal.h" // nsIPrincipal
#include "nsISupportsImpl.h" // NS_IMPL_ISUPPORTS
#include "nsStringFwd.h" // nsACString
namespace mozilla::dom {
ScriptHashKey::ScriptHashKey(ScriptLoader* aLoader,
const JS::loader::ScriptLoadRequest* aRequest)
: PLDHashEntryHdr(),
mURI(aRequest->mURI),
mLoaderPrincipal(aLoader->LoaderPrincipal()),
mPartitionPrincipal(aLoader->PartitionedPrincipal()),
mCORSMode(aRequest->CORSMode()),
mSRIMetadata(aRequest->mIntegrity),
mKind(aRequest->mKind),
mNonce(aRequest->Nonce()),
mIsLinkRelPreload(aRequest->GetScriptLoadContext()->IsPreload()) {
if (mKind == JS::loader::ScriptKind::eClassic) {
if (aRequest->GetScriptLoadContext()->HasScriptElement()) {
aRequest->GetScriptLoadContext()->GetHintCharset(mHintCharset);
}
}
MOZ_COUNT_CTOR(ScriptHashKey);
}
ScriptHashKey::ScriptHashKey(const ScriptLoadData& aLoadData)
: ScriptHashKey(aLoadData.CacheKey()) {}
bool ScriptHashKey::KeyEquals(const ScriptHashKey& aKey) const {
if (mKind != aKey.mKind) {
return false;
}
{
bool eq;
if (NS_FAILED(mURI->Equals(aKey.mURI, &eq)) || !eq) {
return false;
}
}
if (!mPartitionPrincipal->Equals(aKey.mPartitionPrincipal)) {
return false;
}
if (mCORSMode != aKey.mCORSMode) {
return false;
}
if (mNonce != aKey.mNonce) {
return false;
}
// NOTE: module always use UTF-8.
if (mKind == JS::loader::ScriptKind::eClassic) {
if (mHintCharset != aKey.mHintCharset) {
return false;
}
}
if (!mSRIMetadata.CanTrustBeDelegatedTo(aKey.mSRIMetadata) ||
!aKey.mSRIMetadata.CanTrustBeDelegatedTo(mSRIMetadata)) {
return false;
}
return true;
}
NS_IMPL_ISUPPORTS(ScriptLoadData, nsISupports)
ScriptLoadData::ScriptLoadData(ScriptLoader* aLoader,
JS::loader::ScriptLoadRequest* aRequest)
: mExpirationTime(aRequest->ExpirationTime()),
mLoader(aLoader),
mKey(aLoader, aRequest),
mLoadedScript(aRequest->getLoadedScript()),
mNetworkMetadata(aRequest->mNetworkMetadata) {}
NS_IMPL_ISUPPORTS(SharedScriptCache, nsIMemoryReporter)
MOZ_DEFINE_MALLOC_SIZE_OF(SharedScriptCacheMallocSizeOf)
SharedScriptCache::SharedScriptCache() = default;
void SharedScriptCache::Init() {
RegisterWeakMemoryReporter(this);
// URL classification (tracking protection etc) are handled inside
// nsHttpChannel.
// The cache reflects the policy for whether to block or not, and once
// the policy is modified, we should discard the cache, to avoid running
// a cached script which is supposed to be blocked.
auto ClearCache = [](const char*, void*) { Clear(); };
Preferences::RegisterPrefixCallback(ClearCache, "urlclassifier.");
Preferences::RegisterCallback(ClearCache,
"privacy.trackingprotection.enabled");
}
SharedScriptCache::~SharedScriptCache() { UnregisterWeakMemoryReporter(this); }
void SharedScriptCache::LoadCompleted(SharedScriptCache* aCache,
ScriptLoadData& aData) {}
NS_IMETHODIMP
SharedScriptCache::CollectReports(nsIHandleReportCallback* aHandleReport,
nsISupports* aData, bool aAnonymize) {
MOZ_COLLECT_REPORT("explicit/js-non-window/cache", KIND_HEAP, UNITS_BYTES,
SharedScriptCacheMallocSizeOf(this) +
SizeOfExcludingThis(SharedScriptCacheMallocSizeOf),
"Memory used for SharedScriptCache to share script "
"across documents");
return NS_OK;
}
void SharedScriptCache::Clear(const Maybe<bool>& aChrome,
const Maybe<nsCOMPtr<nsIPrincipal>>& aPrincipal,
const Maybe<nsCString>& aSchemelessSite,
const Maybe<OriginAttributesPattern>& aPattern,
const Maybe<nsCString>& aURL) {
using ContentParent = dom::ContentParent;
if (XRE_IsParentProcess()) {
for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
Unused << cp->SendClearScriptCache(aChrome, aPrincipal, aSchemelessSite,
aPattern, aURL);
}
}
if (sSingleton) {
sSingleton->ClearInProcess(aChrome, aPrincipal, aSchemelessSite, aPattern,
aURL);
}
}
/* static */
void SharedScriptCache::PrepareForLastCC() {
if (sSingleton) {
sSingleton->mComplete.Clear();
sSingleton->mPending.Clear();
sSingleton->mLoading.Clear();
}
}
} // namespace mozilla::dom
|