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
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.
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. */
41 #include "nsSystemPrincipal.h"
42 #include "nsIComponentManager.h"
43 #include "nsIServiceManager.h"
46 #include "nsXPIDLString.h"
47 #include "nsReadableUtils.h"
50 #include "nsIClassInfoImpl.h"
53 NS_IMPL_QUERY_INTERFACE2_CI(nsSystemPrincipal
,
56 NS_IMPL_CI_INTERFACE_GETTER2(nsSystemPrincipal
,
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));
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");
83 ///////////////////////////////////////
84 // Methods implementing nsIPrincipal //
85 ///////////////////////////////////////
88 nsSystemPrincipal::GetPreferences(char** aPrefName
, char** aID
,
90 char** aGrantedList
, char** aDeniedList
,
93 // The system principal should never be streamed out
96 *aSubjectName
= nsnull
;
97 *aGrantedList
= nsnull
;
98 *aDeniedList
= nsnull
;
99 *aIsTrusted
= PR_FALSE
;
101 return NS_ERROR_FAILURE
;
105 nsSystemPrincipal::Equals(nsIPrincipal
*other
, PRBool
*result
)
107 *result
= (other
== this);
112 nsSystemPrincipal::Subsumes(nsIPrincipal
*other
, PRBool
*result
)
119 nsSystemPrincipal::CheckMayLoad(nsIURI
* uri
, PRBool aReport
)
125 nsSystemPrincipal::GetHashValue(PRUint32
*result
)
127 *result
= NS_PTR_TO_INT32(this);
132 nsSystemPrincipal::CanEnableCapability(const char *capability
,
135 // System principal can enable all capabilities.
136 *result
= nsIPrincipal::ENABLE_GRANTED
;
141 nsSystemPrincipal::SetCanEnableCapability(const char *capability
,
144 return NS_ERROR_FAILURE
;
149 nsSystemPrincipal::IsCapabilityEnabled(const char *capability
,
158 nsSystemPrincipal::EnableCapability(const char *capability
, void **annotation
)
160 *annotation
= nsnull
;
165 nsSystemPrincipal::RevertCapability(const char *capability
, void **annotation
)
167 *annotation
= nsnull
;
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
;
181 nsSystemPrincipal::GetURI(nsIURI
** aURI
)
188 nsSystemPrincipal::GetOrigin(char** aOrigin
)
190 *aOrigin
= ToNewCString(NS_LITERAL_CSTRING("[System]"));
191 return *aOrigin
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
195 nsSystemPrincipal::GetFingerprint(nsACString
& aID
)
197 return NS_ERROR_NOT_AVAILABLE
;
201 nsSystemPrincipal::GetPrettyName(nsACString
& aName
)
203 return NS_ERROR_NOT_AVAILABLE
;
207 nsSystemPrincipal::GetSubjectName(nsACString
& aName
)
209 return NS_ERROR_NOT_AVAILABLE
;
213 nsSystemPrincipal::GetCertificate(nsISupports
** aCertificate
)
215 *aCertificate
= nsnull
;
220 nsSystemPrincipal::GetHasCertificate(PRBool
* aResult
)
227 nsSystemPrincipal::GetDomain(nsIURI
** aDomain
)
234 nsSystemPrincipal::SetDomain(nsIURI
* aDomain
)
240 nsSystemPrincipal::GetSecurityPolicy(void** aSecurityPolicy
)
242 *aSecurityPolicy
= nsnull
;
247 nsSystemPrincipal::SetSecurityPolicy(void* aSecurityPolicy
)
253 nsSystemPrincipal::GetJSPrincipals(JSContext
*cx
, JSPrincipals
**jsprin
)
255 NS_PRECONDITION(mJSPrincipals
.nsIPrincipalPtr
, "mJSPrincipals is uninitialized!");
257 JSPRINCIPALS_HOLD(cx
, &mJSPrincipals
);
258 *jsprin
= &mJSPrincipals
;
263 //////////////////////////////////////////
264 // Methods implementing nsISerializable //
265 //////////////////////////////////////////
268 nsSystemPrincipal::Read(nsIObjectInputStream
* aStream
)
270 // no-op: CID is sufficient to identify the mSystemPrincipal singleton
275 nsSystemPrincipal::Write(nsIObjectOutputStream
* aStream
)
277 // no-op: CID is sufficient to identify the mSystemPrincipal singleton
281 /////////////////////////////////////////////
282 // Constructor, Destructor, initialization //
283 /////////////////////////////////////////////
285 nsSystemPrincipal::nsSystemPrincipal()
289 #define SYSTEM_PRINCIPAL_SPEC "[System Principal]"
292 nsSystemPrincipal::Init()
294 // Use an nsCString so we only do the allocation once here and then
295 // share with nsJSPrincipals
296 nsCString
str(SYSTEM_PRINCIPAL_SPEC
);
297 if (!str
.EqualsLiteral(SYSTEM_PRINCIPAL_SPEC
)) {
298 NS_WARNING("Out of memory initializing system principal");
299 return NS_ERROR_OUT_OF_MEMORY
;
302 return mJSPrincipals
.Init(this, str
);
305 nsSystemPrincipal::~nsSystemPrincipal(void)