Line data Source code
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 NullPrincipal_h
13 : #define NullPrincipal_h
14 :
15 : #include "nsIPrincipal.h"
16 : #include "nsJSPrincipals.h"
17 : #include "nsIScriptSecurityManager.h"
18 : #include "nsCOMPtr.h"
19 : #include "nsIContentSecurityPolicy.h"
20 :
21 : #include "mozilla/BasePrincipal.h"
22 :
23 : class nsIDocShell;
24 : class nsIURI;
25 :
26 : #define NS_NULLPRINCIPAL_CID \
27 : { 0xbd066e5f, 0x146f, 0x4472, \
28 : { 0x83, 0x31, 0x7b, 0xfd, 0x05, 0xb1, 0xed, 0x90 } }
29 : #define NS_NULLPRINCIPAL_CONTRACTID "@mozilla.org/nullprincipal;1"
30 :
31 : #define NS_NULLPRINCIPAL_SCHEME "moz-nullprincipal"
32 :
33 : class NullPrincipal final : public mozilla::BasePrincipal
34 : {
35 : public:
36 : // This should only be used by deserialization, and the factory constructor.
37 : // Other consumers should use the Create and CreateWithInheritedAttributes
38 : // methods.
39 141 : NullPrincipal()
40 141 : : BasePrincipal(eNullPrincipal)
41 : {
42 141 : }
43 :
44 : NS_DECL_NSISERIALIZABLE
45 :
46 : NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
47 : NS_IMETHOD GetHashValue(uint32_t* aHashValue) override;
48 : NS_IMETHOD SetCsp(nsIContentSecurityPolicy* aCsp) override;
49 : NS_IMETHOD GetURI(nsIURI** aURI) override;
50 : NS_IMETHOD GetDomain(nsIURI** aDomain) override;
51 : NS_IMETHOD SetDomain(nsIURI* aDomain) override;
52 : NS_IMETHOD GetBaseDomain(nsACString& aBaseDomain) override;
53 : NS_IMETHOD GetAddonId(nsAString& aAddonId) override;
54 :
55 : static already_AddRefed<NullPrincipal> CreateWithInheritedAttributes(nsIPrincipal* aInheritFrom);
56 :
57 : // Create NullPrincipal with origin attributes from docshell.
58 : // If aIsFirstParty is true, and the pref 'privacy.firstparty.isolate' is also
59 : // enabled, the mFirstPartyDomain value of the origin attributes will be set
60 : // to an unique value.
61 : static already_AddRefed<NullPrincipal>
62 : CreateWithInheritedAttributes(nsIDocShell* aDocShell, bool aIsFirstParty = false);
63 :
64 : static already_AddRefed<NullPrincipal>
65 : Create(const mozilla::OriginAttributes& aOriginAttributes = mozilla::OriginAttributes(),
66 : nsIURI* aURI = nullptr);
67 :
68 : nsresult Init(const mozilla::OriginAttributes& aOriginAttributes = mozilla::OriginAttributes(),
69 : nsIURI* aURI = nullptr);
70 :
71 : virtual nsresult GetScriptLocation(nsACString &aStr) override;
72 :
73 : protected:
74 192 : virtual ~NullPrincipal() = default;
75 :
76 1169 : bool SubsumesInternal(nsIPrincipal* aOther, DocumentDomainConsideration aConsideration) override
77 : {
78 1169 : return aOther == this;
79 : }
80 :
81 : bool MayLoadInternal(nsIURI* aURI) override;
82 :
83 : nsCOMPtr<nsIURI> mURI;
84 :
85 : private:
86 : // If aIsFirstParty is true, this NullPrincipal will be initialized base on
87 : // the aOriginAttributes with FirstPartyDomain set to an unique value, and this
88 : // value is generated from mURI.path, with ".mozilla" appending at the end.
89 : nsresult Init(const mozilla::OriginAttributes& aOriginAttributes, bool aIsFirstParty);
90 : };
91 :
92 : #endif // NullPrincipal_h__
|