Bug 1383215: Part 2 - Split out URI resolution code into ResolveURI helper. r?mccr8 draft
authorKris Maglione <maglione.k@gmail.com>
Fri, 21 Jul 2017 15:12:32 -0700
changeset 613438 2758832556ae4acc1fff9f9947a0a99e3e175041
parent 613437 222ee87757319b9d7ea6bf6d8e460c75166558ca
child 613439 21d94c910f5cc4cc199b472f005a18fe9295e71c
push id69800
push usermaglione.k@gmail.com
push dateFri, 21 Jul 2017 23:03:45 +0000
reviewersmccr8
bugs1383215
milestone56.0a1
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_