Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * vim: sw=2 ts=2 sts=2 expandtab
3 : * This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "NullPrincipalURI.h"
8 :
9 : #include "mozilla/DebugOnly.h"
10 : #include "mozilla/MemoryReporting.h"
11 :
12 : #include "mozilla/ipc/URIParams.h"
13 :
14 : #include "nsEscape.h"
15 : #include "nsCRT.h"
16 : #include "nsIUUIDGenerator.h"
17 :
18 : ////////////////////////////////////////////////////////////////////////////////
19 : //// NullPrincipalURI
20 :
21 141 : NullPrincipalURI::NullPrincipalURI()
22 141 : : mPath(mPathBytes, ArrayLength(mPathBytes), ArrayLength(mPathBytes) - 1)
23 : {
24 141 : }
25 :
26 33 : NullPrincipalURI::NullPrincipalURI(const NullPrincipalURI& aOther)
27 33 : : mPath(mPathBytes, ArrayLength(mPathBytes), ArrayLength(mPathBytes) - 1)
28 : {
29 33 : mPath.Assign(aOther.mPath);
30 33 : }
31 :
32 : nsresult
33 141 : NullPrincipalURI::Init()
34 : {
35 : // FIXME: bug 327161 -- make sure the uuid generator is reseeding-resistant.
36 282 : nsCOMPtr<nsIUUIDGenerator> uuidgen = services::GetUUIDGenerator();
37 141 : NS_ENSURE_TRUE(uuidgen, NS_ERROR_NOT_AVAILABLE);
38 :
39 : nsID id;
40 141 : nsresult rv = uuidgen->GenerateUUIDInPlace(&id);
41 141 : NS_ENSURE_SUCCESS(rv, rv);
42 :
43 141 : MOZ_ASSERT(mPathBytes == mPath.BeginWriting());
44 :
45 141 : id.ToProvidedString(mPathBytes);
46 :
47 141 : MOZ_ASSERT(mPath.Length() == NSID_LENGTH - 1);
48 141 : MOZ_ASSERT(strlen(mPath.get()) == NSID_LENGTH - 1);
49 :
50 141 : return NS_OK;
51 : }
52 :
53 : /* static */
54 : already_AddRefed<NullPrincipalURI>
55 141 : NullPrincipalURI::Create()
56 : {
57 282 : RefPtr<NullPrincipalURI> uri = new NullPrincipalURI();
58 141 : nsresult rv = uri->Init();
59 141 : NS_ENSURE_SUCCESS(rv, nullptr);
60 141 : return uri.forget();
61 : }
62 :
63 : static NS_DEFINE_CID(kNullPrincipalURIImplementationCID,
64 : NS_NULLPRINCIPALURI_IMPLEMENTATION_CID);
65 :
66 392 : NS_IMPL_ADDREF(NullPrincipalURI)
67 409 : NS_IMPL_RELEASE(NullPrincipalURI)
68 :
69 259 : NS_INTERFACE_MAP_BEGIN(NullPrincipalURI)
70 259 : NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIURI)
71 258 : if (aIID.Equals(kNullPrincipalURIImplementationCID))
72 0 : foundInterface = static_cast<nsIURI*>(this);
73 : else
74 258 : NS_INTERFACE_MAP_ENTRY(nsIURI)
75 45 : NS_INTERFACE_MAP_ENTRY(nsISizeOf)
76 45 : NS_INTERFACE_MAP_ENTRY(nsIIPCSerializableURI)
77 45 : NS_INTERFACE_MAP_END
78 :
79 : ////////////////////////////////////////////////////////////////////////////////
80 : //// nsIURI
81 :
82 : NS_IMETHODIMP
83 2 : NullPrincipalURI::GetAsciiHost(nsACString& _host)
84 : {
85 2 : _host.Truncate();
86 2 : return NS_OK;
87 : }
88 :
89 : NS_IMETHODIMP
90 0 : NullPrincipalURI::GetAsciiHostPort(nsACString& _hostport)
91 : {
92 0 : return NS_ERROR_NOT_IMPLEMENTED;
93 : }
94 :
95 : NS_IMETHODIMP
96 0 : NullPrincipalURI::GetAsciiSpec(nsACString& _spec)
97 : {
98 0 : nsAutoCString buffer;
99 : // Ignore the return value -- NullPrincipalURI::GetSpec() is infallible.
100 0 : Unused << GetSpec(buffer);
101 : // This uses the infallible version of |NS_EscapeURL| as |GetSpec| is
102 : // already infallible.
103 0 : NS_EscapeURL(buffer, esc_OnlyNonASCII | esc_AlwaysCopy, _spec);
104 0 : return NS_OK;
105 : }
106 :
107 : NS_IMETHODIMP
108 2 : NullPrincipalURI::GetHost(nsACString& _host)
109 : {
110 2 : _host.Truncate();
111 2 : return NS_OK;
112 : }
113 :
114 : NS_IMETHODIMP
115 0 : NullPrincipalURI::SetHost(const nsACString& aHost)
116 : {
117 0 : return NS_ERROR_NOT_IMPLEMENTED;
118 : }
119 :
120 : NS_IMETHODIMP
121 0 : NullPrincipalURI::GetHostPort(nsACString& _host)
122 : {
123 0 : return NS_ERROR_NOT_IMPLEMENTED;
124 : }
125 :
126 : NS_IMETHODIMP
127 0 : NullPrincipalURI::SetHostPort(const nsACString& aHost)
128 : {
129 0 : return NS_ERROR_NOT_IMPLEMENTED;
130 : }
131 :
132 : NS_IMETHODIMP
133 0 : NullPrincipalURI::SetHostAndPort(const nsACString& aHost)
134 : {
135 0 : return NS_ERROR_NOT_IMPLEMENTED;
136 : }
137 :
138 : NS_IMETHODIMP
139 0 : NullPrincipalURI::GetOriginCharset(nsACString& _charset)
140 : {
141 0 : _charset.Truncate();
142 0 : return NS_OK;
143 : }
144 :
145 : NS_IMETHODIMP
146 0 : NullPrincipalURI::GetPassword(nsACString& _password)
147 : {
148 0 : return NS_ERROR_NOT_IMPLEMENTED;
149 : }
150 :
151 : NS_IMETHODIMP
152 0 : NullPrincipalURI::SetPassword(const nsACString& aPassword)
153 : {
154 0 : return NS_ERROR_NOT_IMPLEMENTED;
155 : }
156 :
157 : NS_IMETHODIMP
158 13 : NullPrincipalURI::GetPath(nsACString& _path)
159 : {
160 13 : _path = mPath;
161 13 : return NS_OK;
162 : }
163 :
164 : NS_IMETHODIMP
165 0 : NullPrincipalURI::SetPath(const nsACString& aPath)
166 : {
167 0 : return NS_ERROR_NOT_IMPLEMENTED;
168 : }
169 :
170 : NS_IMETHODIMP
171 0 : NullPrincipalURI::GetFilePath(nsACString& aFilePath)
172 : {
173 0 : aFilePath.Truncate();
174 0 : return NS_ERROR_NOT_IMPLEMENTED;
175 : }
176 :
177 : NS_IMETHODIMP
178 0 : NullPrincipalURI::SetFilePath(const nsACString& aFilePath)
179 : {
180 0 : return NS_ERROR_NOT_IMPLEMENTED;
181 : }
182 :
183 : NS_IMETHODIMP
184 0 : NullPrincipalURI::GetQuery(nsACString& aQuery)
185 : {
186 0 : aQuery.Truncate();
187 0 : return NS_ERROR_NOT_IMPLEMENTED;
188 : }
189 :
190 : NS_IMETHODIMP
191 0 : NullPrincipalURI::SetQuery(const nsACString& aQuery)
192 : {
193 0 : return NS_ERROR_NOT_IMPLEMENTED;
194 : }
195 :
196 : NS_IMETHODIMP
197 0 : NullPrincipalURI::GetRef(nsACString& _ref)
198 : {
199 0 : _ref.Truncate();
200 0 : return NS_ERROR_NOT_IMPLEMENTED;
201 : }
202 :
203 : NS_IMETHODIMP
204 0 : NullPrincipalURI::SetRef(const nsACString& aRef)
205 : {
206 0 : return NS_ERROR_NOT_IMPLEMENTED;
207 : }
208 :
209 : NS_IMETHODIMP
210 0 : NullPrincipalURI::GetPrePath(nsACString& _prePath)
211 : {
212 0 : _prePath = NS_LITERAL_CSTRING(NS_NULLPRINCIPAL_SCHEME ":");
213 0 : return NS_OK;
214 : }
215 :
216 : NS_IMETHODIMP
217 0 : NullPrincipalURI::GetPort(int32_t* _port)
218 : {
219 0 : return NS_ERROR_NOT_IMPLEMENTED;
220 : }
221 :
222 : NS_IMETHODIMP
223 0 : NullPrincipalURI::SetPort(int32_t aPort)
224 : {
225 0 : return NS_ERROR_NOT_IMPLEMENTED;
226 : }
227 :
228 : NS_IMETHODIMP
229 1 : NullPrincipalURI::GetScheme(nsACString& _scheme)
230 : {
231 1 : _scheme = NS_LITERAL_CSTRING(NS_NULLPRINCIPAL_SCHEME);
232 1 : return NS_OK;
233 : }
234 :
235 : NS_IMETHODIMP
236 0 : NullPrincipalURI::SetScheme(const nsACString& aScheme)
237 : {
238 0 : return NS_ERROR_NOT_IMPLEMENTED;
239 : }
240 :
241 : NS_IMETHODIMP
242 141 : NullPrincipalURI::GetSpec(nsACString& _spec)
243 : {
244 141 : _spec = NS_LITERAL_CSTRING(NS_NULLPRINCIPAL_SCHEME ":") + mPath;
245 141 : return NS_OK;
246 : }
247 :
248 : // result may contain unescaped UTF-8 characters
249 : NS_IMETHODIMP
250 0 : NullPrincipalURI::GetSpecIgnoringRef(nsACString& _result)
251 : {
252 0 : return GetSpec(_result);
253 : }
254 :
255 : NS_IMETHODIMP
256 0 : NullPrincipalURI::GetHasRef(bool* _result)
257 : {
258 0 : *_result = false;
259 0 : return NS_OK;
260 : }
261 :
262 : NS_IMETHODIMP
263 0 : NullPrincipalURI::SetSpec(const nsACString& aSpec)
264 : {
265 0 : return NS_ERROR_NOT_IMPLEMENTED;
266 : }
267 :
268 : NS_IMETHODIMP
269 0 : NullPrincipalURI::GetUsername(nsACString& _username)
270 : {
271 0 : return NS_ERROR_NOT_IMPLEMENTED;
272 : }
273 :
274 : NS_IMETHODIMP
275 0 : NullPrincipalURI::SetUsername(const nsACString& aUsername)
276 : {
277 0 : return NS_ERROR_NOT_IMPLEMENTED;
278 : }
279 :
280 : NS_IMETHODIMP
281 0 : NullPrincipalURI::GetUserPass(nsACString& _userPass)
282 : {
283 0 : return NS_ERROR_NOT_IMPLEMENTED;
284 : }
285 :
286 : NS_IMETHODIMP
287 0 : NullPrincipalURI::SetUserPass(const nsACString& aUserPass)
288 : {
289 0 : return NS_ERROR_NOT_IMPLEMENTED;
290 : }
291 :
292 : NS_IMETHODIMP
293 33 : NullPrincipalURI::Clone(nsIURI** _newURI)
294 : {
295 66 : nsCOMPtr<nsIURI> uri = new NullPrincipalURI(*this);
296 33 : uri.forget(_newURI);
297 66 : return NS_OK;
298 : }
299 :
300 : NS_IMETHODIMP
301 0 : NullPrincipalURI::CloneIgnoringRef(nsIURI** _newURI)
302 : {
303 : // GetRef/SetRef not supported by NullPrincipalURI, so
304 : // CloneIgnoringRef() is the same as Clone().
305 0 : return Clone(_newURI);
306 : }
307 :
308 : NS_IMETHODIMP
309 0 : NullPrincipalURI::CloneWithNewRef(const nsACString& newRef, nsIURI** _newURI)
310 : {
311 : // GetRef/SetRef not supported by NullPrincipalURI, so
312 : // CloneWithNewRef() is the same as Clone().
313 0 : return Clone(_newURI);
314 : }
315 :
316 : NS_IMETHODIMP
317 0 : NullPrincipalURI::Equals(nsIURI* aOther, bool* _equals)
318 : {
319 0 : *_equals = false;
320 0 : RefPtr<NullPrincipalURI> otherURI;
321 0 : nsresult rv = aOther->QueryInterface(kNullPrincipalURIImplementationCID,
322 0 : getter_AddRefs(otherURI));
323 0 : if (NS_SUCCEEDED(rv)) {
324 0 : *_equals = mPath == otherURI->mPath;
325 : }
326 0 : return NS_OK;
327 : }
328 :
329 : NS_IMETHODIMP
330 0 : NullPrincipalURI::EqualsExceptRef(nsIURI* aOther, bool* _equals)
331 : {
332 : // GetRef/SetRef not supported by NullPrincipalURI, so
333 : // EqualsExceptRef() is the same as Equals().
334 0 : return Equals(aOther, _equals);
335 : }
336 :
337 : NS_IMETHODIMP
338 0 : NullPrincipalURI::Resolve(const nsACString& aRelativePath,
339 : nsACString& _resolvedURI)
340 : {
341 0 : _resolvedURI = aRelativePath;
342 0 : return NS_OK;
343 : }
344 :
345 : NS_IMETHODIMP
346 13 : NullPrincipalURI::SchemeIs(const char* aScheme, bool* _schemeIs)
347 : {
348 13 : *_schemeIs = (0 == nsCRT::strcasecmp(NS_NULLPRINCIPAL_SCHEME, aScheme));
349 13 : return NS_OK;
350 : }
351 :
352 : NS_IMETHODIMP
353 0 : NullPrincipalURI::GetDisplaySpec(nsACString &aUnicodeSpec)
354 : {
355 0 : return GetSpec(aUnicodeSpec);
356 : }
357 :
358 : NS_IMETHODIMP
359 0 : NullPrincipalURI::GetDisplayHostPort(nsACString &aUnicodeHostPort)
360 : {
361 0 : return GetHostPort(aUnicodeHostPort);
362 : }
363 :
364 : NS_IMETHODIMP
365 0 : NullPrincipalURI::GetDisplayHost(nsACString &aUnicodeHost)
366 : {
367 0 : return GetHost(aUnicodeHost);
368 : }
369 :
370 : ////////////////////////////////////////////////////////////////////////////////
371 : //// nsIIPCSerializableURI
372 :
373 : void
374 0 : NullPrincipalURI::Serialize(mozilla::ipc::URIParams& aParams)
375 : {
376 0 : aParams = mozilla::ipc::NullPrincipalURIParams();
377 0 : }
378 :
379 : bool
380 0 : NullPrincipalURI::Deserialize(const mozilla::ipc::URIParams& aParams)
381 : {
382 0 : if (aParams.type() != mozilla::ipc::URIParams::TNullPrincipalURIParams) {
383 0 : MOZ_ASSERT_UNREACHABLE("unexpected URIParams type");
384 : return false;
385 : }
386 :
387 0 : nsresult rv = Init();
388 0 : NS_ENSURE_SUCCESS(rv, false);
389 :
390 0 : return true;
391 : }
392 :
393 : ////////////////////////////////////////////////////////////////////////////////
394 : //// nsISizeOf
395 :
396 : size_t
397 0 : NullPrincipalURI::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
398 : {
399 0 : return mPath.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
400 : }
401 :
402 : size_t
403 0 : NullPrincipalURI::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
404 : {
405 0 : return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
406 : }
|