Source code

Revision control

Other Tools

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/. */
5
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.
10
*/
11
12
#ifndef mozilla_NullPrincipal_h
13
#define mozilla_NullPrincipal_h
14
15
#include "nsIPrincipal.h"
16
#include "nsJSPrincipals.h"
17
#include "nsIScriptSecurityManager.h"
18
#include "nsCOMPtr.h"
19
20
#include "mozilla/BasePrincipal.h"
21
22
class nsIDocShell;
23
class nsIURI;
24
namespace Json {
25
class Value;
26
}
27
28
#define NS_NULLPRINCIPAL_CID \
29
{ \
30
0xbd066e5f, 0x146f, 0x4472, { \
31
0x83, 0x31, 0x7b, 0xfd, 0x05, 0xb1, 0xed, 0x90 \
32
} \
33
}
34
#define NS_NULLPRINCIPAL_CONTRACTID "@mozilla.org/nullprincipal;1"
35
36
#define NS_NULLPRINCIPAL_SCHEME "moz-nullprincipal"
37
38
namespace mozilla {
39
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) {}
46
47
static PrincipalKind Kind() { return eNullPrincipal; }
48
49
NS_DECL_NSISERIALIZABLE
50
51
NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
52
uint32_t GetHashValue() override;
53
NS_IMETHOD GetURI(nsIURI** aURI) 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;
58
59
static already_AddRefed<NullPrincipal> CreateWithInheritedAttributes(
60
nsIPrincipal* aInheritFrom);
61
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);
70
71
static already_AddRefed<NullPrincipal> Create(
72
const OriginAttributes& aOriginAttributes, nsIURI* aURI = nullptr);
73
74
static already_AddRefed<NullPrincipal> CreateWithoutOriginAttributes();
75
76
nsresult Init(const OriginAttributes& aOriginAttributes = OriginAttributes(),
77
nsIURI* aURI = nullptr);
78
79
virtual nsresult GetScriptLocation(nsACString& aStr) override;
80
81
nsresult GetSiteIdentifier(SiteIdentifier& aSite) override {
82
aSite.Init(this);
83
return NS_OK;
84
}
85
86
virtual nsresult PopulateJSONObject(Json::Value& aObject) override;
87
88
// Serializable keys are the valid enum fields the serialization supports
89
enum SerializableKeys { eSpec = 0, eSuffix, eMax = eSuffix };
90
// KeyVal is a lightweight storage that passes
91
// SerializableKeys and values after JSON parsing in the BasePrincipal to
92
// FromProperties
93
struct KeyVal {
94
bool valueWasSerialized;
95
nsCString value;
96
SerializableKeys key;
97
};
98
static already_AddRefed<BasePrincipal> FromProperties(
99
nsTArray<NullPrincipal::KeyVal>& aFields);
100
101
protected:
102
virtual ~NullPrincipal() = default;
103
104
bool SubsumesInternal(nsIPrincipal* aOther,
105
DocumentDomainConsideration aConsideration) override {
106
MOZ_ASSERT(aOther);
107
return FastEquals(aOther);
108
}
109
110
bool MayLoadInternal(nsIURI* aURI) override;
111
112
nsCOMPtr<nsIURI> mURI;
113
114
private:
115
// If aIsFirstParty is true, this NullPrincipal will be initialized base on
116
// the aOriginAttributes with FirstPartyDomain set to an unique value, and
117
// this value is generated from mURI.path, with ".mozilla" appending at the
118
// end.
119
nsresult Init(const OriginAttributes& aOriginAttributes, bool aIsFirstParty);
120
};
121
122
} // namespace mozilla
123
124
#endif // mozilla_NullPrincipal_h