Bug 1824753 [wpt PR 39216] - [FLEDGE] Add WPT test that FLEDGE is not allowed in...
[gecko.git] / dom / ipc / WindowGlobalParent.h
blobde96207d0cdebd66d1cf5715019c3c412eadb078
1 /* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 8 -*- */
2 /* vim: set sw=2 ts=8 et tw=80 ft=cpp : */
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_dom_WindowGlobalParent_h
8 #define mozilla_dom_WindowGlobalParent_h
10 #include "mozilla/ContentBlockingLog.h"
11 #include "mozilla/ContentBlockingNotifier.h"
12 #include "mozilla/Maybe.h"
13 #include "mozilla/RefPtr.h"
14 #include "mozilla/UniquePtr.h"
15 #include "mozilla/dom/ClientInfo.h"
16 #include "mozilla/dom/ClientIPCTypes.h"
17 #include "mozilla/dom/DOMRect.h"
18 #include "mozilla/dom/PWindowGlobalParent.h"
19 #include "mozilla/dom/WindowContext.h"
20 #include "mozilla/dom/WindowGlobalActorsBinding.h"
21 #include "nsTHashMap.h"
22 #include "nsRefPtrHashtable.h"
23 #include "nsWrapperCache.h"
24 #include "nsISupports.h"
25 #include "nsIDOMProcessParent.h"
26 #include "mozilla/dom/WindowGlobalActor.h"
27 #include "mozilla/dom/CanonicalBrowsingContext.h"
28 #include "mozilla/net/CookieJarSettings.h"
30 class nsIPrincipal;
31 class nsIURI;
32 class nsFrameLoader;
34 namespace mozilla {
36 namespace gfx {
37 class CrossProcessPaint;
38 } // namespace gfx
40 namespace dom {
42 class BrowserParent;
43 class WindowGlobalChild;
44 class JSWindowActorParent;
45 class JSActorMessageMeta;
46 struct PageUseCounters;
47 class WindowSessionStoreState;
48 struct WindowSessionStoreUpdate;
49 class SSCacheQueryResult;
51 /**
52 * A handle in the parent process to a specific nsGlobalWindowInner object.
54 class WindowGlobalParent final : public WindowContext,
55 public WindowGlobalActor,
56 public PWindowGlobalParent {
57 friend class gfx::CrossProcessPaint;
58 friend class PWindowGlobalParent;
60 public:
61 NS_DECL_ISUPPORTS_INHERITED
62 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(WindowGlobalParent,
63 WindowContext)
65 static already_AddRefed<WindowGlobalParent> GetByInnerWindowId(
66 uint64_t aInnerWindowId);
68 static already_AddRefed<WindowGlobalParent> GetByInnerWindowId(
69 const GlobalObject& aGlobal, uint64_t aInnerWindowId) {
70 return GetByInnerWindowId(aInnerWindowId);
73 // The same as the corresponding methods on `WindowContext`, except that the
74 // return types are already cast to their parent-process type variants, such
75 // as `WindowGlobalParent` or `CanonicalBrowsingContext`.
76 WindowGlobalParent* GetParentWindowContext() {
77 return static_cast<WindowGlobalParent*>(
78 WindowContext::GetParentWindowContext());
80 WindowGlobalParent* TopWindowContext() {
81 return static_cast<WindowGlobalParent*>(WindowContext::TopWindowContext());
83 CanonicalBrowsingContext* GetBrowsingContext() const {
84 return CanonicalBrowsingContext::Cast(WindowContext::GetBrowsingContext());
87 Element* GetRootOwnerElement();
89 // Has this actor been shut down
90 bool IsClosed() { return !CanSend(); }
92 // Get the other side of this actor if it is an in-process actor. Returns
93 // |nullptr| if the actor has been torn down, or is not in-process.
94 already_AddRefed<WindowGlobalChild> GetChildActor();
96 // Get a JS actor object by name.
97 already_AddRefed<JSWindowActorParent> GetActor(JSContext* aCx,
98 const nsACString& aName,
99 ErrorResult& aRv);
100 already_AddRefed<JSWindowActorParent> GetExistingActor(
101 const nsACString& aName);
103 // Get this actor's manager if it is not an in-process actor. Returns
104 // |nullptr| if the actor has been torn down, or is in-process.
105 BrowserParent* GetBrowserParent();
107 ContentParent* GetContentParent();
109 // The principal of this WindowGlobal. This value will not change over the
110 // lifetime of the WindowGlobal object, even to reflect changes in
111 // |document.domain|.
112 nsIPrincipal* DocumentPrincipal() { return mDocumentPrincipal; }
114 nsIPrincipal* DocumentStoragePrincipal() { return mDocumentStoragePrincipal; }
116 // The BrowsingContext which this WindowGlobal has been loaded into.
117 // FIXME: It's quite awkward that this method has a slightly different name
118 // than the one on WindowContext.
119 CanonicalBrowsingContext* BrowsingContext() override {
120 return GetBrowsingContext();
123 // Get the root nsFrameLoader object for the tree of BrowsingContext nodes
124 // which this WindowGlobal is a part of. This will be the nsFrameLoader
125 // holding the BrowserParent for remote tabs, and the root content frameloader
126 // for non-remote tabs.
127 already_AddRefed<nsFrameLoader> GetRootFrameLoader();
129 // The current URI which loaded in the document.
130 nsIURI* GetDocumentURI() override { return mDocumentURI; }
132 void GetDocumentTitle(nsAString& aTitle) const {
133 aTitle = mDocumentTitle.valueOr(nsString());
136 nsIPrincipal* GetContentBlockingAllowListPrincipal() const {
137 return mDocContentBlockingAllowListPrincipal;
140 Maybe<ClientInfo> GetClientInfo() { return mClientInfo; }
142 uint64_t ContentParentId();
144 int32_t OsPid();
146 bool IsCurrentGlobal();
148 bool IsProcessRoot();
150 uint32_t ContentBlockingEvents();
152 void GetContentBlockingLog(nsAString& aLog);
154 bool IsInitialDocument() { return mIsInitialDocument; }
156 already_AddRefed<mozilla::dom::Promise> PermitUnload(
157 PermitUnloadAction aAction, uint32_t aTimeout, mozilla::ErrorResult& aRv);
159 void PermitUnload(std::function<void(bool)>&& aResolver);
161 already_AddRefed<mozilla::dom::Promise> DrawSnapshot(
162 const DOMRect* aRect, double aScale, const nsACString& aBackgroundColor,
163 bool aResetScrollPosition, mozilla::ErrorResult& aRv);
165 static already_AddRefed<WindowGlobalParent> CreateDisconnected(
166 const WindowGlobalInit& aInit);
168 // Initialize the mFrameLoader fields for a created WindowGlobalParent. Must
169 // be called after setting the Manager actor.
170 void Init() final;
172 nsIGlobalObject* GetParentObject();
173 JSObject* WrapObject(JSContext* aCx,
174 JS::Handle<JSObject*> aGivenProto) override;
176 void NotifyContentBlockingEvent(
177 uint32_t aEvent, nsIRequest* aRequest, bool aBlocked,
178 const nsACString& aTrackingOrigin,
179 const nsTArray<nsCString>& aTrackingFullHashes,
180 const Maybe<
181 ContentBlockingNotifier::StorageAccessPermissionGrantedReason>&
182 aReason = Nothing());
184 ContentBlockingLog* GetContentBlockingLog() { return &mContentBlockingLog; }
186 nsIDOMProcessParent* GetDomProcess();
188 nsICookieJarSettings* CookieJarSettings() { return mCookieJarSettings; }
190 nsICookieJarSettings* GetCookieJarSettings() const {
191 return mCookieJarSettings;
194 bool DocumentHasLoaded() { return mDocumentHasLoaded; }
196 bool DocumentHasUserInteracted() { return mDocumentHasUserInteracted; }
198 uint32_t SandboxFlags() { return mSandboxFlags; }
200 bool GetDocumentBlockAllMixedContent() { return mBlockAllMixedContent; }
202 bool GetDocumentUpgradeInsecureRequests() { return mUpgradeInsecureRequests; }
204 void DidBecomeCurrentWindowGlobal(bool aCurrent);
206 uint32_t HttpsOnlyStatus() { return mHttpsOnlyStatus; }
208 void AddSecurityState(uint32_t aStateFlags);
209 uint32_t GetSecurityFlags() { return mSecurityState; }
211 nsITransportSecurityInfo* GetSecurityInfo() { return mSecurityInfo; }
213 const nsACString& GetRemoteType() override;
215 void NotifySessionStoreUpdatesComplete(Element* aEmbedder);
217 Maybe<uint64_t> GetSingleChannelId() { return mSingleChannelId; }
219 uint32_t GetBFCacheStatus() { return mBFCacheStatus; }
221 bool HasActivePeerConnections();
223 bool Fullscreen() { return mFullscreen; }
224 void SetFullscreen(bool aFullscreen) { mFullscreen = aFullscreen; }
226 void ExitTopChromeDocumentFullscreen();
228 void SetShouldReportHasBlockedOpaqueResponse(
229 nsContentPolicyType aContentPolicy);
231 protected:
232 already_AddRefed<JSActor> InitJSActor(JS::Handle<JSObject*> aMaybeActor,
233 const nsACString& aName,
234 ErrorResult& aRv) override;
235 mozilla::ipc::IProtocol* AsNativeActor() override { return this; }
237 // IPC messages
238 mozilla::ipc::IPCResult RecvLoadURI(
239 const MaybeDiscarded<dom::BrowsingContext>& aTargetBC,
240 nsDocShellLoadState* aLoadState, bool aSetNavigating);
241 mozilla::ipc::IPCResult RecvInternalLoad(nsDocShellLoadState* aLoadState);
242 mozilla::ipc::IPCResult RecvUpdateDocumentURI(nsIURI* aURI);
243 mozilla::ipc::IPCResult RecvUpdateDocumentPrincipal(
244 nsIPrincipal* aNewDocumentPrincipal,
245 nsIPrincipal* aNewDocumentStoragePrincipal);
246 mozilla::ipc::IPCResult RecvUpdateDocumentHasLoaded(bool aDocumentHasLoaded);
247 mozilla::ipc::IPCResult RecvUpdateDocumentHasUserInteracted(
248 bool aDocumentHasUserInteracted);
249 mozilla::ipc::IPCResult RecvUpdateSandboxFlags(uint32_t aSandboxFlags);
250 mozilla::ipc::IPCResult RecvUpdateDocumentCspSettings(
251 bool aBlockAllMixedContent, bool aUpgradeInsecureRequests);
252 mozilla::ipc::IPCResult RecvUpdateDocumentTitle(const nsString& aTitle);
253 mozilla::ipc::IPCResult RecvUpdateHttpsOnlyStatus(uint32_t aHttpsOnlyStatus);
254 mozilla::ipc::IPCResult RecvSetIsInitialDocument(bool aIsInitialDocument) {
255 mIsInitialDocument = aIsInitialDocument;
256 return IPC_OK();
258 mozilla::ipc::IPCResult RecvUpdateDocumentSecurityInfo(
259 nsITransportSecurityInfo* aSecurityInfo);
260 mozilla::ipc::IPCResult RecvSetClientInfo(
261 const IPCClientInfo& aIPCClientInfo);
262 mozilla::ipc::IPCResult RecvDestroy();
263 mozilla::ipc::IPCResult RecvRawMessage(
264 const JSActorMessageMeta& aMeta, const Maybe<ClonedMessageData>& aData,
265 const Maybe<ClonedMessageData>& aStack);
267 mozilla::ipc::IPCResult RecvGetContentBlockingEvents(
268 GetContentBlockingEventsResolver&& aResolver);
269 mozilla::ipc::IPCResult RecvUpdateCookieJarSettings(
270 const CookieJarSettingsArgs& aCookieJarSettingsArgs);
272 void ActorDestroy(ActorDestroyReason aWhy) override;
274 void DrawSnapshotInternal(gfx::CrossProcessPaint* aPaint,
275 const Maybe<IntRect>& aRect, float aScale,
276 nscolor aBackgroundColor, uint32_t aFlags);
278 // WebShare API - try to share
279 mozilla::ipc::IPCResult RecvShare(IPCWebShareData&& aData,
280 ShareResolver&& aResolver);
282 mozilla::ipc::IPCResult RecvCheckPermitUnload(
283 bool aHasInProcessBlocker, XPCOMPermitUnloadAction aAction,
284 CheckPermitUnloadResolver&& aResolver);
286 mozilla::ipc::IPCResult RecvExpectPageUseCounters(
287 const MaybeDiscarded<dom::WindowContext>& aTop);
288 mozilla::ipc::IPCResult RecvAccumulatePageUseCounters(
289 const UseCounters& aUseCounters);
291 mozilla::ipc::IPCResult RecvRequestRestoreTabContent();
293 mozilla::ipc::IPCResult RecvUpdateBFCacheStatus(const uint32_t& aOnFlags,
294 const uint32_t& aOffFlags);
296 // This IPC method is to notify the parent process that the caller process
297 // creates the first active peer connection (aIsAdded = true) or closes the
298 // last active peer connection (aIsAdded = false).
299 mozilla::ipc::IPCResult RecvUpdateActivePeerConnectionStatus(bool aIsAdded);
301 public:
302 mozilla::ipc::IPCResult RecvSetSingleChannelId(
303 const Maybe<uint64_t>& aSingleChannelId);
305 mozilla::ipc::IPCResult RecvSetDocumentDomain(nsIURI* aDomain);
307 mozilla::ipc::IPCResult RecvReloadWithHttpsOnlyException();
309 mozilla::ipc::IPCResult RecvDiscoverIdentityCredentialFromExternalSource(
310 const IdentityCredentialRequestOptions& aOptions,
311 const DiscoverIdentityCredentialFromExternalSourceResolver& aResolver);
313 private:
314 WindowGlobalParent(CanonicalBrowsingContext* aBrowsingContext,
315 uint64_t aInnerWindowId, uint64_t aOuterWindowId,
316 FieldValues&& aInit);
318 ~WindowGlobalParent();
320 bool ShouldTrackSiteOriginTelemetry();
321 void FinishAccumulatingPageUseCounters();
323 // Returns failure if the new storage principal cannot be validated
324 // against the current document principle.
325 nsresult SetDocumentStoragePrincipal(
326 nsIPrincipal* aNewDocumentStoragePrincipal);
328 // NOTE: Neither this document principal nor the document storage
329 // principal doesn't reflect possible |document.domain| mutations
330 // which may have been made in the actual document.
331 nsCOMPtr<nsIPrincipal> mDocumentPrincipal;
332 nsCOMPtr<nsIPrincipal> mDocumentStoragePrincipal;
334 // The principal to use for the content blocking allow list.
335 nsCOMPtr<nsIPrincipal> mDocContentBlockingAllowListPrincipal;
337 nsCOMPtr<nsIURI> mDocumentURI;
338 Maybe<nsString> mDocumentTitle;
340 bool mIsInitialDocument;
342 // True if this window has a "beforeunload" event listener.
343 bool mHasBeforeUnload;
345 // The log of all content blocking actions taken on the document related to
346 // this WindowGlobalParent. This is only stored on top-level documents and
347 // includes the activity log for all of the nested subdocuments as well.
348 ContentBlockingLog mContentBlockingLog;
350 uint32_t mSecurityState = 0;
352 Maybe<ClientInfo> mClientInfo;
353 // Fields being mirrored from the corresponding document
354 nsCOMPtr<nsICookieJarSettings> mCookieJarSettings;
355 nsCOMPtr<nsITransportSecurityInfo> mSecurityInfo;
357 uint32_t mSandboxFlags;
359 struct OriginCounter {
360 void UpdateSiteOriginsFrom(WindowGlobalParent* aParent, bool aIncrease);
361 void Accumulate();
363 nsTHashMap<nsCStringHashKey, int32_t> mOriginMap;
364 uint32_t mMaxOrigins = 0;
367 // Used to collect unique site origin telemetry.
369 // Is only Some() on top-level content windows.
370 Maybe<OriginCounter> mOriginCounter;
372 bool mDocumentHasLoaded;
373 bool mDocumentHasUserInteracted;
374 bool mDocumentTreeWouldPreloadResources = false;
375 bool mBlockAllMixedContent;
376 bool mUpgradeInsecureRequests;
378 // HTTPS-Only Mode flags
379 uint32_t mHttpsOnlyStatus;
381 // The window of the document whose page use counters our document's use
382 // counters will contribute to. (If we are a top-level document, this
383 // will point to ourselves.)
384 RefPtr<WindowGlobalParent> mPageUseCountersWindow;
386 // Our page use counters, if we are a top-level document.
387 UniquePtr<PageUseCounters> mPageUseCounters;
389 // Whether we have sent our page use counters, and so should ignore any
390 // subsequent ExpectPageUseCounters calls.
391 bool mSentPageUseCounters = false;
393 uint32_t mBFCacheStatus = 0;
395 // If this WindowGlobalParent is a top window, this field indicates how many
396 // child processes have active peer connections for this window and its
397 // descendants.
398 uint32_t mNumOfProcessesWithActivePeerConnections = 0;
400 // mSingleChannelId records whether the loadgroup contains a single request
401 // with an id. If there is one channel in the loadgroup and it has an id then
402 // mSingleChannelId is set to Some(id) (ids are non-zero). If there is one
403 // request in the loadgroup and it's not a channel or it doesn't have an id,
404 // or there are multiple requests in the loadgroup, then mSingleChannelId is
405 // set to Some(0). If there are no requests in the loadgroup then
406 // mSingleChannelId is set to Nothing().
407 // Note: We ignore favicon loads when considering the requests in the
408 // loadgroup.
409 Maybe<uint64_t> mSingleChannelId;
411 // True if the current loaded document is in fullscreen.
412 bool mFullscreen = false;
414 bool mShouldReportHasBlockedOpaqueResponse = false;
417 } // namespace dom
418 } // namespace mozilla
420 inline nsISupports* ToSupports(
421 mozilla::dom::WindowGlobalParent* aWindowGlobal) {
422 return static_cast<mozilla::dom::WindowContext*>(aWindowGlobal);
425 #endif // !defined(mozilla_dom_WindowGlobalParent_h)