1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsPrincipal_h__
7 #define nsPrincipal_h__
11 #include "nsJSPrincipals.h"
13 #include "nsAutoPtr.h"
14 #include "nsIProtocolHandler.h"
15 #include "nsNetUtil.h"
16 #include "nsScriptSecurityManager.h"
18 class nsIObjectInputStream
;
19 class nsIObjectOutputStream
;
21 class nsBasePrincipal
: public nsJSPrincipals
27 virtual ~nsBasePrincipal();
30 NS_IMETHOD_(MozExternalRefCountType
) AddRef(void);
31 NS_IMETHOD_(MozExternalRefCountType
) Release(void);
32 NS_IMETHOD
GetCsp(nsIContentSecurityPolicy
** aCsp
);
33 NS_IMETHOD
SetCsp(nsIContentSecurityPolicy
* aCsp
);
36 static const char sInvalid
[];
41 virtual void dumpImpl() = 0;
44 nsCOMPtr
<nsIContentSecurityPolicy
> mCSP
;
47 class nsPrincipal
: public nsBasePrincipal
50 NS_DECL_ISUPPORTS_INHERITED
51 NS_DECL_NSISERIALIZABLE
52 NS_IMETHOD
Equals(nsIPrincipal
* other
, bool* _retval
);
53 NS_IMETHOD
EqualsConsideringDomain(nsIPrincipal
* other
, bool* _retval
);
54 NS_IMETHOD
GetHashValue(uint32_t* aHashValue
);
55 NS_IMETHOD
GetURI(nsIURI
** aURI
);
56 NS_IMETHOD
GetDomain(nsIURI
** aDomain
);
57 NS_IMETHOD
SetDomain(nsIURI
* aDomain
);
58 NS_IMETHOD
GetOrigin(char** aOrigin
);
59 NS_IMETHOD
Subsumes(nsIPrincipal
* other
, bool* _retval
);
60 NS_IMETHOD
SubsumesConsideringDomain(nsIPrincipal
* other
, bool* _retval
);
61 NS_IMETHOD
CheckMayLoad(nsIURI
* uri
, bool report
, bool allowIfInheritsPrincipal
);
62 NS_IMETHOD
GetJarPrefix(nsACString
& aJarPrefix
);
63 NS_IMETHOD
GetAppStatus(uint16_t* aAppStatus
);
64 NS_IMETHOD
GetAppId(uint32_t* aAppStatus
);
65 NS_IMETHOD
GetIsInBrowserElement(bool* aIsInBrowserElement
);
66 NS_IMETHOD
GetUnknownAppId(bool* aUnknownAppId
);
67 NS_IMETHOD
GetIsNullPrincipal(bool* aIsNullPrincipal
);
68 NS_IMETHOD
GetBaseDomain(nsACString
& aBaseDomain
);
70 virtual void dumpImpl();
75 // Init() must be called before the principal is in a usable state.
76 nsresult
Init(nsIURI
* aCodebase
,
80 virtual void GetScriptLocation(nsACString
& aStr
) MOZ_OVERRIDE
;
81 void SetURI(nsIURI
* aURI
);
83 static bool IsPrincipalInherited(nsIURI
* aURI
) {
84 // return true if the loadee URI has
85 // the URI_INHERITS_SECURITY_CONTEXT flag set.
86 bool doesInheritSecurityContext
;
88 NS_URIChainHasFlags(aURI
,
89 nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT
,
90 &doesInheritSecurityContext
);
92 if (NS_SUCCEEDED(rv
) && doesInheritSecurityContext
) {
101 * Computes the puny-encoded origin of aURI.
103 static nsresult
GetOriginForURI(nsIURI
* aURI
, char **aOrigin
);
105 nsCOMPtr
<nsIURI
> mDomain
;
106 nsCOMPtr
<nsIURI
> mCodebase
;
109 // If mCodebaseImmutable is true, mCodebase is non-null and immutable
110 bool mCodebaseImmutable
;
111 bool mDomainImmutable
;
115 virtual ~nsPrincipal();
118 * Returns the app status of the principal based on mAppId and mInMozBrowser.
120 uint16_t GetAppStatus();
123 class nsExpandedPrincipal
: public nsIExpandedPrincipal
, public nsBasePrincipal
126 nsExpandedPrincipal(nsTArray
< nsCOMPtr
<nsIPrincipal
> > &aWhiteList
);
129 virtual ~nsExpandedPrincipal();
132 NS_DECL_ISUPPORTS_INHERITED
133 NS_DECL_NSIEXPANDEDPRINCIPAL
134 NS_DECL_NSISERIALIZABLE
135 NS_IMETHOD
Equals(nsIPrincipal
* other
, bool* _retval
);
136 NS_IMETHOD
EqualsConsideringDomain(nsIPrincipal
* other
, bool* _retval
);
137 NS_IMETHOD
GetHashValue(uint32_t* aHashValue
);
138 NS_IMETHOD
GetURI(nsIURI
** aURI
);
139 NS_IMETHOD
GetDomain(nsIURI
** aDomain
);
140 NS_IMETHOD
SetDomain(nsIURI
* aDomain
);
141 NS_IMETHOD
GetOrigin(char** aOrigin
);
142 NS_IMETHOD
Subsumes(nsIPrincipal
* other
, bool* _retval
);
143 NS_IMETHOD
SubsumesConsideringDomain(nsIPrincipal
* other
, bool* _retval
);
144 NS_IMETHOD
CheckMayLoad(nsIURI
* uri
, bool report
, bool allowIfInheritsPrincipal
);
145 NS_IMETHOD
GetJarPrefix(nsACString
& aJarPrefix
);
146 NS_IMETHOD
GetAppStatus(uint16_t* aAppStatus
);
147 NS_IMETHOD
GetAppId(uint32_t* aAppStatus
);
148 NS_IMETHOD
GetIsInBrowserElement(bool* aIsInBrowserElement
);
149 NS_IMETHOD
GetUnknownAppId(bool* aUnknownAppId
);
150 NS_IMETHOD
GetIsNullPrincipal(bool* aIsNullPrincipal
);
151 NS_IMETHOD
GetBaseDomain(nsACString
& aBaseDomain
);
153 virtual void dumpImpl();
156 virtual void GetScriptLocation(nsACString
&aStr
) MOZ_OVERRIDE
;
159 nsTArray
< nsCOMPtr
<nsIPrincipal
> > mPrincipals
;
162 #define NS_PRINCIPAL_CONTRACTID "@mozilla.org/principal;1"
163 #define NS_PRINCIPAL_CID \
164 { 0x09b7e598, 0x490d, 0x423f, \
165 { 0xa8, 0xa6, 0x2e, 0x6c, 0x4e, 0xc8, 0x77, 0x50 }}
167 #define NS_EXPANDEDPRINCIPAL_CONTRACTID "@mozilla.org/expandedprincipal;1"
168 #define NS_EXPANDEDPRINCIPAL_CID \
169 { 0xb33a3807, 0xb76c, 0x44e5, \
170 { 0xb9, 0x9d, 0x95, 0x7e, 0xe9, 0xba, 0x6e, 0x39 }}
172 #endif // nsPrincipal_h__