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
4 : *
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/
9 : *
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.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : *
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.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : /* The privileged system principal. */
39 :
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"
51 :
52 : NS_IMPL_CLASSINFO(nsSystemPrincipal, NULL,
53 : nsIClassInfo::SINGLETON | nsIClassInfo::MAIN_THREAD_ONLY,
54 : NS_SYSTEMPRINCIPAL_CID)
55 1343581 : NS_IMPL_QUERY_INTERFACE2_CI(nsSystemPrincipal,
56 : nsIPrincipal,
57 14950 : nsISerializable)
58 98 : NS_IMPL_CI_INTERFACE_GETTER2(nsSystemPrincipal,
59 : nsIPrincipal,
60 98 : nsISerializable)
61 :
62 : NS_IMETHODIMP_(nsrefcnt)
63 2611596 : nsSystemPrincipal::AddRef()
64 : {
65 2611596 : NS_PRECONDITION(PRInt32(refcount) >= 0, "illegal refcnt");
66 2611596 : nsrefcnt count = PR_ATOMIC_INCREMENT(&refcount);
67 2611596 : NS_LOG_ADDREF(this, count, "nsSystemPrincipal", sizeof(*this));
68 2611596 : return count;
69 : }
70 :
71 : NS_IMETHODIMP_(nsrefcnt)
72 2611575 : nsSystemPrincipal::Release()
73 : {
74 2611575 : NS_PRECONDITION(0 != refcount, "dup release");
75 2611575 : nsrefcnt count = PR_ATOMIC_DECREMENT(&refcount);
76 2611575 : NS_LOG_RELEASE(this, count, "nsSystemPrincipal");
77 2611575 : if (count == 0) {
78 1386 : delete this;
79 : }
80 :
81 2611575 : return count;
82 : }
83 :
84 : static const char SYSTEM_PRINCIPAL_SPEC[] = "[System Principal]";
85 :
86 : void
87 6 : nsSystemPrincipal::GetScriptLocation(nsACString &aStr)
88 : {
89 6 : aStr.Assign(SYSTEM_PRINCIPAL_SPEC);
90 6 : }
91 :
92 : #ifdef DEBUG
93 0 : void nsSystemPrincipal::dumpImpl()
94 : {
95 0 : fprintf(stderr, "nsSystemPrincipal (%p)\n", this);
96 0 : }
97 : #endif
98 :
99 :
100 : ///////////////////////////////////////
101 : // Methods implementing nsIPrincipal //
102 : ///////////////////////////////////////
103 :
104 : NS_IMETHODIMP
105 0 : nsSystemPrincipal::GetPreferences(char** aPrefName, char** aID,
106 : char** aSubjectName,
107 : char** aGrantedList, char** aDeniedList,
108 : bool* aIsTrusted)
109 : {
110 : // The system principal should never be streamed out
111 0 : *aPrefName = nsnull;
112 0 : *aID = nsnull;
113 0 : *aSubjectName = nsnull;
114 0 : *aGrantedList = nsnull;
115 0 : *aDeniedList = nsnull;
116 0 : *aIsTrusted = false;
117 :
118 0 : return NS_ERROR_FAILURE;
119 : }
120 :
121 : NS_IMETHODIMP
122 1906450 : nsSystemPrincipal::Equals(nsIPrincipal *other, bool *result)
123 : {
124 1906450 : *result = (other == this);
125 1906450 : return NS_OK;
126 : }
127 :
128 : NS_IMETHODIMP
129 0 : nsSystemPrincipal::EqualsIgnoringDomain(nsIPrincipal *other, bool *result)
130 : {
131 0 : return Equals(other, result);
132 : }
133 :
134 : NS_IMETHODIMP
135 326 : nsSystemPrincipal::Subsumes(nsIPrincipal *other, bool *result)
136 : {
137 326 : *result = true;
138 326 : return NS_OK;
139 : }
140 :
141 : NS_IMETHODIMP
142 172 : nsSystemPrincipal::SubsumesIgnoringDomain(nsIPrincipal *other, bool *result)
143 : {
144 172 : *result = true;
145 172 : return NS_OK;
146 : }
147 :
148 : NS_IMETHODIMP
149 0 : nsSystemPrincipal::CheckMayLoad(nsIURI* uri, bool aReport)
150 : {
151 0 : return NS_OK;
152 : }
153 :
154 : NS_IMETHODIMP
155 0 : nsSystemPrincipal::GetHashValue(PRUint32 *result)
156 : {
157 0 : *result = NS_PTR_TO_INT32(this);
158 0 : return NS_OK;
159 : }
160 :
161 : NS_IMETHODIMP
162 410104 : nsSystemPrincipal::CanEnableCapability(const char *capability,
163 : PRInt16 *result)
164 : {
165 : // System principal can enable all capabilities.
166 410104 : *result = nsIPrincipal::ENABLE_GRANTED;
167 410104 : return NS_OK;
168 : }
169 :
170 : NS_IMETHODIMP
171 0 : nsSystemPrincipal::SetCanEnableCapability(const char *capability,
172 : PRInt16 canEnable)
173 : {
174 0 : return NS_ERROR_FAILURE;
175 : }
176 :
177 :
178 : NS_IMETHODIMP
179 410104 : nsSystemPrincipal::IsCapabilityEnabled(const char *capability,
180 : void *annotation,
181 : bool *result)
182 : {
183 410104 : *result = true;
184 410104 : return NS_OK;
185 : }
186 :
187 : NS_IMETHODIMP
188 0 : nsSystemPrincipal::EnableCapability(const char *capability, void **annotation)
189 : {
190 0 : *annotation = nsnull;
191 0 : return NS_OK;
192 : }
193 :
194 : NS_IMETHODIMP
195 0 : nsSystemPrincipal::RevertCapability(const char *capability, void **annotation)
196 : {
197 0 : *annotation = nsnull;
198 0 : return NS_OK;
199 : }
200 :
201 : NS_IMETHODIMP
202 0 : nsSystemPrincipal::DisableCapability(const char *capability, void **annotation)
203 : {
204 : // Can't disable the capabilities of the system principal.
205 : // XXX might be handy to be able to do so!
206 0 : *annotation = nsnull;
207 0 : return NS_ERROR_FAILURE;
208 : }
209 :
210 : NS_IMETHODIMP
211 16513 : nsSystemPrincipal::GetURI(nsIURI** aURI)
212 : {
213 16513 : *aURI = nsnull;
214 16513 : return NS_OK;
215 : }
216 :
217 : NS_IMETHODIMP
218 0 : nsSystemPrincipal::GetOrigin(char** aOrigin)
219 : {
220 0 : *aOrigin = ToNewCString(NS_LITERAL_CSTRING(SYSTEM_PRINCIPAL_SPEC));
221 0 : return *aOrigin ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
222 : }
223 :
224 : NS_IMETHODIMP
225 0 : nsSystemPrincipal::GetFingerprint(nsACString& aID)
226 : {
227 0 : return NS_ERROR_NOT_AVAILABLE;
228 : }
229 :
230 : NS_IMETHODIMP
231 0 : nsSystemPrincipal::GetPrettyName(nsACString& aName)
232 : {
233 0 : return NS_ERROR_NOT_AVAILABLE;
234 : }
235 :
236 : NS_IMETHODIMP
237 0 : nsSystemPrincipal::GetSubjectName(nsACString& aName)
238 : {
239 0 : return NS_ERROR_NOT_AVAILABLE;
240 : }
241 :
242 : NS_IMETHODIMP
243 0 : nsSystemPrincipal::GetCertificate(nsISupports** aCertificate)
244 : {
245 0 : *aCertificate = nsnull;
246 0 : return NS_OK;
247 : }
248 :
249 : NS_IMETHODIMP
250 0 : nsSystemPrincipal::GetHasCertificate(bool* aResult)
251 : {
252 0 : *aResult = false;
253 0 : return NS_OK;
254 : }
255 :
256 : NS_IMETHODIMP
257 652 : nsSystemPrincipal::GetCsp(nsIContentSecurityPolicy** aCsp)
258 : {
259 652 : *aCsp = nsnull;
260 652 : return NS_OK;
261 : }
262 :
263 : NS_IMETHODIMP
264 0 : nsSystemPrincipal::SetCsp(nsIContentSecurityPolicy* aCsp)
265 : {
266 : // CSP on a null principal makes no sense
267 0 : return NS_OK;
268 : }
269 :
270 : NS_IMETHODIMP
271 0 : nsSystemPrincipal::GetDomain(nsIURI** aDomain)
272 : {
273 0 : *aDomain = nsnull;
274 0 : return NS_OK;
275 : }
276 :
277 : NS_IMETHODIMP
278 0 : nsSystemPrincipal::SetDomain(nsIURI* aDomain)
279 : {
280 0 : return NS_OK;
281 : }
282 :
283 : NS_IMETHODIMP
284 0 : nsSystemPrincipal::GetSecurityPolicy(void** aSecurityPolicy)
285 : {
286 0 : *aSecurityPolicy = nsnull;
287 0 : return NS_OK;
288 : }
289 :
290 : NS_IMETHODIMP
291 0 : nsSystemPrincipal::SetSecurityPolicy(void* aSecurityPolicy)
292 : {
293 0 : return NS_OK;
294 : }
295 :
296 :
297 : //////////////////////////////////////////
298 : // Methods implementing nsISerializable //
299 : //////////////////////////////////////////
300 :
301 : NS_IMETHODIMP
302 642 : nsSystemPrincipal::Read(nsIObjectInputStream* aStream)
303 : {
304 : // no-op: CID is sufficient to identify the mSystemPrincipal singleton
305 642 : return NS_OK;
306 : }
307 :
308 : NS_IMETHODIMP
309 7375 : nsSystemPrincipal::Write(nsIObjectOutputStream* aStream)
310 : {
311 : // no-op: CID is sufficient to identify the mSystemPrincipal singleton
312 7375 : return NS_OK;
313 : }
314 :
315 : /////////////////////////////////////////////
316 : // Constructor, Destructor, initialization //
317 : /////////////////////////////////////////////
318 :
319 1404 : nsSystemPrincipal::nsSystemPrincipal()
320 : {
321 1404 : }
322 :
323 2772 : nsSystemPrincipal::~nsSystemPrincipal()
324 : {
325 5544 : }
|