Bug 1885602 - Part 5: Implement navigating to the SUMO help topic from the menu heade...
[gecko.git] / dom / ipc / WindowGlobalParent.h
blobbe801de0d3b847222c8b7e925a8bb9544fd5ca5a
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 IsActiveInTab();
150 bool IsProcessRoot();
152 uint32_t ContentBlockingEvents();
154 void GetContentBlockingLog(nsAString& aLog);
156 bool IsInitialDocument() {
157 return mIsInitialDocument.isSome() && mIsInitialDocument.value();
160 already_AddRefed<mozilla::dom::Promise> PermitUnload(
161 PermitUnloadAction aAction, uint32_t aTimeout, mozilla::ErrorResult& aRv);
163 void PermitUnload(std::function<void(bool)>&& aResolver);
165 already_AddRefed<mozilla::dom::Promise> DrawSnapshot(
166 const DOMRect* aRect, double aScale, const nsACString& aBackgroundColor,
167 bool aResetScrollPosition, mozilla::ErrorResult& aRv);
169 static already_AddRefed<WindowGlobalParent> CreateDisconnected(
170 const WindowGlobalInit& aInit);
172 // Initialize the mFrameLoader fields for a created WindowGlobalParent. Must
173 // be called after setting the Manager actor.
174 void Init() final;
176 nsIGlobalObject* GetParentObject();
177 JSObject* WrapObject(JSContext* aCx,
178 JS::Handle<JSObject*> aGivenProto) override;
180 void NotifyContentBlockingEvent(
181 uint32_t aEvent, nsIRequest* aRequest, bool aBlocked,
182 const nsACString& aTrackingOrigin,
183 const nsTArray<nsCString>& aTrackingFullHashes,
184 const Maybe<
185 ContentBlockingNotifier::StorageAccessPermissionGrantedReason>&
186 aReason,
187 const Maybe<ContentBlockingNotifier::CanvasFingerprinter>&
188 aCanvasFingerprinter,
189 const Maybe<bool> aCanvasFingerprinterKnownText);
191 ContentBlockingLog* GetContentBlockingLog() { return &mContentBlockingLog; }
193 nsIDOMProcessParent* GetDomProcess();
195 nsICookieJarSettings* CookieJarSettings() { return mCookieJarSettings; }
197 nsICookieJarSettings* GetCookieJarSettings() const {
198 return mCookieJarSettings;
201 bool DocumentHasLoaded() { return mDocumentHasLoaded; }
203 bool DocumentHasUserInteracted() { return mDocumentHasUserInteracted; }
205 uint32_t SandboxFlags() { return mSandboxFlags; }
207 bool GetDocumentBlockAllMixedContent() { return mBlockAllMixedContent; }
209 bool GetDocumentUpgradeInsecureRequests() { return mUpgradeInsecureRequests; }
211 void DidBecomeCurrentWindowGlobal(bool aCurrent);
213 uint32_t HttpsOnlyStatus() { return mHttpsOnlyStatus; }
215 void AddSecurityState(uint32_t aStateFlags);
216 uint32_t GetSecurityFlags() { return mSecurityState; }
218 nsITransportSecurityInfo* GetSecurityInfo() { return mSecurityInfo; }
220 const nsACString& GetRemoteType() override;
222 void NotifySessionStoreUpdatesComplete(Element* aEmbedder);
224 Maybe<uint64_t> GetSingleChannelId() { return mSingleChannelId; }
226 uint32_t GetBFCacheStatus() { return mBFCacheStatus; }
228 bool HasActivePeerConnections();
230 bool Fullscreen() { return mFullscreen; }
231 void SetFullscreen(bool aFullscreen) { mFullscreen = aFullscreen; }
233 void ExitTopChromeDocumentFullscreen();
235 void SetShouldReportHasBlockedOpaqueResponse(
236 nsContentPolicyType aContentPolicy);
238 protected:
239 already_AddRefed<JSActor> InitJSActor(JS::Handle<JSObject*> aMaybeActor,
240 const nsACString& aName,
241 ErrorResult& aRv) override;
242 mozilla::ipc::IProtocol* AsNativeActor() override { return this; }
244 // IPC messages
245 mozilla::ipc::IPCResult RecvLoadURI(
246 const MaybeDiscarded<dom::BrowsingContext>& aTargetBC,
247 nsDocShellLoadState* aLoadState, bool aSetNavigating);
248 mozilla::ipc::IPCResult RecvInternalLoad(nsDocShellLoadState* aLoadState);
249 mozilla::ipc::IPCResult RecvUpdateDocumentURI(NotNull<nsIURI*> aURI);
250 mozilla::ipc::IPCResult RecvUpdateDocumentPrincipal(
251 nsIPrincipal* aNewDocumentPrincipal,
252 nsIPrincipal* aNewDocumentStoragePrincipal);
253 mozilla::ipc::IPCResult RecvUpdateDocumentHasLoaded(bool aDocumentHasLoaded);
254 mozilla::ipc::IPCResult RecvUpdateDocumentHasUserInteracted(
255 bool aDocumentHasUserInteracted);
256 mozilla::ipc::IPCResult RecvUpdateSandboxFlags(uint32_t aSandboxFlags);
257 mozilla::ipc::IPCResult RecvUpdateDocumentCspSettings(
258 bool aBlockAllMixedContent, bool aUpgradeInsecureRequests);
259 mozilla::ipc::IPCResult RecvUpdateDocumentTitle(const nsString& aTitle);
260 mozilla::ipc::IPCResult RecvUpdateHttpsOnlyStatus(uint32_t aHttpsOnlyStatus);
261 mozilla::ipc::IPCResult RecvSetIsInitialDocument(bool aIsInitialDocument) {
262 if (aIsInitialDocument && mIsInitialDocument.isSome() &&
263 (mIsInitialDocument.value() != aIsInitialDocument)) {
264 return IPC_FAIL_NO_REASON(this);
267 mIsInitialDocument = Some(aIsInitialDocument);
268 return IPC_OK();
270 mozilla::ipc::IPCResult RecvUpdateDocumentSecurityInfo(
271 nsITransportSecurityInfo* aSecurityInfo);
272 mozilla::ipc::IPCResult RecvSetClientInfo(
273 const IPCClientInfo& aIPCClientInfo);
274 mozilla::ipc::IPCResult RecvDestroy();
275 mozilla::ipc::IPCResult RecvRawMessage(
276 const JSActorMessageMeta& aMeta, const Maybe<ClonedMessageData>& aData,
277 const Maybe<ClonedMessageData>& aStack);
279 mozilla::ipc::IPCResult RecvGetContentBlockingEvents(
280 GetContentBlockingEventsResolver&& aResolver);
281 mozilla::ipc::IPCResult RecvUpdateCookieJarSettings(
282 const CookieJarSettingsArgs& aCookieJarSettingsArgs);
284 void ActorDestroy(ActorDestroyReason aWhy) override;
286 void DrawSnapshotInternal(gfx::CrossProcessPaint* aPaint,
287 const Maybe<IntRect>& aRect, float aScale,
288 nscolor aBackgroundColor, uint32_t aFlags);
290 // WebShare API - try to share
291 mozilla::ipc::IPCResult RecvShare(IPCWebShareData&& aData,
292 ShareResolver&& aResolver);
294 mozilla::ipc::IPCResult RecvCheckPermitUnload(
295 bool aHasInProcessBlocker, XPCOMPermitUnloadAction aAction,
296 CheckPermitUnloadResolver&& aResolver);
298 mozilla::ipc::IPCResult RecvExpectPageUseCounters(
299 const MaybeDiscarded<dom::WindowContext>& aTop);
300 mozilla::ipc::IPCResult RecvAccumulatePageUseCounters(
301 const UseCounters& aUseCounters);
303 mozilla::ipc::IPCResult RecvRequestRestoreTabContent();
305 mozilla::ipc::IPCResult RecvUpdateBFCacheStatus(const uint32_t& aOnFlags,
306 const uint32_t& aOffFlags);
308 // This IPC method is to notify the parent process that the caller process
309 // creates the first active peer connection (aIsAdded = true) or closes the
310 // last active peer connection (aIsAdded = false).
311 mozilla::ipc::IPCResult RecvUpdateActivePeerConnectionStatus(bool aIsAdded);
313 public:
314 mozilla::ipc::IPCResult RecvSetSingleChannelId(
315 const Maybe<uint64_t>& aSingleChannelId);
317 mozilla::ipc::IPCResult RecvSetDocumentDomain(NotNull<nsIURI*> aDomain);
319 mozilla::ipc::IPCResult RecvReloadWithHttpsOnlyException();
321 mozilla::ipc::IPCResult RecvDiscoverIdentityCredentialFromExternalSource(
322 const IdentityCredentialRequestOptions& aOptions,
323 const DiscoverIdentityCredentialFromExternalSourceResolver& aResolver);
325 mozilla::ipc::IPCResult RecvGetStorageAccessPermission(
326 GetStorageAccessPermissionResolver&& aResolve);
328 mozilla::ipc::IPCResult RecvSetCookies(
329 const nsCString& aBaseDomain, const OriginAttributes& aOriginAttributes,
330 nsIURI* aHost, bool aFromHttp, const nsTArray<CookieStruct>& aCookies);
332 private:
333 WindowGlobalParent(CanonicalBrowsingContext* aBrowsingContext,
334 uint64_t aInnerWindowId, uint64_t aOuterWindowId,
335 FieldValues&& aInit);
337 ~WindowGlobalParent();
339 bool ShouldTrackSiteOriginTelemetry();
340 void FinishAccumulatingPageUseCounters();
342 // Returns failure if the new storage principal cannot be validated
343 // against the current document principle.
344 nsresult SetDocumentStoragePrincipal(
345 nsIPrincipal* aNewDocumentStoragePrincipal);
347 // NOTE: Neither this document principal nor the document storage
348 // principal doesn't reflect possible |document.domain| mutations
349 // which may have been made in the actual document.
350 nsCOMPtr<nsIPrincipal> mDocumentPrincipal;
351 nsCOMPtr<nsIPrincipal> mDocumentStoragePrincipal;
353 // The principal to use for the content blocking allow list.
354 nsCOMPtr<nsIPrincipal> mDocContentBlockingAllowListPrincipal;
356 nsCOMPtr<nsIURI> mDocumentURI;
357 Maybe<nsString> mDocumentTitle;
359 Maybe<bool> mIsInitialDocument;
361 // True if this window has a "beforeunload" event listener.
362 bool mHasBeforeUnload;
364 // The log of all content blocking actions taken on the document related to
365 // this WindowGlobalParent. This is only stored on top-level documents and
366 // includes the activity log for all of the nested subdocuments as well.
367 ContentBlockingLog mContentBlockingLog;
369 uint32_t mSecurityState = 0;
371 Maybe<ClientInfo> mClientInfo;
372 // Fields being mirrored from the corresponding document
373 nsCOMPtr<nsICookieJarSettings> mCookieJarSettings;
374 nsCOMPtr<nsITransportSecurityInfo> mSecurityInfo;
376 uint32_t mSandboxFlags;
378 struct OriginCounter {
379 void UpdateSiteOriginsFrom(WindowGlobalParent* aParent, bool aIncrease);
380 void Accumulate();
382 nsTHashMap<nsCStringHashKey, int32_t> mOriginMap;
383 uint32_t mMaxOrigins = 0;
386 // Used to collect unique site origin telemetry.
388 // Is only Some() on top-level content windows.
389 Maybe<OriginCounter> mOriginCounter;
391 bool mDocumentHasLoaded;
392 bool mDocumentHasUserInteracted;
393 bool mDocumentTreeWouldPreloadResources = false;
394 bool mBlockAllMixedContent;
395 bool mUpgradeInsecureRequests;
397 // HTTPS-Only Mode flags
398 uint32_t mHttpsOnlyStatus;
400 // The window of the document whose page use counters our document's use
401 // counters will contribute to. (If we are a top-level document, this
402 // will point to ourselves.)
403 RefPtr<WindowGlobalParent> mPageUseCountersWindow;
405 // Our page use counters, if we are a top-level document.
406 UniquePtr<PageUseCounters> mPageUseCounters;
408 // Whether we have sent our page use counters, and so should ignore any
409 // subsequent ExpectPageUseCounters calls.
410 bool mSentPageUseCounters = false;
412 uint32_t mBFCacheStatus = 0;
414 // If this WindowGlobalParent is a top window, this field indicates how many
415 // child processes have active peer connections for this window and its
416 // descendants.
417 uint32_t mNumOfProcessesWithActivePeerConnections = 0;
419 // mSingleChannelId records whether the loadgroup contains a single request
420 // with an id. If there is one channel in the loadgroup and it has an id then
421 // mSingleChannelId is set to Some(id) (ids are non-zero). If there is one
422 // request in the loadgroup and it's not a channel or it doesn't have an id,
423 // or there are multiple requests in the loadgroup, then mSingleChannelId is
424 // set to Some(0). If there are no requests in the loadgroup then
425 // mSingleChannelId is set to Nothing().
426 // Note: We ignore favicon loads when considering the requests in the
427 // loadgroup.
428 Maybe<uint64_t> mSingleChannelId;
430 // True if the current loaded document is in fullscreen.
431 bool mFullscreen = false;
433 bool mShouldReportHasBlockedOpaqueResponse = false;
436 } // namespace dom
437 } // namespace mozilla
439 inline nsISupports* ToSupports(
440 mozilla::dom::WindowGlobalParent* aWindowGlobal) {
441 return static_cast<mozilla::dom::WindowContext*>(aWindowGlobal);
444 #endif // !defined(mozilla_dom_WindowGlobalParent_h)