Bug 1802588 [wpt PR 37170] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / caps / ExpandedPrincipal.h
blob18f8ff8e4e2028e86a083ffa69afcd5588f47416
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 ExpandedPrincipal_h
7 #define ExpandedPrincipal_h
9 #include "nsCOMPtr.h"
10 #include "nsJSPrincipals.h"
11 #include "nsProxyRelease.h"
12 #include "nsTArray.h"
13 #include "nsNetUtil.h"
14 #include "mozilla/BasePrincipal.h"
16 class nsIContentSecurityPolicy;
18 namespace Json {
19 class Value;
22 class ExpandedPrincipal : public nsIExpandedPrincipal,
23 public mozilla::BasePrincipal {
24 public:
25 static already_AddRefed<ExpandedPrincipal> Create(
26 const nsTArray<nsCOMPtr<nsIPrincipal>>& aAllowList,
27 const mozilla::OriginAttributes& aAttrs);
29 static PrincipalKind Kind() { return eExpandedPrincipal; }
31 NS_DECL_NSIEXPANDEDPRINCIPAL
33 NS_IMETHOD_(MozExternalRefCountType) AddRef() override {
34 return nsJSPrincipals::AddRef();
36 NS_IMETHOD_(MozExternalRefCountType) Release() override {
37 return nsJSPrincipals::Release();
39 NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
40 uint32_t GetHashValue() override;
41 NS_IMETHOD GetURI(nsIURI** aURI) override;
42 NS_IMETHOD GetDomain(nsIURI** aDomain) override;
43 NS_IMETHOD SetDomain(nsIURI* aDomain) override;
44 NS_IMETHOD GetBaseDomain(nsACString& aBaseDomain) override;
45 NS_IMETHOD GetAddonId(nsAString& aAddonId) override;
46 NS_IMETHOD IsThirdPartyURI(nsIURI* uri, bool* aRes) override;
47 virtual bool AddonHasPermission(const nsAtom* aPerm) override;
48 virtual nsresult GetScriptLocation(nsACString& aStr) override;
50 bool AddonAllowsLoad(nsIURI* aURI, bool aExplicit = false);
52 void SetCsp(nsIContentSecurityPolicy* aCSP);
54 // Returns the principal to inherit when this principal requests the given
55 // URL. See BasePrincipal::PrincipalToInherit.
56 nsIPrincipal* PrincipalToInherit(nsIURI* aRequestedURI = nullptr);
58 nsresult GetSiteIdentifier(mozilla::SiteIdentifier& aSite) override;
60 virtual nsresult PopulateJSONObject(Json::Value& aObject) override;
61 // Serializable keys are the valid enum fields the serialization supports
62 enum SerializableKeys : uint8_t { eSpecs = 0, eSuffix, eMax = eSuffix };
63 typedef mozilla::BasePrincipal::KeyValT<SerializableKeys> KeyVal;
65 static already_AddRefed<BasePrincipal> FromProperties(
66 nsTArray<ExpandedPrincipal::KeyVal>& aFields);
68 class Deserializer : public BasePrincipal::Deserializer {
69 public:
70 NS_IMETHOD Read(nsIObjectInputStream* aStream) override;
73 protected:
74 explicit ExpandedPrincipal(nsTArray<nsCOMPtr<nsIPrincipal>>&& aPrincipals,
75 const nsACString& aOriginNoSuffix,
76 const mozilla::OriginAttributes& aAttrs);
78 virtual ~ExpandedPrincipal();
80 bool SubsumesInternal(nsIPrincipal* aOther,
81 DocumentDomainConsideration aConsideration) override;
83 bool MayLoadInternal(nsIURI* aURI) override;
85 private:
86 const nsTArray<nsCOMPtr<nsIPrincipal>> mPrincipals;
87 nsMainThreadPtrHandle<nsIContentSecurityPolicy> mCSP
88 MOZ_GUARDED_BY(mozilla::sMainThreadCapability);
91 #define NS_EXPANDEDPRINCIPAL_CID \
92 { \
93 0xe8ee88b0, 0x5571, 0x4086, { \
94 0xa4, 0x5b, 0x39, 0xa7, 0x16, 0x90, 0x6b, 0xdb \
95 } \
98 #endif // ExpandedPrincipal_h