Bug 1840070 [wpt PR 40722] - [@scope] Support @scope nested within style rules, a...
[gecko.git] / caps / NullPrincipal.h
blobcbce27c700dbdda3346d6bfad8e831a1e5038bb5
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 } \
35 #define NS_NULLPRINCIPAL_SCHEME "moz-nullprincipal"
37 namespace mozilla {
39 class NullPrincipal final : public BasePrincipal {
40 public:
41 static PrincipalKind Kind() { return eNullPrincipal; }
43 NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
44 uint32_t GetHashValue() override;
45 NS_IMETHOD GetURI(nsIURI** aURI) override;
46 NS_IMETHOD GetIsOriginPotentiallyTrustworthy(bool* aResult) override;
47 NS_IMETHOD GetDomain(nsIURI** aDomain) override;
48 NS_IMETHOD SetDomain(nsIURI* aDomain) override;
49 NS_IMETHOD GetBaseDomain(nsACString& aBaseDomain) override;
50 NS_IMETHOD GetAddonId(nsAString& aAddonId) override;
51 NS_IMETHOD GetPrecursorPrincipal(nsIPrincipal** aPrecursor) override;
53 // Create a NullPrincipal, inheriting origin attributes from the given
54 // principal.
55 // If aInheritFrom is a content principal, or has a content principal
56 // precursor, it will be used as the precursor for this principal.
57 static already_AddRefed<NullPrincipal> CreateWithInheritedAttributes(
58 nsIPrincipal* aInheritFrom);
60 // Create a new NullPrincipal with the specified OriginAttributes.
62 // If `aNullPrincipalURI` is specified, it must be a NS_NULLPRINCIPAL_SCHEME
63 // URI previously created using `NullPrincipal::CreateURI`, and will be used
64 // as the origin URI for this principal.
65 static already_AddRefed<NullPrincipal> Create(
66 const OriginAttributes& aOriginAttributes,
67 nsIURI* aNullPrincipalURI = nullptr);
69 static already_AddRefed<NullPrincipal> CreateWithoutOriginAttributes();
71 // Generates a new unique `moz-nullprincipal:` URI. If `aPrecursor` is
72 // specified, it will be included in the generated URI as the null principal's
73 // precursor.
75 // The `aPrincipalID` attribute is used to force the creation of a
76 // deterministic NullPrincipal in situations where that is required. Avoid
77 // using this parameter unless absolutely necessary.
78 static already_AddRefed<nsIURI> CreateURI(nsIPrincipal* aPrecursor = nullptr,
79 const nsID* aPrincipalID = nullptr);
81 virtual nsresult GetScriptLocation(nsACString& aStr) override;
83 nsresult GetSiteIdentifier(SiteIdentifier& aSite) override {
84 aSite.Init(this);
85 return NS_OK;
88 virtual nsresult PopulateJSONObject(Json::Value& aObject) override;
90 // Serializable keys are the valid enum fields the serialization supports
91 enum SerializableKeys : uint8_t { eSpec = 0, eSuffix, eMax = eSuffix };
92 typedef mozilla::BasePrincipal::KeyValT<SerializableKeys> KeyVal;
94 static already_AddRefed<BasePrincipal> FromProperties(
95 nsTArray<NullPrincipal::KeyVal>& aFields);
97 class Deserializer : public BasePrincipal::Deserializer {
98 public:
99 NS_IMETHOD Read(nsIObjectInputStream* aStream) override;
102 protected:
103 NullPrincipal(nsIURI* aURI, const nsACString& aOriginNoSuffix,
104 const OriginAttributes& aOriginAttributes);
106 virtual ~NullPrincipal() = default;
108 bool SubsumesInternal(nsIPrincipal* aOther,
109 DocumentDomainConsideration aConsideration) override {
110 MOZ_ASSERT(aOther);
111 return FastEquals(aOther);
114 bool MayLoadInternal(nsIURI* aURI) override;
116 const nsCOMPtr<nsIURI> mURI;
118 private:
119 FRIEND_TEST(NullPrincipalPrecursor, EscapingRoundTrips);
121 static void EscapePrecursorQuery(nsACString& aPrecursorQuery);
122 static void UnescapePrecursorQuery(nsACString& aPrecursorQuery);
125 } // namespace mozilla
127 #endif // mozilla_NullPrincipal_h