Bug 1864985: Add hasUserPass attribute to nsIURI r=necko-reviewers,valentin

We will remove the user and pass from the URL for Histories and Favicons using
nsIOService::CreateExposableURI()[1], then store them to the database. And, for
Favicons. And, for Favicons, we will also read them. Therefore, as the
frequency calling CreateExposableURI() will be higher, we want to optimize it.
Currently, CreateExposableURI() gets/copies the String of UserPass to check whether there is a user pass info in the URL[2]. Its copying will be waste. To
prevent it, implement hasUserPass in nsIURI, and refer to it instead.

[1] https://searchfox.org/mozilla-central/rev/90dce6b0223b4dc17bb10f1125b44f70951585f9/netwerk/base/nsIOService.cpp#1031
[2] https://searchfox.org/mozilla-central/rev/90dce6b0223b4dc17bb10f1125b44f70951585f9/netwerk/base/nsIOService.cpp#1017

Differential Revision: https://phabricator.services.mozilla.com/D193623
This commit is contained in:
Daisuke Akatsuka
2023-11-17 20:12:10 +00:00
parent e1b8062832
commit be7580b365
9 changed files with 47 additions and 4 deletions

View File

@@ -144,6 +144,12 @@ nsMozIconURI::GetHasRef(bool* result) {
return NS_OK;
}
NS_IMETHODIMP
nsMozIconURI::GetHasUserPass(bool* result) {
*result = false;
return NS_OK;
}
NS_IMETHODIMP
nsMozIconURI::GetHasQuery(bool* result) {
*result = false;

View File

@@ -170,6 +170,11 @@ nsJARURI::GetDisplayHost(nsACString& aUnicodeHost) {
NS_IMETHODIMP
nsJARURI::GetHasRef(bool* result) { return mJAREntry->GetHasRef(result); }
NS_IMETHODIMP
nsJARURI::GetHasUserPass(bool* result) {
return mJAREntry->GetHasUserPass(result);
}
NS_IMETHODIMP
nsJARURI::GetHasQuery(bool* result) { return mJAREntry->GetHasQuery(result); }

View File

@@ -228,6 +228,11 @@ NS_IMETHODIMP DefaultURI::GetHasRef(bool* aHasRef) {
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetHasUserPass(bool* aHasUserPass) {
*aHasUserPass = !mURL->Username().IsEmpty() || !mURL->Password().IsEmpty();
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetFilePath(nsACString& aFilePath) {
aFilePath = mURL->FilePath();
return NS_OK;

View File

@@ -250,6 +250,11 @@ interface nsIURI : nsISupports
*/
readonly attribute boolean hasRef;
/**
* Returns if there is user and pass in the URI.
*/
readonly attribute boolean hasUserPass;
/************************************************************************
* Additional attributes added for .query support:
*/

View File

@@ -264,6 +264,12 @@ nsSimpleURI::GetHasRef(bool* result) {
return NS_OK;
}
NS_IMETHODIMP
nsSimpleURI::GetHasUserPass(bool* result) {
*result = false;
return NS_OK;
}
nsresult nsSimpleURI::SetSpecInternal(const nsACString& aSpec,
bool aStripWhitespace) {
if (StaticPrefs::network_url_max_length() &&

View File

@@ -2980,6 +2980,12 @@ nsStandardURL::GetHasRef(bool* result) {
return NS_OK;
}
NS_IMETHODIMP
nsStandardURL::GetHasUserPass(bool* result) {
*result = (mUsername.mLen >= 0) || (mPassword.mLen >= 0);
return NS_OK;
}
// result may contain unescaped UTF-8 characters
NS_IMETHODIMP
nsStandardURL::GetDirectory(nsACString& result) {

View File

@@ -122,6 +122,10 @@ class SubstitutingJARURI : public nsIJARURI,
NS_IMETHOD GetHasRef(bool* aHasRef) override {
return !mSource ? NS_ERROR_NULL_POINTER : mSource->GetHasRef(aHasRef);
}
NS_IMETHOD GetHasUserPass(bool* aHasUserPass) override {
return !mSource ? NS_ERROR_NULL_POINTER
: mSource->GetHasUserPass(aHasUserPass);
}
NS_IMETHOD GetHasQuery(bool* aHasQuery) override {
return !mSource ? NS_ERROR_NULL_POINTER : mSource->GetHasQuery(aHasQuery);
}

View File

@@ -493,10 +493,15 @@ function do_test_uri_basic(aTest) {
do_check_property(aTest, URI, "password");
do_check_property(aTest, URI, "host");
do_check_property(aTest, URI, "specIgnoringRef");
if ("hasRef" in aTest) {
do_info("testing hasref: " + aTest.hasRef + " vs " + URI.hasRef);
Assert.equal(aTest.hasRef, URI.hasRef);
}
do_info("testing hasRef");
Assert.equal(URI.hasRef, !!aTest.ref, "URI.hasRef is correct");
do_info("testing hasUserPass");
Assert.equal(
URI.hasUserPass,
!!aTest.username || !!aTest.password,
"URI.hasUserPass is correct"
);
}
// Test that a given URI parses correctly when we add a given ref to the end

View File

@@ -17,6 +17,7 @@ add_task(function test_getters() {
equal(uri.userPass, "user:password");
equal(uri.username, "user");
equal(uri.password, "password");
equal(uri.hasUserPass, true);
equal(uri.hostPort, "hostname:123");
equal(uri.host, "hostname");
equal(uri.port, 123);