Bumping manifests a=b2g-bump
[gecko.git] / caps / nsPrincipal.cpp
blobbcbc909f2c75e42561c43e12a75b8aea0a759b39
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 sw=2 et tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsPrincipal.h"
9 #include "mozIThirdPartyUtil.h"
10 #include "nscore.h"
11 #include "nsScriptSecurityManager.h"
12 #include "nsString.h"
13 #include "nsReadableUtils.h"
14 #include "pratom.h"
15 #include "nsIURI.h"
16 #include "nsJSPrincipals.h"
17 #include "nsIObjectInputStream.h"
18 #include "nsIObjectOutputStream.h"
19 #include "nsIClassInfoImpl.h"
20 #include "nsIProtocolHandler.h"
21 #include "nsError.h"
22 #include "nsIContentSecurityPolicy.h"
23 #include "jswrapper.h"
25 #include "mozilla/dom/ScriptSettings.h"
26 #include "mozilla/Preferences.h"
27 #include "mozilla/HashFunctions.h"
29 #include "nsIAppsService.h"
30 #include "mozIApplication.h"
32 using namespace mozilla;
34 static bool gCodeBasePrincipalSupport = false;
35 static bool gIsObservingCodeBasePrincipalSupport = false;
37 static bool URIIsImmutable(nsIURI* aURI)
39 nsCOMPtr<nsIMutable> mutableObj(do_QueryInterface(aURI));
40 bool isMutable;
41 return
42 mutableObj &&
43 NS_SUCCEEDED(mutableObj->GetMutable(&isMutable)) &&
44 !isMutable;
47 // Static member variables
48 const char nsBasePrincipal::sInvalid[] = "Invalid";
50 NS_IMETHODIMP_(MozExternalRefCountType)
51 nsBasePrincipal::AddRef()
53 NS_PRECONDITION(int32_t(refcount) >= 0, "illegal refcnt");
54 // XXXcaa does this need to be threadsafe? See bug 143559.
55 nsrefcnt count = ++refcount;
56 NS_LOG_ADDREF(this, count, "nsBasePrincipal", sizeof(*this));
57 return count;
60 NS_IMETHODIMP_(MozExternalRefCountType)
61 nsBasePrincipal::Release()
63 NS_PRECONDITION(0 != refcount, "dup release");
64 nsrefcnt count = --refcount;
65 NS_LOG_RELEASE(this, count, "nsBasePrincipal");
66 if (count == 0) {
67 delete this;
70 return count;
73 nsBasePrincipal::nsBasePrincipal()
75 if (!gIsObservingCodeBasePrincipalSupport) {
76 nsresult rv =
77 Preferences::AddBoolVarCache(&gCodeBasePrincipalSupport,
78 "signed.applets.codebase_principal_support",
79 false);
80 gIsObservingCodeBasePrincipalSupport = NS_SUCCEEDED(rv);
81 NS_WARN_IF_FALSE(gIsObservingCodeBasePrincipalSupport,
82 "Installing gCodeBasePrincipalSupport failed!");
86 nsBasePrincipal::~nsBasePrincipal(void)
90 NS_IMETHODIMP
91 nsBasePrincipal::GetCsp(nsIContentSecurityPolicy** aCsp)
93 NS_IF_ADDREF(*aCsp = mCSP);
94 return NS_OK;
97 NS_IMETHODIMP
98 nsBasePrincipal::SetCsp(nsIContentSecurityPolicy* aCsp)
100 // If CSP was already set, it should not be destroyed! Instead, it should
101 // get set anew when a new principal is created.
102 if (mCSP)
103 return NS_ERROR_ALREADY_INITIALIZED;
105 mCSP = aCsp;
106 return NS_OK;
109 #ifdef DEBUG
110 void nsPrincipal::dumpImpl()
112 nsAutoCString str;
113 GetScriptLocation(str);
114 fprintf(stderr, "nsPrincipal (%p) = %s\n", static_cast<void*>(this), str.get());
116 #endif
118 NS_IMPL_CLASSINFO(nsPrincipal, nullptr, nsIClassInfo::MAIN_THREAD_ONLY,
119 NS_PRINCIPAL_CID)
120 NS_IMPL_QUERY_INTERFACE_CI(nsPrincipal,
121 nsIPrincipal,
122 nsISerializable)
123 NS_IMPL_CI_INTERFACE_GETTER(nsPrincipal,
124 nsIPrincipal,
125 nsISerializable)
126 NS_IMPL_ADDREF_INHERITED(nsPrincipal, nsBasePrincipal)
127 NS_IMPL_RELEASE_INHERITED(nsPrincipal, nsBasePrincipal)
129 nsPrincipal::nsPrincipal()
130 : mAppId(nsIScriptSecurityManager::UNKNOWN_APP_ID)
131 , mInMozBrowser(false)
132 , mCodebaseImmutable(false)
133 , mDomainImmutable(false)
134 , mInitialized(false)
137 nsPrincipal::~nsPrincipal()
140 nsresult
141 nsPrincipal::Init(nsIURI *aCodebase,
142 uint32_t aAppId,
143 bool aInMozBrowser)
145 NS_ENSURE_STATE(!mInitialized);
146 NS_ENSURE_ARG(aCodebase);
148 mInitialized = true;
150 mCodebase = NS_TryToMakeImmutable(aCodebase);
151 mCodebaseImmutable = URIIsImmutable(mCodebase);
153 mAppId = aAppId;
154 mInMozBrowser = aInMozBrowser;
156 return NS_OK;
159 void
160 nsPrincipal::GetScriptLocation(nsACString &aStr)
162 mCodebase->GetSpec(aStr);
165 /* static */ nsresult
166 nsPrincipal::GetOriginForURI(nsIURI* aURI, char **aOrigin)
168 if (!aURI) {
169 return NS_ERROR_FAILURE;
172 *aOrigin = nullptr;
174 nsCOMPtr<nsIURI> origin = NS_GetInnermostURI(aURI);
175 if (!origin) {
176 return NS_ERROR_FAILURE;
179 nsAutoCString hostPort;
181 // chrome: URLs don't have a meaningful origin, so make
182 // sure we just get the full spec for them.
183 // XXX this should be removed in favor of the solution in
184 // bug 160042.
185 bool isChrome;
186 nsresult rv = origin->SchemeIs("chrome", &isChrome);
187 if (NS_SUCCEEDED(rv) && !isChrome) {
188 rv = origin->GetAsciiHost(hostPort);
189 // Some implementations return an empty string, treat it as no support
190 // for asciiHost by that implementation.
191 if (hostPort.IsEmpty()) {
192 rv = NS_ERROR_FAILURE;
196 int32_t port;
197 if (NS_SUCCEEDED(rv) && !isChrome) {
198 rv = origin->GetPort(&port);
201 if (NS_SUCCEEDED(rv) && !isChrome) {
202 if (port != -1) {
203 hostPort.Append(':');
204 hostPort.AppendInt(port, 10);
207 nsAutoCString scheme;
208 rv = origin->GetScheme(scheme);
209 NS_ENSURE_SUCCESS(rv, rv);
211 *aOrigin = ToNewCString(scheme + NS_LITERAL_CSTRING("://") + hostPort);
213 else {
214 // Some URIs (e.g., nsSimpleURI) don't support asciiHost. Just
215 // get the full spec.
216 nsAutoCString spec;
217 // XXX nsMozIconURI and nsJARURI don't implement this correctly, they
218 // both fall back to GetSpec. That needs to be fixed.
219 rv = origin->GetAsciiSpec(spec);
220 NS_ENSURE_SUCCESS(rv, rv);
222 *aOrigin = ToNewCString(spec);
225 return *aOrigin ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
228 NS_IMETHODIMP
229 nsPrincipal::GetOrigin(char **aOrigin)
231 return GetOriginForURI(mCodebase, aOrigin);
234 NS_IMETHODIMP
235 nsPrincipal::EqualsConsideringDomain(nsIPrincipal *aOther, bool *aResult)
237 *aResult = false;
239 if (!aOther) {
240 NS_WARNING("Need a principal to compare this to!");
241 return NS_OK;
244 if (aOther == this) {
245 *aResult = true;
246 return NS_OK;
249 if (!nsScriptSecurityManager::AppAttributesEqual(this, aOther)) {
250 return NS_OK;
253 // If either the subject or the object has changed its principal by
254 // explicitly setting document.domain then the other must also have
255 // done so in order to be considered the same origin. This prevents
256 // DNS spoofing based on document.domain (154930)
258 nsCOMPtr<nsIURI> thisURI;
259 this->GetDomain(getter_AddRefs(thisURI));
260 bool thisSetDomain = !!thisURI;
261 if (!thisURI) {
262 this->GetURI(getter_AddRefs(thisURI));
265 nsCOMPtr<nsIURI> otherURI;
266 aOther->GetDomain(getter_AddRefs(otherURI));
267 bool otherSetDomain = !!otherURI;
268 if (!otherURI) {
269 aOther->GetURI(getter_AddRefs(otherURI));
272 *aResult = thisSetDomain == otherSetDomain &&
273 nsScriptSecurityManager::SecurityCompareURIs(thisURI, otherURI);
274 return NS_OK;
277 NS_IMETHODIMP
278 nsPrincipal::Equals(nsIPrincipal *aOther, bool *aResult)
280 *aResult = false;
282 if (!aOther) {
283 NS_WARNING("Need a principal to compare this to!");
284 return NS_OK;
287 if (aOther == this) {
288 *aResult = true;
289 return NS_OK;
292 if (!nsScriptSecurityManager::AppAttributesEqual(this, aOther)) {
293 return NS_OK;
296 nsCOMPtr<nsIURI> otherURI;
297 nsresult rv = aOther->GetURI(getter_AddRefs(otherURI));
298 if (NS_FAILED(rv)) {
299 return rv;
302 NS_ASSERTION(mCodebase,
303 "shouldn't be calling this on principals from preferences");
305 // Compare codebases.
306 *aResult = nsScriptSecurityManager::SecurityCompareURIs(mCodebase,
307 otherURI);
308 return NS_OK;
311 NS_IMETHODIMP
312 nsPrincipal::Subsumes(nsIPrincipal *aOther, bool *aResult)
314 return Equals(aOther, aResult);
317 NS_IMETHODIMP
318 nsPrincipal::SubsumesConsideringDomain(nsIPrincipal *aOther, bool *aResult)
320 return EqualsConsideringDomain(aOther, aResult);
323 NS_IMETHODIMP
324 nsPrincipal::GetURI(nsIURI** aURI)
326 if (mCodebaseImmutable) {
327 NS_ADDREF(*aURI = mCodebase);
328 return NS_OK;
331 if (!mCodebase) {
332 *aURI = nullptr;
333 return NS_OK;
336 return NS_EnsureSafeToReturn(mCodebase, aURI);
339 NS_IMETHODIMP
340 nsPrincipal::CheckMayLoad(nsIURI* aURI, bool aReport, bool aAllowIfInheritsPrincipal)
342 if (aAllowIfInheritsPrincipal) {
343 // If the caller specified to allow loads of URIs that inherit
344 // our principal, allow the load if this URI inherits its principal
345 if (nsPrincipal::IsPrincipalInherited(aURI)) {
346 return NS_OK;
350 // See if aURI is something like a Blob URI that is actually associated with
351 // a principal.
352 nsCOMPtr<nsIURIWithPrincipal> uriWithPrin = do_QueryInterface(aURI);
353 nsCOMPtr<nsIPrincipal> uriPrin;
354 if (uriWithPrin) {
355 uriWithPrin->GetPrincipal(getter_AddRefs(uriPrin));
357 if (uriPrin && nsIPrincipal::Subsumes(uriPrin)) {
358 return NS_OK;
361 if (nsScriptSecurityManager::SecurityCompareURIs(mCodebase, aURI)) {
362 return NS_OK;
365 // If strict file origin policy is in effect, local files will always fail
366 // SecurityCompareURIs unless they are identical. Explicitly check file origin
367 // policy, in that case.
368 if (nsScriptSecurityManager::GetStrictFileOriginPolicy() &&
369 NS_URIIsLocalFile(aURI) &&
370 NS_RelaxStrictFileOriginPolicy(aURI, mCodebase)) {
371 return NS_OK;
374 if (aReport) {
375 nsScriptSecurityManager::ReportError(nullptr, NS_LITERAL_STRING("CheckSameOriginError"), mCodebase, aURI);
377 return NS_ERROR_DOM_BAD_URI;
380 void
381 nsPrincipal::SetURI(nsIURI* aURI)
383 mCodebase = NS_TryToMakeImmutable(aURI);
384 mCodebaseImmutable = URIIsImmutable(mCodebase);
387 NS_IMETHODIMP
388 nsPrincipal::GetHashValue(uint32_t* aValue)
390 NS_PRECONDITION(mCodebase, "Need a codebase");
392 *aValue = nsScriptSecurityManager::HashPrincipalByOrigin(this);
393 return NS_OK;
396 NS_IMETHODIMP
397 nsPrincipal::GetDomain(nsIURI** aDomain)
399 if (!mDomain) {
400 *aDomain = nullptr;
401 return NS_OK;
404 if (mDomainImmutable) {
405 NS_ADDREF(*aDomain = mDomain);
406 return NS_OK;
409 return NS_EnsureSafeToReturn(mDomain, aDomain);
412 NS_IMETHODIMP
413 nsPrincipal::SetDomain(nsIURI* aDomain)
415 mDomain = NS_TryToMakeImmutable(aDomain);
416 mDomainImmutable = URIIsImmutable(mDomain);
418 // Recompute all wrappers between compartments using this principal and other
419 // non-chrome compartments.
420 AutoSafeJSContext cx;
421 JSPrincipals *principals = nsJSPrincipals::get(static_cast<nsIPrincipal*>(this));
422 bool success = js::RecomputeWrappers(cx, js::ContentCompartmentsOnly(),
423 js::CompartmentsWithPrincipals(principals));
424 NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
425 success = js::RecomputeWrappers(cx, js::CompartmentsWithPrincipals(principals),
426 js::ContentCompartmentsOnly());
427 NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
429 return NS_OK;
432 NS_IMETHODIMP
433 nsPrincipal::GetJarPrefix(nsACString& aJarPrefix)
435 MOZ_ASSERT(mAppId != nsIScriptSecurityManager::UNKNOWN_APP_ID);
437 mozilla::GetJarPrefix(mAppId, mInMozBrowser, aJarPrefix);
438 return NS_OK;
441 NS_IMETHODIMP
442 nsPrincipal::GetAppStatus(uint16_t* aAppStatus)
444 *aAppStatus = GetAppStatus();
445 return NS_OK;
448 NS_IMETHODIMP
449 nsPrincipal::GetAppId(uint32_t* aAppId)
451 if (mAppId == nsIScriptSecurityManager::UNKNOWN_APP_ID) {
452 MOZ_ASSERT(false);
453 *aAppId = nsIScriptSecurityManager::NO_APP_ID;
454 return NS_OK;
457 *aAppId = mAppId;
458 return NS_OK;
461 NS_IMETHODIMP
462 nsPrincipal::GetIsInBrowserElement(bool* aIsInBrowserElement)
464 *aIsInBrowserElement = mInMozBrowser;
465 return NS_OK;
468 NS_IMETHODIMP
469 nsPrincipal::GetUnknownAppId(bool* aUnknownAppId)
471 *aUnknownAppId = mAppId == nsIScriptSecurityManager::UNKNOWN_APP_ID;
472 return NS_OK;
475 NS_IMETHODIMP
476 nsPrincipal::GetIsNullPrincipal(bool* aIsNullPrincipal)
478 *aIsNullPrincipal = false;
479 return NS_OK;
482 NS_IMETHODIMP
483 nsPrincipal::GetBaseDomain(nsACString& aBaseDomain)
485 // For a file URI, we return the file path.
486 if (NS_URIIsLocalFile(mCodebase)) {
487 nsCOMPtr<nsIURL> url = do_QueryInterface(mCodebase);
489 if (url) {
490 return url->GetFilePath(aBaseDomain);
494 bool hasNoRelativeFlag;
495 nsresult rv = NS_URIChainHasFlags(mCodebase,
496 nsIProtocolHandler::URI_NORELATIVE,
497 &hasNoRelativeFlag);
498 if (NS_WARN_IF(NS_FAILED(rv))) {
499 return rv;
502 if (hasNoRelativeFlag) {
503 return mCodebase->GetSpec(aBaseDomain);
506 // For everything else, we ask the TLD service via
507 // the ThirdPartyUtil.
508 nsCOMPtr<mozIThirdPartyUtil> thirdPartyUtil =
509 do_GetService(THIRDPARTYUTIL_CONTRACTID);
510 if (thirdPartyUtil) {
511 return thirdPartyUtil->GetBaseDomain(mCodebase, aBaseDomain);
514 return NS_OK;
517 NS_IMETHODIMP
518 nsPrincipal::Read(nsIObjectInputStream* aStream)
520 nsCOMPtr<nsISupports> supports;
521 nsCOMPtr<nsIURI> codebase;
522 nsresult rv = NS_ReadOptionalObject(aStream, true, getter_AddRefs(supports));
523 if (NS_FAILED(rv)) {
524 return rv;
527 codebase = do_QueryInterface(supports);
529 nsCOMPtr<nsIURI> domain;
530 rv = NS_ReadOptionalObject(aStream, true, getter_AddRefs(supports));
531 if (NS_FAILED(rv)) {
532 return rv;
535 domain = do_QueryInterface(supports);
537 uint32_t appId;
538 rv = aStream->Read32(&appId);
539 NS_ENSURE_SUCCESS(rv, rv);
541 bool inMozBrowser;
542 rv = aStream->ReadBoolean(&inMozBrowser);
543 NS_ENSURE_SUCCESS(rv, rv);
545 rv = NS_ReadOptionalObject(aStream, true, getter_AddRefs(supports));
546 NS_ENSURE_SUCCESS(rv, rv);
548 // This may be null.
549 nsCOMPtr<nsIContentSecurityPolicy> csp = do_QueryInterface(supports, &rv);
551 rv = Init(codebase, appId, inMozBrowser);
552 NS_ENSURE_SUCCESS(rv, rv);
554 rv = SetCsp(csp);
555 NS_ENSURE_SUCCESS(rv, rv);
557 // need to link in the CSP context here (link in the URI of the protected
558 // resource).
559 if (csp) {
560 csp->SetRequestContext(codebase, nullptr, nullptr);
563 SetDomain(domain);
565 return NS_OK;
568 NS_IMETHODIMP
569 nsPrincipal::Write(nsIObjectOutputStream* aStream)
571 NS_ENSURE_STATE(mCodebase);
573 nsresult rv = NS_WriteOptionalCompoundObject(aStream, mCodebase, NS_GET_IID(nsIURI),
574 true);
575 if (NS_FAILED(rv)) {
576 return rv;
579 rv = NS_WriteOptionalCompoundObject(aStream, mDomain, NS_GET_IID(nsIURI),
580 true);
581 if (NS_FAILED(rv)) {
582 return rv;
585 aStream->Write32(mAppId);
586 aStream->WriteBoolean(mInMozBrowser);
588 rv = NS_WriteOptionalCompoundObject(aStream, mCSP,
589 NS_GET_IID(nsIContentSecurityPolicy),
590 true);
591 if (NS_FAILED(rv)) {
592 return rv;
595 // mCodebaseImmutable and mDomainImmutable will be recomputed based
596 // on the deserialized URIs in Read().
598 return NS_OK;
601 uint16_t
602 nsPrincipal::GetAppStatus()
604 if (mAppId == nsIScriptSecurityManager::UNKNOWN_APP_ID) {
605 NS_WARNING("Asking for app status on a principal with an unknown app id");
606 return nsIPrincipal::APP_STATUS_NOT_INSTALLED;
608 return nsScriptSecurityManager::AppStatusForPrincipal(this);
611 /************************************************************************************************************************/
613 static const char EXPANDED_PRINCIPAL_SPEC[] = "[Expanded Principal]";
615 NS_IMPL_CLASSINFO(nsExpandedPrincipal, nullptr, nsIClassInfo::MAIN_THREAD_ONLY,
616 NS_EXPANDEDPRINCIPAL_CID)
617 NS_IMPL_QUERY_INTERFACE_CI(nsExpandedPrincipal,
618 nsIPrincipal,
619 nsIExpandedPrincipal)
620 NS_IMPL_CI_INTERFACE_GETTER(nsExpandedPrincipal,
621 nsIPrincipal,
622 nsIExpandedPrincipal)
623 NS_IMPL_ADDREF_INHERITED(nsExpandedPrincipal, nsBasePrincipal)
624 NS_IMPL_RELEASE_INHERITED(nsExpandedPrincipal, nsBasePrincipal)
626 nsExpandedPrincipal::nsExpandedPrincipal(nsTArray<nsCOMPtr <nsIPrincipal> > &aWhiteList)
628 mPrincipals.AppendElements(aWhiteList);
631 nsExpandedPrincipal::~nsExpandedPrincipal()
634 NS_IMETHODIMP
635 nsExpandedPrincipal::GetDomain(nsIURI** aDomain)
637 *aDomain = nullptr;
638 return NS_OK;
641 NS_IMETHODIMP
642 nsExpandedPrincipal::SetDomain(nsIURI* aDomain)
644 return NS_OK;
647 NS_IMETHODIMP
648 nsExpandedPrincipal::GetOrigin(char** aOrigin)
650 *aOrigin = ToNewCString(NS_LITERAL_CSTRING(EXPANDED_PRINCIPAL_SPEC));
651 return *aOrigin ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
654 typedef nsresult (NS_STDCALL nsIPrincipal::*nsIPrincipalMemFn)(nsIPrincipal* aOther,
655 bool* aResult);
656 #define CALL_MEMBER_FUNCTION(THIS,MEM_FN) ((THIS)->*(MEM_FN))
658 // nsExpandedPrincipal::Equals and nsExpandedPrincipal::EqualsConsideringDomain
659 // shares the same logic. The difference only that Equals requires 'this'
660 // and 'aOther' to Subsume each other while EqualsConsideringDomain requires
661 // bidirectional SubsumesConsideringDomain.
662 static nsresult
663 Equals(nsExpandedPrincipal* aThis, nsIPrincipalMemFn aFn, nsIPrincipal* aOther,
664 bool* aResult)
666 // If (and only if) 'aThis' and 'aOther' both Subsume/SubsumesConsideringDomain
667 // each other, then they are Equal.
668 *aResult = false;
669 // Calling the corresponding subsume function on this (aFn).
670 nsresult rv = CALL_MEMBER_FUNCTION(aThis, aFn)(aOther, aResult);
671 NS_ENSURE_SUCCESS(rv, rv);
672 if (!*aResult)
673 return NS_OK;
675 // Calling the corresponding subsume function on aOther (aFn).
676 rv = CALL_MEMBER_FUNCTION(aOther, aFn)(aThis, aResult);
677 NS_ENSURE_SUCCESS(rv, rv);
678 return NS_OK;
681 NS_IMETHODIMP
682 nsExpandedPrincipal::Equals(nsIPrincipal* aOther, bool* aResult)
684 return ::Equals(this, &nsIPrincipal::Subsumes, aOther, aResult);
687 NS_IMETHODIMP
688 nsExpandedPrincipal::EqualsConsideringDomain(nsIPrincipal* aOther, bool* aResult)
690 return ::Equals(this, &nsIPrincipal::SubsumesConsideringDomain, aOther, aResult);
693 // nsExpandedPrincipal::Subsumes and nsExpandedPrincipal::SubsumesConsideringDomain
694 // shares the same logic. The difference only that Subsumes calls are replaced
695 //with SubsumesConsideringDomain calls in the second case.
696 static nsresult
697 Subsumes(nsExpandedPrincipal* aThis, nsIPrincipalMemFn aFn, nsIPrincipal* aOther,
698 bool* aResult)
700 nsresult rv;
701 nsCOMPtr<nsIExpandedPrincipal> expanded = do_QueryInterface(aOther);
702 if (expanded) {
703 // If aOther is an ExpandedPrincipal too, check if all of its
704 // principals are subsumed.
705 nsTArray< nsCOMPtr<nsIPrincipal> >* otherList;
706 expanded->GetWhiteList(&otherList);
707 for (uint32_t i = 0; i < otherList->Length(); ++i){
708 rv = CALL_MEMBER_FUNCTION(aThis, aFn)((*otherList)[i], aResult);
709 NS_ENSURE_SUCCESS(rv, rv);
710 if (!*aResult) {
711 // If we don't subsume at least one principal of aOther, return false.
712 return NS_OK;
715 } else {
716 // For a regular aOther, one of our principals must subsume it.
717 nsTArray< nsCOMPtr<nsIPrincipal> >* list;
718 aThis->GetWhiteList(&list);
719 for (uint32_t i = 0; i < list->Length(); ++i){
720 rv = CALL_MEMBER_FUNCTION((*list)[i], aFn)(aOther, aResult);
721 NS_ENSURE_SUCCESS(rv, rv);
722 if (*aResult) {
723 // If one of our principal subsumes it, return true.
724 return NS_OK;
728 return NS_OK;
731 #undef CALL_MEMBER_FUNCTION
733 NS_IMETHODIMP
734 nsExpandedPrincipal::Subsumes(nsIPrincipal* aOther, bool* aResult)
736 return ::Subsumes(this, &nsIPrincipal::Subsumes, aOther, aResult);
739 NS_IMETHODIMP
740 nsExpandedPrincipal::SubsumesConsideringDomain(nsIPrincipal* aOther, bool* aResult)
742 return ::Subsumes(this, &nsIPrincipal::SubsumesConsideringDomain, aOther, aResult);
745 NS_IMETHODIMP
746 nsExpandedPrincipal::CheckMayLoad(nsIURI* uri, bool aReport, bool aAllowIfInheritsPrincipal)
748 nsresult rv;
749 for (uint32_t i = 0; i < mPrincipals.Length(); ++i){
750 rv = mPrincipals[i]->CheckMayLoad(uri, aReport, aAllowIfInheritsPrincipal);
751 if (NS_SUCCEEDED(rv))
752 return rv;
755 return NS_ERROR_DOM_BAD_URI;
758 NS_IMETHODIMP
759 nsExpandedPrincipal::GetHashValue(uint32_t* result)
761 MOZ_CRASH("extended principal should never be used as key in a hash map");
764 NS_IMETHODIMP
765 nsExpandedPrincipal::GetURI(nsIURI** aURI)
767 *aURI = nullptr;
768 return NS_OK;
771 NS_IMETHODIMP
772 nsExpandedPrincipal::GetWhiteList(nsTArray<nsCOMPtr<nsIPrincipal> >** aWhiteList)
774 *aWhiteList = &mPrincipals;
775 return NS_OK;
778 NS_IMETHODIMP
779 nsExpandedPrincipal::GetJarPrefix(nsACString& aJarPrefix)
781 aJarPrefix.Truncate();
782 return NS_OK;
785 NS_IMETHODIMP
786 nsExpandedPrincipal::GetAppStatus(uint16_t* aAppStatus)
788 *aAppStatus = nsIPrincipal::APP_STATUS_NOT_INSTALLED;
789 return NS_OK;
792 NS_IMETHODIMP
793 nsExpandedPrincipal::GetAppId(uint32_t* aAppId)
795 *aAppId = nsIScriptSecurityManager::NO_APP_ID;
796 return NS_OK;
799 NS_IMETHODIMP
800 nsExpandedPrincipal::GetIsInBrowserElement(bool* aIsInBrowserElement)
802 *aIsInBrowserElement = false;
803 return NS_OK;
806 NS_IMETHODIMP
807 nsExpandedPrincipal::GetUnknownAppId(bool* aUnknownAppId)
809 *aUnknownAppId = false;
810 return NS_OK;
813 NS_IMETHODIMP
814 nsExpandedPrincipal::GetIsNullPrincipal(bool* aIsNullPrincipal)
816 *aIsNullPrincipal = false;
817 return NS_OK;
820 NS_IMETHODIMP
821 nsExpandedPrincipal::GetBaseDomain(nsACString& aBaseDomain)
823 return NS_ERROR_NOT_AVAILABLE;
826 void
827 nsExpandedPrincipal::GetScriptLocation(nsACString& aStr)
829 // Is that a good idea to list it's principals?
830 aStr.Assign(EXPANDED_PRINCIPAL_SPEC);
833 #ifdef DEBUG
834 void nsExpandedPrincipal::dumpImpl()
836 fprintf(stderr, "nsExpandedPrincipal (%p)\n", static_cast<void*>(this));
838 #endif
840 //////////////////////////////////////////
841 // Methods implementing nsISerializable //
842 //////////////////////////////////////////
844 NS_IMETHODIMP
845 nsExpandedPrincipal::Read(nsIObjectInputStream* aStream)
847 return NS_ERROR_NOT_IMPLEMENTED;
850 NS_IMETHODIMP
851 nsExpandedPrincipal::Write(nsIObjectOutputStream* aStream)
853 return NS_ERROR_NOT_IMPLEMENTED;