Bug 1433958 - Change code that sets nsIURI.username to use nsIURIMutator
authorValentin Gosu <valentin.gosu@gmail.com>
Mon, 29 Jan 2018 19:46:30 +0100
changeset 1408786 6f5c8274f51eaab4a5a0cf65b3f769807dbcccd4
parent 1408785 8bbdaa80c6f8cec8140982eafc956f393906828a
child 1408787 f062fd9e2b84c6ea19ff3cbe106ed322b851cba2
push id249057
push uservalentin.gosu@gmail.com
push dateMon, 29 Jan 2018 19:06:36 +0000
treeherdertry@e0c29cc61125 [default view] [failures only]
bugs1433958
milestone60.0a1
Bug 1433958 - Change code that sets nsIURI.username to use nsIURIMutator MozReview-Commit-ID: 7E63pAIOZCs
dom/base/Link.cpp
dom/url/URLMainThread.cpp
netwerk/test/unit/test_standardurl.js
--- a/dom/base/Link.cpp
+++ b/dom/base/Link.cpp
@@ -457,24 +457,28 @@ Link::SetPassword(const nsAString &aPass
 
   uri->SetPassword(NS_ConvertUTF16toUTF8(aPassword));
   SetHrefAttribute(uri);
 }
 
 void
 Link::SetUsername(const nsAString &aUsername)
 {
-  nsCOMPtr<nsIURI> uri(GetURIToMutate());
+  nsCOMPtr<nsIURI> uri(GetURI());
   if (!uri) {
     // Ignore failures to be compatible with NS4.
     return;
   }
 
-  uri->SetUsername(NS_ConvertUTF16toUTF8(aUsername));
-  SetHrefAttribute(uri);
+  nsresult rv = NS_MutateURI(uri)
+                  .SetUsername(NS_ConvertUTF16toUTF8(aUsername))
+                  .Finalize(uri);
+  if (NS_SUCCEEDED(rv)) {
+    SetHrefAttribute(uri);
+  }
 }
 
 void
 Link::SetHost(const nsAString &aHost)
 {
   nsCOMPtr<nsIURI> uri(GetURI());
   if (!uri) {
     // Ignore failures to be compatible with NS4.
--- a/dom/url/URLMainThread.cpp
+++ b/dom/url/URLMainThread.cpp
@@ -286,17 +286,19 @@ void
 URLMainThread::GetUsername(nsAString& aUsername, ErrorResult& aRv) const
 {
   URL_GETTER(aUsername, GetUsername);
 }
 
 void
 URLMainThread::SetUsername(const nsAString& aUsername, ErrorResult& aRv)
 {
-  mURI->SetUsername(NS_ConvertUTF16toUTF8(aUsername));
+  (void) NS_MutateURI(mURI)
+           .SetUsername(NS_ConvertUTF16toUTF8(aUsername))
+           .Finalize(mURI);
 }
 
 void
 URLMainThread::GetPassword(nsAString& aPassword, ErrorResult& aRv) const
 {
   URL_GETTER(aPassword, GetPassword);
 }
 
--- a/netwerk/test/unit/test_standardurl.js
+++ b/netwerk/test/unit/test_standardurl.js
@@ -296,27 +296,28 @@ add_test(function test_percentDecoding()
 add_test(function test_hugeStringThrows()
 {
   let prefs = Cc["@mozilla.org/preferences-service;1"]
                 .getService(Ci.nsIPrefService);
   let maxLen = prefs.getIntPref("network.standard-url.max-length");
   let url = stringToURL("http://test:test@example.com");
 
   let hugeString = new Array(maxLen + 1).fill("a").join("");
-  let properties = ["scheme", "username",
+  let properties = ["scheme",
                     "password", "host", "ref",
                     "query"];
   for (let prop of properties) {
     Assert.throws(() => url[prop] = hugeString,
                   /NS_ERROR_MALFORMED_URI/,
                   `Passing a huge string to "${prop}" should throw`);
   }
 
   let setters = [
     { method: "setSpec", qi: Ci.nsIURIMutator },
+    { method: "setUsername", qi: Ci.nsIURIMutator },
     { method: "setFilePath", qi: Ci.nsIURIMutator },
     { method: "setHostPort", qi: Ci.nsIURIMutator },
     { method: "setUserPass", qi: Ci.nsIURIMutator },
     { method: "setPathQueryRef", qi: Ci.nsIURIMutator },
     { method: "setFileName", qi: Ci.nsIURLMutator },
     { method: "setFileExtension", qi: Ci.nsIURLMutator },
     { method: "setFileBaseName", qi: Ci.nsIURLMutator },
   ];