Skip to content

Commit

Permalink
Bug 1752212 - Part 1: Pass loader type to PathifyURI. r=nbp
Browse files Browse the repository at this point in the history
  • Loading branch information
arai-a committed Apr 13, 2022
1 parent 7229252 commit c53ade8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 39 deletions.
12 changes: 6 additions & 6 deletions dom/xul/nsXULPrototypeCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ nsresult nsXULPrototypeCache::WritePrototype(

nsresult nsXULPrototypeCache::GetInputStream(nsIURI* uri,
nsIObjectInputStream** stream) {
nsAutoCString spec(kXULCachePrefix);
nsresult rv = PathifyURI(uri, spec);
nsAutoCString spec;
nsresult rv = PathifyURI(kXULCachePrefix, uri, spec);
if (NS_FAILED(rv)) return NS_ERROR_NOT_AVAILABLE;

const char* buf;
Expand Down Expand Up @@ -278,8 +278,8 @@ nsresult nsXULPrototypeCache::FinishOutputStream(nsIURI* uri) {
NS_ENSURE_SUCCESS(rv, rv);

if (!mStartupCacheURITable.GetEntry(uri)) {
nsAutoCString spec(kXULCachePrefix);
rv = PathifyURI(uri, spec);
nsAutoCString spec;
rv = PathifyURI(kXULCachePrefix, uri, spec);
if (NS_FAILED(rv)) return NS_ERROR_NOT_AVAILABLE;
rv = sc->PutBuffer(spec.get(), std::move(buf), len);
if (NS_SUCCEEDED(rv)) {
Expand All @@ -298,8 +298,8 @@ nsresult nsXULPrototypeCache::HasData(nsIURI* uri, bool* exists) {
*exists = true;
return NS_OK;
}
nsAutoCString spec(kXULCachePrefix);
nsresult rv = PathifyURI(uri, spec);
nsAutoCString spec;
nsresult rv = PathifyURI(kXULCachePrefix, uri, spec);
if (NS_FAILED(rv)) {
*exists = false;
return NS_OK;
Expand Down
5 changes: 3 additions & 2 deletions js/xpconnect/loader/mozJSComponentLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -747,8 +747,9 @@ nsresult mozJSComponentLoader::ObjectForLocation(

aInfo.EnsureResolvedURI();

nsAutoCString cachePath(JS_CACHE_PREFIX("non-syntactic"));
rv = PathifyURI(aInfo.ResolvedURI(), cachePath);
nsAutoCString cachePath;
rv = PathifyURI(JS_CACHE_PREFIX("non-syntactic"), aInfo.ResolvedURI(),
cachePath);
NS_ENSURE_SUCCESS(rv, rv);

JS::DecodeOptions decodeOptions;
Expand Down
5 changes: 2 additions & 3 deletions js/xpconnect/loader/mozJSSubScriptLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,10 @@ static void SubscriptCachePath(JSContext* cx, nsIURI* uri,
// StartupCache must distinguish between non-syntactic vs global when
// computing the cache key.
if (!JS_IsGlobalObject(targetObj)) {
cachePath.AssignLiteral(JSSUB_CACHE_PREFIX("non-syntactic"));
PathifyURI(JSSUB_CACHE_PREFIX("non-syntactic"), uri, cachePath);
} else {
cachePath.AssignLiteral(JSSUB_CACHE_PREFIX("global"));
PathifyURI(JSSUB_CACHE_PREFIX("global"), uri, cachePath);
}
PathifyURI(uri, cachePath);
}

static void ReportError(JSContext* cx, const nsACString& msg) {
Expand Down
36 changes: 9 additions & 27 deletions startupcache/StartupCacheUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,32 +166,7 @@ nsresult ResolveURI(nsIURI* in, nsIURI** out) {
return NS_OK;
}

/**
* 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) {
static nsresult PathifyURIImpl(nsIURI* in, nsACString& out) {
nsCOMPtr<nsIURI> uri;
nsresult rv = ResolveURI(in, getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, rv);
Expand Down Expand Up @@ -219,7 +194,7 @@ nsresult PathifyURI(nsIURI* in, nsACString& out) {
rv = jarURI->GetJARFile(getter_AddRefs(jarFileURI));
NS_ENSURE_SUCCESS(rv, rv);

rv = PathifyURI(jarFileURI, out);
rv = PathifyURIImpl(jarFileURI, out);
NS_ENSURE_SUCCESS(rv, rv);

nsAutoCString path;
Expand All @@ -238,5 +213,12 @@ nsresult PathifyURI(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
33 changes: 32 additions & 1 deletion startupcache/StartupCacheUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,38 @@ nsresult NewBufferFromStorageStream(nsIStorageStream* storageStream,

nsresult ResolveURI(nsIURI* in, nsIURI** out);

nsresult PathifyURI(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 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 <int N>
nsresult PathifyURI(const char (&loaderType)[N], nsIURI* in, nsACString& out) {
return PathifyURI(loaderType, N - 1, in, out);
}

} // namespace scache
} // namespace mozilla

Expand Down

0 comments on commit c53ade8

Please sign in to comment.