michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsPrincipal_h__ michael@0: #define nsPrincipal_h__ michael@0: michael@0: #include "nsAutoPtr.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsJSPrincipals.h" michael@0: #include "nsTArray.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsIProtocolHandler.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsScriptSecurityManager.h" michael@0: michael@0: class nsIObjectInputStream; michael@0: class nsIObjectOutputStream; michael@0: michael@0: class nsBasePrincipal : public nsJSPrincipals michael@0: { michael@0: public: michael@0: nsBasePrincipal(); michael@0: michael@0: protected: michael@0: virtual ~nsBasePrincipal(); michael@0: michael@0: public: michael@0: NS_IMETHOD_(MozExternalRefCountType) AddRef(void); michael@0: NS_IMETHOD_(MozExternalRefCountType) Release(void); michael@0: NS_IMETHOD GetCsp(nsIContentSecurityPolicy** aCsp); michael@0: NS_IMETHOD SetCsp(nsIContentSecurityPolicy* aCsp); michael@0: public: michael@0: michael@0: static const char sInvalid[]; michael@0: michael@0: protected: michael@0: michael@0: #ifdef DEBUG michael@0: virtual void dumpImpl() = 0; michael@0: #endif michael@0: michael@0: nsCOMPtr mCSP; michael@0: }; michael@0: michael@0: class nsPrincipal : public nsBasePrincipal michael@0: { michael@0: public: michael@0: NS_DECL_ISUPPORTS_INHERITED michael@0: NS_DECL_NSISERIALIZABLE michael@0: NS_IMETHOD Equals(nsIPrincipal* other, bool* _retval); michael@0: NS_IMETHOD EqualsConsideringDomain(nsIPrincipal* other, bool* _retval); michael@0: NS_IMETHOD GetHashValue(uint32_t* aHashValue); michael@0: NS_IMETHOD GetURI(nsIURI** aURI); michael@0: NS_IMETHOD GetDomain(nsIURI** aDomain); michael@0: NS_IMETHOD SetDomain(nsIURI* aDomain); michael@0: NS_IMETHOD GetOrigin(char** aOrigin); michael@0: NS_IMETHOD Subsumes(nsIPrincipal* other, bool* _retval); michael@0: NS_IMETHOD SubsumesConsideringDomain(nsIPrincipal* other, bool* _retval); michael@0: NS_IMETHOD CheckMayLoad(nsIURI* uri, bool report, bool allowIfInheritsPrincipal); michael@0: NS_IMETHOD GetJarPrefix(nsACString& aJarPrefix); michael@0: NS_IMETHOD GetAppStatus(uint16_t* aAppStatus); michael@0: NS_IMETHOD GetAppId(uint32_t* aAppStatus); michael@0: NS_IMETHOD GetIsInBrowserElement(bool* aIsInBrowserElement); michael@0: NS_IMETHOD GetUnknownAppId(bool* aUnknownAppId); michael@0: NS_IMETHOD GetIsNullPrincipal(bool* aIsNullPrincipal); michael@0: NS_IMETHOD GetBaseDomain(nsACString& aBaseDomain); michael@0: #ifdef DEBUG michael@0: virtual void dumpImpl(); michael@0: #endif michael@0: michael@0: nsPrincipal(); michael@0: michael@0: // Init() must be called before the principal is in a usable state. michael@0: nsresult Init(nsIURI* aCodebase, michael@0: uint32_t aAppId, michael@0: bool aInMozBrowser); michael@0: michael@0: virtual void GetScriptLocation(nsACString& aStr) MOZ_OVERRIDE; michael@0: void SetURI(nsIURI* aURI); michael@0: michael@0: static bool IsPrincipalInherited(nsIURI* aURI) { michael@0: // return true if the loadee URI has michael@0: // the URI_INHERITS_SECURITY_CONTEXT flag set. michael@0: bool doesInheritSecurityContext; michael@0: nsresult rv = michael@0: NS_URIChainHasFlags(aURI, michael@0: nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT, michael@0: &doesInheritSecurityContext); michael@0: michael@0: if (NS_SUCCEEDED(rv) && doesInheritSecurityContext) { michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Computes the puny-encoded origin of aURI. michael@0: */ michael@0: static nsresult GetOriginForURI(nsIURI* aURI, char **aOrigin); michael@0: michael@0: nsCOMPtr mDomain; michael@0: nsCOMPtr mCodebase; michael@0: uint32_t mAppId; michael@0: bool mInMozBrowser; michael@0: // If mCodebaseImmutable is true, mCodebase is non-null and immutable michael@0: bool mCodebaseImmutable; michael@0: bool mDomainImmutable; michael@0: bool mInitialized; michael@0: michael@0: protected: michael@0: virtual ~nsPrincipal(); michael@0: michael@0: /** michael@0: * Returns the app status of the principal based on mAppId and mInMozBrowser. michael@0: */ michael@0: uint16_t GetAppStatus(); michael@0: }; michael@0: michael@0: class nsExpandedPrincipal : public nsIExpandedPrincipal, public nsBasePrincipal michael@0: { michael@0: public: michael@0: nsExpandedPrincipal(nsTArray< nsCOMPtr > &aWhiteList); michael@0: michael@0: protected: michael@0: virtual ~nsExpandedPrincipal(); michael@0: michael@0: public: michael@0: NS_DECL_ISUPPORTS_INHERITED michael@0: NS_DECL_NSIEXPANDEDPRINCIPAL michael@0: NS_DECL_NSISERIALIZABLE michael@0: NS_IMETHOD Equals(nsIPrincipal* other, bool* _retval); michael@0: NS_IMETHOD EqualsConsideringDomain(nsIPrincipal* other, bool* _retval); michael@0: NS_IMETHOD GetHashValue(uint32_t* aHashValue); michael@0: NS_IMETHOD GetURI(nsIURI** aURI); michael@0: NS_IMETHOD GetDomain(nsIURI** aDomain); michael@0: NS_IMETHOD SetDomain(nsIURI* aDomain); michael@0: NS_IMETHOD GetOrigin(char** aOrigin); michael@0: NS_IMETHOD Subsumes(nsIPrincipal* other, bool* _retval); michael@0: NS_IMETHOD SubsumesConsideringDomain(nsIPrincipal* other, bool* _retval); michael@0: NS_IMETHOD CheckMayLoad(nsIURI* uri, bool report, bool allowIfInheritsPrincipal); michael@0: NS_IMETHOD GetJarPrefix(nsACString& aJarPrefix); michael@0: NS_IMETHOD GetAppStatus(uint16_t* aAppStatus); michael@0: NS_IMETHOD GetAppId(uint32_t* aAppStatus); michael@0: NS_IMETHOD GetIsInBrowserElement(bool* aIsInBrowserElement); michael@0: NS_IMETHOD GetUnknownAppId(bool* aUnknownAppId); michael@0: NS_IMETHOD GetIsNullPrincipal(bool* aIsNullPrincipal); michael@0: NS_IMETHOD GetBaseDomain(nsACString& aBaseDomain); michael@0: #ifdef DEBUG michael@0: virtual void dumpImpl(); michael@0: #endif michael@0: michael@0: virtual void GetScriptLocation(nsACString &aStr) MOZ_OVERRIDE; michael@0: michael@0: private: michael@0: nsTArray< nsCOMPtr > mPrincipals; michael@0: }; michael@0: michael@0: #define NS_PRINCIPAL_CONTRACTID "@mozilla.org/principal;1" michael@0: #define NS_PRINCIPAL_CID \ michael@0: { 0x09b7e598, 0x490d, 0x423f, \ michael@0: { 0xa8, 0xa6, 0x2e, 0x6c, 0x4e, 0xc8, 0x77, 0x50 }} michael@0: michael@0: #define NS_EXPANDEDPRINCIPAL_CONTRACTID "@mozilla.org/expandedprincipal;1" michael@0: #define NS_EXPANDEDPRINCIPAL_CID \ michael@0: { 0xb33a3807, 0xb76c, 0x44e5, \ michael@0: { 0xb9, 0x9d, 0x95, 0x7e, 0xe9, 0xba, 0x6e, 0x39 }} michael@0: michael@0: #endif // nsPrincipal_h__