1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 sw=2 et tw=80: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
40 #ifndef nsPIDOMWindow_h__
41 #define nsPIDOMWindow_h__
43 #include "nsISupports.h"
44 #include "nsIDOMLocation.h"
45 #include "nsIDOMXULCommandDispatcher.h"
46 #include "nsIDOMElement.h"
47 #include "nsIDOMWindowInternal.h"
48 #include "nsPIDOMEventTarget.h"
49 #include "nsIDOMDocument.h"
53 #define DOM_WINDOW_DESTROYED_TOPIC "dom-window-destroyed"
57 // Popup control state enum. The values in this enum must go from most
58 // permissive to least permissive so that it's safe to push state in
59 // all situations. Pushing popup state onto the stack never makes the
60 // current popup state less permissive (see
61 // nsGlobalWindow::PushPopupControlState()).
62 enum PopupControlState
{
63 openAllowed
= 0, // open that window without worries
64 openControlled
, // it's a popup, but allow it
65 openAbused
, // it's a popup. disallow it, but allow domain override.
66 openOverridden
// disallow window open
72 class nsIScriptTimeoutHandler
;
74 class nsScriptObjectHolder
;
75 class nsXBLPrototypeHandler
;
79 #define NS_PIDOMWINDOW_IID \
80 { 0x4beac1da, 0x513e, 0x4a8b, \
81 { 0x96, 0x94, 0x1c, 0xf6, 0x4f, 0xba, 0xa8, 0x1c } }
83 class nsPIDOMWindow
: public nsIDOMWindowInternal
86 NS_DECLARE_STATIC_IID_ACCESSOR(NS_PIDOMWINDOW_IID
)
88 virtual nsPIDOMWindow
* GetPrivateRoot() = 0;
90 virtual void ActivateOrDeactivate(PRBool aActivate
) = 0;
92 // this is called GetTopWindowRoot to avoid conflicts with nsIDOMWindow2::GetWindowRoot
93 virtual already_AddRefed
<nsPIWindowRoot
> GetTopWindowRoot() = 0;
95 virtual void SetActive(PRBool aActive
)
105 nsPIDOMEventTarget
* GetChromeEventHandler() const
107 return mChromeEventHandler
;
110 virtual void SetChromeEventHandler(nsPIDOMEventTarget
* aChromeEventHandler
) = 0;
112 nsPIDOMEventTarget
* GetParentTarget()
114 if (!mParentTarget
) {
115 UpdateParentTarget();
117 return mParentTarget
;
120 PRBool
HasMutationListeners(PRUint32 aMutationEventType
) const
122 const nsPIDOMWindow
*win
;
124 if (IsOuterWindow()) {
125 win
= GetCurrentInnerWindow();
128 NS_ERROR("No current inner window available!");
134 NS_ERROR("HasMutationListeners() called on orphan inner window!");
142 return (win
->mMutationBits
& aMutationEventType
) != 0;
145 void SetMutationListeners(PRUint32 aType
)
149 if (IsOuterWindow()) {
150 win
= GetCurrentInnerWindow();
153 NS_ERROR("No inner window available to set mutation bits on!");
159 NS_ERROR("HasMutationListeners() called on orphan inner window!");
167 win
->mMutationBits
|= aType
;
170 virtual void MaybeUpdateTouchState() {}
171 virtual void UpdateTouchState() {}
173 // GetExtantDocument provides a backdoor to the DOM GetDocument accessor
174 nsIDOMDocument
* GetExtantDocument() const
179 // Internal getter/setter for the frame element, this version of the
180 // getter crosses chrome boundaries whereas the public scriptable
181 // one doesn't for security reasons.
182 nsIDOMElement
* GetFrameElementInternal() const
185 return mOuterWindow
->GetFrameElementInternal();
188 NS_ASSERTION(!IsInnerWindow(),
189 "GetFrameElementInternal() called on orphan inner window");
191 return mFrameElement
;
194 void SetFrameElementInternal(nsIDOMElement
*aFrameElement
)
196 if (IsOuterWindow()) {
197 mFrameElement
= aFrameElement
;
203 NS_ERROR("frameElement set on inner window with no outer!");
208 mOuterWindow
->SetFrameElementInternal(aFrameElement
);
211 PRBool
IsLoadingOrRunningTimeout() const
213 const nsPIDOMWindow
*win
= GetCurrentInnerWindow();
219 return !win
->mIsDocumentLoaded
|| win
->mRunningTimeout
;
222 // Check whether a document is currently loading
223 PRBool
IsLoading() const
225 const nsPIDOMWindow
*win
;
227 if (IsOuterWindow()) {
228 win
= GetCurrentInnerWindow();
231 NS_ERROR("No current inner window available!");
237 NS_ERROR("IsLoading() called on orphan inner window!");
245 return !win
->mIsDocumentLoaded
;
248 PRBool
IsHandlingResizeEvent() const
250 const nsPIDOMWindow
*win
;
252 if (IsOuterWindow()) {
253 win
= GetCurrentInnerWindow();
256 NS_ERROR("No current inner window available!");
262 NS_ERROR("IsHandlingResizeEvent() called on orphan inner window!");
270 return win
->mIsHandlingResizeEvent
;
273 // Tell this window who opened it. This only has an effect if there is
274 // either no document currently in the window or if the document is the
275 // original document this window came with (an about:blank document either
276 // preloaded into it when it was created, or created by
277 // CreateAboutBlankContentViewer()).
278 virtual void SetOpenerScriptPrincipal(nsIPrincipal
* aPrincipal
) = 0;
279 // Ask this window who opened it.
280 virtual nsIPrincipal
* GetOpenerScriptPrincipal() = 0;
282 virtual PopupControlState
PushPopupControlState(PopupControlState aState
,
283 PRBool aForce
) const = 0;
284 virtual void PopPopupControlState(PopupControlState state
) const = 0;
285 virtual PopupControlState
GetPopupControlState() const = 0;
287 // Returns an object containing the window's state. This also suspends
288 // all running timeouts in the window.
289 virtual nsresult
SaveWindowState(nsISupports
**aState
) = 0;
291 // Restore the window state from aState.
292 virtual nsresult
RestoreWindowState(nsISupports
*aState
) = 0;
294 // Suspend timeouts in this window and in child windows.
295 virtual void SuspendTimeouts(PRUint32 aIncrease
= 1,
296 PRBool aFreezeChildren
= PR_TRUE
) = 0;
298 // Resume suspended timeouts in this window and in child windows.
299 virtual nsresult
ResumeTimeouts(PRBool aThawChildren
= PR_TRUE
) = 0;
301 virtual PRUint32
TimeoutSuspendCount() = 0;
303 // Fire any DOM notification events related to things that happened while
304 // the window was frozen.
305 virtual nsresult
FireDelayedDOMEvents() = 0;
307 virtual PRBool
IsFrozen() const = 0;
309 // Add a timeout to this window.
310 virtual nsresult
SetTimeoutOrInterval(nsIScriptTimeoutHandler
*aHandler
,
312 PRBool aIsInterval
, PRInt32
*aReturn
) = 0;
314 // Clear a timeout from this window.
315 virtual nsresult
ClearTimeoutOrInterval(PRInt32 aTimerID
) = 0;
317 nsPIDOMWindow
*GetOuterWindow()
319 return mIsInnerWindow
? mOuterWindow
: this;
322 nsPIDOMWindow
*GetCurrentInnerWindow() const
327 nsPIDOMWindow
*EnsureInnerWindow()
329 NS_ASSERTION(IsOuterWindow(), "EnsureInnerWindow called on inner window");
330 // GetDocument forces inner window creation if there isn't one already
331 nsCOMPtr
<nsIDOMDocument
> doc
;
332 GetDocument(getter_AddRefs(doc
));
333 return GetCurrentInnerWindow();
336 PRBool
IsInnerWindow() const
338 return mIsInnerWindow
;
341 PRBool
IsOuterWindow() const
343 return !IsInnerWindow();
346 virtual PRBool
WouldReuseInnerWindow(nsIDocument
*aNewDocument
) = 0;
349 * Get the docshell in this window.
351 nsIDocShell
*GetDocShell()
354 return mOuterWindow
->mDocShell
;
361 * Set or unset the docshell in the window.
363 virtual void SetDocShell(nsIDocShell
*aDocShell
) = 0;
366 * Set a new document in the window. Calling this method will in
367 * most cases create a new inner window. If this method is called on
368 * an inner window the call will be forewarded to the outer window,
369 * if the inner window is not the current inner window an
370 * NS_ERROR_NOT_AVAILABLE error code will be returned. This may be
371 * called with a pointer to the current document, in that case the
372 * document remains unchanged, but a new inner window will be
375 virtual nsresult
SetNewDocument(nsIDocument
*aDocument
,
376 nsISupports
*aState
) = 0;
379 * Set the opener window. aOriginalOpener is true if and only if this is the
380 * original opener for the window. That is, it can only be true at most once
381 * during the life cycle of a window, and then only the first time
382 * SetOpenerWindow is called. It might never be true, of course, if the
383 * window does not have an opener when it's created.
385 virtual void SetOpenerWindow(nsIDOMWindowInternal
*aOpener
,
386 PRBool aOriginalOpener
) = 0;
388 virtual void EnsureSizeUpToDate() = 0;
391 * Callback for notifying a window about a modal dialog being
392 * opened/closed with the window as a parent.
394 virtual void EnterModalState() = 0;
395 virtual void LeaveModalState() = 0;
397 virtual PRBool
CanClose() = 0;
398 virtual nsresult
ForceClose() = 0;
400 PRBool
IsModalContentWindow() const
402 return mIsModalContentWindow
;
406 * Call this to indicate that some node (this window, its document,
407 * or content in that document) has a paint event listener.
409 void SetHasPaintEventListeners()
411 mMayHavePaintEventListener
= PR_TRUE
;
415 * Call this to check whether some node (this window, its document,
416 * or content in that document) has a paint event listener.
418 PRBool
HasPaintEventListeners()
420 return mMayHavePaintEventListener
;
424 * Call this to indicate that some node (this window, its document,
425 * or content in that document) has a touch event listener.
427 void SetHasTouchEventListeners()
429 mMayHaveTouchEventListener
= PR_TRUE
;
430 MaybeUpdateTouchState();
434 * Call this to check whether some node (this window, its document,
435 * or content in that document) has a MozAudioAvailable event listener.
437 PRBool
HasAudioAvailableEventListeners()
439 return mMayHaveAudioAvailableEventListener
;
443 * Call this to indicate that some node (this window, its document,
444 * or content in that document) has a MozAudioAvailable event listener.
446 void SetHasAudioAvailableEventListeners()
448 mMayHaveAudioAvailableEventListener
= PR_TRUE
;
452 * Initialize window.java and window.Packages.
454 virtual void InitJavaProperties() = 0;
456 virtual void* GetCachedXBLPrototypeHandler(nsXBLPrototypeHandler
* aKey
) = 0;
457 virtual void CacheXBLPrototypeHandler(nsXBLPrototypeHandler
* aKey
,
458 nsScriptObjectHolder
& aHandler
) = 0;
461 * Get and set the currently focused element within the document. If
462 * aNeedsFocus is true, then set mNeedsFocus to true to indicate that a
463 * document focus event is needed.
465 * DO NOT CALL EITHER OF THESE METHODS DIRECTLY. USE THE FOCUS MANAGER
468 nsIContent
* GetFocusedNode()
470 if (IsOuterWindow()) {
471 return mInnerWindow
? mInnerWindow
->mFocusedNode
.get() : nsnull
;
475 virtual void SetFocusedNode(nsIContent
* aNode
,
476 PRUint32 aFocusMethod
= 0,
477 PRBool aNeedsFocus
= PR_FALSE
) = 0;
480 * Retrieves the method that was used to focus the current node.
482 virtual PRUint32
GetFocusMethod() = 0;
485 * Tells the window that it now has focus or has lost focus, based on the
486 * state of aFocus. If this method returns true, then the document loaded
487 * in the window has never received a focus event and expects to receive
488 * one. If false is returned, the document has received a focus event before
489 * and should only receive one if the window is being focused.
491 * aFocusMethod may be set to one of the focus method constants in
492 * nsIFocusManager to indicate how focus was set.
494 virtual PRBool
TakeFocus(PRBool aFocus
, PRUint32 aFocusMethod
) = 0;
497 * Indicates that the window may now accept a document focus event. This
498 * should be called once a document has been loaded into the window.
500 virtual void SetReadyForFocus() = 0;
503 * Whether the focused content within the window should show a focus ring.
505 virtual PRBool
ShouldShowFocusRing() = 0;
508 * Set the keyboard indicator state for accelerators and focus rings.
510 virtual void SetKeyboardIndicators(UIStateChangeType aShowAccelerators
,
511 UIStateChangeType aShowFocusRings
) = 0;
514 * Get the keyboard indicator state for accelerators and focus rings.
516 virtual void GetKeyboardIndicators(PRBool
* aShowAccelerators
,
517 PRBool
* aShowFocusRings
) = 0;
520 * Indicates that the page in the window has been hidden. This is used to
521 * reset the focus state.
523 virtual void PageHidden() = 0;
526 * Instructs this window to synchronously dispatch a hashchange event.
528 virtual nsresult
DispatchSyncHashchange() = 0;
531 * Instructs this window to synchronously dispatch a popState event.
533 virtual nsresult
DispatchSyncPopState() = 0;
536 * Tell this window that there is an observer for orientation changes
538 virtual void SetHasOrientationEventListener() = 0;
541 * Set a arguments for this window. This will be set on the window
542 * right away (if there's an existing document) and it will also be
543 * installed on the window when the next document is loaded. Each
544 * language impl is responsible for converting to an array of args
545 * as appropriate for that language.
547 virtual nsresult
SetArguments(nsIArray
*aArguments
, nsIPrincipal
*aOrigin
) = 0;
550 // The nsPIDOMWindow constructor. The aOuterWindow argument should
551 // be null if and only if the created window itself is an outer
552 // window. In all other cases aOuterWindow should be the outer
553 // window for the inner window that is being created.
554 nsPIDOMWindow(nsPIDOMWindow
*aOuterWindow
);
558 void SetChromeEventHandlerInternal(nsPIDOMEventTarget
* aChromeEventHandler
) {
559 mChromeEventHandler
= aChromeEventHandler
;
560 // mParentTarget will be set when the next event is dispatched.
561 mParentTarget
= nsnull
;
564 virtual void UpdateParentTarget() = 0;
566 // These two variables are special in that they're set to the same
567 // value on both the outer window and the current inner window. Make
568 // sure you keep them in sync!
569 nsCOMPtr
<nsPIDOMEventTarget
> mChromeEventHandler
; // strong
570 nsCOMPtr
<nsIDOMDocument
> mDocument
; // strong
572 nsCOMPtr
<nsPIDOMEventTarget
> mParentTarget
; // strong
574 // These members are only used on outer windows.
575 nsCOMPtr
<nsIDOMElement
> mFrameElement
;
576 nsIDocShell
*mDocShell
; // Weak Reference
578 PRUint32 mModalStateDepth
;
580 // These variables are only used on inner windows.
581 nsTimeout
*mRunningTimeout
;
583 PRUint32 mMutationBits
;
585 PRPackedBool mIsDocumentLoaded
;
586 PRPackedBool mIsHandlingResizeEvent
;
587 PRPackedBool mIsInnerWindow
;
588 PRPackedBool mMayHavePaintEventListener
;
589 PRPackedBool mMayHaveTouchEventListener
;
590 PRPackedBool mMayHaveAudioAvailableEventListener
;
592 // This variable is used on both inner and outer windows (and they
594 PRPackedBool mIsModalContentWindow
;
596 // Tracks activation state that's used for :-moz-window-inactive.
597 PRPackedBool mIsActive
;
599 // And these are the references between inner and outer windows.
600 nsPIDOMWindow
*mInnerWindow
;
601 nsPIDOMWindow
*mOuterWindow
;
603 // the element within the document that is currently focused when this
605 nsCOMPtr
<nsIContent
> mFocusedNode
;
609 NS_DEFINE_STATIC_IID_ACCESSOR(nsPIDOMWindow
, NS_PIDOMWINDOW_IID
)
611 #ifdef _IMPL_NS_LAYOUT
613 PushPopupControlState(PopupControlState aState
, PRBool aForce
);
616 PopPopupControlState(PopupControlState aState
);
618 #define NS_AUTO_POPUP_STATE_PUSHER nsAutoPopupStatePusherInternal
620 #define NS_AUTO_POPUP_STATE_PUSHER nsAutoPopupStatePusherExternal
623 // Helper class that helps with pushing and popping popup control
624 // state. Note that this class looks different from within code that's
625 // part of the layout library than it does in code outside the layout
626 // library. We give the two object layouts different names so the symbols
627 // don't conflict, but code should always use the name
628 // |nsAutoPopupStatePusher|.
629 class NS_AUTO_POPUP_STATE_PUSHER
632 #ifdef _IMPL_NS_LAYOUT
633 NS_AUTO_POPUP_STATE_PUSHER(PopupControlState aState
, PRBool aForce
= PR_FALSE
)
634 : mOldState(::PushPopupControlState(aState
, aForce
))
638 ~NS_AUTO_POPUP_STATE_PUSHER()
640 PopPopupControlState(mOldState
);
643 NS_AUTO_POPUP_STATE_PUSHER(nsPIDOMWindow
*aWindow
, PopupControlState aState
)
644 : mWindow(aWindow
), mOldState(openAbused
)
647 mOldState
= aWindow
->PushPopupControlState(aState
, PR_FALSE
);
651 ~NS_AUTO_POPUP_STATE_PUSHER()
654 mWindow
->PopPopupControlState(mOldState
);
660 #ifndef _IMPL_NS_LAYOUT
661 nsCOMPtr
<nsPIDOMWindow
> mWindow
;
663 PopupControlState mOldState
;
666 // Hide so that this class can only be stack-allocated
667 static void* operator new(size_t /*size*/) CPP_THROW_NEW
{ return nsnull
; }
668 static void operator delete(void* /*memory*/) {}
671 #define nsAutoPopupStatePusher NS_AUTO_POPUP_STATE_PUSHER
673 #endif // nsPIDOMWindow_h__