Bug 1497301 part 3. Change a bunch of Location function signatures to take an ErrorResult. r=bholley
authorBoris Zbarsky <bzbarsky@mit.edu>
Mon, 15 Oct 2018 14:17:48 -0400
changeset 489713 08174b4a8022f1a0b29bacb9d8f3a247e1497042
parent 489712 b9fec5dd4ff5ed4a098f5254c701d8c47d3100c5
child 489714 90b73f4b4635a692c5528a0703b0e9bb5355caac
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewersbholley
bugs1497301
milestone64.0a1
Bug 1497301 part 3. Change a bunch of Location function signatures to take an ErrorResult. r=bholley This way we can propagate errors out usefully.
dom/base/Location.cpp
dom/base/Location.h
--- a/dom/base/Location.cpp
+++ b/dom/base/Location.cpp
@@ -201,44 +201,47 @@ Location::GetURI(nsIURI** aURI, bool aGe
   NS_ASSERTION(uri, "nsJARURI screwed up?");
 
   nsCOMPtr<nsIURIFixup> urifixup(do_GetService(NS_URIFIXUP_CONTRACTID, &rv));
   NS_ENSURE_SUCCESS(rv, rv);
 
   return urifixup->CreateExposableURI(uri, aURI);
 }
 
-nsresult
-Location::SetURI(nsIURI* aURI, bool aReplace)
+void
+Location::SetURI(nsIURI* aURI, ErrorResult& aRv, bool aReplace)
 {
   nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
   if (docShell) {
     RefPtr<nsDocShellLoadInfo> loadInfo;
 
-    if(NS_FAILED(CheckURL(aURI, getter_AddRefs(loadInfo))))
-      return NS_ERROR_FAILURE;
+    if (NS_FAILED(CheckURL(aURI, getter_AddRefs(loadInfo)))) {
+      aRv.Throw(NS_ERROR_FAILURE);
+      return;
+    }
 
     if (aReplace) {
       loadInfo->SetLoadType(LOAD_STOP_CONTENT_AND_REPLACE);
     } else {
       loadInfo->SetLoadType(LOAD_STOP_CONTENT);
     }
 
     // Get the incumbent script's browsing context to set as source.
     nsCOMPtr<nsPIDOMWindowInner> sourceWindow =
       do_QueryInterface(mozilla::dom::GetIncumbentGlobal());
     if (sourceWindow) {
       loadInfo->SetSourceDocShell(sourceWindow->GetDocShell());
     }
 
-    return docShell->LoadURI(aURI, loadInfo,
-                             nsIWebNavigation::LOAD_FLAGS_NONE, true);
+    nsresult rv = docShell->LoadURI(aURI, loadInfo,
+                                    nsIWebNavigation::LOAD_FLAGS_NONE, true);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      aRv.Throw(rv);
+    }
   }
