Bug 1658791: enable "dom/base/test/test_setting_opener.html" for xorigin iframes...
[gecko.git] / caps / OriginAttributes.h
blobd114477d0bb9a5ba90c139883b0343a48583add8
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_OriginAttributes_h
8 #define mozilla_OriginAttributes_h
10 #include "mozilla/dom/ChromeUtils.h"
11 #include "mozilla/dom/ChromeUtilsBinding.h"
12 #include "mozilla/StaticPrefs_privacy.h"
13 #include "nsIScriptSecurityManager.h"
15 namespace mozilla {
17 class OriginAttributes : public dom::OriginAttributesDictionary {
18 public:
19 OriginAttributes() = default;
21 explicit OriginAttributes(bool aInIsolatedMozBrowser) {
22 mInIsolatedMozBrowser = aInIsolatedMozBrowser;
25 explicit OriginAttributes(const OriginAttributesDictionary& aOther)
26 : OriginAttributesDictionary(aOther) {}
28 void SetFirstPartyDomain(const bool aIsTopLevelDocument, nsIURI* aURI,
29 bool aForced = false);
30 void SetFirstPartyDomain(const bool aIsTopLevelDocument,
31 const nsACString& aDomain);
32 void SetFirstPartyDomain(const bool aIsTopLevelDocument,
33 const nsAString& aDomain, bool aForced = false);
35 void SetPartitionKey(nsIURI* aURI);
36 void SetPartitionKey(const nsACString& aDomain);
37 void SetPartitionKey(const nsAString& aDomain);
39 enum {
40 STRIP_FIRST_PARTY_DOMAIN = 0x01,
41 STRIP_USER_CONTEXT_ID = 0x02,
42 STRIP_PRIVATE_BROWSING_ID = 0x04,
43 STRIP_PARITION_KEY = 0x08,
46 inline void StripAttributes(uint32_t aFlags) {
47 if (aFlags & STRIP_FIRST_PARTY_DOMAIN) {
48 mFirstPartyDomain.Truncate();
51 if (aFlags & STRIP_USER_CONTEXT_ID) {
52 mUserContextId = nsIScriptSecurityManager::DEFAULT_USER_CONTEXT_ID;
55 if (aFlags & STRIP_PRIVATE_BROWSING_ID) {
56 mPrivateBrowsingId =
57 nsIScriptSecurityManager::DEFAULT_PRIVATE_BROWSING_ID;
60 if (aFlags & STRIP_PARITION_KEY) {
61 mPartitionKey.Truncate();
65 bool operator==(const OriginAttributes& aOther) const {
66 return mInIsolatedMozBrowser == aOther.mInIsolatedMozBrowser &&
67 mUserContextId == aOther.mUserContextId &&
68 mPrivateBrowsingId == aOther.mPrivateBrowsingId &&
69 mFirstPartyDomain == aOther.mFirstPartyDomain &&
70 mGeckoViewSessionContextId == aOther.mGeckoViewSessionContextId &&
71 mPartitionKey == aOther.mPartitionKey;
74 bool operator!=(const OriginAttributes& aOther) const {
75 return !(*this == aOther);
78 MOZ_MUST_USE bool EqualsIgnoringFPD(const OriginAttributes& aOther) const {
79 return mInIsolatedMozBrowser == aOther.mInIsolatedMozBrowser &&
80 mUserContextId == aOther.mUserContextId &&
81 mPrivateBrowsingId == aOther.mPrivateBrowsingId &&
82 mGeckoViewSessionContextId == aOther.mGeckoViewSessionContextId;
85 // Serializes/Deserializes non-default values into the suffix format, i.e.
86 // |!key1=value1&key2=value2|. If there are no non-default attributes, this
87 // returns an empty string.
88 void CreateSuffix(nsACString& aStr) const;
90 // Don't use this method for anything else than debugging!
91 void CreateAnonymizedSuffix(nsACString& aStr) const;
93 MOZ_MUST_USE bool PopulateFromSuffix(const nsACString& aStr);
95 // Populates the attributes from a string like
96 // |uri!key1=value1&key2=value2| and returns the uri without the suffix.
97 MOZ_MUST_USE bool PopulateFromOrigin(const nsACString& aOrigin,
98 nsACString& aOriginNoSuffix);
100 // Helper function to match mIsPrivateBrowsing to existing private browsing
101 // flags. Once all other flags are removed, this can be removed too.
102 void SyncAttributesWithPrivateBrowsing(bool aInPrivateBrowsing);
104 // check if "privacy.firstparty.isolate" is enabled.
105 static inline bool IsFirstPartyEnabled() {
106 return StaticPrefs::privacy_firstparty_isolate();
109 static inline bool UseSiteForFirstPartyDomain() {
110 if (IsFirstPartyEnabled()) {
111 return StaticPrefs::privacy_firstparty_isolate_use_site();
113 return StaticPrefs::privacy_dynamic_firstparty_use_site();
116 // check if the access of window.opener across different FPDs is restricted.
117 // We only restrict the access of window.opener when first party isolation
118 // is enabled and "privacy.firstparty.isolate.restrict_opener_access" is on.
119 static inline bool IsRestrictOpenerAccessForFPI() {
120 // We always want to restrict window.opener if first party isolation is
121 // disabled.
122 return !StaticPrefs::privacy_firstparty_isolate() ||
123 StaticPrefs::privacy_firstparty_isolate_restrict_opener_access();
126 // Check whether we block the postMessage across different FPDs when the
127 // targetOrigin is '*'.
128 static inline MOZ_MUST_USE bool IsBlockPostMessageForFPI() {
129 return StaticPrefs::privacy_firstparty_isolate() &&
130 StaticPrefs::privacy_firstparty_isolate_block_post_message();
133 // returns true if the originAttributes suffix has mPrivateBrowsingId value
134 // different than 0.
135 static bool IsPrivateBrowsing(const nsACString& aOrigin);
138 class OriginAttributesPattern : public dom::OriginAttributesPatternDictionary {
139 public:
140 // To convert a JSON string to an OriginAttributesPattern, do the following:
142 // OriginAttributesPattern pattern;
143 // if (!pattern.Init(aJSONString)) {
144 // ... // handle failure.
145 // }
146 OriginAttributesPattern() = default;
148 explicit OriginAttributesPattern(
149 const OriginAttributesPatternDictionary& aOther)
150 : OriginAttributesPatternDictionary(aOther) {}
152 // Performs a match of |aAttrs| against this pattern.
153 bool Matches(const OriginAttributes& aAttrs) const {
154 if (mInIsolatedMozBrowser.WasPassed() &&
155 mInIsolatedMozBrowser.Value() != aAttrs.mInIsolatedMozBrowser) {
156 return false;
159 if (mUserContextId.WasPassed() &&
160 mUserContextId.Value() != aAttrs.mUserContextId) {
161 return false;
164 if (mPrivateBrowsingId.WasPassed() &&
165 mPrivateBrowsingId.Value() != aAttrs.mPrivateBrowsingId) {
166 return false;
169 if (mFirstPartyDomain.WasPassed() &&
170 mFirstPartyDomain.Value() != aAttrs.mFirstPartyDomain) {
171 return false;
174 if (mGeckoViewSessionContextId.WasPassed() &&
175 mGeckoViewSessionContextId.Value() !=
176 aAttrs.mGeckoViewSessionContextId) {
177 return false;
180 if (mPartitionKey.WasPassed() &&
181 mPartitionKey.Value() != aAttrs.mPartitionKey) {
182 return false;
185 return true;
188 bool Overlaps(const OriginAttributesPattern& aOther) const {
189 if (mInIsolatedMozBrowser.WasPassed() &&
190 aOther.mInIsolatedMozBrowser.WasPassed() &&
191 mInIsolatedMozBrowser.Value() != aOther.mInIsolatedMozBrowser.Value()) {
192 return false;
195 if (mUserContextId.WasPassed() && aOther.mUserContextId.WasPassed() &&
196 mUserContextId.Value() != aOther.mUserContextId.Value()) {
197 return false;
200 if (mPrivateBrowsingId.WasPassed() &&
201 aOther.mPrivateBrowsingId.WasPassed() &&
202 mPrivateBrowsingId.Value() != aOther.mPrivateBrowsingId.Value()) {
203 return false;
206 if (mFirstPartyDomain.WasPassed() && aOther.mFirstPartyDomain.WasPassed() &&
207 mFirstPartyDomain.Value() != aOther.mFirstPartyDomain.Value()) {
208 return false;
211 if (mGeckoViewSessionContextId.WasPassed() &&
212 aOther.mGeckoViewSessionContextId.WasPassed() &&
213 mGeckoViewSessionContextId.Value() !=
214 aOther.mGeckoViewSessionContextId.Value()) {
215 return false;
218 if (mPartitionKey.WasPassed() && aOther.mPartitionKey.WasPassed() &&
219 mPartitionKey.Value() != aOther.mPartitionKey.Value()) {
220 return false;
223 return true;
227 } // namespace mozilla
229 #endif /* mozilla_OriginAttributes_h */