Bug 559408: Turn arena pool macros into methods. (r=gal)
[mozilla-central.git] / caps / src / nsSystemPrincipal.cpp
blob2abb7d17ef0da985e9178e4d816c5393c75eb492
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1999-2000
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 /* The privileged system principal. */
40 #include "nscore.h"
41 #include "nsSystemPrincipal.h"
42 #include "nsIComponentManager.h"
43 #include "nsIServiceManager.h"
44 #include "nsIURL.h"
45 #include "nsCOMPtr.h"
46 #include "nsXPIDLString.h"
47 #include "nsReadableUtils.h"
48 #include "nsCRT.h"
49 #include "nsString.h"
50 #include "nsIClassInfoImpl.h"
53 NS_IMPL_QUERY_INTERFACE2_CI(nsSystemPrincipal,
54 nsIPrincipal,
55 nsISerializable)
56 NS_IMPL_CI_INTERFACE_GETTER2(nsSystemPrincipal,
57 nsIPrincipal,
58 nsISerializable)
60 NS_IMETHODIMP_(nsrefcnt)
61 nsSystemPrincipal::AddRef()
63 NS_PRECONDITION(PRInt32(mJSPrincipals.refcount) >= 0, "illegal refcnt");
64 nsrefcnt count = PR_AtomicIncrement((PRInt32 *)&mJSPrincipals.refcount);
65 NS_LOG_ADDREF(this, count, "nsSystemPrincipal", sizeof(*this));
66 return count;
69 NS_IMETHODIMP_(nsrefcnt)
70 nsSystemPrincipal::Release()
72 NS_PRECONDITION(0 != mJSPrincipals.refcount, "dup release");
73 nsrefcnt count = PR_AtomicDecrement((PRInt32 *)&mJSPrincipals.refcount);
74 NS_LOG_RELEASE(this, count, "nsSystemPrincipal");
75 if (count == 0) {
76 NS_DELETEXPCOM(this);
79 return count;
83 ///////////////////////////////////////
84 // Methods implementing nsIPrincipal //
85 ///////////////////////////////////////
87 NS_IMETHODIMP
88 nsSystemPrincipal::GetPreferences(char** aPrefName, char** aID,
89 char** aSubjectName,
90 char** aGrantedList, char** aDeniedList,
91 PRBool* aIsTrusted)
93 // The system principal should never be streamed out
94 *aPrefName = nsnull;
95 *aID = nsnull;
96 *aSubjectName = nsnull;
97 *aGrantedList = nsnull;
98 *aDeniedList = nsnull;
99 *aIsTrusted = PR_FALSE;
101 return NS_ERROR_FAILURE;
104 NS_IMETHODIMP
105 nsSystemPrincipal::Equals(nsIPrincipal *other, PRBool *result)
107 *result = (other == this);
108 return NS_OK;
111 NS_IMETHODIMP
112 nsSystemPrincipal::Subsumes(nsIPrincipal *other, PRBool *result)
114 *result = PR_TRUE;
115 return NS_OK;
118 NS_IMETHODIMP
119 nsSystemPrincipal::CheckMayLoad(nsIURI* uri, PRBool aReport)
121 return NS_OK;
124 NS_IMETHODIMP
125 nsSystemPrincipal::GetHashValue(PRUint32 *result)
127 *result = NS_PTR_TO_INT32(this);
128 return NS_OK;
131 NS_IMETHODIMP
132 nsSystemPrincipal::CanEnableCapability(const char *capability,
133 PRInt16 *result)
135 // System principal can enable all capabilities.
136 *result = nsIPrincipal::ENABLE_GRANTED;
137 return NS_OK;
140 NS_IMETHODIMP
141 nsSystemPrincipal::SetCanEnableCapability(const char *capability,
142 PRInt16 canEnable)
144 return NS_ERROR_FAILURE;
148 NS_IMETHODIMP
149 nsSystemPrincipal::IsCapabilityEnabled(const char *capability,
150 void *annotation,
151 PRBool *result)
153 *result = PR_TRUE;
154 return NS_OK;
157 NS_IMETHODIMP
158 nsSystemPrincipal::EnableCapability(const char *capability, void **annotation)
160 *annotation = nsnull;
161 return NS_OK;
164 NS_IMETHODIMP
165 nsSystemPrincipal::RevertCapability(const char *capability, void **annotation)
167 *annotation = nsnull;
168 return NS_OK;
171 NS_IMETHODIMP
172 nsSystemPrincipal::DisableCapability(const char *capability, void **annotation)
174 // Can't disable the capabilities of the system principal.
175 // XXX might be handy to be able to do so!
176 *annotation = nsnull;
177 return NS_ERROR_FAILURE;
180 NS_IMETHODIMP
181 nsSystemPrincipal::GetURI(nsIURI** aURI)
183 *aURI = nsnull;
184 return NS_OK;
187 NS_IMETHODIMP
188 nsSystemPrincipal::GetOrigin(char** aOrigin)
190 *aOrigin = ToNewCString(NS_LITERAL_CSTRING("[System]"));
191 return *aOrigin ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
194 NS_IMETHODIMP
195 nsSystemPrincipal::GetFingerprint(nsACString& aID)
197 return NS_ERROR_NOT_AVAILABLE;
200 NS_IMETHODIMP
201 nsSystemPrincipal::GetPrettyName(nsACString& aName)
203 return NS_ERROR_NOT_AVAILABLE;
206 NS_IMETHODIMP
207 nsSystemPrincipal::GetSubjectName(nsACString& aName)
209 return NS_ERROR_NOT_AVAILABLE;
212 NS_IMETHODIMP
213 nsSystemPrincipal::GetCertificate(nsISupports** aCertificate)
215 *aCertificate = nsnull;
216 return NS_OK;
219 NS_IMETHODIMP
220 nsSystemPrincipal::GetHasCertificate(PRBool* aResult)
222 *aResult = PR_FALSE;
223 return NS_OK;
226 NS_IMETHODIMP
227 nsSystemPrincipal::GetCsp(nsIContentSecurityPolicy** aCsp)
229 *aCsp = nsnull;
230 return NS_OK;
233 NS_IMETHODIMP
234 nsSystemPrincipal::SetCsp(nsIContentSecurityPolicy* aCsp)
236 // CSP on a null principal makes no sense
237 return NS_OK;
240 NS_IMETHODIMP
241 nsSystemPrincipal::GetDomain(nsIURI** aDomain)
243 *aDomain = nsnull;
244 return NS_OK;
247 NS_IMETHODIMP
248 nsSystemPrincipal::SetDomain(nsIURI* aDomain)
250 return NS_OK;
253 NS_IMETHODIMP
254 nsSystemPrincipal::GetSecurityPolicy(void** aSecurityPolicy)
256 *aSecurityPolicy = nsnull;
257 return NS_OK;
260 NS_IMETHODIMP
261 nsSystemPrincipal::SetSecurityPolicy(void* aSecurityPolicy)
263 return NS_OK;
266 NS_IMETHODIMP
267 nsSystemPrincipal::GetJSPrincipals(JSContext *cx, JSPrincipals **jsprin)
269 NS_PRECONDITION(mJSPrincipals.nsIPrincipalPtr, "mJSPrincipals is uninitialized!");
271 JSPRINCIPALS_HOLD(cx, &mJSPrincipals);
272 *jsprin = &mJSPrincipals;
273 return NS_OK;
277 //////////////////////////////////////////
278 // Methods implementing nsISerializable //
279 //////////////////////////////////////////
281 NS_IMETHODIMP
282 nsSystemPrincipal::Read(nsIObjectInputStream* aStream)
284 // no-op: CID is sufficient to identify the mSystemPrincipal singleton
285 return NS_OK;
288 NS_IMETHODIMP
289 nsSystemPrincipal::Write(nsIObjectOutputStream* aStream)
291 // no-op: CID is sufficient to identify the mSystemPrincipal singleton
292 return NS_OK;
295 /////////////////////////////////////////////
296 // Constructor, Destructor, initialization //
297 /////////////////////////////////////////////
299 nsSystemPrincipal::nsSystemPrincipal()
303 #define SYSTEM_PRINCIPAL_SPEC "[System Principal]"
305 nsresult
306 nsSystemPrincipal::Init()
308 // Use an nsCString so we only do the allocation once here and then
309 // share with nsJSPrincipals
310 nsCString str(SYSTEM_PRINCIPAL_SPEC);
311 if (!str.EqualsLiteral(SYSTEM_PRINCIPAL_SPEC)) {
312 NS_WARNING("Out of memory initializing system principal");
313 return NS_ERROR_OUT_OF_MEMORY;
316 return mJSPrincipals.Init(this, str);
319 nsSystemPrincipal::~nsSystemPrincipal(void)