Bug 1438945 - Remove the string parameter in favor of always passing in an nsIURI. r=asuth
authorBlake Kaplan <mrbkap@gmail.com>
Mon, 19 Nov 2018 15:18:34 -0800
changeset 503581 d71fcd5f9cb5f47d7ffa42d6a59f4523e1811d3a
parent 503580 fab208773c488ca77ce4778ad40598c5bc51c1b2
child 503582 c5751a5efe5beaa4dc82089bc5ede81ee35f3291
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersasuth
bugs1438945
milestone65.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 1438945 - Remove the string parameter in favor of always passing in an nsIURI. r=asuth The duplication wasn't necessary. Differential Revision: https://phabricator.services.mozilla.com/D11827
dom/workers/ScriptLoader.cpp
dom/workers/ScriptLoader.h
dom/workers/WorkerPrivate.cpp
dom/workers/remoteworkers/RemoteWorkerChild.cpp
--- a/dom/workers/ScriptLoader.cpp
+++ b/dom/workers/ScriptLoader.cpp
@@ -108,54 +108,55 @@ GetBaseURI(bool aIsMainScript, WorkerPri
     baseURI = aWorkerPrivate->GetBaseURI();
     NS_ASSERTION(baseURI, "Should have been set already!");
   }
 
   return baseURI;
 }
 
 nsresult
