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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
/* -*- 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/. */
#ifndef mozilla_dom_SharedScriptCache_h
#define mozilla_dom_SharedScriptCache_h
#include "PLDHashTable.h" // PLDHashEntryHdr
#include "js/loader/LoadedScript.h" // JS::loader::LoadedScript
#include "js/loader/ScriptKind.h" // JS::loader::ScriptKind
#include "js/loader/ScriptLoadRequest.h" // JS::loader::ScriptLoadRequest
#include "mozilla/CORSMode.h" // mozilla::CORSMode
#include "mozilla/MemoryReporting.h" // MallocSizeOf
#include "mozilla/RefPtr.h" // RefPtr
#include "mozilla/SharedSubResourceCache.h" // SharedSubResourceCache, SharedSubResourceCacheLoadingValueBase, SubResourceNetworkMetadataHolder
#include "mozilla/WeakPtr.h" // SupportsWeakPtr
#include "mozilla/dom/CacheExpirationTime.h" // CacheExpirationTime
#include "mozilla/dom/SRIMetadata.h" // mozilla::dom::SRIMetadata
#include "nsIMemoryReporter.h" // nsIMemoryReporter, NS_DECL_NSIMEMORYREPORTER
#include "nsIPrincipal.h" // nsIPrincipal
#include "nsISupports.h" // nsISupports, NS_DECL_ISUPPORTS
#include "nsStringFwd.h" // nsACString
namespace mozilla {
namespace dom {
class ScriptLoader;
class ScriptLoadData;
class ScriptHashKey : public PLDHashEntryHdr {
public:
using KeyType = const ScriptHashKey&;
using KeyTypePointer = const ScriptHashKey*;
explicit ScriptHashKey(const ScriptHashKey& aKey)
: PLDHashEntryHdr(),
mURI(aKey.mURI),
mLoaderPrincipal(aKey.mLoaderPrincipal),
mPartitionPrincipal(aKey.mPartitionPrincipal),
mCORSMode(aKey.mCORSMode),
mSRIMetadata(aKey.mSRIMetadata),
mKind(aKey.mKind),
mNonce(aKey.mNonce),
mHintCharset(aKey.mHintCharset),
mIsLinkRelPreload(aKey.mIsLinkRelPreload) {
MOZ_COUNT_CTOR(ScriptHashKey);
}
explicit ScriptHashKey(const ScriptHashKey* aKey) : ScriptHashKey(*aKey) {}
ScriptHashKey(ScriptHashKey&& aKey)
: PLDHashEntryHdr(),
mURI(std::move(aKey.mURI)),
mLoaderPrincipal(std::move(aKey.mLoaderPrincipal)),
mPartitionPrincipal(std::move(aKey.mPartitionPrincipal)),
mCORSMode(std::move(aKey.mCORSMode)),
mSRIMetadata(std::move(aKey.mSRIMetadata)),
mKind(std::move(aKey.mKind)),
mNonce(std::move(aKey.mNonce)),
mHintCharset(std::move(aKey.mHintCharset)),
mIsLinkRelPreload(std::move(aKey.mIsLinkRelPreload)) {
MOZ_COUNT_CTOR(ScriptHashKey);
}
ScriptHashKey(ScriptLoader* aLoader,
const JS::loader::ScriptLoadRequest* aRequest);
explicit ScriptHashKey(const ScriptLoadData& aLoadData);
MOZ_COUNTED_DTOR(ScriptHashKey)
const ScriptHashKey& GetKey() const { return *this; }
const ScriptHashKey* GetKeyPointer() const { return this; }
bool KeyEquals(const ScriptHashKey* aKey) const { return KeyEquals(*aKey); }
bool KeyEquals(const ScriptHashKey&) const;
static const ScriptHashKey* KeyToPointer(const ScriptHashKey& aKey) {
return &aKey;
}
static PLDHashNumber HashKey(const ScriptHashKey* aKey) {
return nsURIHashKey::HashKey(aKey->mURI);
}
nsIPrincipal* LoaderPrincipal() const { return mLoaderPrincipal; }
nsIPrincipal* PartitionPrincipal() const { return mPartitionPrincipal; }
nsIURI* URI() const { return mURI; }
enum { ALLOW_MEMMOVE = true };
protected:
const nsCOMPtr<nsIURI> mURI;
const nsCOMPtr<nsIPrincipal> mLoaderPrincipal;
const nsCOMPtr<nsIPrincipal> mPartitionPrincipal;
const CORSMode mCORSMode;
const SRIMetadata mSRIMetadata;
const JS::loader::ScriptKind mKind;
const nsString mNonce;
// charset attribute for classic script.
// module always use UTF-8.
nsString mHintCharset;
// TODO: Reflect URL classifier data source.
// mozilla::dom::ContentType
// maybe implicit
// top-level document's host
// maybe part of principal?
// what if it's inside frame in different host?
const bool mIsLinkRelPreload;
};
class ScriptLoadData final
: public SupportsWeakPtr,
public nsISupports,
public SharedSubResourceCacheLoadingValueBase<ScriptLoadData> {
protected:
~ScriptLoadData() {}
public:
ScriptLoadData(ScriptLoader* aLoader,
JS::loader::ScriptLoadRequest* aRequest);
NS_DECL_ISUPPORTS
// Only completed loads are used for the cache.
bool IsLoading() const override { return false; }
bool IsCancelled() const override { return false; }
bool IsSyncLoad() const override { return true; }
SubResourceNetworkMetadataHolder* GetNetworkMetadata() const override {
return mNetworkMetadata.get();
}
void StartLoading() override {}
void SetLoadCompleted() override {}
void OnCoalescedTo(const ScriptLoadData& aExistingLoad) override {}
void Cancel() override {}
void DidCancelLoad() {}
bool ShouldDefer() const { return false; }
JS::loader::LoadedScript* ValueForCache() const {
return mLoadedScript.get();
}
const CacheExpirationTime& ExpirationTime() const { return mExpirationTime; }
ScriptLoader& Loader() { return *mLoader; }
const ScriptHashKey& CacheKey() const { return mKey; }
private:
CacheExpirationTime mExpirationTime = CacheExpirationTime::Never();
ScriptLoader* mLoader;
ScriptHashKey mKey;
RefPtr<JS::loader::LoadedScript> mLoadedScript;
RefPtr<SubResourceNetworkMetadataHolder> mNetworkMetadata;
};
struct SharedScriptCacheTraits {
using Loader = ScriptLoader;
using Key = ScriptHashKey;
using Value = JS::loader::LoadedScript;
using LoadingValue = ScriptLoadData;
static ScriptHashKey KeyFromLoadingValue(const LoadingValue& aValue) {
return ScriptHashKey(aValue);
}
};
class SharedScriptCache final
: public SharedSubResourceCache<SharedScriptCacheTraits, SharedScriptCache>,
public nsIMemoryReporter {
public:
using Base =
SharedSubResourceCache<SharedScriptCacheTraits, SharedScriptCache>;
NS_DECL_ISUPPORTS
NS_DECL_NSIMEMORYREPORTER
SharedScriptCache();
void Init();
// This has to be static because it's also called for loaders that don't have
// a sheet cache (loaders that are not owned by a document).
static void LoadCompleted(SharedScriptCache*, ScriptLoadData&);
using Base::LoadCompleted;
static void Clear(const Maybe<bool>& aChrome = Nothing(),
const Maybe<nsCOMPtr<nsIPrincipal>>& aPrincipal = Nothing(),
const Maybe<nsCString>& aSchemelessSite = Nothing(),
const Maybe<OriginAttributesPattern>& aPattern = Nothing(),
const Maybe<nsCString>& aURL = Nothing());
static void PrepareForLastCC();
protected:
~SharedScriptCache();
};
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_SharedScriptCache_h
|