-
-  return NS_OK;
 }
 
 void
 Location::GetHash(nsAString& aHash,
                   nsIPrincipal& aSubjectPrincipal,
                   ErrorResult& aRv)
 {
   if (!CallerSubsumes(&aSubjectPrincipal)) {
@@ -300,17 +303,17 @@ Location::SetHash(const nsAString& aHash
 
   aRv = NS_MutateURI(uri)
           .SetRef(hash)
           .Finalize(uri);
   if (NS_WARN_IF(aRv.Failed()) || !uri) {
     return;
   }
 
-  aRv = SetURI(uri);
+  SetURI(uri, aRv);
 }
 
 void
 Location::GetHost(nsAString& aHost,
                   nsIPrincipal& aSubjectPrincipal,
                   ErrorResult& aRv)
 {
   if (!CallerSubsumes(&aSubjectPrincipal)) {
@@ -354,17 +357,17 @@ Location::SetHost(const nsAString& aHost
 
   aRv = NS_MutateURI(uri)
           .SetHostPort(NS_ConvertUTF16toUTF8(aHost))
           .Finalize(uri);
   if (NS_WARN_IF(aRv.Failed())) {
     return;
   }
 
-  aRv = SetURI(uri);
+  SetURI(uri, aRv);
 }
 
 void
 Location::GetHostname(nsAString& aHostname,
                       nsIPrincipal& aSubjectPrincipal,
                       ErrorResult& aRv)
 {
   if (!CallerSubsumes(&aSubjectPrincipal)) {
@@ -399,17 +402,17 @@ Location::SetHostname(const nsAString& a
 
   aRv = NS_MutateURI(uri)
           .SetHost(NS_ConvertUTF16toUTF8(aHostname))
           .Finalize(uri);
   if (NS_WARN_IF(aRv.Failed())) {
     return;
   }
 
-  aRv = SetURI(uri);
+  SetURI(uri, aRv);
 }
 
 nsresult
 Location::GetHref(nsAString& aHref)
 {
   aHref.Truncate();
 
   nsCOMPtr<nsIURI> uri;
@@ -435,23 +438,22 @@ Location::SetHref(const nsAString& aHref
   DoSetHref(aHref, false, aRv);
 }
 
 void
 Location::DoSetHref(const nsAString& aHref, bool aReplace, ErrorResult& aRv)
 {
   // Get the source of the caller
   nsCOMPtr<nsIURI> base = GetSourceBaseURL();
-
-  aRv = SetHrefWithBase(aHref, base, aReplace);
+  SetHrefWithBase(aHref, base, aReplace, aRv);
 }
 
-nsresult
+void
 Location::SetHrefWithBase(const nsAString& aHref, nsIURI* aBase,
-                          bool aReplace)
+                          bool aReplace, ErrorResult& aRv)
 {
   nsresult result;
   nsCOMPtr<nsIURI> newUri;
 
   nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
 
   if (nsIDocument* doc = GetEntryDocument()) {
     result = NS_NewURI(getter_AddRefs(newUri), aHref,
@@ -483,19 +485,21 @@ Location::SetHrefWithBase(const nsAStrin
         // since we only want to replace if the location is set by a
         // <script> tag in the same window.  See bug 178729.
         nsCOMPtr<nsIScriptGlobalObject> ourGlobal =
           docShell ? docShell->GetScriptGlobalObject() : nullptr;
         inScriptTag = (ourGlobal == scriptContext->GetGlobalObject());
       }
     }
 
-    return SetURI(newUri, aReplace || inScriptTag);
+    SetURI(newUri, aRv, aReplace || inScriptTag);
+    return;
   }
-  return result;
+
+  aRv.Throw(result);
 }
 
 void
 Location::GetOrigin(nsAString& aOrigin,
                     nsIPrincipal& aSubjectPrincipal,
                     ErrorResult& aRv)
 {
   if (!CallerSubsumes(&aSubjectPrincipal)) {
@@ -566,17 +570,17 @@ Location::SetPathname(const nsAString& a
 
   nsresult rv = NS_MutateURI(uri)
                   .SetFilePath(NS_ConvertUTF16toUTF8(aPathname))
                   .Finalize(uri);
   if (NS_FAILED(rv)) {
     return;
   }
 
-  aRv = SetURI(uri);
+  SetURI(uri, aRv);
 }
 
 void
 Location::GetPort(nsAString& aPort,
                   nsIPrincipal& aSubjectPrincipal,
                   ErrorResult& aRv)
 {
   if (!CallerSubsumes(&aSubjectPrincipal)) {
@@ -635,17 +639,17 @@ Location::SetPort(const nsAString& aPort
 
   aRv = NS_MutateURI(uri)
           .SetPort(port)
           .Finalize(uri);
   if (NS_WARN_IF(aRv.Failed())) {
     return;
   }
 
-  aRv = SetURI(uri);
+  SetURI(uri, aRv);
 }
 
 void
 Location::GetProtocol(nsAString& aProtocol,
                       nsIPrincipal& aSubjectPrincipal,
                       ErrorResult& aRv)
 {
   if (!CallerSubsumes(&aSubjectPrincipal)) {
@@ -732,17 +736,17 @@ Location::SetProtocol(const nsAString& a
     return;
   }
 
   if (!isHttp && !isHttps) {
     // No-op, per spec.
     return;
   }
 
-  aRv = SetURI(uri);
+  SetURI(uri, aRv);
 }
 
 void
 Location::GetSearch(nsAString& aSearch,
                     nsIPrincipal& aSubjectPrincipal,
                     ErrorResult& aRv)
 {
   if (!CallerSubsumes(&aSubjectPrincipal)) {
@@ -797,17 +801,17 @@ Location::SetSearch(const nsAString& aSe
     aRv = NS_MutateURI(uri)
             .SetQuery(NS_ConvertUTF16toUTF8(aSearch))
             .Finalize(uri);
   }
   if (NS_WARN_IF(aRv.Failed())) {
     return;
   }
 
-  aRv = SetURI(uri);
+  SetURI(uri, aRv);
 }
 
 nsresult
 Location::Reload(bool aForceget)
 {
   nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
   nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(docShell));
   nsCOMPtr<nsPIDOMWindowOuter> window = docShell ? docShell->GetWindow()
--- a/dom/base/Location.h
+++ b/dom/base/Location.h
@@ -161,19 +161,19 @@ protected:
   virtual ~Location();
 
   // In the case of jar: uris, we sometimes want the place the jar was
   // fetched from as the URI instead of the jar: uri itself.  Pass in
   // true for aGetInnermostURI when that's the case.
   // Note, this method can return NS_OK with a null value for aURL. This happens
   // if the docShell is null.
   nsresult GetURI(nsIURI** aURL, bool aGetInnermostURI = false);
-  nsresult SetURI(nsIURI* aURL, bool aReplace = false);
-  nsresult SetHrefWithBase(const nsAString& aHref, nsIURI* aBase,
-                           bool aReplace);
+  void SetURI(nsIURI* aURL, ErrorResult& aRv, bool aReplace = false);
+  void SetHrefWithBase(const nsAString& aHref, nsIURI* aBase,
+                       bool aReplace, ErrorResult& aRv);
 
   // Helper for Assign/SetHref/Replace
   void DoSetHref(const nsAString& aHref, bool aReplace, ErrorResult& aRv);
 
   // Get the base URL we should be using for our relative URL
   // resolution for SetHref/Assign/Replace.
   already_AddRefed<nsIURI> GetSourceBaseURL();
   nsresult CheckURL(nsIURI *url, nsDocShellLoadInfo** aLoadInfo);