no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / dom / base / nsPIDOMWindow.h
blob4fd24e0010aae484cc40a09b4df1a613a0535467
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 nsPIDOMWindow_h__
8 #define nsPIDOMWindow_h__
10 #include "nsIDOMWindow.h"
11 #include "mozIDOMWindow.h"
13 #include "nsCOMPtr.h"
14 #include "nsTArray.h"
15 #include "Units.h"
16 #include "mozilla/dom/EventTarget.h"
17 #include "mozilla/EventForwards.h"
18 #include "mozilla/Maybe.h"
19 #include "js/TypeDecls.h"
20 #include "nsRefPtrHashtable.h"
21 #include "nsILoadInfo.h"
22 #include "mozilla/MozPromise.h"
24 // Only fired for inner windows.
25 #define DOM_WINDOW_DESTROYED_TOPIC "dom-window-destroyed"
26 #define DOM_WINDOW_FROZEN_TOPIC "dom-window-frozen"
27 #define DOM_WINDOW_THAWED_TOPIC "dom-window-thawed"
29 class nsGlobalWindowInner;
30 class nsGlobalWindowOuter;
31 class nsIArray;
32 class nsIBaseWindow;
33 class nsIChannel;
34 class nsIContent;
35 class nsIContentSecurityPolicy;
36 class nsICSSDeclaration;
37 class nsIDocShell;
38 class nsIDocShellTreeOwner;
39 class nsDocShellLoadState;
40 class nsIPrincipal;
41 class nsIRunnable;
42 class nsIScriptTimeoutHandler;
43 class nsISerialEventTarget;
44 class nsIURI;
45 class nsIWebBrowserChrome;
46 class nsPIDOMWindowInner;
47 class nsPIDOMWindowOuter;
48 class nsPIWindowRoot;
50 using SuspendTypes = uint32_t;
52 namespace mozilla::dom {
53 class AudioContext;
54 class BrowsingContext;
55 class BrowsingContextGroup;
56 class ClientInfo;
57 class ClientState;
58 class ContentFrameMessageManager;
59 class DocGroup;
60 class Document;
61 class Element;
62 class Location;
63 class MediaDevices;
64 class MediaKeys;
65 class Navigator;
66 class Performance;
67 class Selection;
68 class ServiceWorker;
69 class ServiceWorkerDescriptor;
70 class Timeout;
71 class TimeoutManager;
72 class WindowContext;
73 class WindowGlobalChild;
74 class CustomElementRegistry;
75 enum class CallerType : uint32_t;
76 } // namespace mozilla::dom
78 enum class FullscreenReason {
79 // Toggling the fullscreen mode requires trusted context.
80 ForFullscreenMode,
81 // Fullscreen API is the API provided to untrusted content.
82 ForFullscreenAPI,
83 // This reason can only be used with exiting fullscreen.
84 // It is otherwise identical to eForFullscreenAPI except it would
85 // suppress the fullscreen transition.
86 ForForceExitFullscreen
89 // Must be kept in sync with xpcom/rust/xpcom/src/interfaces/nonidl.rs
90 #define NS_PIDOMWINDOWINNER_IID \
91 { \
92 0x775dabc9, 0x8f43, 0x4277, { \
93 0x9a, 0xdb, 0xf1, 0x99, 0x0d, 0x77, 0xcf, 0xfb \
94 } \
97 // Must be kept in sync with xpcom/rust/xpcom/src/interfaces/nonidl.rs
98 #define NS_PIDOMWINDOWOUTER_IID \
99 { \
100 0x769693d4, 0xb009, 0x4fe2, { \
101 0xaf, 0x18, 0x7d, 0xc8, 0xdf, 0x74, 0x96, 0xdf \
105 class nsPIDOMWindowInner : public mozIDOMWindow {
106 protected:
107 using Document = mozilla::dom::Document;
108 friend nsGlobalWindowInner;
109 friend nsGlobalWindowOuter;
111 nsPIDOMWindowInner(nsPIDOMWindowOuter* aOuterWindow,
112 mozilla::dom::WindowGlobalChild* aActor);
114 ~nsPIDOMWindowInner();
116 public:
117 NS_DECLARE_STATIC_IID_ACCESSOR(NS_PIDOMWINDOWINNER_IID)
119 nsIGlobalObject* AsGlobal();
120 const nsIGlobalObject* AsGlobal() const;
122 nsPIDOMWindowOuter* GetOuterWindow() const { return mOuterWindow; }
124 static nsPIDOMWindowInner* From(mozIDOMWindow* aFrom) {
125 return static_cast<nsPIDOMWindowInner*>(aFrom);
128 NS_IMPL_FROMEVENTTARGET_HELPER_WITH_GETTER(nsPIDOMWindowInner,
129 GetAsInnerWindow())
131 // Returns true if this object is the currently-active inner window for its
132 // BrowsingContext.
133 bool IsCurrentInnerWindow() const;
135 // Returns true if the document of this window is the active document. This
136 // is identical to IsCurrentInnerWindow() now that document.open() no longer
137 // creates new inner windows for the document it is called on.
138 inline bool HasActiveDocument() const;
140 // Return true if this object is the currently-active inner window for its
141 // BrowsingContext and any container document is also fully active.
142 // For https://html.spec.whatwg.org/multipage/browsers.html#fully-active
143 bool IsFullyActive() const;
145 // Returns true if this window is the same as mTopInnerWindow
146 inline bool IsTopInnerWindow() const;
148 // Returns true if this was the current window for its BrowsingContext when it
149 // was discarded.
150 virtual bool WasCurrentInnerWindow() const = 0;
152 // Check whether a document is currently loading (really checks if the
153 // load event has completed). May not be reset to false on errors.
154 inline bool IsLoading() const;
155 inline bool IsHandlingResizeEvent() const;
157 // Note: not related to IsLoading. Set to false if there's an error, etc.
158 virtual void SetActiveLoadingState(bool aIsActiveLoading) = 0;
160 bool AddAudioContext(mozilla::dom::AudioContext* aAudioContext);
161 void RemoveAudioContext(mozilla::dom::AudioContext* aAudioContext);
162 void MuteAudioContexts();
163 void UnmuteAudioContexts();
165 void SetAudioCapture(bool aCapture);
168 * Associate this inner window with a MediaKeys instance.
170 void AddMediaKeysInstance(mozilla::dom::MediaKeys* aMediaKeysInstance);
173 * Remove an association between this inner window and a MediaKeys instance.
175 void RemoveMediaKeysInstance(mozilla::dom::MediaKeys* aMediaKeysInstance);
178 * Return if any MediaKeys instances are associated with this window.
180 bool HasActiveMediaKeysInstance();
182 mozilla::dom::Performance* GetPerformance();
184 void QueuePerformanceNavigationTiming();
186 bool HasMutationListeners(uint32_t aMutationEventType) const {
187 if (!mOuterWindow) {
188 NS_ERROR("HasMutationListeners() called on orphan inner window!");
190 return false;
193 return (mMutationBits & aMutationEventType) != 0;
196 void SetMutationListeners(uint32_t aType) {
197 if (!mOuterWindow) {
198 NS_ERROR("HasMutationListeners() called on orphan inner window!");
200 return;
203 mMutationBits |= aType;
207 * Call this to check whether some node (this window, its document,
208 * or content in that document) has a mouseenter/leave event listener.
210 bool HasMouseEnterLeaveEventListeners() const {
211 return mMayHaveMouseEnterLeaveEventListener;
215 * Call this to indicate that some node (this window, its document,
216 * or content in that document) has a mouseenter/leave event listener.
218 void SetHasMouseEnterLeaveEventListeners() {
219 mMayHaveMouseEnterLeaveEventListener = true;
223 * Call this to check whether some node (this window, its document,
224 * or content in that document) has a Pointerenter/leave event listener.
226 bool HasPointerEnterLeaveEventListeners() const {
227 return mMayHavePointerEnterLeaveEventListener;
231 * Call this to indicate that some node (this window, its document,
232 * or content in that document) has a Pointerenter/leave event listener.
234 void SetHasPointerEnterLeaveEventListeners() {
235 mMayHavePointerEnterLeaveEventListener = true;
239 * Call this to check whether some node (this window, its document,
240 * or content in that document) has a transition* event listeners.
242 bool HasTransitionEventListeners() { return mMayHaveTransitionEventListener; }
245 * Call this to indicate that some node (this window, its document,
246 * or content in that document) has a transition* event listener.
248 void SetHasTransitionEventListeners() {
249 mMayHaveTransitionEventListener = true;
253 * Call this to check whether some node (this window, its document,
254 * or content in that document) has a beforeinput event listener.
255 * Returing false may be wrong if some nodes have come from another document
256 * with `Document.adoptNode`.
258 bool HasBeforeInputEventListenersForTelemetry() const {
259 return mMayHaveBeforeInputEventListenerForTelemetry;
263 * Call this to indicate that some node (this window, its document,
264 * or content in that document) has a beforeinput event listener.
266 void SetHasBeforeInputEventListenersForTelemetry() {
267 mMayHaveBeforeInputEventListenerForTelemetry = true;
271 * Call this to check whether some node (The document, or content in the
272 * document) has been observed by web apps with a mutation observer.
273 * (i.e., `MutationObserver.observe()` called by chrome script and addon's
274 * script does not make this returns true).
275 * Returing false may be wrong if some nodes have come from another document
276 * with `Document.adoptNode`.
278 bool MutationObserverHasObservedNodeForTelemetry() const {
279 return mMutationObserverHasObservedNodeForTelemetry;
283 * Call this to indicate that some node (The document, or content in the
284 * document) is observed by web apps with a mutation observer.
286 void SetMutationObserverHasObservedNodeForTelemetry() {
287 mMutationObserverHasObservedNodeForTelemetry = true;
290 // Sets the event for window.event. Does NOT take ownership, so
291 // the caller is responsible for clearing the event before the
292 // event gets deallocated. Pass nullptr to set window.event to
293 // undefined. Returns the previous value.
294 mozilla::dom::Event* SetEvent(mozilla::dom::Event* aEvent) {
295 mozilla::dom::Event* old = mEvent;
296 mEvent = aEvent;
297 return old;
301 * Check whether this window is a secure context.
303 bool IsSecureContext() const;
304 bool IsSecureContextIfOpenerIgnored() const;
306 // Calling suspend should prevent any asynchronous tasks from
307 // executing javascript for this window. This means setTimeout,
308 // requestAnimationFrame, and events should not be fired. Suspending
309 // a window maybe also suspends its children. Workers may
310 // continue to perform computations in the background. A window
311 // can have Suspend() called multiple times and will only resume after
312 // a matching number of Resume() calls.
313 void Suspend(bool aIncludeSubWindows = true);
314 void Resume(bool aIncludeSubWindows = true);
316 // Whether or not this window was suspended by the BrowserContextGroup
317 bool GetWasSuspendedByGroup() const { return mWasSuspendedByGroup; }
318 void SetWasSuspendedByGroup(bool aSuspended) {
319 mWasSuspendedByGroup = aSuspended;
322 // Apply the parent window's suspend, freeze, and modal state to the current
323 // window.
324 void SyncStateFromParentWindow();
327 * Increment active peer connection count.
329 void AddPeerConnection();
332 * Decrement active peer connection count.
334 void RemovePeerConnection();
337 * Check whether the active peer connection count is non-zero.
339 bool HasActivePeerConnections();
341 bool IsPlayingAudio();
343 bool IsDocumentLoaded() const;
345 mozilla::dom::TimeoutManager& TimeoutManager();
347 bool IsRunningTimeout();
349 // To cache top inner-window if available after constructed for tab-wised
350 // indexedDB counters.
351 void TryToCacheTopInnerWindow();
353 // Increase/Decrease the number of active IndexedDB databases for the
354 // decision making of timeout-throttling.
355 void UpdateActiveIndexedDBDatabaseCount(int32_t aDelta);
357 // Return true if there is any active IndexedDB databases which could block
358 // timeout-throttling.
359 bool HasActiveIndexedDBDatabases();
361 // Increase/Decrease the number of open WebSockets.
362 void UpdateWebSocketCount(int32_t aDelta);
364 // Return true if there are any open WebSockets that could block
365 // timeout-throttling.
366 bool HasOpenWebSockets() const;
368 mozilla::Maybe<mozilla::dom::ClientInfo> GetClientInfo() const;
369 mozilla::Maybe<mozilla::dom::ClientState> GetClientState() const;
370 mozilla::Maybe<mozilla::dom::ServiceWorkerDescriptor> GetController() const;
372 void SetCsp(nsIContentSecurityPolicy* aCsp);
373 void SetPreloadCsp(nsIContentSecurityPolicy* aPreloadCsp);
374 nsIContentSecurityPolicy* GetCsp();
376 void NoteCalledRegisterForServiceWorkerScope(const nsACString& aScope);
378 void NoteDOMContentLoaded();
380 virtual mozilla::dom::CustomElementRegistry* CustomElements() = 0;
382 // XXX: This is called on inner windows
383 virtual nsPIDOMWindowOuter* GetInProcessScriptableTop() = 0;
384 virtual nsPIDOMWindowOuter* GetInProcessScriptableParent() = 0;
385 virtual already_AddRefed<nsPIWindowRoot> GetTopWindowRoot() = 0;
387 mozilla::dom::EventTarget* GetChromeEventHandler() const {
388 return mChromeEventHandler;
391 mozilla::dom::EventTarget* GetParentTarget() {
392 if (!mParentTarget) {
393 UpdateParentTarget();
395 return mParentTarget;
398 virtual void MaybeUpdateTouchState() {}
400 Document* GetExtantDoc() const { return mDoc; }
401 nsIURI* GetDocumentURI() const;
402 nsIURI* GetDocBaseURI() const;
404 Document* GetDoc() {
405 if (!mDoc) {
406 MaybeCreateDoc();
408 return mDoc;
411 mozilla::dom::WindowContext* GetWindowContext() const;
412 mozilla::dom::WindowGlobalChild* GetWindowGlobalChild() const {
413 return mWindowGlobalChild;
416 // Removes this inner window from the BFCache, if it is cached, and returns
417 // true if it was.
418 bool RemoveFromBFCacheSync();
420 // Determine if the window is suspended or frozen. Outer windows
421 // will forward this call to the inner window for convenience. If
422 // there is no inner window then the outer window is considered
423 // suspended and frozen by default.
424 virtual bool IsSuspended() const = 0;
425 virtual bool IsFrozen() const = 0;
427 // Fire any DOM notification events related to things that happened while
428 // the window was frozen.
429 virtual nsresult FireDelayedDOMEvents(bool aIncludeSubWindows) = 0;
432 * Get the docshell in this window.
434 inline nsIDocShell* GetDocShell() const;
437 * Get the browsing context in this window.
439 inline mozilla::dom::BrowsingContext* GetBrowsingContext() const;
442 * Get the browsing context group this window belongs to.
444 mozilla::dom::BrowsingContextGroup* GetBrowsingContextGroup() const;
447 * Call this to indicate that some node (this window, its document,
448 * or content in that document) has a DOMActivate event listener.
450 void SetHasDOMActivateEventListeners() {
451 mMayHaveDOMActivateEventListeners = true;
455 * Call this to check whether some node (this window, its document,
456 * or content in that document) has a DOMActivate event listener.
458 bool HasDOMActivateEventListeners() const {
459 return mMayHaveDOMActivateEventListeners;
463 * Call this to indicate that some node (this window, its document,
464 * or content in that document) has a paint event listener.
466 void SetHasPaintEventListeners() { mMayHavePaintEventListener = true; }
469 * Call this to check whether some node (this window, its document,
470 * or content in that document) has a paint event listener.
472 bool HasPaintEventListeners() { return mMayHavePaintEventListener; }
475 * Call this to indicate that some node (this window, its document,
476 * or content in that document) has a touch event listener.
478 void SetHasTouchEventListeners() {
479 if (!mMayHaveTouchEventListener) {
480 mMayHaveTouchEventListener = true;
481 MaybeUpdateTouchState();
486 * Call this to indicate that some node (this window, its document,
487 * or content in that document) has a selectionchange event listener.
489 void SetHasSelectionChangeEventListeners() {
490 mMayHaveSelectionChangeEventListener = true;
494 * Call this to check whether some node (this window, its document,
495 * or content in that document) has a selectionchange event listener.
497 bool HasSelectionChangeEventListeners() const {
498 return mMayHaveSelectionChangeEventListener;
502 * Call this to indicate that some node (this window, its document,
503 * or content in that document) has a select event listener of form controls.
505 void SetHasFormSelectEventListeners() {
506 mMayHaveFormSelectEventListener = true;
510 * Call this to check whether some node (this window, its document,
511 * or content in that document) has a select event listener of form controls.
513 bool HasFormSelectEventListeners() const {
514 return mMayHaveFormSelectEventListener;
518 * Get and set the currently focused element within the document. If
519 * aNeedsFocus is true, then set mNeedsFocus to true to indicate that a
520 * document focus event is needed.
522 * DO NOT CALL EITHER OF THESE METHODS DIRECTLY. USE THE FOCUS MANAGER
523 * INSTEAD.
525 mozilla::dom::Element* GetFocusedElement() const {
526 return mFocusedElement.get();
529 virtual void SetFocusedElement(mozilla::dom::Element* aElement,
530 uint32_t aFocusMethod = 0,
531 bool aNeedsFocus = false) = 0;
533 bool UnknownFocusMethodShouldShowOutline() const {
534 return mUnknownFocusMethodShouldShowOutline;
538 * Retrieves the method that was used to focus the current node.
540 virtual uint32_t GetFocusMethod() = 0;
543 * Tells the window that it now has focus or has lost focus, based on the
544 * state of aFocus. If this method returns true, then the document loaded
545 * in the window has never received a focus event and expects to receive
546 * one. If false is returned, the document has received a focus event before
547 * and should only receive one if the window is being focused.
549 * aFocusMethod may be set to one of the focus method constants in
550 * nsIFocusManager to indicate how focus was set.
552 virtual bool TakeFocus(bool aFocus, uint32_t aFocusMethod) = 0;
555 * Indicates that the window may now accept a document focus event. This
556 * should be called once a document has been loaded into the window.
558 virtual void SetReadyForFocus() = 0;
561 * Whether the focused content within the window should show a focus ring.
563 virtual bool ShouldShowFocusRing() = 0;
566 * Indicates that the page in the window has been hidden. This is used to
567 * reset the focus state.
569 virtual void PageHidden() = 0;
572 * Instructs this window to asynchronously dispatch a hashchange event. This
573 * method must be called on an inner window.
575 virtual nsresult DispatchAsyncHashchange(nsIURI* aOldURI,
576 nsIURI* aNewURI) = 0;
579 * Instructs this window to synchronously dispatch a popState event.
581 virtual nsresult DispatchSyncPopState() = 0;
584 * Tell this window that it should listen for sensor changes of the given
585 * type.
587 virtual void EnableDeviceSensor(uint32_t aType) = 0;
590 * Tell this window that it should remove itself from sensor change
591 * notifications.
593 virtual void DisableDeviceSensor(uint32_t aType) = 0;
595 #if defined(MOZ_WIDGET_ANDROID)
596 virtual void EnableOrientationChangeListener() = 0;
597 virtual void DisableOrientationChangeListener() = 0;
598 #endif
601 * Tell this window that there is an observer for gamepad input
603 * Inner windows only.
605 virtual void SetHasGamepadEventListener(bool aHasGamepad = true) = 0;
608 * Return the window id of this window
610 uint64_t WindowID() const { return mWindowID; }
612 // WebIDL-ish APIs
613 void MarkUncollectableForCCGeneration(uint32_t aGeneration) {
614 mMarkedCCGeneration = aGeneration;
617 uint32_t GetMarkedCCGeneration() { return mMarkedCCGeneration; }
619 mozilla::dom::Navigator* Navigator();
620 mozilla::dom::MediaDevices* GetExtantMediaDevices() const;
621 virtual mozilla::dom::Location* Location() = 0;
623 virtual nsresult GetControllers(nsIControllers** aControllers) = 0;
625 virtual nsresult GetInnerWidth(double* aWidth) = 0;
626 virtual nsresult GetInnerHeight(double* aHeight) = 0;
628 virtual already_AddRefed<nsICSSDeclaration> GetComputedStyle(
629 mozilla::dom::Element& aElt, const nsAString& aPseudoElt,
630 mozilla::ErrorResult& aError) = 0;
632 virtual bool GetFullScreen() = 0;
634 virtual nsresult Focus(mozilla::dom::CallerType aCallerType) = 0;
635 virtual nsresult Close() = 0;
637 mozilla::dom::DocGroup* GetDocGroup() const;
639 RefPtr<mozilla::GenericPromise> SaveStorageAccessPermissionGranted();
640 RefPtr<mozilla::GenericPromise> SaveStorageAccessPermissionRevoked();
642 bool UsingStorageAccess();
644 uint32_t UpdateLockCount(bool aIncrement) {
645 MOZ_ASSERT_IF(!aIncrement, mLockCount > 0);
646 mLockCount += aIncrement ? 1 : -1;
647 return mLockCount;
649 bool HasActiveLocks() { return mLockCount > 0; }
651 uint32_t UpdateWebTransportCount(bool aIncrement) {
652 MOZ_ASSERT_IF(!aIncrement, mWebTransportCount > 0);
653 mWebTransportCount += aIncrement ? 1 : -1;
654 return mWebTransportCount;
656 bool HasActiveWebTransports() { return mWebTransportCount > 0; }
658 protected:
659 void CreatePerformanceObjectIfNeeded();
661 // Lazily instantiate an about:blank document if necessary, and if
662 // we have what it takes to do so.
663 void MaybeCreateDoc();
665 void SetChromeEventHandlerInternal(
666 mozilla::dom::EventTarget* aChromeEventHandler) {
667 mChromeEventHandler = aChromeEventHandler;
668 // mParentTarget will be set when the next event is dispatched.
669 mParentTarget = nullptr;
672 virtual void UpdateParentTarget() = 0;
674 // These two variables are special in that they're set to the same
675 // value on both the outer window and the current inner window. Make
676 // sure you keep them in sync!
677 nsCOMPtr<mozilla::dom::EventTarget> mChromeEventHandler; // strong
678 RefPtr<Document> mDoc;
679 // Cache the URI when mDoc is cleared.
680 nsCOMPtr<nsIURI> mDocumentURI; // strong
681 nsCOMPtr<nsIURI> mDocBaseURI; // strong
683 nsCOMPtr<mozilla::dom::EventTarget> mParentTarget; // strong
685 RefPtr<mozilla::dom::Performance> mPerformance;
686 mozilla::UniquePtr<mozilla::dom::TimeoutManager> mTimeoutManager;
688 RefPtr<mozilla::dom::Navigator> mNavigator;
690 // These variables are only used on inner windows.
691 uint32_t mMutationBits;
693 uint32_t mActivePeerConnections = 0;
695 bool mIsDocumentLoaded;
696 bool mIsHandlingResizeEvent;
697 bool mMayHaveDOMActivateEventListeners;
698 bool mMayHavePaintEventListener;
699 bool mMayHaveTouchEventListener;
700 bool mMayHaveSelectionChangeEventListener;
701 bool mMayHaveFormSelectEventListener;
702 bool mMayHaveMouseEnterLeaveEventListener;
703 bool mMayHavePointerEnterLeaveEventListener;
704 bool mMayHaveTransitionEventListener;
705 // Only used for telemetry probes. This may be wrong if some nodes have
706 // come from another document with `Document.adoptNode`.
707 bool mMayHaveBeforeInputEventListenerForTelemetry;
708 bool mMutationObserverHasObservedNodeForTelemetry;
710 // Our inner window's outer window.
711 nsCOMPtr<nsPIDOMWindowOuter> mOuterWindow;
713 // The element within the document that is currently focused when this
714 // window is active.
715 RefPtr<mozilla::dom::Element> mFocusedElement;
717 // The AudioContexts created for the current document, if any.
718 nsTArray<mozilla::dom::AudioContext*> mAudioContexts; // Weak
720 // Instances of MediaKeys created in this inner window. Storing these allows
721 // us to shutdown MediaKeys when an inner windows is destroyed. We can also
722 // use the presence of MediaKeys to assess if a window has EME activity.
723 nsTArray<mozilla::dom::MediaKeys*> mMediaKeysInstances; // Weak
725 RefPtr<mozilla::dom::BrowsingContext> mBrowsingContext;
727 // A unique (as long as our 64-bit counter doesn't roll over) id for
728 // this window.
729 uint64_t mWindowID;
731 // Set to true once we've sent the (chrome|content)-document-global-created
732 // notification.
733 bool mHasNotifiedGlobalCreated;
735 // Whether when focused via an "unknown" focus method, we should show outlines
736 // by default or not. The initial value of this is true (so as to show
737 // outlines for stuff like html autofocus, or initial programmatic focus
738 // without any other user interaction).
739 bool mUnknownFocusMethodShouldShowOutline = true;
741 uint32_t mMarkedCCGeneration;
743 // mTopInnerWindow is used for tab-wise check by timeout throttling. It could
744 // be null.
745 nsCOMPtr<nsPIDOMWindowInner> mTopInnerWindow;
747 // The evidence that we have tried to cache mTopInnerWindow only once from
748 // SetNewDocument(). Note: We need this extra flag because mTopInnerWindow
749 // could be null and we don't want it to be set multiple times.
750 bool mHasTriedToCacheTopInnerWindow;
752 // The number of active IndexedDB databases.
753 uint32_t mNumOfIndexedDBDatabases;
755 // The number of open WebSockets.
756 uint32_t mNumOfOpenWebSockets;
758 // The event dispatch code sets and unsets this while keeping
759 // the event object alive.
760 mozilla::dom::Event* mEvent;
762 // The WindowGlobalChild actor for this window.
764 // This will be non-null during the full lifetime of the window, initialized
765 // during SetNewDocument, and cleared during FreeInnerObjects.
766 RefPtr<mozilla::dom::WindowGlobalChild> mWindowGlobalChild;
768 bool mWasSuspendedByGroup;
771 * Count of the number of active LockRequest objects, including ones from
772 * workers.
774 uint32_t mLockCount = 0;
776 * Count of the number of active WebTransport objects, including ones from
777 * workers.
779 uint32_t mWebTransportCount = 0;
782 NS_DEFINE_STATIC_IID_ACCESSOR(nsPIDOMWindowInner, NS_PIDOMWINDOWINNER_IID)
784 class nsPIDOMWindowOuter : public mozIDOMWindowProxy {
785 protected:
786 using Document = mozilla::dom::Document;
788 explicit nsPIDOMWindowOuter(uint64_t aWindowID);
790 ~nsPIDOMWindowOuter();
792 void NotifyResumingDelayedMedia();
794 public:
795 NS_DECLARE_STATIC_IID_ACCESSOR(NS_PIDOMWINDOWOUTER_IID)
797 NS_IMPL_FROMEVENTTARGET_HELPER_WITH_GETTER(nsPIDOMWindowOuter,
798 GetAsOuterWindow())
800 static nsPIDOMWindowOuter* From(mozIDOMWindowProxy* aFrom) {
801 return static_cast<nsPIDOMWindowOuter*>(aFrom);
804 // Given an inner window, return its outer if the inner is the current inner.
805 // Otherwise (argument null or not an inner or not current) return null.
806 static nsPIDOMWindowOuter* GetFromCurrentInner(nsPIDOMWindowInner* aInner);
808 // Check whether a document is currently loading
809 inline bool IsLoading() const;
810 inline bool IsHandlingResizeEvent() const;
812 nsPIDOMWindowInner* GetCurrentInnerWindow() const { return mInnerWindow; }
814 nsPIDOMWindowInner* EnsureInnerWindow() {
815 // GetDoc forces inner window creation if there isn't one already
816 GetDoc();
817 return GetCurrentInnerWindow();
820 bool IsRootOuterWindow() { return mIsRootOuterWindow; }
822 // Internal getter/setter for the frame element, this version of the
823 // getter crosses chrome boundaries whereas the public scriptable
824 // one doesn't for security reasons.
825 mozilla::dom::Element* GetFrameElementInternal() const;
826 void SetFrameElementInternal(mozilla::dom::Element* aFrameElement);
828 bool IsBackground() { return mIsBackground; }
830 // Audio API
831 bool GetAudioMuted() const;
833 // No longer to delay media from starting for this window.
834 void ActivateMediaComponents();
835 bool ShouldDelayMediaFromStart() const;
837 void RefreshMediaElementsVolume();
839 virtual nsPIDOMWindowOuter* GetPrivateRoot() = 0;
842 * |top| gets the root of the window hierarchy.
844 * This function does not cross chrome-content boundaries, so if this
845 * window's parent is of a different type, |top| will return this window.
847 * When script reads the top property, we run GetInProcessScriptableTop,
848 * which will not cross an <iframe mozbrowser> boundary.
850 * In contrast, C++ calls to GetTop are forwarded to GetRealTop, which
851 * ignores <iframe mozbrowser> boundaries.
854 virtual already_AddRefed<nsPIDOMWindowOuter>
855 GetInProcessTop() = 0; // Outer only
856 virtual already_AddRefed<nsPIDOMWindowOuter> GetInProcessParent() = 0;
857 virtual nsPIDOMWindowOuter* GetInProcessScriptableTop() = 0;
858 virtual nsPIDOMWindowOuter* GetInProcessScriptableParent() = 0;
859 virtual already_AddRefed<nsPIWindowRoot> GetTopWindowRoot() = 0;
862 * Behaves identically to GetInProcessScriptableParent except that it
863 * returns null if GetInProcessScriptableParent would return this window.
865 virtual nsPIDOMWindowOuter* GetInProcessScriptableParentOrNull() = 0;
867 virtual void SetIsBackground(bool aIsBackground) = 0;
869 mozilla::dom::EventTarget* GetChromeEventHandler() const {
870 return mChromeEventHandler;
873 virtual void SetChromeEventHandler(
874 mozilla::dom::EventTarget* aChromeEventHandler) = 0;
876 mozilla::dom::EventTarget* GetParentTarget() {
877 if (!mParentTarget) {
878 UpdateParentTarget();
880 return mParentTarget;
883 mozilla::dom::ContentFrameMessageManager* GetMessageManager() {
884 // We maintain our mMessageManager state alongside mParentTarget.
885 if (!mParentTarget) {
886 UpdateParentTarget();
888 return mMessageManager;
891 Document* GetExtantDoc() const { return mDoc; }
892 nsIURI* GetDocumentURI() const;
894 Document* GetDoc() {
895 if (!mDoc) {
896 MaybeCreateDoc();
898 return mDoc;
901 // Set the window up with an about:blank document with the given principal and
902 // potentially a CSP and a COEP.
903 virtual void SetInitialPrincipal(
904 nsIPrincipal* aNewWindowPrincipal, nsIContentSecurityPolicy* aCSP,
905 const mozilla::Maybe<nsILoadInfo::CrossOriginEmbedderPolicy>& aCoep) = 0;
907 // Returns an object containing the window's state. This also suspends
908 // all running timeouts in the window.
909 virtual already_AddRefed<nsISupports> SaveWindowState() = 0;
911 // Restore the window state from aState.
912 virtual nsresult RestoreWindowState(nsISupports* aState) = 0;
914 // Determine if the window is suspended or frozen. Outer windows
915 // will forward this call to the inner window for convenience. If
916 // there is no inner window then the outer window is considered
917 // suspended and frozen by default.
918 virtual bool IsSuspended() const = 0;
919 virtual bool IsFrozen() const = 0;
921 // Fire any DOM notification events related to things that happened while
922 // the window was frozen.
923 virtual nsresult FireDelayedDOMEvents(bool aIncludeSubWindows) = 0;
926 * Get the docshell in this window.
928 inline nsIDocShell* GetDocShell() const;
931 * Get the browsing context in this window.
933 inline mozilla::dom::BrowsingContext* GetBrowsingContext() const;
936 * Get the browsing context group this window belongs to.
938 mozilla::dom::BrowsingContextGroup* GetBrowsingContextGroup() const;
941 * Set a new document in the window. Calling this method will in most cases
942 * create a new inner window. This may be called with a pointer to the current
943 * document, in that case the document remains unchanged, but a new inner
944 * window will be created.
946 * aDocument must not be null.
948 virtual nsresult SetNewDocument(
949 Document* aDocument, nsISupports* aState, bool aForceReuseInnerWindow,
950 mozilla::dom::WindowGlobalChild* aActor = nullptr) = 0;
953 * Ensure the size and position of this window are up-to-date by doing
954 * a layout flush in the parent (which will in turn, do a layout flush
955 * in its parent, etc.).
957 virtual void EnsureSizeAndPositionUpToDate() = 0;
960 * Suppresses/unsuppresses user initiated event handling in window's document
961 * and all in-process descendant documents.
963 virtual void SuppressEventHandling() = 0;
964 virtual void UnsuppressEventHandling() = 0;
967 * Callback for notifying a window about a modal dialog being
968 * opened/closed with the window as a parent.
970 * If any script can run between the enter and leave modal states, and the
971 * window isn't top, the LeaveModalState() should be called on the window
972 * returned by EnterModalState().
974 virtual nsPIDOMWindowOuter* EnterModalState() = 0;
975 virtual void LeaveModalState() = 0;
977 virtual bool CanClose() = 0;
978 virtual void ForceClose() = 0;
981 * Moves the top-level window into fullscreen mode if aIsFullScreen is true,
982 * otherwise exits fullscreen.
984 virtual nsresult SetFullscreenInternal(FullscreenReason aReason,
985 bool aIsFullscreen) = 0;
986 virtual void FullscreenWillChange(bool aIsFullscreen) = 0;
988 * This function should be called when the fullscreen state is flipped.
989 * If no widget is involved the fullscreen change, this method is called
990 * by SetFullscreenInternal, otherwise, it is called when the widget
991 * finishes its change to or from fullscreen.
993 * @param aIsFullscreen indicates whether the widget is in fullscreen.
995 virtual void FinishFullscreenChange(bool aIsFullscreen) = 0;
997 virtual void ForceFullScreenInWidget() = 0;
999 virtual void MacFullscreenMenubarOverlapChanged(
1000 mozilla::DesktopCoord aOverlapAmount) = 0;
1002 // XXX: These focus methods all forward to the inner, could we change
1003 // consumers to call these on the inner directly?
1006 * Get and set the currently focused element within the document. If
1007 * aNeedsFocus is true, then set mNeedsFocus to true to indicate that a
1008 * document focus event is needed.
1010 * DO NOT CALL EITHER OF THESE METHODS DIRECTLY. USE THE FOCUS MANAGER
1011 * INSTEAD.
1013 inline mozilla::dom::Element* GetFocusedElement() const;
1015 virtual void SetFocusedElement(mozilla::dom::Element* aElement,
1016 uint32_t aFocusMethod = 0,
1017 bool aNeedsFocus = false) = 0;
1019 * Get whether a focused element focused by unknown reasons (like script
1020 * focus) should match the :focus-visible pseudo-class.
1022 bool UnknownFocusMethodShouldShowOutline() const;
1025 * Retrieves the method that was used to focus the current node.
1027 virtual uint32_t GetFocusMethod() = 0;
1030 * Tells the window that it now has focus or has lost focus, based on the
1031 * state of aFocus. If this method returns true, then the document loaded
1032 * in the window has never received a focus event and expects to receive
1033 * one. If false is returned, the document has received a focus event before
1034 * and should only receive one if the window is being focused.
1036 * aFocusMethod may be set to one of the focus method constants in
1037 * nsIFocusManager to indicate how focus was set.
1039 virtual bool TakeFocus(bool aFocus, uint32_t aFocusMethod) = 0;
1042 * Indicates that the window may now accept a document focus event. This
1043 * should be called once a document has been loaded into the window.
1045 virtual void SetReadyForFocus() = 0;
1048 * Whether the focused content within the window should show a focus ring.
1050 virtual bool ShouldShowFocusRing() = 0;
1053 * Indicates that the page in the window has been hidden. This is used to
1054 * reset the focus state.
1056 virtual void PageHidden() = 0;
1059 * Return the window id of this window
1061 uint64_t WindowID() const { return mWindowID; }
1064 * Dispatch a custom event with name aEventName targeted at this window.
1065 * Returns whether the default action should be performed.
1067 * Outer windows only.
1069 virtual bool DispatchCustomEvent(
1070 const nsAString& aEventName,
1071 mozilla::ChromeOnlyDispatch aChromeOnlyDispatch =
1072 mozilla::ChromeOnlyDispatch::eNo) = 0;
1075 * Like nsIDOMWindow::Open, except that we don't navigate to the given URL.
1077 * Outer windows only.
1079 virtual nsresult OpenNoNavigate(const nsAString& aUrl, const nsAString& aName,
1080 const nsAString& aOptions,
1081 mozilla::dom::BrowsingContext** _retval) = 0;
1084 * Fire a popup blocked event on the document.
1086 virtual void FirePopupBlockedEvent(Document* aDoc, nsIURI* aPopupURI,
1087 const nsAString& aPopupWindowName,
1088 const nsAString& aPopupWindowFeatures) = 0;
1090 // WebIDL-ish APIs
1091 void MarkUncollectableForCCGeneration(uint32_t aGeneration) {
1092 mMarkedCCGeneration = aGeneration;
1095 uint32_t GetMarkedCCGeneration() { return mMarkedCCGeneration; }
1097 // XXX(nika): These feel like they should be inner window only, but they're
1098 // called on the outer window.
1099 virtual mozilla::dom::Navigator* GetNavigator() = 0;
1100 virtual mozilla::dom::Location* GetLocation() = 0;
1102 virtual nsresult GetPrompter(nsIPrompt** aPrompt) = 0;
1103 virtual nsresult GetControllers(nsIControllers** aControllers) = 0;
1104 virtual already_AddRefed<mozilla::dom::Selection> GetSelection() = 0;
1105 virtual mozilla::dom::Nullable<mozilla::dom::WindowProxyHolder>
1106 GetOpener() = 0;
1108 // aLoadState will be passed on through to the windowwatcher.
1109 // aForceNoOpener will act just like a "noopener" feature in aOptions except
1110 // will not affect any other window features.
1111 virtual nsresult Open(const nsAString& aUrl, const nsAString& aName,
1112 const nsAString& aOptions,
1113 nsDocShellLoadState* aLoadState, bool aForceNoOpener,
1114 mozilla::dom::BrowsingContext** _retval) = 0;
1115 virtual nsresult OpenDialog(const nsAString& aUrl, const nsAString& aName,
1116 const nsAString& aOptions,
1117 nsISupports* aExtraArgument,
1118 mozilla::dom::BrowsingContext** _retval) = 0;
1120 virtual nsresult GetInnerWidth(double* aWidth) = 0;
1121 virtual nsresult GetInnerHeight(double* aHeight) = 0;
1123 virtual mozilla::dom::Element* GetFrameElement() = 0;
1125 virtual bool Closed() = 0;
1126 virtual bool GetFullScreen() = 0;
1127 virtual nsresult SetFullScreen(bool aFullscreen) = 0;
1129 virtual nsresult Focus(mozilla::dom::CallerType aCallerType) = 0;
1130 virtual nsresult Close() = 0;
1132 virtual nsresult MoveBy(int32_t aXDif, int32_t aYDif) = 0;
1134 virtual void UpdateCommands(const nsAString& anAction) = 0;
1136 mozilla::dom::DocGroup* GetDocGroup() const;
1138 already_AddRefed<nsIDocShellTreeOwner> GetTreeOwner();
1139 already_AddRefed<nsIBaseWindow> GetTreeOwnerWindow();
1140 already_AddRefed<nsIWebBrowserChrome> GetWebBrowserChrome();
1142 protected:
1143 // Lazily instantiate an about:blank document if necessary, and if
1144 // we have what it takes to do so.
1145 void MaybeCreateDoc();
1147 void SetChromeEventHandlerInternal(
1148 mozilla::dom::EventTarget* aChromeEventHandler);
1150 virtual void UpdateParentTarget() = 0;
1152 // These two variables are special in that they're set to the same
1153 // value on both the outer window and the current inner window. Make
1154 // sure you keep them in sync!
1155 nsCOMPtr<mozilla::dom::EventTarget> mChromeEventHandler; // strong
1156 RefPtr<Document> mDoc;
1157 // Cache the URI when mDoc is cleared.
1158 nsCOMPtr<nsIURI> mDocumentURI; // strong
1160 nsCOMPtr<mozilla::dom::EventTarget> mParentTarget; // strong
1161 RefPtr<mozilla::dom::ContentFrameMessageManager> mMessageManager; // strong
1163 nsCOMPtr<mozilla::dom::Element> mFrameElement;
1165 // These references are used by nsGlobalWindow.
1166 nsCOMPtr<nsIDocShell> mDocShell;
1167 RefPtr<mozilla::dom::BrowsingContext> mBrowsingContext;
1169 uint32_t mModalStateDepth;
1171 uint32_t mSuppressEventHandlingDepth;
1173 // Tracks whether our docshell is active. If it is, mIsBackground
1174 // is false. Too bad we have so many different concepts of
1175 // "active".
1176 bool mIsBackground;
1178 bool mIsRootOuterWindow;
1180 // And these are the references between inner and outer windows.
1181 nsPIDOMWindowInner* MOZ_NON_OWNING_REF mInnerWindow;
1183 // A unique (as long as our 64-bit counter doesn't roll over) id for
1184 // this window.
1185 uint64_t mWindowID;
1187 uint32_t mMarkedCCGeneration;
1190 NS_DEFINE_STATIC_IID_ACCESSOR(nsPIDOMWindowOuter, NS_PIDOMWINDOWOUTER_IID)
1192 #include "nsPIDOMWindowInlines.h"
1194 #endif // nsPIDOMWindow_h__