Bug 1383215: Part 2 - Split out URI resolution code into ResolveURI helper. r=mccr8
authorKris Maglione <maglione.k@gmail.com>
Fri, 21 Jul 2017 15:12:32 -0700
changeset 421896 93ef01e8aee53dc19814a9040fd503853fe12bae
parent 421895 188f217f41e6eccc06ebf3cad6fd00d8a2bc390c
child 421897 5cfde6ac518b8a8704c090f4f46bbb0996665b72
push id1517
push userjlorenzo@mozilla.com
push dateThu, 14 Sep 2017 16:50:54 +0000
treeherdermozilla-release@3b41fd564418 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmccr8
bugs1383215
milestone56.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1383215: Part 2 - Split out URI resolution code into ResolveURI helper. r=mccr8 MozReview-Commit-ID: Bfr67WQPq9l
startupcache/StartupCacheUtils.cpp
startupcache/StartupCacheUtils.h
--- 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_