Bug 1383215: Part 2 - Split out URI resolution code into ResolveURI helper. r?mccr8
MozReview-Commit-ID: Bfr67WQPq9l
--- a/startupcache/StartupCacheUtils.cpp
+++ b/startupcache/StartupCacheUtils.cpp
@@ -141,16 +141,57 @@ canonicalizeBase(nsAutoCString &spec,
out.AppendLiteral("/resource/");
out.Append(baseName[underGre ? mozilla::Omnijar::GRE : mozilla::Omnijar::APP]);
out.Append(Substring(spec, underGre ? greBase.Length() : appBase.Length()));
return true;
}
/**
+ * ResolveURI transforms a chrome: or resource: URI into the URI for its
+ * underlying resource, or returns any other URI unchanged.
+ */
+nsresult
+ResolveURI(nsIURI *in, nsIURI **out)
+{
+ bool equals;
+ nsresult rv;
+
+ // Resolve resource:// URIs. At the end of this if/else block, we
+ // have both spec and uri variables identifying the same URI.
+ if (NS_SUCCEEDED(in->SchemeIs("resource", &equals)) && equals) {
+ nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
+ NS_ENSURE_SUCCESS(rv, rv);
+
+ nsCOMPtr<nsIProtocolHandler> ph;
+ rv = ioService->GetProtocolHandler("resource", getter_AddRefs(ph));
+ NS_ENSURE_SUCCESS(rv, rv);
+
+ nsCOMPtr<nsIResProtocolHandler> irph(do_QueryInterface(ph, &rv));
+ NS_ENSURE_SUCCESS(rv, rv);
+
+ nsAutoCString spec;
+ rv = irph->ResolveURI(in, spec);
+ NS_ENSURE_SUCCESS(rv, rv);
+
+ return ioService->NewURI(spec, nullptr, nullptr, out);
+ } else if (NS_SUCCEEDED(in->SchemeIs("chrome", &equals)) && equals) {
+ nsCOMPtr<nsIChromeRegistry> chromeReg =
+ mozilla::services::GetChromeRegistryService();
+ if (!chromeReg)
+ return NS_ERROR_UNEXPECTED;
+
+ return chromeReg->ConvertChromeURL(in, out);
+ }
+
+ *out = do_AddRef(in).take();
+ 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.
@@ -170,51 +211,24 @@ canonicalizeBase(nsAutoCString &spec,
* 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)
{
bool equals;
nsresult rv;
- nsCOMPtr<nsIURI> uri = in;
- nsAutoCString spec;
-
- // Resolve resource:// URIs. At the end of this if/else block, we
- // have both spec and uri variables identifying the same URI.
- if (NS_SUCCEEDED(in->SchemeIs("resource", &equals)) && equals) {
- nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
- NS_ENSURE_SUCCESS(rv, rv);
-
- nsCOMPtr<nsIProtocolHandler> ph;
- rv = ioService->GetProtocolHandler("resource", getter_AddRefs(ph));
- NS_ENSURE_SUCCESS(rv, rv);
-
- nsCOMPtr<nsIResProtocolHandler> irph(do_QueryInterface(ph, &rv));
- NS_ENSURE_SUCCESS(rv, rv);
- rv = irph->ResolveURI(in, spec);
- NS_ENSURE_SUCCESS(rv, rv);
+ nsCOMPtr<nsIURI> uri;
+ rv = ResolveURI(in, getter_AddRefs(uri));
+ NS_ENSURE_SUCCESS(rv, rv);
- rv = ioService->NewURI(spec, nullptr, nullptr, getter_AddRefs(uri));
- NS_ENSURE_SUCCESS(rv, rv);
- } else {
- if (NS_SUCCEEDED(in->SchemeIs("chrome", &equals)) && equals) {
- nsCOMPtr<nsIChromeRegistry> chromeReg =
- mozilla::services::GetChromeRegistryService();
- if (!chromeReg)
- return NS_ERROR_UNEXPECTED;
-
- rv = chromeReg->ConvertChromeURL(in, getter_AddRefs(uri));
- NS_ENSURE_SUCCESS(rv, rv);
- }
-
- rv = uri->GetSpec(spec);
- NS_ENSURE_SUCCESS(rv, rv);
- }
+ nsAutoCString spec;
+ rv = uri->GetSpec(spec);
+ NS_ENSURE_SUCCESS(rv, rv);
if (!canonicalizeBase(spec, out)) {
if (NS_SUCCEEDED(uri->SchemeIs("file", &equals)) && equals) {
nsCOMPtr<nsIFileURL> baseFileURL;
baseFileURL = do_QueryInterface(uri, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString path;
--- a/startupcache/StartupCacheUtils.h
+++ b/startupcache/StartupCacheUtils.h
@@ -34,13 +34,16 @@ NewObjectOutputWrappedStorageStream(nsIO
// Creates a buffer for storing the stream into the cache. The buffer is
// allocated with 'new []'. After calling this function, the caller would
// typically call nsIStartupCache::PutBuffer with the returned buffer.
nsresult
NewBufferFromStorageStream(nsIStorageStream *storageStream,
UniquePtr<char[]>* buffer, uint32_t* len);
nsresult
+ResolveURI(nsIURI *in, nsIURI **out);
+
+nsresult
PathifyURI(nsIURI *in, nsACString &out);
} // namespace scache
} // namespace mozilla
#endif //nsStartupCacheUtils_h_