Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / caps / src / nsSystemPrincipal.cpp
blobfb48d4feaa0dbb303b3c323daf2f972e531f9b80
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"
52 NS_IMPL_CLASSINFO(nsSystemPrincipal, NULL,
53 nsIClassInfo::SINGLETON | nsIClassInfo::MAIN_THREAD_ONLY,
54 NS_SYSTEMPRINCIPAL_CID)
55 NS_IMPL_QUERY_INTERFACE2_CI(nsSystemPrincipal,
56 nsIPrincipal,
57 nsISerializable)
58 NS_IMPL_CI_INTERFACE_GETTER2(nsSystemPrincipal,
59 nsIPrincipal,
60 nsISerializable)
62 NS_IMETHODIMP_(nsrefcnt)
63 nsSystemPrincipal::AddRef()
65 NS_PRECONDITION(PRInt32(mJSPrincipals.refcount) >= 0, "illegal refcnt");
66 nsrefcnt count = PR_AtomicIncrement((PRInt32 *)&mJSPrincipals.refcount);
67 NS_LOG_ADDREF(this, count, "nsSystemPrincipal", sizeof(*this));
68 return count;
71 NS_IMETHODIMP_(nsrefcnt)
72 nsSystemPrincipal::Release()
74 NS_PRECONDITION(0 != mJSPrincipals.refcount, "dup release");
75 nsrefcnt count = PR_AtomicDecrement((PRInt32 *)&mJSPrincipals.refcount);
76 NS_LOG_RELEASE(this, count, "nsSystemPrincipal");
77 if (count == 0) {
78 delete this;
81 return count;
85 ///////////////////////////////////////
86 // Methods implementing nsIPrincipal //
87 ///////////////////////////////////////
89 NS_IMETHODIMP
90 nsSystemPrincipal::GetPreferences(char** aPrefName, char** aID,
91 char** aSubjectName,
92 char** aGrantedList, char** aDeniedList,
93 PRBool* aIsTrusted)
95 // The system principal should never be streamed out
96 *aPrefName = nsnull;
97 *aID = nsnull;
98 *aSubjectName = nsnull;
99 *aGrantedList = nsnull;
100 *aDeniedList = nsnull;
101 *aIsTrusted = PR_FALSE;
103 return NS_ERROR_FAILURE;
106 NS_IMETHODIMP
107 nsSystemPrincipal::Equals(nsIPrincipal *other, PRBool *result)
109 *result = (other == this);
110 return NS_OK;
113 NS_IMETHODIMP
114 nsSystemPrincipal::Subsumes(nsIPrincipal *other, PRBool *result)
116 *result = PR_TRUE;
117 return NS_OK;
120 NS_IMETHODIMP
121 nsSystemPrincipal::CheckMayLoad(nsIURI* uri, PRBool aReport)
123 return NS_OK;
126 NS_IMETHODIMP
127 nsSystemPrincipal::GetHashValue(PRUint32 *result)
129 *result = NS_PTR_TO_INT32(this);
130 return NS_OK;
133 NS_IMETHODIMP
134 nsSystemPrincipal::CanEnableCapability(const char *capability,
135 PRInt16 *result)
137 // System principal can enable all capabilities.
138 *result = nsIPrincipal::ENABLE_GRANTED;
139 return NS_OK;
142 NS_IMETHODIMP
143 nsSystemPrincipal::SetCanEnableCapability(const char *capability,
144 PRInt16 canEnable)
146 return NS_ERROR_FAILURE;
150 NS_IMETHODIMP
151 nsSystemPrincipal::IsCapabilityEnabled(const char *capability,
152 void *annotation,
153 PRBool *result)
155 *result = PR_TRUE;
156 return NS_OK;
159 NS_IMETHODIMP
160 nsSystemPrincipal::EnableCapability(const char *capability, void **annotation)
162 *annotation = nsnull;
163 return NS_OK;
166 NS_IMETHODIMP
167 nsSystemPrincipal::RevertCapability(const char *capability, void **annotation)
169 *annotation = nsnull;
170 return NS_OK;
173 NS_IMETHODIMP
174 nsSystemPrincipal::DisableCapability(const char *capability, void **annotation)
176 // Can't disable the capabilities of the system principal.
177 // XXX might be handy to be able to do so!
178 *annotation = nsnull;
179 return NS_ERROR_FAILURE;
182 NS_IMETHODIMP
183 nsSystemPrincipal::GetURI(nsIURI** aURI)
185 *aURI = nsnull;
186 return NS_OK;
189 NS_IMETHODIMP
190 nsSystemPrincipal::GetOrigin(char** aOrigin)
192 *aOrigin = ToNewCString(NS_LITERAL_CSTRING("[System Principal]"));
193 return *aOrigin ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
196 NS_IMETHODIMP
197 nsSystemPrincipal::GetFingerprint(nsACString& aID)
199 return NS_ERROR_NOT_AVAILABLE;
202 NS_IMETHODIMP
203 nsSystemPrincipal::GetPrettyName(nsACString& aName)
205 return NS_ERROR_NOT_AVAILABLE;
208 NS_IMETHODIMP
209 nsSystemPrincipal::GetSubjectName(nsACString& aName)
211 return NS_ERROR_NOT_AVAILABLE;
214 NS_IMETHODIMP
215 nsSystemPrincipal::GetCertificate(nsISupports** aCertificate)
217 *aCertificate = nsnull;
218 return NS_OK;
221 NS_IMETHODIMP
222 nsSystemPrincipal::GetHasCertificate(PRBool* aResult)
224 *aResult = PR_FALSE;
225 return NS_OK;
228 NS_IMETHODIMP
229 nsSystemPrincipal::GetCsp(nsIContentSecurityPolicy** aCsp)
231 *aCsp = nsnull;
232 return NS_OK;
235 NS_IMETHODIMP
236 nsSystemPrincipal::SetCsp(nsIContentSecurityPolicy* aCsp)
238 // CSP on a null principal makes no sense
239 return NS_OK;
242 NS_IMETHODIMP
243 nsSystemPrincipal::GetDomain(nsIURI** aDomain)
245 *aDomain = nsnull;
246 return NS_OK;
249 NS_IMETHODIMP
250 nsSystemPrincipal::SetDomain(nsIURI* aDomain)
252 return NS_OK;
255 NS_IMETHODIMP
256 nsSystemPrincipal::GetSecurityPolicy(void** aSecurityPolicy)
258 *aSecurityPolicy = nsnull;
259 return NS_OK;
262 NS_IMETHODIMP
263 nsSystemPrincipal::SetSecurityPolicy(void* aSecurityPolicy)
265 return NS_OK;
268 NS_IMETHODIMP
269 nsSystemPrincipal::GetJSPrincipals(JSContext *cx, JSPrincipals **jsprin)
271 NS_PRECONDITION(mJSPrincipals.nsIPrincipalPtr, "mJSPrincipals is uninitialized!");
273 JSPRINCIPALS_HOLD(cx, &mJSPrincipals);
274 *jsprin = &mJSPrincipals;
275 return NS_OK;
279 //////////////////////////////////////////
280 // Methods implementing nsISerializable //
281 //////////////////////////////////////////
283 NS_IMETHODIMP
284 nsSystemPrincipal::Read(nsIObjectInputStream* aStream)
286 // no-op: CID is sufficient to identify the mSystemPrincipal singleton
287 return NS_OK;
290 NS_IMETHODIMP
291 nsSystemPrincipal::Write(nsIObjectOutputStream* aStream)
293 // no-op: CID is sufficient to identify the mSystemPrincipal singleton
294 return NS_OK;
297 /////////////////////////////////////////////
298 // Constructor, Destructor, initialization //
299 /////////////////////////////////////////////
301 nsSystemPrincipal::nsSystemPrincipal()
305 #define SYSTEM_PRINCIPAL_SPEC "[System Principal]"
307 nsresult
308 nsSystemPrincipal::Init()
310 // Use an nsCString so we only do the allocation once here and then
311 // share with nsJSPrincipals
312 nsCString str(SYSTEM_PRINCIPAL_SPEC);
313 if (!str.EqualsLiteral(SYSTEM_PRINCIPAL_SPEC)) {
314 NS_WARNING("Out of memory initializing system principal");
315 return NS_ERROR_OUT_OF_MEMORY;
318 return mJSPrincipals.Init(this, str);
321 nsSystemPrincipal::~nsSystemPrincipal(void)