Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / caps / src / nsNullPrincipal.cpp
blobf5c11a481ff66c049a0b856690fadea927d00bec
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * the Mozilla Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2006
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Boris Zbarsky <bzbarsky@mit.edu> (Original author)
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 /**
40 * This is the principal that has no rights and can't be accessed by
41 * anything other than itself and chrome; null principals are not
42 * same-origin with anything but themselves.
45 #include "nsNullPrincipal.h"
46 #include "nsNullPrincipalURI.h"
47 #include "nsMemory.h"
48 #include "nsIUUIDGenerator.h"
49 #include "nsID.h"
50 #include "nsNetUtil.h"
51 #include "nsIClassInfoImpl.h"
52 #include "nsNetCID.h"
53 #include "nsDOMError.h"
54 #include "nsScriptSecurityManager.h"
56 NS_IMPL_CLASSINFO(nsNullPrincipal, NULL, nsIClassInfo::MAIN_THREAD_ONLY,
57 NS_NULLPRINCIPAL_CID)
58 NS_IMPL_QUERY_INTERFACE2_CI(nsNullPrincipal,
59 nsIPrincipal,
60 nsISerializable)
61 NS_IMPL_CI_INTERFACE_GETTER2(nsNullPrincipal,
62 nsIPrincipal,
63 nsISerializable)
65 NS_IMETHODIMP_(nsrefcnt)
66 nsNullPrincipal::AddRef()
68 NS_PRECONDITION(PRInt32(mJSPrincipals.refcount) >= 0, "illegal refcnt");
69 nsrefcnt count = PR_AtomicIncrement((PRInt32 *)&mJSPrincipals.refcount);
70 NS_LOG_ADDREF(this, count, "nsNullPrincipal", sizeof(*this));
71 return count;
74 NS_IMETHODIMP_(nsrefcnt)
75 nsNullPrincipal::Release()
77 NS_PRECONDITION(0 != mJSPrincipals.refcount, "dup release");
78 nsrefcnt count = PR_AtomicDecrement((PRInt32 *)&mJSPrincipals.refcount);
79 NS_LOG_RELEASE(this, count, "nsNullPrincipal");
80 if (count == 0) {
81 delete this;
84 return count;
87 nsNullPrincipal::nsNullPrincipal()
91 nsNullPrincipal::~nsNullPrincipal()
95 #define NS_NULLPRINCIPAL_PREFIX NS_NULLPRINCIPAL_SCHEME ":"
97 nsresult
98 nsNullPrincipal::Init()
100 // FIXME: bug 327161 -- make sure the uuid generator is reseeding-resistant.
101 nsresult rv;
102 nsCOMPtr<nsIUUIDGenerator> uuidgen =
103 do_GetService("@mozilla.org/uuid-generator;1", &rv);
104 NS_ENSURE_SUCCESS(rv, rv);
106 nsID id;
107 rv = uuidgen->GenerateUUIDInPlace(&id);
108 NS_ENSURE_SUCCESS(rv, rv);
110 char chars[NSID_LENGTH];
111 id.ToProvidedString(chars);
113 PRUint32 suffixLen = NSID_LENGTH - 1;
114 PRUint32 prefixLen = NS_ARRAY_LENGTH(NS_NULLPRINCIPAL_PREFIX) - 1;
116 // Use an nsCString so we only do the allocation once here and then share
117 // with nsJSPrincipals
118 nsCString str;
119 str.SetCapacity(prefixLen + suffixLen);
121 str.Append(NS_NULLPRINCIPAL_PREFIX);
122 str.Append(chars);
124 if (str.Length() != prefixLen + suffixLen) {
125 NS_WARNING("Out of memory allocating null-principal URI");
126 return NS_ERROR_OUT_OF_MEMORY;
129 mURI = new nsNullPrincipalURI(str);
130 NS_ENSURE_TRUE(mURI, NS_ERROR_OUT_OF_MEMORY);
132 return mJSPrincipals.Init(this, str);
136 * nsIPrincipal implementation
139 NS_IMETHODIMP
140 nsNullPrincipal::GetPreferences(char** aPrefName, char** aID,
141 char** aSubjectName,
142 char** aGrantedList, char** aDeniedList,
143 PRBool* aIsTrusted)
145 // The null principal should never be written to preferences.
146 *aPrefName = nsnull;
147 *aID = nsnull;
148 *aSubjectName = nsnull;
149 *aGrantedList = nsnull;
150 *aDeniedList = nsnull;
151 *aIsTrusted = PR_FALSE;
153 return NS_ERROR_FAILURE;
156 NS_IMETHODIMP
157 nsNullPrincipal::Equals(nsIPrincipal *aOther, PRBool *aResult)
159 // Just equal to ourselves. Note that nsPrincipal::Equals will return false
160 // for us since we have a unique domain/origin/etc.
161 *aResult = (aOther == this);
162 return NS_OK;
165 NS_IMETHODIMP
166 nsNullPrincipal::GetHashValue(PRUint32 *aResult)
168 *aResult = (NS_PTR_TO_INT32(this) >> 2);
169 return NS_OK;
172 NS_IMETHODIMP
173 nsNullPrincipal::GetJSPrincipals(JSContext *cx, JSPrincipals **aJsprin)
175 NS_PRECONDITION(mJSPrincipals.nsIPrincipalPtr,
176 "mJSPrincipals is uninitalized!");
178 JSPRINCIPALS_HOLD(cx, &mJSPrincipals);
179 *aJsprin = &mJSPrincipals;
180 return NS_OK;
183 NS_IMETHODIMP
184 nsNullPrincipal::GetSecurityPolicy(void** aSecurityPolicy)
186 // We don't actually do security policy caching. And it's not like anyone
187 // can set a security policy for us anyway.
188 *aSecurityPolicy = nsnull;
189 return NS_OK;
192 NS_IMETHODIMP
193 nsNullPrincipal::SetSecurityPolicy(void* aSecurityPolicy)
195 // We don't actually do security policy caching. And it's not like anyone
196 // can set a security policy for us anyway.
197 return NS_OK;
200 NS_IMETHODIMP
201 nsNullPrincipal::CanEnableCapability(const char *aCapability,
202 PRInt16 *aResult)
204 // Null principal can enable no capabilities.
205 *aResult = nsIPrincipal::ENABLE_DENIED;
206 return NS_OK;
209 NS_IMETHODIMP
210 nsNullPrincipal::SetCanEnableCapability(const char *aCapability,
211 PRInt16 aCanEnable)
213 return NS_ERROR_NOT_AVAILABLE;
217 NS_IMETHODIMP
218 nsNullPrincipal::IsCapabilityEnabled(const char *aCapability,
219 void *aAnnotation,
220 PRBool *aResult)
222 // Nope. No capabilities, I say!
223 *aResult = PR_FALSE;
224 return NS_OK;
227 NS_IMETHODIMP
228 nsNullPrincipal::EnableCapability(const char *aCapability, void **aAnnotation)
230 NS_NOTREACHED("Didn't I say it? NO CAPABILITIES!");
231 *aAnnotation = nsnull;
232 return NS_OK;
235 NS_IMETHODIMP
236 nsNullPrincipal::RevertCapability(const char *aCapability, void **aAnnotation)
238 *aAnnotation = nsnull;
239 return NS_OK;
242 NS_IMETHODIMP
243 nsNullPrincipal::DisableCapability(const char *aCapability, void **aAnnotation)
245 // Just a no-op. They're all disabled anyway.
246 *aAnnotation = nsnull;
247 return NS_OK;
250 NS_IMETHODIMP
251 nsNullPrincipal::GetURI(nsIURI** aURI)
253 return NS_EnsureSafeToReturn(mURI, aURI);
256 NS_IMETHODIMP
257 nsNullPrincipal::GetCsp(nsIContentSecurityPolicy** aCsp)
259 // CSP on a null principal makes no sense
260 *aCsp = nsnull;
261 return NS_OK;
264 NS_IMETHODIMP
265 nsNullPrincipal::SetCsp(nsIContentSecurityPolicy* aCsp)
267 // CSP on a null principal makes no sense
268 return NS_ERROR_NOT_AVAILABLE;
271 NS_IMETHODIMP
272 nsNullPrincipal::GetDomain(nsIURI** aDomain)
274 return NS_EnsureSafeToReturn(mURI, aDomain);
277 NS_IMETHODIMP
278 nsNullPrincipal::SetDomain(nsIURI* aDomain)
280 // I think the right thing to do here is to just throw... Silently failing
281 // seems counterproductive.
282 return NS_ERROR_NOT_AVAILABLE;
285 NS_IMETHODIMP
286 nsNullPrincipal::GetOrigin(char** aOrigin)
288 *aOrigin = nsnull;
290 nsCAutoString str;
291 nsresult rv = mURI->GetSpec(str);
292 NS_ENSURE_SUCCESS(rv, rv);
294 *aOrigin = ToNewCString(str);
295 NS_ENSURE_TRUE(*aOrigin, NS_ERROR_OUT_OF_MEMORY);
297 return NS_OK;
300 NS_IMETHODIMP
301 nsNullPrincipal::GetHasCertificate(PRBool* aResult)
303 *aResult = PR_FALSE;
304 return NS_OK;
307 NS_IMETHODIMP
308 nsNullPrincipal::GetFingerprint(nsACString& aID)
310 return NS_ERROR_NOT_AVAILABLE;
313 NS_IMETHODIMP
314 nsNullPrincipal::GetPrettyName(nsACString& aName)
316 return NS_ERROR_NOT_AVAILABLE;
319 NS_IMETHODIMP
320 nsNullPrincipal::Subsumes(nsIPrincipal *aOther, PRBool *aResult)
322 // We don't subsume anything except ourselves. Note that nsPrincipal::Equals
323 // will return false for us, since we're not about:blank and not Equals to
324 // reasonable nsPrincipals.
325 *aResult = (aOther == this);
326 return NS_OK;
329 NS_IMETHODIMP
330 nsNullPrincipal::CheckMayLoad(nsIURI* aURI, PRBool aReport)
332 if (aReport) {
333 nsScriptSecurityManager::ReportError(
334 nsnull, NS_LITERAL_STRING("CheckSameOriginError"), mURI, aURI);
337 return NS_ERROR_DOM_BAD_URI;
340 NS_IMETHODIMP
341 nsNullPrincipal::GetSubjectName(nsACString& aName)
343 return NS_ERROR_NOT_AVAILABLE;
346 NS_IMETHODIMP
347 nsNullPrincipal::GetCertificate(nsISupports** aCertificate)
349 *aCertificate = nsnull;
350 return NS_OK;
354 * nsISerializable implementation
356 NS_IMETHODIMP
357 nsNullPrincipal::Read(nsIObjectInputStream* aStream)
359 // no-op: CID is sufficient to create a useful nsNullPrincipal, since the URI
360 // is not really relevant.
361 return NS_OK;
364 NS_IMETHODIMP
365 nsNullPrincipal::Write(nsIObjectOutputStream* aStream)
367 // no-op: CID is sufficient to create a useful nsNullPrincipal, since the URI
368 // is not really relevant.
369 return NS_OK;