Bug 1444151 - Part 4: Remove the now-unnecessary xpcom-style segment getters, r=valentin

This commit is contained in:
Nika Layzell 2018-03-08 13:24:15 -05:00
parent 4f380cfe7d
commit da7423dfa5
5 changed files with 38 additions and 129 deletions

View File

@ -2122,15 +2122,12 @@ ReadResponse(mozIStorageConnection* aConn, EntryId aEntryId,
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
#ifdef DEBUG
nsCString scheme;
rv = url->GetScheme(scheme);
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
nsDependentCSubstring scheme = url->Scheme();
MOZ_ASSERT(scheme == "http" || scheme == "https" || scheme == "file");
#endif
nsCString origin;
rv = url->GetOrigin(origin);
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
url->Origin(origin);
aSavedResponseOut->mValue.principalInfo() =
mozilla::ipc::ContentPrincipalInfo(attrs, origin, specNoSuffix);

View File

@ -49,12 +49,10 @@ ClientIsValidPrincipalInfo(const PrincipalInfo& aPrincipalInfo)
NS_ENSURE_SUCCESS(rv, false);
nsAutoCString originOrigin;
rv = originURL->GetOrigin(originOrigin);
NS_ENSURE_SUCCESS(rv, false);
originURL->Origin(originOrigin);
nsAutoCString specOrigin;
rv = specURL->GetOrigin(specOrigin);
NS_ENSURE_SUCCESS(rv, false);
specURL->Origin(specOrigin);
// For now require Clients to have a principal where both its
// originNoSuffix and spec have the same origin. This will
@ -101,12 +99,10 @@ ClientIsValidCreationURL(const PrincipalInfo& aPrincipalInfo,
NS_ENSURE_SUCCESS(rv, false);
nsAutoCString origin;
rv = url->GetOrigin(origin);
NS_ENSURE_SUCCESS(rv, false);
url->Origin(origin);
nsAutoCString principalOrigin;
rv = principalURL->GetOrigin(principalOrigin);
NS_ENSURE_SUCCESS(rv, false);
principalURL->Origin(principalOrigin);
// The vast majority of sites should simply result in the same principal
// and URL origin.
@ -114,9 +110,7 @@ ClientIsValidCreationURL(const PrincipalInfo& aPrincipalInfo,
return true;
}
nsAutoCString scheme;
rv = url->GetScheme(scheme);
NS_ENSURE_SUCCESS(rv, false);
nsDependentCSubstring scheme = url->Scheme();
// Generally any origin can also open javascript: windows and workers.
if (scheme.LowerCaseEqualsLiteral("javascript")) {
@ -130,10 +124,6 @@ ClientIsValidCreationURL(const PrincipalInfo& aPrincipalInfo,
return true;
}
nsAutoCString principalScheme;
rv = principalURL->GetScheme(principalScheme);
NS_ENSURE_SUCCESS(rv, false);
// Otherwise don't support this URL type in the clients sub-system for
// now. This will exclude a variety of internal browser clients, but
// currently we don't need to support those. This function can be
@ -142,9 +132,7 @@ ClientIsValidCreationURL(const PrincipalInfo& aPrincipalInfo,
}
case PrincipalInfo::TSystemPrincipalInfo:
{
nsAutoCString scheme;
rv = url->GetScheme(scheme);
NS_ENSURE_SUCCESS(rv, false);
nsDependentCSubstring scheme = url->Scheme();
// While many types of documents can be created with a system principal,
// there are only a few that can reasonably become windows. We attempt

View File

@ -66,11 +66,7 @@ GetOrigin(const nsACString& aURL, nsACString& aOrigin)
return rv;
}
rv = url->GetOrigin(aOrigin);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
url->Origin(aOrigin);
return NS_OK;
}

View File

@ -54,12 +54,18 @@ public:
nsDependentCSubstring Password() const {
return mozurl_password(this);
}
// Will return the hostname of URL. If the hostname is an IPv6 address,
// it will be enclosed in square brackets, such as `[::1]`
nsDependentCSubstring Host() const {
return mozurl_host(this);
}
// Will return the port number, if specified, or -1
int32_t Port() const {
return mozurl_port(this);
}
// If the URL's port number is equal to the default port, will only return the
// hostname, otherwise it will return a string of the form `{host}:{port}`
// See: https://url.spec.whatwg.org/#default-port
nsDependentCSubstring HostPort() const {
return mozurl_host_port(this);
}
@ -93,58 +99,6 @@ public:
return mozurl_relative(this, aOther, aRelative);
}
// Legacy XPCOM-style getters
nsresult GetScheme(nsACString& aScheme) const {
aScheme.Assign(Scheme());
return NS_OK;
}
nsresult GetSpec(nsACString& aSpec) const {
aSpec.Assign(Spec());
return NS_OK;
}
nsresult GetUsername(nsACString& aUser) const {
aUser.Assign(Username());
return NS_OK;
}
nsresult GetPassword(nsACString& aPassword) const {
aPassword.Assign(Password());
return NS_OK;
}
// Will return the hostname of URL. If the hostname is an IPv6 address,
// it will be enclosed in square brackets, such as `[::1]`
nsresult GetHostname(nsACString& aHost) const {
aHost.Assign(Host());
return NS_OK;
}
// If the URL's port number is equal to the default port, will only return the
// hostname, otherwise it will return a string of the form `{host}:{port}`
// See: https://url.spec.whatwg.org/#default-port
nsresult GetHostPort(nsACString& aHostPort) const {
aHostPort.Assign(HostPort());
return NS_OK;
}
// Will return the port number, if specified, or -1
nsresult GetPort(int32_t* aPort) const {
*aPort = Port();
return NS_OK;
}
nsresult GetFilePath(nsACString& aPath) const {
aPath.Assign(FilePath());
return NS_OK;
}
nsresult GetQuery(nsACString& aQuery) const {
aQuery.Assign(Query());
return NS_OK;
}
nsresult GetRef(nsACString& aRef) const {
aRef.Assign(Ref());
return NS_OK;
}
nsresult GetOrigin(nsACString& aOrigin) const {
Origin(aOrigin);
return NS_OK;
}
class MOZ_STACK_CLASS Mutator
{
public:

View File

@ -12,31 +12,21 @@ TEST(TestMozURL, Getters)
RefPtr<MozURL> url;
ASSERT_EQ(MozURL::Init(getter_AddRefs(url), href), NS_OK);
nsAutoCString out;
ASSERT_TRUE(url->Scheme().EqualsLiteral("http"));
ASSERT_EQ(url->GetScheme(out), NS_OK);
ASSERT_TRUE(out.EqualsLiteral("http"));
ASSERT_TRUE(url->Spec() == href);
ASSERT_EQ(url->GetSpec(out), NS_OK);
ASSERT_TRUE(out == href);
ASSERT_TRUE(url->Username().EqualsLiteral("user"));
ASSERT_EQ(url->GetUsername(out), NS_OK);
ASSERT_TRUE(out.EqualsLiteral("user"));
ASSERT_TRUE(url->Password().EqualsLiteral("pass"));
ASSERT_EQ(url->GetPassword(out), NS_OK);
ASSERT_TRUE(out.EqualsLiteral("pass"));
ASSERT_TRUE(url->Host().EqualsLiteral("example.com"));
ASSERT_EQ(url->GetHostname(out), NS_OK);
ASSERT_TRUE(out.EqualsLiteral("example.com"));
ASSERT_TRUE(url->FilePath().EqualsLiteral("/path"));
ASSERT_EQ(url->GetFilePath(out), NS_OK);
ASSERT_TRUE(out.EqualsLiteral("/path"));
ASSERT_TRUE(url->Query().EqualsLiteral("query"));
ASSERT_EQ(url->GetQuery(out), NS_OK);
ASSERT_TRUE(out.EqualsLiteral("query"));
ASSERT_EQ(url->GetRef(out), NS_OK);
ASSERT_TRUE(out.EqualsLiteral("ref"));
ASSERT_TRUE(url->Ref().EqualsLiteral("ref"));
url = nullptr;
ASSERT_EQ(MozURL::Init(getter_AddRefs(url), NS_LITERAL_CSTRING("")),
@ -61,8 +51,7 @@ TEST(TestMozURL, MutatorChain)
.SetRef(NS_LITERAL_CSTRING("huh"))
.Finalize(getter_AddRefs(url2)), NS_OK);
ASSERT_EQ(url2->GetSpec(out), NS_OK);
ASSERT_TRUE(out.EqualsLiteral("https://newuser:newpass@test/new/file/path?bla#huh"));
ASSERT_TRUE(url2->Spec().EqualsLiteral("https://newuser:newpass@test/new/file/path?bla#huh"));
}
TEST(TestMozURL, MutatorFinalizeTwice)
@ -76,8 +65,7 @@ TEST(TestMozURL, MutatorFinalizeTwice)
MozURL::Mutator mut = url->Mutate();
mut.SetScheme(NS_LITERAL_CSTRING("https")); // Change the scheme to https
ASSERT_EQ(mut.Finalize(getter_AddRefs(url2)), NS_OK);
ASSERT_EQ(url2->GetSpec(out), NS_OK);
ASSERT_TRUE(out.EqualsLiteral("https://user:pass@example.com/path?query#ref"));
ASSERT_TRUE(url2->Spec().EqualsLiteral("https://user:pass@example.com/path?query#ref"));
// Test that a second call to Finalize will result in an error code
url2 = nullptr;
@ -107,17 +95,14 @@ TEST(TestMozURL, InitWithBase)
nsAutoCString href("https://example.net/a/b.html");
RefPtr<MozURL> url;
ASSERT_EQ(MozURL::Init(getter_AddRefs(url), href), NS_OK);
nsAutoCString out;
ASSERT_EQ(url->GetSpec(out), NS_OK);
ASSERT_TRUE(out.EqualsLiteral("https://example.net/a/b.html"));
ASSERT_TRUE(url->Spec().EqualsLiteral("https://example.net/a/b.html"));
RefPtr<MozURL> url2;
ASSERT_EQ(MozURL::Init(getter_AddRefs(url2), NS_LITERAL_CSTRING("c.png"),
url), NS_OK);
ASSERT_EQ(url2->GetSpec(out), NS_OK);
ASSERT_TRUE(out.EqualsLiteral("https://example.net/a/c.png"));
ASSERT_TRUE(url2->Spec().EqualsLiteral("https://example.net/a/c.png"));
}
TEST(TestMozURL, Path)
@ -125,16 +110,12 @@ TEST(TestMozURL, Path)
nsAutoCString href("about:blank");
RefPtr<MozURL> url;
ASSERT_EQ(MozURL::Init(getter_AddRefs(url), href), NS_OK);
nsAutoCString out;
ASSERT_EQ(url->GetSpec(out), NS_OK);
ASSERT_TRUE(out.EqualsLiteral("about:blank"));
ASSERT_TRUE(url->Spec().EqualsLiteral("about:blank"));
ASSERT_EQ(url->GetScheme(out), NS_OK);
ASSERT_TRUE(out.EqualsLiteral("about"));
ASSERT_TRUE(url->Scheme().EqualsLiteral("about"));
ASSERT_EQ(url->GetFilePath(out), NS_OK);
ASSERT_TRUE(out.EqualsLiteral("blank"));
ASSERT_TRUE(url->FilePath().EqualsLiteral("blank"));
}
TEST(TestMozURL, HostPort)
@ -142,27 +123,20 @@ TEST(TestMozURL, HostPort)
nsAutoCString href("https://user:pass@example.net:1234/path?query#ref");
RefPtr<MozURL> url;
ASSERT_EQ(MozURL::Init(getter_AddRefs(url), href), NS_OK);
nsAutoCString out;
ASSERT_EQ(url->GetHostPort(out), NS_OK);
ASSERT_TRUE(out.EqualsLiteral("example.net:1234"));
ASSERT_TRUE(url->HostPort().EqualsLiteral("example.net:1234"));
RefPtr<MozURL> url2;
url->Mutate().SetHostPort(NS_LITERAL_CSTRING("test:321"))
.Finalize(getter_AddRefs(url2));
ASSERT_EQ(url2->GetHostPort(out), NS_OK);
ASSERT_TRUE(out.EqualsLiteral("test:321"));
ASSERT_EQ(url2->GetSpec(out), NS_OK);
ASSERT_TRUE(out.EqualsLiteral("https://user:pass@test:321/path?query#ref"));
ASSERT_TRUE(url2->HostPort().EqualsLiteral("test:321"));
ASSERT_TRUE(url2->Spec().EqualsLiteral("https://user:pass@test:321/path?query#ref"));
href.Assign("https://user:pass@example.net:443/path?query#ref");
ASSERT_EQ(MozURL::Init(getter_AddRefs(url), href), NS_OK);
ASSERT_EQ(url->GetHostPort(out), NS_OK);
ASSERT_TRUE(out.EqualsLiteral("example.net"));
int32_t port;
ASSERT_EQ(url->GetPort(&port), NS_OK);
ASSERT_EQ(port, -1);
ASSERT_TRUE(url->HostPort().EqualsLiteral("example.net"));
ASSERT_EQ(url->Port(), -1);
}
TEST(TestMozURL, Origin)
@ -170,14 +144,14 @@ TEST(TestMozURL, Origin)
nsAutoCString href("https://user:pass@example.net:1234/path?query#ref");
RefPtr<MozURL> url;
ASSERT_EQ(MozURL::Init(getter_AddRefs(url), href), NS_OK);
nsAutoCString out;
ASSERT_EQ(url->GetOrigin(out), NS_OK);
nsAutoCString out;
url->Origin(out);
ASSERT_TRUE(out.EqualsLiteral("https://example.net:1234"));
RefPtr<MozURL> url2;
ASSERT_EQ(MozURL::Init(getter_AddRefs(url2),
NS_LITERAL_CSTRING("file:///tmp/foo")), NS_OK);
ASSERT_EQ(url2->GetOrigin(out), NS_OK);
url2->Origin(out);
ASSERT_TRUE(out.EqualsLiteral("null"));
}