-ChannelFromScriptURL(nsIPrincipal* principal,
-                     nsIURI* baseURI,
-                     nsIDocument* parentDoc,
-                     WorkerPrivate* aWorkerPrivate,
-                     nsILoadGroup* loadGroup,
-                     nsIIOService* ios,
-                     nsIScriptSecurityManager* secMan,
-                     const nsAString& aScriptURL,
-                     nsIURI* aResolvedScriptURL,
-                     const Maybe<ClientInfo>& aClientInfo,
-                     const Maybe<ServiceWorkerDescriptor>& aController,
-                     bool aIsMainScript,
-                     WorkerScriptType aWorkerScriptType,
-                     nsContentPolicyType aMainScriptContentPolicyType,
-                     nsLoadFlags aLoadFlags,
-                     bool aDefaultURIEncoding,
-                     nsIChannel** aChannel)
+ConstructURI(const nsAString& aScriptURL, nsIURI* baseURI,
+             nsIDocument* parentDoc, bool aDefaultURIEncoding,
+             nsIURI** aResult)
 {
-  AssertIsOnMainThread();
-  MOZ_ASSERT(!aResolvedScriptURL || aScriptURL.IsEmpty());
-
   nsresult rv;
-  nsCOMPtr<nsIURI> uri;
-
-  if (aResolvedScriptURL) {
-    uri = aResolvedScriptURL;
-    rv = NS_OK;
-  } else if (aDefaultURIEncoding) {
-    rv = NS_NewURI(getter_AddRefs(uri), aScriptURL, nullptr, baseURI);
+  if (aDefaultURIEncoding) {
+    rv = NS_NewURI(aResult, aScriptURL, nullptr, baseURI);
   } else {
-    rv = nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(uri),
+    rv = nsContentUtils::NewURIWithDocumentCharset(aResult,
                                                    aScriptURL, parentDoc,
                                                    baseURI);
   }
 
   if (NS_FAILED(rv)) {
     return NS_ERROR_DOM_SYNTAX_ERR;
   }
+  return NS_OK;
+}
+
+nsresult
+ChannelFromScriptURL(nsIPrincipal* principal,
+                     nsIDocument* parentDoc,
+                     WorkerPrivate* aWorkerPrivate,
+                     nsILoadGroup* loadGroup,
+                     nsIIOService* ios,
+                     nsIScriptSecurityManager* secMan,
+                     nsIURI* aScriptURL,
+                     const Maybe<ClientInfo>& aClientInfo,
+                     const Maybe<ServiceWorkerDescriptor>& aController,
+                     bool aIsMainScript,
+                     WorkerScriptType aWorkerScriptType,
+                     nsContentPolicyType aMainScriptContentPolicyType,
+                     nsLoadFlags aLoadFlags,
+                     nsIChannel** aChannel)
+{
+  AssertIsOnMainThread();
+
+  nsresult rv;
+  nsCOMPtr<nsIURI> uri = aScriptURL;
 
   // If we have the document, use it. Unfortunately, for dedicated workers
   // 'parentDoc' ends up being the parent document, which is not the document
   // that we want to use. So make sure to avoid using 'parentDoc' in that
   // situation.
   if (parentDoc && parentDoc->NodePrincipal() != principal) {
     parentDoc = nullptr;
   }
@@ -1054,24 +1055,30 @@ private:
         }
       }
     }
 
     if (!channel) {
       // Only top level workers' main script use the document charset for the
       // script uri encoding. Otherwise, default encoding (UTF-8) is applied.
       bool useDefaultEncoding = !(!parentWorker && IsMainWorkerScript());
-      rv = ChannelFromScriptURL(principal, baseURI, parentDoc, mWorkerPrivate,
+      nsCOMPtr<nsIURI> url;
+      rv = ConstructURI(loadInfo.mURL, baseURI, parentDoc, useDefaultEncoding,
+                        getter_AddRefs(url));
+      if (NS_FAILED(rv)) {
+        return rv;
+      }
+
+      rv = ChannelFromScriptURL(principal, parentDoc, mWorkerPrivate,
                                 loadGroup, ios,
-                                secMan, loadInfo.mURL, nullptr,
+                                secMan, url,
                                 mClientInfo, mController,
                                 IsMainWorkerScript(),
                                 mWorkerScriptType,
                                 mWorkerPrivate->ContentPolicyType(), loadFlags,
-                                useDefaultEncoding,
                                 getter_AddRefs(channel));
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
     }
 
     // We need to know which index we're on in OnStreamComplete so we know
     // where to put the result.
@@ -1972,31 +1979,34 @@ public:
     nsCOMPtr<nsIURI> baseURI = mWorkerPrivate->GetBaseURI();
     MOZ_ASSERT(baseURI);
 
     // May be null.
     nsCOMPtr<nsIDocument> parentDoc = mWorkerPrivate->GetDocument();
 
     mLoadInfo.mLoadGroup = mWorkerPrivate->GetLoadGroup();
 
+    // Nested workers use default uri encoding.
+    nsCOMPtr<nsIURI> url;
+    mResult = ConstructURI(mScriptURL, baseURI, parentDoc, true,
+                           getter_AddRefs(url));
+    NS_ENSURE_SUCCESS(mResult, true);
+
     Maybe<ClientInfo> clientInfo;
     clientInfo.emplace(mClientInfo);
 
     nsCOMPtr<nsIChannel> channel;
     mResult = workerinternals::
       ChannelFromScriptURLMainThread(mLoadInfo.mLoadingPrincipal,
-                                     baseURI, parentDoc,
+                                     parentDoc,
                                      mLoadInfo.mLoadGroup,
-                                     mScriptURL,
-                                     nullptr,
+                                     url,
                                      clientInfo,
                                      // Nested workers are always dedicated.
                                      nsIContentPolicy::TYPE_INTERNAL_WORKER,
-                                     // Nested workers use default uri encoding.
-                                     true,
                                      getter_AddRefs(channel));
     NS_ENSURE_SUCCESS(mResult, true);
 
     mResult = mLoadInfo.SetPrincipalFromChannel(channel);
     NS_ENSURE_SUCCESS(mResult, true);
 
     mLoadInfo.mChannel = channel.forget();
     return true;
@@ -2324,41 +2334,35 @@ LoadAllScripts(WorkerPrivate* aWorkerPri
 }
 
 } /* anonymous namespace */
 
 namespace workerinternals {
 
 nsresult
 ChannelFromScriptURLMainThread(nsIPrincipal* aPrincipal,
-                               nsIURI* aBaseURI,
                                nsIDocument* aParentDoc,
                                nsILoadGroup* aLoadGroup,
-                               const nsAString& aScriptURL,
-                               nsIURI* aResolvedScriptURL,
+                               nsIURI* aScriptURL,
                                const Maybe<ClientInfo>& aClientInfo,
                                nsContentPolicyType aMainScriptContentPolicyType,
-                               bool aDefaultURIEncoding,
                                nsIChannel** aChannel)
 {
   AssertIsOnMainThread();
-  MOZ_ASSERT(!aResolvedScriptURL || aScriptURL.IsEmpty());
 
   nsCOMPtr<nsIIOService> ios(do_GetIOService());
 
   nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
   NS_ASSERTION(secMan, "This should never be null!");
 
-  return ChannelFromScriptURL(aPrincipal, aBaseURI, aParentDoc, nullptr,
-                              aLoadGroup, ios, secMan, aScriptURL,
-                              aResolvedScriptURL, aClientInfo,
+  return ChannelFromScriptURL(aPrincipal, aParentDoc, nullptr,
+                              aLoadGroup, ios, secMan, aScriptURL, aClientInfo,
                               Maybe<ServiceWorkerDescriptor>(),
                               true, WorkerScript, aMainScriptContentPolicyType,
-                              nsIRequest::LOAD_NORMAL, aDefaultURIEncoding,
-                              aChannel);
+                              nsIRequest::LOAD_NORMAL, aChannel);
 }
 
 nsresult
 ChannelFromScriptURLWorkerThread(JSContext* aCx,
                                  WorkerPrivate* aParent,
                                  const nsAString& aScriptURL,
                                  WorkerLoadInfo& aLoadInfo)
 {
--- a/dom/workers/ScriptLoader.h
+++ b/dom/workers/ScriptLoader.h
@@ -30,24 +30,21 @@ enum WorkerScriptType {
   WorkerScript,
   DebuggerScript
 };
 
 namespace workerinternals {
 
 nsresult
 ChannelFromScriptURLMainThread(nsIPrincipal* aPrincipal,
-                               nsIURI* aBaseURI,
                                nsIDocument* aParentDoc,
                                nsILoadGroup* aLoadGroup,
-                               const nsAString& aScriptURL,
-                               nsIURI* aResolvedScriptURL,
+                               nsIURI* aScriptURL,
                                const Maybe<ClientInfo>& aClientInfo,
                                nsContentPolicyType aContentPolicyType,
-                               bool aDefaultURIEncoding,
                                nsIChannel** aChannel);
 
 nsresult
 ChannelFromScriptURLWorkerThread(JSContext* aCx,
                                  WorkerPrivate* aParent,
                                  const nsAString& aScriptURL,
                                  WorkerLoadInfo& aLoadInfo);
 
--- a/dom/workers/WorkerPrivate.cpp
+++ b/dom/workers/WorkerPrivate.cpp
@@ -2731,24 +2731,28 @@ WorkerPrivate::GetLoadInfo(JSContext* aC
     if (!loadInfo.mLoadGroup || aLoadGroupBehavior == OverrideLoadGroup) {
       OverrideLoadInfoLoadGroup(loadInfo, loadInfo.mLoadingPrincipal);
     }
     MOZ_ASSERT(NS_LoadGroupMatchesPrincipal(loadInfo.mLoadGroup,
                                             loadInfo.mLoadingPrincipal));
 
     // Top level workers' main script use the document charset for the script
     // uri encoding.
-    bool useDefaultEncoding = false;
+    nsCOMPtr<nsIURI> url;
+    rv = nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(url),
+                                                   aScriptURL,
+                                                   document,
+                                                   loadInfo.mBaseURI);
+    NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
+
     rv = ChannelFromScriptURLMainThread(loadInfo.mLoadingPrincipal,
-                                        loadInfo.mBaseURI,
                                         document, loadInfo.mLoadGroup,
-                                        aScriptURL, nullptr,
+                                        url,
                                         clientInfo,
                                         ContentPolicyType(aWorkerType),
-                                        useDefaultEncoding,
                                         getter_AddRefs(loadInfo.mChannel));
     NS_ENSURE_SUCCESS(rv, rv);
 
     rv = NS_GetFinalChannelURI(loadInfo.mChannel,
                                getter_AddRefs(loadInfo.mResolvedScriptURI));
     NS_ENSURE_SUCCESS(rv, rv);
 
     rv = loadInfo.SetPrincipalFromChannel(loadInfo.mChannel);
--- a/dom/workers/remoteworkers/RemoteWorkerChild.cpp
+++ b/dom/workers/remoteworkers/RemoteWorkerChild.cpp
@@ -324,26 +324,23 @@ RemoteWorkerChild::ExecWorkerOnMainThrea
   Maybe<ClientInfo> clientInfo;
   if (aData.clientInfo().type() == OptionalIPCClientInfo::TIPCClientInfo) {
     clientInfo.emplace(ClientInfo(aData.clientInfo().get_IPCClientInfo()));
   }
 
   // Top level workers' main script use the document charset for the script
   // uri encoding.
   rv = ChannelFromScriptURLMainThread(info.mLoadingPrincipal,
-                                      info.mBaseURI,
                                       nullptr /* parent document */,
                                       info.mLoadGroup,
-                                      EmptyString(),
                                       info.mResolvedScriptURI,
                                       clientInfo,
                                       aData.isSharedWorker()
                                         ? nsIContentPolicy::TYPE_INTERNAL_SHARED_WORKER
                                         : nsIContentPolicy::TYPE_INTERNAL_SERVICE_WORKER,
-                                      false /* default encoding */,
                                       getter_AddRefs(info.mChannel));
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   AutoJSAPI jsapi;
   jsapi.Init();