Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit da90b10

Browse files
committed
Bug 1431204 - Change calls to nsIURI.spec setter to use nsIURIMutator instead r=mayhemer
* changes call to use nsIURIMutator.setSpec() * Add new NS_MutateURI constructor that takes new Mutator object * Make nsSimpleNestedURI::Mutate() and nsNestedAboutURI::Mutate() return mutable URIs * Make the finalizers for nsSimpleNestedURI and nsNestedAboutURI make the returned URIs immutable MozReview-Commit-ID: 1kcv6zMxnv7 --HG-- extra : rebase_source : 99b13e9dbc8eaaa9615843b05e1539e19b527504
1 parent ea3dd63 commit da90b10

17 files changed

Lines changed: 189 additions & 107 deletions

dom/file/nsHostObjectProtocolHandler.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -892,18 +892,20 @@ nsHostObjectProtocolHandler::NewURI(const nsACString& aSpec,
892892

893893
DataInfo* info = GetDataInfo(aSpec);
894894

895-
RefPtr<nsHostObjectURI> uri;
895+
nsCOMPtr<nsIURI> uri;
896+
rv = NS_MutateURI(new nsHostObjectURI::Mutator())
897+
.SetSpec(aSpec)
898+
.Finalize(uri);
899+
NS_ENSURE_SUCCESS(rv, rv);
900+
901+
RefPtr<nsHostObjectURI> hostURI = static_cast<nsHostObjectURI*>(uri.get());
896902
if (info && info->mObjectType == DataInfo::eBlobImpl) {
897903
MOZ_ASSERT(info->mBlobImpl);
898-
uri = new nsHostObjectURI(info->mPrincipal, info->mBlobImpl);
899-
} else {
900-
uri = new nsHostObjectURI(nullptr, nullptr);
904+
hostURI->mPrincipal = info->mPrincipal;
905+
hostURI->mBlobImpl = info->mBlobImpl;
901906
}
902907

903-
rv = uri->SetSpec(aSpec);
904-
NS_ENSURE_SUCCESS(rv, rv);
905-
906-
NS_TryToSetImmutable(uri);
908+
NS_TryToSetImmutable(hostURI);
907909
uri.forget(aResult);
908910

909911
if (info && info->mObjectType == DataInfo::eBlobImpl) {
@@ -1120,21 +1122,23 @@ nsFontTableProtocolHandler::NewURI(const nsACString& aSpec,
11201122
nsIURI *aBaseURI,
11211123
nsIURI **aResult)
11221124
{
1123-
RefPtr<nsIURI> uri;
1125+
nsresult rv;
1126+
nsCOMPtr<nsIURI> uri;
11241127

11251128
// Either you got here via a ref or a fonttable: uri
11261129
if (aSpec.Length() && aSpec.CharAt(0) == '#') {
1127-
nsresult rv = aBaseURI->CloneIgnoringRef(getter_AddRefs(uri));
1130+
rv = NS_MutateURI(aBaseURI)
1131+
.SetRef(aSpec)
1132+
.Finalize(uri);
11281133
NS_ENSURE_SUCCESS(rv, rv);
1129-
1130-
uri->SetRef(aSpec);
11311134
} else {
11321135
// Relative URIs (other than #ref) are not meaningful within the
11331136
// fonttable: scheme.
11341137
// If aSpec is a relative URI -other- than a bare #ref,
11351138
// this will leave uri empty, and we'll return a failure code below.
1136-
uri = new mozilla::net::nsSimpleURI();
1137-
nsresult rv = uri->SetSpec(aSpec);
1139+
rv = NS_MutateURI(new mozilla::net::nsSimpleURI::Mutator())
1140+
.SetSpec(aSpec)
1141+
.Finalize(uri);
11381142
NS_ENSURE_SUCCESS(rv, rv);
11391143
}
11401144

dom/jsurl/nsJSProtocolHandler.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,20 +1196,22 @@ nsJSProtocolHandler::NewURI(const nsACString &aSpec,
11961196
// CreateInstance.
11971197

11981198
nsCOMPtr<nsIURI> url = new nsJSURI(aBaseURI);
1199-
1200-
if (!aCharset || !nsCRT::strcasecmp("UTF-8", aCharset))
1201-
rv = url->SetSpec(aSpec);
1202-
else {
1199+
NS_MutateURI mutator(url);
1200+
if (!aCharset || !nsCRT::strcasecmp("UTF-8", aCharset)) {
1201+
mutator.SetSpec(aSpec);
1202+
} else {
12031203
nsAutoCString utf8Spec;
12041204
rv = EnsureUTF8Spec(PromiseFlatCString(aSpec), aCharset, utf8Spec);
12051205
if (NS_SUCCEEDED(rv)) {
1206-
if (utf8Spec.IsEmpty())
1207-
rv = url->SetSpec(aSpec);
1208-
else
1209-
rv = url->SetSpec(utf8Spec);
1206+
if (utf8Spec.IsEmpty()) {
1207+
mutator.SetSpec(aSpec);
1208+
} else {
1209+
mutator.SetSpec(utf8Spec);
1210+
}
12101211
}
12111212
}
12121213

1214+
rv = mutator.Finalize(url);
12131215
if (NS_FAILED(rv)) {
12141216
return rv;
12151217
}

dom/url/URLWorker.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -630,16 +630,20 @@ URLWorker::Init(const nsAString& aURL, const Optional<nsAString>& aBase,
630630
}
631631

632632
if (scheme.EqualsLiteral("http") || scheme.EqualsLiteral("https")) {
633-
RefPtr<nsStandardURL> baseURL;
633+
nsCOMPtr<nsIURI> baseURL;
634634
if (aBase.WasPassed()) {
635635
baseURL = new nsStandardURL();
636636

637637
// XXXcatalinb: SetSpec only writes a warning to the console on urls
638638
// without a valid scheme. I can't fix that because we've come to rely
639639
// on that behaviour in a bunch of different places.
640-
nsresult rv = baseURL->SetSpec(NS_ConvertUTF16toUTF8(aBase.Value()));
640+
nsresult rv = NS_MutateURI(new nsStandardURL::Mutator())
641+
.SetSpec(NS_ConvertUTF16toUTF8(aBase.Value()))
642+
.Finalize(baseURL);
641643
nsAutoCString baseScheme;
642-
baseURL->GetScheme(baseScheme);
644+
if (baseURL) {
645+
baseURL->GetScheme(baseScheme);
646+
}
643647
if (NS_WARN_IF(NS_FAILED(rv)) || baseScheme.IsEmpty()) {
644648
aRv.ThrowTypeError<MSG_INVALID_URL>(aBase.Value());
645649
return;
@@ -707,8 +711,11 @@ URLWorker::SetHref(const nsAString& aHref, ErrorResult& aRv)
707711
}
708712

709713
if (scheme.EqualsLiteral("http") || scheme.EqualsLiteral("https")) {
710-
mStdURL = new nsStandardURL();
711-
aRv = mStdURL->SetSpec(NS_ConvertUTF16toUTF8(aHref));
714+
nsCOMPtr<nsIURI> uri;
715+
aRv = NS_MutateURI(new nsStandardURL::Mutator())
716+
.SetSpec(NS_ConvertUTF16toUTF8(aHref))
717+
.Finalize(uri);
718+
mStdURL = static_cast<net::nsStandardURL*>(uri.get());
712719
if (mURLProxy) {
713720
mWorkerPrivate->AssertIsOnWorkerThread();
714721

image/decoders/icon/nsIconProtocolHandler.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,9 @@ nsIconProtocolHandler::NewURI(const nsACString& aSpec,
6868
nsIURI* aBaseURI,
6969
nsIURI** result)
7070
{
71-
nsCOMPtr<nsIMozIconURI> uri = new nsMozIconURI();
72-
if (!uri) return NS_ERROR_OUT_OF_MEMORY;
73-
74-
nsresult rv = uri->SetSpec(aSpec);
75-
if (NS_FAILED(rv)) return rv;
76-
77-
NS_ADDREF(*result = uri);
78-
return NS_OK;
71+
return NS_MutateURI(new nsMozIconURI::Mutator())
72+
.SetSpec(aSpec)
73+
.Finalize(result);
7974
}
8075

8176
NS_IMETHODIMP

netwerk/base/nsIURIMutator.idl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,12 @@ public:
278278
explicit NS_MutateURI(nsIURI* aURI);
279279
explicit NS_MutateURI(const char * aContractID);
280280

281+
explicit NS_MutateURI(nsIURIMutator* m)
282+
{
283+
mStatus = m ? NS_OK : NS_ERROR_NULL_POINTER;
284+
mMutator = m;
285+
}
286+
281287
NS_MutateURI& SetSpec(const nsACString& aSpec)
282288
{
283289
NS_ENSURE_SUCCESS(mStatus, *this);

netwerk/base/nsSimpleNestedURI.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ nsSimpleNestedURI::Mutate(nsIURIMutator** aMutator)
196196
if (NS_FAILED(rv)) {
197197
return rv;
198198
}
199+
// StartClone calls SetMutable(false) but we need the mutator clone
200+
// to be mutable
201+
mutator->ResetMutable();
199202
mutator.forget(aMutator);
200203
return NS_OK;
201204
}

netwerk/base/nsSimpleNestedURI.h

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,48 @@ class nsSimpleNestedURI : public nsSimpleURI,
7777
, public BaseURIMutator<nsSimpleNestedURI>
7878
{
7979
NS_DECL_ISUPPORTS
80-
NS_DEFINE_NSIMUTATOR_COMMON
8180
NS_FORWARD_SAFE_NSIURISETTERS_RET(mURI)
8281

8382
explicit Mutator() { }
8483
private:
8584
virtual ~Mutator() { }
8685

86+
MOZ_MUST_USE NS_IMETHOD
87+
Deserialize(const mozilla::ipc::URIParams& aParams) override
88+
{
89+
return InitFromIPCParams(aParams);
90+
}
91+
92+
MOZ_MUST_USE NS_IMETHOD
93+
Read(nsIObjectInputStream* aStream) override
94+
{
95+
return InitFromInputStream(aStream);
96+
}
97+
98+
MOZ_MUST_USE NS_IMETHOD
99+
Finalize(nsIURI** aURI) override
100+
{
101+
mURI->mMutable = false;
102+
mURI.forget(aURI);
103+
return NS_OK;
104+
}
105+
106+
MOZ_MUST_USE NS_IMETHOD
107+
SetSpec(const nsACString& aSpec, nsIURIMutator** aMutator) override
108+
{
109+
if (aMutator) {
110+
NS_ADDREF(*aMutator = this);
111+
}
112+
return InitFromSpec(aSpec);
113+
}
114+
115+
void ResetMutable()
116+
{
117+
if (mURI) {
118+
mURI->mMutable = true;
119+
}
120+
}
121+
87122
friend class nsSimpleNestedURI;
88123
};
89124
};

netwerk/protocol/about/nsAboutProtocolHandler.cpp

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,13 @@ nsAboutProtocolHandler::NewURI(const nsACString &aSpec,
113113
*result = nullptr;
114114
nsresult rv;
115115

116+
116117
// Use a simple URI to parse out some stuff first
117-
nsCOMPtr<nsIURI> url = do_CreateInstance(kSimpleURICID, &rv);
118-
if (NS_FAILED(rv)) return rv;
118+
nsCOMPtr<nsIURI> url;
119+
rv = NS_MutateURI(new nsSimpleURI::Mutator())
120+
.SetSpec(aSpec)
121+
.Finalize(url);
119122

120-
rv = url->SetSpec(aSpec);
121123
if (NS_FAILED(rv)) {
122124
return rv;
123125
}
@@ -148,13 +150,10 @@ nsAboutProtocolHandler::NewURI(const nsACString &aSpec,
148150
rv = NS_NewURI(getter_AddRefs(inner), spec);
149151
NS_ENSURE_SUCCESS(rv, rv);
150152

151-
nsSimpleNestedURI* outer = new nsNestedAboutURI(inner, aBaseURI);
152-
NS_ENSURE_TRUE(outer, NS_ERROR_OUT_OF_MEMORY);
153-
154-
// Take a ref to it in the COMPtr we plan to return
155-
url = outer;
156-
157-
rv = outer->SetSpec(aSpec);
153+
RefPtr<nsSimpleNestedURI> outer = new nsNestedAboutURI(inner, aBaseURI);
154+
rv = NS_MutateURI(outer)
155+
.SetSpec(aSpec)
156+
.Finalize(url);
158157
NS_ENSURE_SUCCESS(rv, rv);
159158
}
160159

@@ -298,21 +297,15 @@ nsSafeAboutProtocolHandler::NewURI(const nsACString &aSpec,
298297
nsIURI *aBaseURI,
299298
nsIURI **result)
300299
{
301-
nsresult rv;
302-
303-
nsCOMPtr<nsIURI> url = do_CreateInstance(kSimpleURICID, &rv);
304-
if (NS_FAILED(rv)) return rv;
305-
306-
rv = url->SetSpec(aSpec);
300+
nsresult rv = NS_MutateURI(new nsSimpleURI::Mutator())
301+
.SetSpec(aSpec)
302+
.Finalize(result);
307303
if (NS_FAILED(rv)) {
308304
return rv;
309305
}
310306

311-
NS_TryToSetImmutable(url);
312-
313-
*result = nullptr;
314-
url.swap(*result);
315-
return rv;
307+
NS_TryToSetImmutable(*result);
308+
return NS_OK;
316309
}
317310

318311
NS_IMETHODIMP
@@ -437,6 +430,9 @@ nsNestedAboutURI::Mutate(nsIURIMutator** aMutator)
437430
if (NS_FAILED(rv)) {
438431
return rv;
439432
}
433+
// StartClone calls SetMutable(false) but we need the mutator clone
434+
// to be mutable
435+
mutator->ResetMutable();
440436
mutator.forget(aMutator);
441437
return NS_OK;
442438
}

netwerk/protocol/about/nsAboutProtocolHandler.h

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,47 @@ class nsNestedAboutURI : public nsSimpleNestedURI {
9393
{
9494
NS_DECL_ISUPPORTS
9595
NS_FORWARD_SAFE_NSIURISETTERS_RET(mURI)
96-
NS_DEFINE_NSIMUTATOR_COMMON
9796

9897
explicit Mutator() { }
9998
private:
10099
virtual ~Mutator() { }
101100

101+
MOZ_MUST_USE NS_IMETHOD
102+
Deserialize(const mozilla::ipc::URIParams& aParams) override
103+
{
104+
return InitFromIPCParams(aParams);
105+
}
106+
107+
MOZ_MUST_USE NS_IMETHOD
108+
Read(nsIObjectInputStream* aStream) override
109+
{
110+
return InitFromInputStream(aStream);
111+
}
112+
113+
MOZ_MUST_USE NS_IMETHOD
114+
Finalize(nsIURI** aURI) override
115+
{
116+
mURI->mMutable = false;
117+
mURI.forget(aURI);
118+
return NS_OK;
119+
}
120+
121+
MOZ_MUST_USE NS_IMETHOD
122+
SetSpec(const nsACString& aSpec, nsIURIMutator** aMutator) override
123+
{
124+
if (aMutator) {
125+
NS_ADDREF(*aMutator = this);
126+
}
127+
return InitFromSpec(aSpec);
128+
}
129+
130+
void ResetMutable()
131+
{
132+
if (mURI) {
133+
mURI->mMutable = true;
134+
}
135+
}
136+
102137
friend class nsNestedAboutURI;
103138
};
104139
};

netwerk/protocol/data/nsDataHandler.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
#include "nsError.h"
1010
#include "DataChannelChild.h"
1111
#include "plstr.h"
12-
13-
static NS_DEFINE_CID(kSimpleURICID, NS_SIMPLEURI_CID);
12+
#include "nsSimpleURI.h"
1413

1514
////////////////////////////////////////////////////////////////////////////////
1615

@@ -64,7 +63,7 @@ nsDataHandler::NewURI(const nsACString &aSpec,
6463
nsIURI *aBaseURI,
6564
nsIURI **result) {
6665
nsresult rv;
67-
RefPtr<nsIURI> uri;
66+
nsCOMPtr<nsIURI> uri;
6867

6968
nsCString spec(aSpec);
7069

@@ -94,10 +93,9 @@ nsDataHandler::NewURI(const nsACString &aSpec,
9493
}
9594
}
9695

97-
uri = do_CreateInstance(kSimpleURICID, &rv);
98-
if (NS_FAILED(rv))
99-
return rv;
100-
rv = uri->SetSpec(spec);
96+
rv = NS_MutateURI(new nsSimpleURI::Mutator())
97+
.SetSpec(spec)
98+
.Finalize(uri);
10199
}
102100

103101
if (NS_FAILED(rv))

0 commit comments

Comments
 (0)