Back to index

lightning-sunbird  0.9+nobinonly
nsSystemPrincipal.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /* The privileged system principal. */
00039 
00040 #include "nscore.h"
00041 #include "nsSystemPrincipal.h"
00042 #include "nsIComponentManager.h"
00043 #include "nsIServiceManager.h"
00044 #include "nsIURL.h"
00045 #include "nsCOMPtr.h"
00046 #include "nsXPIDLString.h"
00047 #include "nsReadableUtils.h"
00048 #include "nsCRT.h"
00049 #include "nsString.h"
00050 
00051 
00052 NS_IMPL_QUERY_INTERFACE2_CI(nsSystemPrincipal,
00053                             nsIPrincipal,
00054                             nsISerializable)
00055 NS_IMPL_CI_INTERFACE_GETTER2(nsSystemPrincipal,
00056                              nsIPrincipal,
00057                              nsISerializable)
00058 
00059 NS_IMETHODIMP_(nsrefcnt) 
00060 nsSystemPrincipal::AddRef()
00061 {
00062   NS_PRECONDITION(PRInt32(mJSPrincipals.refcount) >= 0, "illegal refcnt");
00063   nsrefcnt count = PR_AtomicIncrement((PRInt32 *)&mJSPrincipals.refcount);
00064   NS_LOG_ADDREF(this, count, "nsSystemPrincipal", sizeof(*this));
00065   return count;
00066 }
00067 
00068 NS_IMETHODIMP_(nsrefcnt)
00069 nsSystemPrincipal::Release()
00070 {
00071   NS_PRECONDITION(0 != mJSPrincipals.refcount, "dup release");
00072   nsrefcnt count = PR_AtomicDecrement((PRInt32 *)&mJSPrincipals.refcount);
00073   NS_LOG_RELEASE(this, count, "nsSystemPrincipal");
00074   if (count == 0) {
00075     NS_DELETEXPCOM(this);
00076   }
00077 
00078   return count;
00079 }
00080 
00081 
00083 // Methods implementing nsIPrincipal //
00085 
00086 NS_IMETHODIMP
00087 nsSystemPrincipal::GetPreferences(char** aPrefName, char** aID,
00088                                   char** aSubjectName,
00089                                   char** aGrantedList, char** aDeniedList)
00090 {
00091     // The system principal should never be streamed out
00092     *aPrefName = nsnull;
00093     *aID = nsnull;
00094     *aSubjectName = nsnull;
00095     *aGrantedList = nsnull;
00096     *aDeniedList = nsnull;
00097 
00098     return NS_ERROR_FAILURE; 
00099 }
00100 
00101 NS_IMETHODIMP
00102 nsSystemPrincipal::Equals(nsIPrincipal *other, PRBool *result)
00103 {
00104     *result = (other == this);
00105     return NS_OK;
00106 }
00107 
00108 NS_IMETHODIMP
00109 nsSystemPrincipal::Subsumes(nsIPrincipal *other, PRBool *result)
00110 {
00111     *result = PR_TRUE;
00112     return NS_OK;
00113 }
00114 
00115 NS_IMETHODIMP
00116 nsSystemPrincipal::GetHashValue(PRUint32 *result)
00117 {
00118     *result = NS_PTR_TO_INT32(this);
00119     return NS_OK;
00120 }
00121 
00122 NS_IMETHODIMP 
00123 nsSystemPrincipal::CanEnableCapability(const char *capability, 
00124                                        PRInt16 *result)
00125 {
00126     // System principal can enable all capabilities.
00127     *result = nsIPrincipal::ENABLE_GRANTED;
00128     return NS_OK;
00129 }
00130 
00131 NS_IMETHODIMP 
00132 nsSystemPrincipal::SetCanEnableCapability(const char *capability, 
00133                                           PRInt16 canEnable)
00134 {
00135     return NS_ERROR_FAILURE;
00136 }
00137 
00138 
00139 NS_IMETHODIMP 
00140 nsSystemPrincipal::IsCapabilityEnabled(const char *capability, 
00141                                        void *annotation, 
00142                                        PRBool *result)
00143 {
00144     *result = PR_TRUE;
00145     return NS_OK;
00146 }
00147 
00148 NS_IMETHODIMP 
00149 nsSystemPrincipal::EnableCapability(const char *capability, void **annotation)
00150 {
00151     *annotation = nsnull;
00152     return NS_OK;
00153 }
00154 
00155 NS_IMETHODIMP 
00156 nsSystemPrincipal::RevertCapability(const char *capability, void **annotation)
00157 {
00158     *annotation = nsnull;
00159     return NS_OK;
00160 }
00161 
00162 NS_IMETHODIMP 
00163 nsSystemPrincipal::DisableCapability(const char *capability, void **annotation) 
00164 {
00165     // Can't disable the capabilities of the system principal.
00166     // XXX might be handy to be able to do so!
00167     *annotation = nsnull;
00168     return NS_ERROR_FAILURE;
00169 }
00170 
00171 NS_IMETHODIMP 
00172 nsSystemPrincipal::GetURI(nsIURI** aURI)
00173 {
00174     *aURI = nsnull;
00175     return NS_OK;
00176 }
00177 
00178 NS_IMETHODIMP 
00179 nsSystemPrincipal::GetOrigin(char** aOrigin)
00180 {
00181     *aOrigin = ToNewCString(NS_LITERAL_CSTRING("[System]"));
00182     return *aOrigin ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00183 }
00184 
00185 NS_IMETHODIMP 
00186 nsSystemPrincipal::GetFingerprint(nsACString& aID)
00187 {
00188     return NS_ERROR_NOT_AVAILABLE;
00189 }
00190 
00191 NS_IMETHODIMP 
00192 nsSystemPrincipal::GetPrettyName(nsACString& aName)
00193 {
00194     return NS_ERROR_NOT_AVAILABLE;
00195 }
00196 
00197 NS_IMETHODIMP 
00198 nsSystemPrincipal::GetSubjectName(nsACString& aName)
00199 {
00200     return NS_ERROR_NOT_AVAILABLE;
00201 }
00202 
00203 NS_IMETHODIMP
00204 nsSystemPrincipal::GetCertificate(nsISupports** aCertificate)
00205 {
00206     *aCertificate = nsnull;
00207     return NS_OK;
00208 }
00209 
00210 NS_IMETHODIMP 
00211 nsSystemPrincipal::GetHasCertificate(PRBool* aResult)
00212 {
00213     *aResult = PR_FALSE;
00214     return NS_OK;
00215 }
00216 
00217 NS_IMETHODIMP
00218 nsSystemPrincipal::GetDomain(nsIURI** aDomain)
00219 {
00220     *aDomain = nsnull;
00221     return NS_OK;
00222 }
00223 
00224 NS_IMETHODIMP
00225 nsSystemPrincipal::SetDomain(nsIURI* aDomain)
00226 {
00227   return NS_OK;
00228 }
00229 
00230 NS_IMETHODIMP
00231 nsSystemPrincipal::GetSecurityPolicy(void** aSecurityPolicy)
00232 {
00233     *aSecurityPolicy = nsnull;
00234     return NS_OK;
00235 }
00236 
00237 NS_IMETHODIMP
00238 nsSystemPrincipal::SetSecurityPolicy(void* aSecurityPolicy)
00239 {
00240     return NS_OK;
00241 }
00242 
00243 NS_IMETHODIMP
00244 nsSystemPrincipal::GetJSPrincipals(JSContext *cx, JSPrincipals **jsprin)
00245 {
00246     NS_PRECONDITION(mJSPrincipals.nsIPrincipalPtr, "mJSPrincipals is uninitalized!");
00247 
00248     JSPRINCIPALS_HOLD(cx, &mJSPrincipals);
00249     *jsprin = &mJSPrincipals;
00250     return NS_OK;
00251 }
00252 
00253 
00255 // Methods implementing nsISerializable //
00257 
00258 NS_IMETHODIMP
00259 nsSystemPrincipal::Read(nsIObjectInputStream* aStream)
00260 {
00261     // no-op: CID is sufficient to identify the mSystemPrincipal singleton
00262     return NS_OK;
00263 }
00264 
00265 NS_IMETHODIMP
00266 nsSystemPrincipal::Write(nsIObjectOutputStream* aStream)
00267 {
00268     // no-op: CID is sufficient to identify the mSystemPrincipal singleton
00269     return NS_OK;
00270 }
00271 
00273 // Constructor, Destructor, initialization //
00275 
00276 nsSystemPrincipal::nsSystemPrincipal()
00277 {
00278 }
00279 
00280 nsresult
00281 nsSystemPrincipal::Init()
00282 {
00283     return mJSPrincipals.Init(this, "[System Principal]"); 
00284 }
00285 
00286 nsSystemPrincipal::~nsSystemPrincipal(void)
00287 {
00288 }