Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
* License, v. 2.0. If a copy of the MPL was not distributed with this
4
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#include "base/basictypes.h"
7
8
#include "nsNetCID.h"
9
#include "nsNetUtil.h"
10
#include "nsSimpleNestedURI.h"
11
#include "nsIObjectInputStream.h"
12
#include "nsIObjectOutputStream.h"
13
14
#include "mozilla/ipc/URIUtils.h"
15
16
namespace mozilla {
17
namespace net {
18
19
NS_IMPL_ISUPPORTS_INHERITED(nsSimpleNestedURI, nsSimpleURI, nsINestedURI)
20
21
nsSimpleNestedURI::nsSimpleNestedURI(nsIURI* innerURI) : mInnerURI(innerURI) {
22
NS_ASSERTION(innerURI, "Must have inner URI");
23
}
24
25
nsresult nsSimpleNestedURI::SetPathQueryRef(const nsACString& aPathQueryRef) {
26
NS_ENSURE_TRUE(mInnerURI, NS_ERROR_NOT_INITIALIZED);
27
28
nsCOMPtr<nsIURI> inner;
29
nsresult rv =
30
NS_MutateURI(mInnerURI).SetPathQueryRef(aPathQueryRef).Finalize(inner);
31
NS_ENSURE_SUCCESS(rv, rv);
32
rv = nsSimpleURI::SetPathQueryRef(aPathQueryRef);
33
NS_ENSURE_SUCCESS(rv, rv);
34
// If the regular SetPathQueryRef worked, also set it on the inner URI
35
mInnerURI = inner;
36
return NS_OK;
37
}
38
39
nsresult nsSimpleNestedURI::SetQuery(const nsACString& aQuery) {
40
NS_ENSURE_TRUE(mInnerURI, NS_ERROR_NOT_INITIALIZED);
41
42
nsCOMPtr<nsIURI> inner;
43
nsresult rv = NS_MutateURI(mInnerURI).SetQuery(aQuery).Finalize(inner);
44
NS_ENSURE_SUCCESS(rv, rv);
45
rv = nsSimpleURI::SetQuery(aQuery);
46
NS_ENSURE_SUCCESS(rv, rv);
47
// If the regular SetQuery worked, also set it on the inner URI
48
mInnerURI = inner;
49
return NS_OK;
50
}
51
52
nsresult nsSimpleNestedURI::SetRef(const nsACString& aRef) {
53
NS_ENSURE_TRUE(mInnerURI, NS_ERROR_NOT_INITIALIZED);
54
55
nsCOMPtr<nsIURI> inner;
56
nsresult rv = NS_MutateURI(mInnerURI).SetRef(aRef).Finalize(inner);
57
NS_ENSURE_SUCCESS(rv, rv);
58
rv = nsSimpleURI::SetRef(aRef);
59
NS_ENSURE_SUCCESS(rv, rv);
60
// If the regular SetRef worked, also set it on the inner URI
61
mInnerURI = inner;
62
return NS_OK;
63
}
64
65
// nsISerializable
66
67
NS_IMETHODIMP
68
nsSimpleNestedURI::Read(nsIObjectInputStream* aStream) {
69
MOZ_ASSERT_UNREACHABLE("Use nsIURIMutator.read() instead");
70
return NS_ERROR_NOT_IMPLEMENTED;
71
}
72
73
nsresult nsSimpleNestedURI::ReadPrivate(nsIObjectInputStream* aStream) {
74
nsresult rv = nsSimpleURI::ReadPrivate(aStream);
75
if (NS_FAILED(rv)) return rv;
76
77
nsCOMPtr<nsISupports> supports;
78
rv = aStream->ReadObject(true, getter_AddRefs(supports));
79
if (NS_FAILED(rv)) return rv;
80
81
mInnerURI = do_QueryInterface(supports, &rv);
82
if (NS_FAILED(rv)) return rv;
83
84
return rv;
85
}
86
87
NS_IMETHODIMP
88
nsSimpleNestedURI::Write(nsIObjectOutputStream* aStream) {
89
nsCOMPtr<nsISerializable> serializable = do_QueryInterface(mInnerURI);
90
if (!serializable) {
91
// We can't serialize ourselves
92
return NS_ERROR_NOT_AVAILABLE;
93
}
94
95
nsresult rv = nsSimpleURI::Write(aStream);
96
if (NS_FAILED(rv)) return rv;
97
98
rv = aStream->WriteCompoundObject(mInnerURI, NS_GET_IID(nsIURI), true);
99
return rv;
100
}
101
102
NS_IMETHODIMP_(void)
103
nsSimpleNestedURI::Serialize(mozilla::ipc::URIParams& aParams) {
104
using namespace mozilla::ipc;
105
106
SimpleNestedURIParams params;
107
URIParams simpleParams;
108
109
nsSimpleURI::Serialize(simpleParams);
110
params.simpleParams() = simpleParams;
111
112
SerializeURI(mInnerURI, params.innerURI());
113
114
aParams = params;
115
}
116
117
bool nsSimpleNestedURI::Deserialize(const mozilla::ipc::URIParams& aParams) {
118
using namespace mozilla::ipc;
119
120
if (aParams.type() != URIParams::TSimpleNestedURIParams) {
121
NS_ERROR("Received unknown parameters from the other process!");
122
return false;
123
}
124
125
const SimpleNestedURIParams& params = aParams.get_SimpleNestedURIParams();
126
if (!nsSimpleURI::Deserialize(params.simpleParams())) return false;
127
128
mInnerURI = DeserializeURI(params.innerURI());
129
return true;
130
}
131
132
// nsINestedURI
133
134
NS_IMETHODIMP
135
nsSimpleNestedURI::GetInnerURI(nsIURI** aURI) {
136
NS_ENSURE_TRUE(mInnerURI, NS_ERROR_NOT_INITIALIZED);
137
138
nsCOMPtr<nsIURI> uri = mInnerURI;
139
uri.forget(aURI);
140
return NS_OK;
141
}
142
143
NS_IMETHODIMP
144
nsSimpleNestedURI::GetInnermostURI(nsIURI** uri) {
145
return NS_ImplGetInnermostURI(this, uri);
146
}
147
148
// nsSimpleURI overrides
149
/* virtual */
150
nsresult nsSimpleNestedURI::EqualsInternal(
151
nsIURI* other, nsSimpleURI::RefHandlingEnum refHandlingMode, bool* result) {
152
*result = false;
153
NS_ENSURE_TRUE(mInnerURI, NS_ERROR_NOT_INITIALIZED);
154
155
if (other) {
156
bool correctScheme;
157
nsresult rv = other->SchemeIs(mScheme.get(), &correctScheme);
158
NS_ENSURE_SUCCESS(rv, rv);
159
160
if (correctScheme) {
161
nsCOMPtr<nsINestedURI> nest = do_QueryInterface(other);
162
if (nest) {
163
nsCOMPtr<nsIURI> otherInner;
164
rv = nest->GetInnerURI(getter_AddRefs(otherInner));
165
NS_ENSURE_SUCCESS(rv, rv);
166
167
return (refHandlingMode == eHonorRef)
168
? otherInner->Equals(mInnerURI, result)
169
: otherInner->EqualsExceptRef(mInnerURI, result);
170
}
171
}
172
}
173
174
return NS_OK;
175
}
176
177
/* virtual */
178
nsSimpleURI* nsSimpleNestedURI::StartClone(
179
nsSimpleURI::RefHandlingEnum refHandlingMode, const nsACString& newRef) {
180
NS_ENSURE_TRUE(mInnerURI, nullptr);
181
182
nsCOMPtr<nsIURI> innerClone;
183
nsresult rv = NS_OK;
184
if (refHandlingMode == eHonorRef) {
185
innerClone = mInnerURI;
186
} else if (refHandlingMode == eReplaceRef) {
187
rv = NS_GetURIWithNewRef(mInnerURI, newRef, getter_AddRefs(innerClone));
188
} else {
189
rv = NS_GetURIWithoutRef(mInnerURI, getter_AddRefs(innerClone));
190
}
191
192
if (NS_FAILED(rv)) {
193
return nullptr;
194
}
195
196
nsSimpleNestedURI* url = new nsSimpleNestedURI(innerClone);
197
SetRefOnClone(url, refHandlingMode, newRef);
198
199
return url;
200
}
201
202
// nsIClassInfo overrides
203
204
NS_IMETHODIMP
205
nsSimpleNestedURI::GetClassIDNoAlloc(nsCID* aClassIDNoAlloc) {
206
static NS_DEFINE_CID(kSimpleNestedURICID, NS_SIMPLENESTEDURI_CID);
207
208
*aClassIDNoAlloc = kSimpleNestedURICID;
209
return NS_OK;
210
}
211
212
// Queries this list of interfaces. If none match, it queries mURI.
213
NS_IMPL_NSIURIMUTATOR_ISUPPORTS(nsSimpleNestedURI::Mutator, nsIURISetters,
214
nsIURIMutator, nsISerializable,
215
nsINestedURIMutator)
216
217
NS_IMETHODIMP
218
nsSimpleNestedURI::Mutate(nsIURIMutator** aMutator) {
219
RefPtr<nsSimpleNestedURI::Mutator> mutator = new nsSimpleNestedURI::Mutator();
220
nsresult rv = mutator->InitFromURI(this);
221
if (NS_FAILED(rv)) {
222
return rv;
223
}
224
mutator.forget(aMutator);
225
return NS_OK;
226
}
227
228
} // namespace net
229
} // namespace mozilla