Bug 1693315 - Insert "Page Source" before "Extensions for Developers" in the DevTools...
[gecko.git] / caps / NullPrincipal.h
blob8e5a164be80f013bd2680d3a70a4f86e8eacac2d
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"
20 #include "gtest/MozGtestFriend.h"
22 class nsIDocShell;
23 class nsIURI;
24 namespace Json {
25 class Value;
28 #define NS_NULLPRINCIPAL_CID \
29 { \
30 0xbd066e5f, 0x146f, 0x4472, { \
31 0x83, 0x31, 0x7b, 0xfd, 0x05, 0xb1, 0xed, 0x90 \
32 } \
34 #define NS_NULLPRINCIPAL_CONTRACTID "@mozilla.org/nullprincipal;1"
36 #define NS_NULLPRINCIPAL_SCHEME "moz-nullprincipal"
38 namespace mozilla {
40 class NullPrincipal final : public BasePrincipal {
41 public:
42 // This should only be used by deserialization, and the factory constructor.
43 // Other consumers should use the Create and CreateWithInheritedAttributes
44 // methods.
45 NullPrincipal() : BasePrincipal(eNullPrincipal) {}
47 static PrincipalKind Kind() { return eNullPrincipal; }
49 NS_DECL_NSISERIALIZABLE
51 NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
52 uint32_t GetHashValue() override;
53 NS_IMETHOD GetURI(nsIURI** aURI) override;
54 NS_IMETHOD GetIsOriginPotentiallyTrustworthy(bool* aResult) override;
55 NS_IMETHOD GetDomain(nsIURI** aDomain) override;
56 NS_IMETHOD SetDomain(nsIURI* aDomain) override;
57 NS_IMETHOD GetBaseDomain(nsACString& aBaseDomain) override;
58 NS_IMETHOD GetAddonId(nsAString& aAddonId) override;
60 static already_AddRefed<NullPrincipal> CreateWithInheritedAttributes(
61 nsIPrincipal* aInheritFrom);
63 // Create NullPrincipal with origin attributes from docshell.
64 // If aIsFirstParty is true, and the pref 'privacy.firstparty.isolate' is also
65 // enabled, the mFirstPartyDomain value of the origin attributes will be set
66 // to an unique value.
67 static already_AddRefed<NullPrincipal> CreateWithInheritedAttributes(
68 nsIDocShell* aDocShell, bool aIsFirstParty = false);
69 static already_AddRefed<NullPrincipal> CreateWithInheritedAttributes(
70 const OriginAttributes& aOriginAttributes, bool aIsFirstParty = false);
72 static already_AddRefed<NullPrincipal> Create(
73 const OriginAttributes& aOriginAttributes, nsIURI* aURI = nullptr);
75 static already_AddRefed<NullPrincipal> CreateWithoutOriginAttributes();
77 nsresult Init(const OriginAttributes& aOriginAttributes = OriginAttributes(),
78 nsIURI* aURI = nullptr);
80 virtual nsresult GetScriptLocation(nsACString& aStr) override;
82 nsresult GetSiteIdentifier(SiteIdentifier& aSite) override {
83 aSite.Init(this);
84 return NS_OK;
87 virtual nsresult PopulateJSONObject(Json::Value& aObject) override;
89 // Serializable keys are the valid enum fields the serialization supports
90 enum SerializableKeys : uint8_t { eSpec = 0, eSuffix, eMax = eSuffix };
91 typedef mozilla::BasePrincipal::KeyValT<SerializableKeys> KeyVal;
93 static already_AddRefed<BasePrincipal> FromProperties(
94 nsTArray<NullPrincipal::KeyVal>& aFields);
96 protected:
97 virtual ~NullPrincipal() = default;
99 bool SubsumesInternal(nsIPrincipal* aOther,
100 DocumentDomainConsideration aConsideration) override {
101 MOZ_ASSERT(aOther);
102 return FastEquals(aOther);
105 bool MayLoadInternal(nsIURI* aURI) override;
107 nsCOMPtr<nsIURI> mURI;
109 private:
110 FRIEND_TEST(OriginAttributes, NullPrincipal);
112 // If aIsFirstParty is true, this NullPrincipal will be initialized based on
113 // the aOriginAttributes with FirstPartyDomain set to a unique value.
114 // This value is generated from mURI.path, with ".mozilla" appended at the
115 // end. aURI is used for testing purpose to assign specific UUID rather than
116 // random generated one.
117 nsresult Init(const OriginAttributes& aOriginAttributes, bool aIsFirstParty,
118 nsIURI* aURI = nullptr);
121 } // namespace mozilla
123 #endif // mozilla_NullPrincipal_h