|
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/. */ |
|
5 |
|
6 #ifndef nsPrincipal_h__ |
|
7 #define nsPrincipal_h__ |
|
8 |
|
9 #include "nsAutoPtr.h" |
|
10 #include "nsCOMPtr.h" |
|
11 #include "nsJSPrincipals.h" |
|
12 #include "nsTArray.h" |
|
13 #include "nsAutoPtr.h" |
|
14 #include "nsIProtocolHandler.h" |
|
15 #include "nsNetUtil.h" |
|
16 #include "nsScriptSecurityManager.h" |
|
17 |
|
18 class nsIObjectInputStream; |
|
19 class nsIObjectOutputStream; |
|
20 |
|
21 class nsBasePrincipal : public nsJSPrincipals |
|
22 { |
|
23 public: |
|
24 nsBasePrincipal(); |
|
25 |
|
26 protected: |
|
27 virtual ~nsBasePrincipal(); |
|
28 |
|
29 public: |
|
30 NS_IMETHOD_(MozExternalRefCountType) AddRef(void); |
|
31 NS_IMETHOD_(MozExternalRefCountType) Release(void); |
|
32 NS_IMETHOD GetCsp(nsIContentSecurityPolicy** aCsp); |
|
33 NS_IMETHOD SetCsp(nsIContentSecurityPolicy* aCsp); |
|
34 public: |
|
35 |
|
36 static const char sInvalid[]; |
|
37 |
|
38 protected: |
|
39 |
|
40 #ifdef DEBUG |
|
41 virtual void dumpImpl() = 0; |
|
42 #endif |
|
43 |
|
44 nsCOMPtr<nsIContentSecurityPolicy> mCSP; |
|
45 }; |
|
46 |
|
47 class nsPrincipal : public nsBasePrincipal |
|
48 { |
|
49 public: |
|
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); |
|
69 #ifdef DEBUG |
|
70 virtual void dumpImpl(); |
|
71 #endif |
|
72 |
|
73 nsPrincipal(); |
|
74 |
|
75 // Init() must be called before the principal is in a usable state. |
|
76 nsresult Init(nsIURI* aCodebase, |
|
77 uint32_t aAppId, |
|
78 bool aInMozBrowser); |
|
79 |
|
80 virtual void GetScriptLocation(nsACString& aStr) MOZ_OVERRIDE; |
|
81 void SetURI(nsIURI* aURI); |
|
82 |
|
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; |
|
87 nsresult rv = |
|
88 NS_URIChainHasFlags(aURI, |
|
89 nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT, |
|
90 &doesInheritSecurityContext); |
|
91 |
|
92 if (NS_SUCCEEDED(rv) && doesInheritSecurityContext) { |
|
93 return true; |
|
94 } |
|
95 |
|
96 return false; |
|
97 } |
|
98 |
|
99 |
|
100 /** |
|
101 * Computes the puny-encoded origin of aURI. |
|
102 */ |
|
103 static nsresult GetOriginForURI(nsIURI* aURI, char **aOrigin); |
|
104 |
|
105 nsCOMPtr<nsIURI> mDomain; |
|
106 nsCOMPtr<nsIURI> mCodebase; |
|
107 uint32_t mAppId; |
|
108 bool mInMozBrowser; |
|
109 // If mCodebaseImmutable is true, mCodebase is non-null and immutable |
|
110 bool mCodebaseImmutable; |
|
111 bool mDomainImmutable; |
|
112 bool mInitialized; |
|
113 |
|
114 protected: |
|
115 virtual ~nsPrincipal(); |
|
116 |
|
117 /** |
|
118 * Returns the app status of the principal based on mAppId and mInMozBrowser. |
|
119 */ |
|
120 uint16_t GetAppStatus(); |
|
121 }; |
|
122 |
|
123 class nsExpandedPrincipal : public nsIExpandedPrincipal, public nsBasePrincipal |
|
124 { |
|
125 public: |
|
126 nsExpandedPrincipal(nsTArray< nsCOMPtr<nsIPrincipal> > &aWhiteList); |
|
127 |
|
128 protected: |
|
129 virtual ~nsExpandedPrincipal(); |
|
130 |
|
131 public: |
|
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); |
|
152 #ifdef DEBUG |
|
153 virtual void dumpImpl(); |
|
154 #endif |
|
155 |
|
156 virtual void GetScriptLocation(nsACString &aStr) MOZ_OVERRIDE; |
|
157 |
|
158 private: |
|
159 nsTArray< nsCOMPtr<nsIPrincipal> > mPrincipals; |
|
160 }; |
|
161 |
|
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 }} |
|
166 |
|
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 }} |
|
171 |
|
172 #endif // nsPrincipal_h__ |