diff --git a/dom/base/nsFrameMessageManager.cpp b/dom/base/nsFrameMessageManager.cpp index 754af8a4092b..246e4b3b55bc 100644 --- a/dom/base/nsFrameMessageManager.cpp +++ b/dom/base/nsFrameMessageManager.cpp @@ -69,7 +69,6 @@ #include "mozilla/dom/MessageManagerCallback.h" #include "mozilla/dom/ipc/SharedMap.h" #include "mozilla/dom/ipc/StructuredCloneData.h" -#include "mozilla/scache/StartupCacheUtils.h" #include "nsASCIIMask.h" #include "nsBaseHashtable.h" #include "nsCOMPtr.h" @@ -131,8 +130,6 @@ using namespace mozilla; using namespace mozilla::dom; using namespace mozilla::dom::ipc; -#define CACHE_PREFIX(type) "mm/" type - nsFrameMessageManager::nsFrameMessageManager(MessageManagerCallback* aCallback, MessageManagerFlags aFlags) : mChrome(aFlags & MessageManagerFlags::MM_CHROME), @@ -1275,14 +1272,10 @@ nsMessageManagerScriptExecutor::TryCacheLoadAndCompileScript( RefPtr stencil; if (useScriptPreloader) { - nsAutoCString cachePath; - rv = scache::PathifyURI(CACHE_PREFIX("script"), uri, cachePath); - NS_ENSURE_SUCCESS(rv, nullptr); - JS::DecodeOptions decodeOptions; ScriptPreloader::FillDecodeOptionsForCachedStencil(decodeOptions); stencil = ScriptPreloader::GetChildSingleton().GetCachedStencil( - cx, decodeOptions, cachePath); + cx, decodeOptions, url); } if (!stencil) { @@ -1357,10 +1350,7 @@ nsMessageManagerScriptExecutor::TryCacheLoadAndCompileScript( MOZ_ASSERT(stencil); if (useScriptPreloader) { - nsAutoCString cachePath; - rv = scache::PathifyURI(CACHE_PREFIX("script"), uri, cachePath); - NS_ENSURE_SUCCESS(rv, nullptr); - ScriptPreloader::GetChildSingleton().NoteStencil(url, cachePath, stencil, + ScriptPreloader::GetChildSingleton().NoteStencil(url, url, stencil, isRunOnce); } diff --git a/dom/prototype/PrototypeDocumentContentSink.cpp b/dom/prototype/PrototypeDocumentContentSink.cpp index e52efb9eeae3..b8fd5a59e646 100644 --- a/dom/prototype/PrototypeDocumentContentSink.cpp +++ b/dom/prototype/PrototypeDocumentContentSink.cpp @@ -671,8 +671,7 @@ nsresult PrototypeDocumentContentSink::DoneWalking() { if (IsChromeURI(mDocumentURI) && nsXULPrototypeCache::GetInstance()->IsEnabled()) { bool isCachedOnDisk; - nsXULPrototypeCache::GetInstance()->HasPrototype(mDocumentURI, - &isCachedOnDisk); + nsXULPrototypeCache::GetInstance()->HasData(mDocumentURI, &isCachedOnDisk); if (!isCachedOnDisk) { nsXULPrototypeCache::GetInstance()->WritePrototype(mCurrentPrototype); } diff --git a/dom/xul/nsXULElement.cpp b/dom/xul/nsXULElement.cpp index 05306107f151..40ece19bc27d 100644 --- a/dom/xul/nsXULElement.cpp +++ b/dom/xul/nsXULElement.cpp @@ -1670,7 +1670,7 @@ nsresult nsXULPrototypeScript::SerializeOutOfLine( NS_ASSERTION(cache->IsEnabled(), "writing to the cache file, but the XUL cache is off?"); bool exists; - cache->HasScript(mSrcURI, &exists); + cache->HasData(mSrcURI, &exists); /* return will be NS_OK from GetAsciiSpec. * that makes no sense. @@ -1680,14 +1680,14 @@ nsresult nsXULPrototypeScript::SerializeOutOfLine( if (exists) return NS_OK; nsCOMPtr oos; - nsresult rv = cache->GetScriptOutputStream(mSrcURI, getter_AddRefs(oos)); + nsresult rv = cache->GetOutputStream(mSrcURI, getter_AddRefs(oos)); NS_ENSURE_SUCCESS(rv, rv); nsresult tmp = Serialize(oos, aProtoDoc, nullptr); if (NS_FAILED(tmp)) { rv = tmp; } - tmp = cache->FinishScriptOutputStream(mSrcURI); + tmp = cache->FinishOutputStream(mSrcURI); if (NS_FAILED(tmp)) { rv = tmp; } @@ -1754,7 +1754,7 @@ nsresult nsXULPrototypeScript::DeserializeOutOfLine( if (!mStencil) { if (mSrcURI) { - rv = cache->GetScriptInputStream(mSrcURI, getter_AddRefs(objectInput)); + rv = cache->GetInputStream(mSrcURI, getter_AddRefs(objectInput)); } // If !mSrcURI, we have an inline script. We shouldn't have // to do anything else in that case, I think. @@ -1772,7 +1772,7 @@ nsresult nsXULPrototypeScript::DeserializeOutOfLine( if (useXULCache && mSrcURI && mSrcURI->SchemeIs("chrome")) { cache->PutStencil(mSrcURI, GetStencil()); } - cache->FinishScriptInputStream(mSrcURI); + cache->FinishInputStream(mSrcURI); } else { // If mSrcURI is not in the cache, // rv will be NS_ERROR_NOT_AVAILABLE and we'll try to diff --git a/dom/xul/nsXULPrototypeCache.cpp b/dom/xul/nsXULPrototypeCache.cpp index 358eff2378b8..ab642db1f484 100644 --- a/dom/xul/nsXULPrototypeCache.cpp +++ b/dom/xul/nsXULPrototypeCache.cpp @@ -37,7 +37,7 @@ using namespace mozilla::scache; using mozilla::intl::LocaleService; static const char kXULCacheInfoKey[] = "nsXULPrototypeCache.startupCache"; -#define CACHE_PREFIX(aCompilationTarget) "xulcache/" aCompilationTarget +static const char kXULCachePrefix[] = "xulcache"; static void DisableXULCacheChangedCallback(const char* aPref, void* aClosure) { if (nsXULPrototypeCache* cache = nsXULPrototypeCache::GetInstance()) { @@ -110,7 +110,7 @@ nsXULPrototypeDocument* nsXULPrototypeCache::GetPrototype(nsIURI* aURI) { // No prototype in XUL memory cache. Spin up the cache Service. nsCOMPtr ois; - rv = GetPrototypeInputStream(aURI, getter_AddRefs(ois)); + rv = GetInputStream(aURI, getter_AddRefs(ois)); if (NS_FAILED(rv)) { return nullptr; } @@ -197,29 +197,19 @@ nsresult nsXULPrototypeCache::WritePrototype( nsCOMPtr protoURI = aPrototypeDocument->GetURI(); nsCOMPtr oos; - rv = GetPrototypeOutputStream(protoURI, getter_AddRefs(oos)); + rv = GetOutputStream(protoURI, getter_AddRefs(oos)); NS_ENSURE_SUCCESS(rv, rv); rv = aPrototypeDocument->Write(oos); NS_ENSURE_SUCCESS(rv, rv); - FinishPrototypeOutputStream(protoURI); + FinishOutputStream(protoURI); return NS_FAILED(rv) ? rv : rv2; } -static nsresult PathifyURIForType(nsXULPrototypeCache::CacheType cacheType, - nsIURI* in, nsACString& out) { - switch (cacheType) { - case nsXULPrototypeCache::CacheType::Prototype: - return PathifyURI(CACHE_PREFIX("proto"), in, out); - case nsXULPrototypeCache::CacheType::Script: - return PathifyURI(CACHE_PREFIX("script"), in, out); - } -} - -nsresult nsXULPrototypeCache::GetInputStream(CacheType cacheType, nsIURI* uri, +nsresult nsXULPrototypeCache::GetInputStream(nsIURI* uri, nsIObjectInputStream** stream) { - nsAutoCString spec; - nsresult rv = PathifyURIForType(cacheType, uri, spec); + nsAutoCString spec(kXULCachePrefix); + nsresult rv = PathifyURI(uri, spec); if (NS_FAILED(rv)) return NS_ERROR_NOT_AVAILABLE; const char* buf; @@ -271,8 +261,7 @@ nsresult nsXULPrototypeCache::GetOutputStream(nsIURI* uri, return NS_OK; } -nsresult nsXULPrototypeCache::FinishOutputStream(CacheType cacheType, - nsIURI* uri) { +nsresult nsXULPrototypeCache::FinishOutputStream(nsIURI* uri) { nsresult rv; StartupCache* sc = StartupCache::GetSingleton(); if (!sc) return NS_ERROR_NOT_AVAILABLE; @@ -289,8 +278,8 @@ nsresult nsXULPrototypeCache::FinishOutputStream(CacheType cacheType, NS_ENSURE_SUCCESS(rv, rv); if (!mStartupCacheURITable.GetEntry(uri)) { - nsAutoCString spec; - rv = PathifyURIForType(cacheType, uri, spec); + nsAutoCString spec(kXULCachePrefix); + rv = PathifyURI(uri, spec); if (NS_FAILED(rv)) return NS_ERROR_NOT_AVAILABLE; rv = sc->PutBuffer(spec.get(), std::move(buf), len); if (NS_SUCCEEDED(rv)) { @@ -304,14 +293,13 @@ nsresult nsXULPrototypeCache::FinishOutputStream(CacheType cacheType, // We have data if we're in the middle of writing it or we already // have it in the cache. -nsresult nsXULPrototypeCache::HasData(CacheType cacheType, nsIURI* uri, - bool* exists) { +nsresult nsXULPrototypeCache::HasData(nsIURI* uri, bool* exists) { if (mOutputStreamTable.Get(uri, nullptr)) { *exists = true; return NS_OK; } - nsAutoCString spec; - nsresult rv = PathifyURIForType(cacheType, uri, spec); + nsAutoCString spec(kXULCachePrefix); + nsresult rv = PathifyURI(uri, spec); if (NS_FAILED(rv)) { *exists = false; return NS_OK; diff --git a/dom/xul/nsXULPrototypeCache.h b/dom/xul/nsXULPrototypeCache.h index eac697887752..e3fefa0a00ba 100644 --- a/dom/xul/nsXULPrototypeCache.h +++ b/dom/xul/nsXULPrototypeCache.h @@ -34,8 +34,6 @@ class StyleSheet; */ class nsXULPrototypeCache : public nsIObserver { public: - enum class CacheType { Prototype, Script }; - // nsISupports NS_DECL_THREADSAFE_ISUPPORTS NS_DECL_NSIOBSERVER @@ -73,52 +71,12 @@ class nsXULPrototypeCache : public nsIObserver { * This interface allows partial reads and writes from the buffers in the * startupCache. */ - - inline nsresult GetPrototypeInputStream(nsIURI* aURI, - nsIObjectInputStream** objectInput) { - return GetInputStream(CacheType::Prototype, aURI, objectInput); - } - inline nsresult GetScriptInputStream(nsIURI* aURI, - nsIObjectInputStream** objectInput) { - return GetInputStream(CacheType::Script, aURI, objectInput); - } - inline nsresult FinishScriptInputStream(nsIURI* aURI) { - return FinishInputStream(aURI); - } - - inline nsresult GetPrototypeOutputStream( - nsIURI* aURI, nsIObjectOutputStream** objectOutput) { - return GetOutputStream(aURI, objectOutput); - } - inline nsresult GetScriptOutputStream(nsIURI* aURI, - nsIObjectOutputStream** objectOutput) { - return GetOutputStream(aURI, objectOutput); - } - - inline nsresult FinishPrototypeOutputStream(nsIURI* aURI) { - return FinishOutputStream(CacheType::Prototype, aURI); - } - inline nsresult FinishScriptOutputStream(nsIURI* aURI) { - return FinishOutputStream(CacheType::Script, aURI); - } - - inline nsresult HasPrototype(nsIURI* aURI, bool* exists) { - return HasData(CacheType::Prototype, aURI, exists); - } - inline nsresult HasScript(nsIURI* aURI, bool* exists) { - return HasData(CacheType::Script, aURI, exists); - } - - private: - nsresult GetInputStream(CacheType cacheType, nsIURI* uri, - nsIObjectInputStream** stream); + nsresult GetInputStream(nsIURI* aURI, nsIObjectInputStream** objectInput); nsresult FinishInputStream(nsIURI* aURI); - nsresult GetOutputStream(nsIURI* aURI, nsIObjectOutputStream** objectOutput); - nsresult FinishOutputStream(CacheType cacheType, nsIURI* aURI); - nsresult HasData(CacheType cacheType, nsIURI* aURI, bool* exists); + nsresult FinishOutputStream(nsIURI* aURI); + nsresult HasData(nsIURI* aURI, bool* exists); - public: static nsXULPrototypeCache* GetInstance(); static nsXULPrototypeCache* MaybeGetInstance() { return sInstance; } diff --git a/js/xpconnect/loader/mozJSComponentLoader.cpp b/js/xpconnect/loader/mozJSComponentLoader.cpp index 1380d100bbee..104f260f1779 100644 --- a/js/xpconnect/loader/mozJSComponentLoader.cpp +++ b/js/xpconnect/loader/mozJSComponentLoader.cpp @@ -75,8 +75,7 @@ using namespace mozilla::loader; using namespace xpc; using namespace JS; -#define JS_CACHE_PREFIX(aScopeType, aCompilationTarget) \ - "jsloader/" aScopeType "/" aCompilationTarget +#define JS_CACHE_PREFIX(aType) "jsloader/" aType /** * Buffer sizes for serialization and deserialization of scripts. @@ -748,9 +747,8 @@ nsresult mozJSComponentLoader::ObjectForLocation( aInfo.EnsureResolvedURI(); - nsAutoCString cachePath; - rv = PathifyURI(JS_CACHE_PREFIX("non-syntactic", "script"), - aInfo.ResolvedURI(), cachePath); + nsAutoCString cachePath(JS_CACHE_PREFIX("non-syntactic")); + rv = PathifyURI(aInfo.ResolvedURI(), cachePath); NS_ENSURE_SUCCESS(rv, rv); JS::DecodeOptions decodeOptions; diff --git a/js/xpconnect/loader/mozJSSubScriptLoader.cpp b/js/xpconnect/loader/mozJSSubScriptLoader.cpp index fcc85605e145..5f1331e1a84b 100644 --- a/js/xpconnect/loader/mozJSSubScriptLoader.cpp +++ b/js/xpconnect/loader/mozJSSubScriptLoader.cpp @@ -81,8 +81,7 @@ mozJSSubScriptLoader::~mozJSSubScriptLoader() = default; NS_IMPL_ISUPPORTS(mozJSSubScriptLoader, mozIJSSubScriptLoader) -#define JSSUB_CACHE_PREFIX(aScopeType, aCompilationTarget) \ - "jssubloader/" aScopeType "/" aCompilationTarget +#define JSSUB_CACHE_PREFIX(aType) "jssubloader/" aType static void SubscriptCachePath(JSContext* cx, nsIURI* uri, JS::HandleObject targetObj, @@ -90,10 +89,11 @@ static void SubscriptCachePath(JSContext* cx, nsIURI* uri, // StartupCache must distinguish between non-syntactic vs global when // computing the cache key. if (!JS_IsGlobalObject(targetObj)) { - PathifyURI(JSSUB_CACHE_PREFIX("non-syntactic", "script"), uri, cachePath); + cachePath.AssignLiteral(JSSUB_CACHE_PREFIX("non-syntactic")); } else { - PathifyURI(JSSUB_CACHE_PREFIX("global", "script"), uri, cachePath); + cachePath.AssignLiteral(JSSUB_CACHE_PREFIX("global")); } + PathifyURI(uri, cachePath); } static void ReportError(JSContext* cx, const nsACString& msg) { diff --git a/startupcache/StartupCacheUtils.cpp b/startupcache/StartupCacheUtils.cpp index 34ca60204550..8be15d896bf5 100644 --- a/startupcache/StartupCacheUtils.cpp +++ b/startupcache/StartupCacheUtils.cpp @@ -166,7 +166,32 @@ nsresult ResolveURI(nsIURI* in, nsIURI** out) { return NS_OK; } -static nsresult PathifyURIImpl(nsIURI* in, nsACString& out) { +/** + * PathifyURI transforms uris into useful zip paths + * to make it easier to manipulate startup cache entries + * using standard zip tools. + * Transformations applied: + * * resource:// URIs are resolved to their corresponding file/jar URI to + * canonicalize resources URIs other than gre and app. + * * Paths under GRE or APP directory have their base path replaced with + * resource/gre or resource/app to avoid depending on install location. + * * jar:file:///path/to/file.jar!/sub/path urls are replaced with + * /path/to/file.jar/sub/path + * + * The result is appended to the string passed in. Adding a prefix before + * calling is recommended to avoid colliding with other cache users. + * + * For example, in the js loader (string is prefixed with jsloader by caller): + * resource://gre/modules/XPCOMUtils.jsm or + * file://$GRE_DIR/modules/XPCOMUtils.jsm or + * jar:file://$GRE_DIR/omni.jar!/modules/XPCOMUtils.jsm becomes + * jsloader/resource/gre/modules/XPCOMUtils.jsm + * file://$PROFILE_DIR/extensions/{uuid}/components/component.js becomes + * jsloader/$PROFILE_DIR/extensions/%7Buuid%7D/components/component.js + * jar:file://$PROFILE_DIR/extensions/some.xpi!/components/component.js becomes + * jsloader/$PROFILE_DIR/extensions/some.xpi/components/component.js + */ +nsresult PathifyURI(nsIURI* in, nsACString& out) { nsCOMPtr uri; nsresult rv = ResolveURI(in, getter_AddRefs(uri)); NS_ENSURE_SUCCESS(rv, rv); @@ -194,7 +219,7 @@ static nsresult PathifyURIImpl(nsIURI* in, nsACString& out) { rv = jarURI->GetJARFile(getter_AddRefs(jarFileURI)); NS_ENSURE_SUCCESS(rv, rv); - rv = PathifyURIImpl(jarFileURI, out); + rv = PathifyURI(jarFileURI, out); NS_ENSURE_SUCCESS(rv, rv); nsAutoCString path; @@ -213,12 +238,5 @@ static nsresult PathifyURIImpl(nsIURI* in, nsACString& out) { return NS_OK; } -nsresult PathifyURI(const char* loaderType, size_t loaderTypeLength, nsIURI* in, - nsACString& out) { - out.AssignASCII(loaderType, loaderTypeLength); - - return PathifyURIImpl(in, out); -} - } // namespace scache } // namespace mozilla diff --git a/startupcache/StartupCacheUtils.h b/startupcache/StartupCacheUtils.h index e92f1542c8f2..ebd00d951c48 100644 --- a/startupcache/StartupCacheUtils.h +++ b/startupcache/StartupCacheUtils.h @@ -37,38 +37,7 @@ nsresult NewBufferFromStorageStream(nsIStorageStream* storageStream, nsresult ResolveURI(nsIURI* in, nsIURI** out); -// PathifyURI transforms uris into useful zip paths -// to make it easier to manipulate startup cache entries -// using standard zip tools. -// -// Transformations applied: -// * resource:// URIs are resolved to their corresponding file/jar URI to -// canonicalize resources URIs other than gre and app. -// * Paths under GRE or APP directory have their base path replaced with -// resource/gre or resource/app to avoid depending on install location. -// * jar:file:///path/to/file.jar!/sub/path urls are replaced with -// /path/to/file.jar/sub/path -// -// The result is concatenated with loaderType and stored into the string -// passed in. -// -// For example, in the js loader (loaderType = "jsloader"): -// resource://gre/modules/XPCOMUtils.jsm or -// file://$GRE_DIR/modules/XPCOMUtils.jsm or -// jar:file://$GRE_DIR/omni.jar!/modules/XPCOMUtils.jsm becomes -// jsloader/resource/gre/modules/XPCOMUtils.jsm -// file://$PROFILE_DIR/extensions/{uuid}/components/component.js becomes -// jsloader/$PROFILE_DIR/extensions/%7Buuid%7D/components/component.js -// jar:file://$PROFILE_DIR/extensions/some.xpi!/components/component.js becomes -// jsloader/$PROFILE_DIR/extensions/some.xpi/components/component.js -nsresult PathifyURI(const char* loaderType, size_t loaderTypeLength, nsIURI* in, - nsACString& out); - -template -nsresult PathifyURI(const char (&loaderType)[N], nsIURI* in, nsACString& out) { - return PathifyURI(loaderType, N - 1, in, out); -} - +nsresult PathifyURI(nsIURI* in, nsACString& out); } // namespace scache } // namespace mozilla