Bug 1638135 [wpt PR 23616] - Move mouse-pointer-updown-events.html to wpt, a=testonly
[gecko.git] / caps / NullPrincipal.h
blob2019b3e1d54aedcfe149e343da0a88a5cb5a0545
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 /**
7 * This is the principal that has no rights and can't be accessed by
8 * anything other than itself and chrome; null principals are not
9 * same-origin with anything but themselves.
12 #ifndef mozilla_NullPrincipal_h
13 #define mozilla_NullPrincipal_h
15 #include "nsIPrincipal.h"
16 #include "nsJSPrincipals.h"
17 #include "nsCOMPtr.h"
19 #include "mozilla/BasePrincipal.h"
21 class nsIDocShell;
22 class nsIURI;
23 namespace Json {
24 class Value;
27 #define NS_NULLPRINCIPAL_CID \
28 { \
29 0xbd066e5f, 0x146f, 0x4472, { \
30 0x83, 0x31, 0x7b, 0xfd, 0x05, 0xb1, 0xed, 0x90 \
31 } \
33 #define NS_NULLPRINCIPAL_CONTRACTID "@mozilla.org/nullprincipal;1"
35 #define NS_NULLPRINCIPAL_SCHEME "moz-nullprincipal"
37 namespace mozilla {
39 class NullPrincipal final : public BasePrincipal {
40 public:
41 // This should only be used by deserialization, and the factory constructor.
42 // Other consumers should use the Create and CreateWithInheritedAttributes
43 // methods.
44 NullPrincipal() : BasePrincipal(eNullPrincipal) {}
46 static PrincipalKind Kind() { return eNullPrincipal; }
48 NS_DECL_NSISERIALIZABLE
50 NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
51 uint32_t GetHashValue() override;
52 NS_IMETHOD GetURI(nsIURI** aURI) override;
53 NS_IMETHOD GetIsOriginPotentiallyTrustworthy(bool* aResult) override;
54 NS_IMETHOD GetDomain(nsIURI** aDomain) override;
55 NS_IMETHOD SetDomain(nsIURI* aDomain) override;
56 NS_IMETHOD GetBaseDomain(nsACString& aBaseDomain) override;
57 NS_IMETHOD GetAddonId(nsAString& aAddonId) override;
59 static already_AddRefed<NullPrincipal> CreateWithInheritedAttributes(
60 nsIPrincipal* aInheritFrom);
62 // Create NullPrincipal with origin attributes from docshell.
63 // If aIsFirstParty is true, and the pref 'privacy.firstparty.isolate' is also
64 // enabled, the mFirstPartyDomain value of the origin attributes will be set
65 // to an unique value.
66 static already_AddRefed<NullPrincipal> CreateWithInheritedAttributes(
67 nsIDocShell* aDocShell, bool aIsFirstParty = false);
68 static already_AddRefed<NullPrincipal> CreateWithInheritedAttributes(
69 const OriginAttributes& aOriginAttributes, bool aIsFirstParty = false);
71 static already_AddRefed<NullPrincipal> Create(
72 const OriginAttributes& aOriginAttributes, nsIURI* aURI = nullptr);
74 static already_AddRefed<NullPrincipal> CreateWithoutOriginAttributes();
76 nsresult Init(const OriginAttributes& aOriginAttributes = OriginAttributes(),
77 nsIURI* aURI = nullptr);
79 virtual nsresult GetScriptLocation(nsACString& aStr) override;
81 nsresult GetSiteIdentifier(SiteIdentifier& aSite) override {
82 aSite.Init(this);
83 return NS_OK;
86 virtual nsresult PopulateJSONObject(Json::Value& aObject) override;
88 // Serializable keys are the valid enum fields the serialization supports
89 enum SerializableKeys : uint8_t { eSpec = 0, eSuffix, eMax = eSuffix };
90 typedef mozilla::BasePrincipal::KeyValT<SerializableKeys> KeyVal;
92 static already_AddRefed<BasePrincipal> FromProperties(
93 nsTArray<NullPrincipal::KeyVal>& aFields);
95 protected:
96 virtual ~NullPrincipal() = default;
98 bool SubsumesInternal(nsIPrincipal* aOther,
99 DocumentDomainConsideration aConsideration) override {
100 MOZ_ASSERT(aOther);
101 return FastEquals(aOther);
104 bool MayLoadInternal(nsIURI* aURI) override;
106 nsCOMPtr<nsIURI> mURI;
108 private:
109 // If aIsFirstParty is true, this NullPrincipal will be initialized based on
110 // the aOriginAttributes with FirstPartyDomain set to a unique value.
111 // This value is generated from mURI.path, with ".mozilla" appended at the
112 // end.
113 void Init(const OriginAttributes& aOriginAttributes, bool aIsFirstParty);
116 } // namespace mozilla
118 #endif // mozilla_NullPrincipal_h