Bug 513162 - Widget additions for recycling top level widgets as content containers...
[mozilla-central.git] / dom / base / nsPIDOMWindow.h
blobac47662b2d559b0409f569363544f9bc4a8d7535
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
14 * License.
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.
23 * Contributor(s):
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"
50 #include "nsCOMPtr.h"
51 #include "nsEvent.h"
52 #include "nsGUIEvent.h"
54 #define DOM_WINDOW_DESTROYED_TOPIC "dom-window-destroyed"
56 class nsIPrincipal;
58 // Popup control state enum. The values in this enum must go from most
59 // permissive to least permissive so that it's safe to push state in
60 // all situations. Pushing popup state onto the stack never makes the
61 // current popup state less permissive (see
62 // nsGlobalWindow::PushPopupControlState()).
63 enum PopupControlState {
64 openAllowed = 0, // open that window without worries
65 openControlled, // it's a popup, but allow it
66 openAbused, // it's a popup. disallow it, but allow domain override.
67 openOverridden // disallow window open
70 class nsIDocShell;
71 class nsIContent;
72 class nsIDocument;
73 class nsIScriptTimeoutHandler;
74 struct nsTimeout;
75 class nsScriptObjectHolder;
76 class nsXBLPrototypeHandler;
77 class nsIArray;
78 class nsPIWindowRoot;
80 #define NS_PIDOMWINDOW_IID \
81 { 0x4beac1da, 0x513e, 0x4a8b, \
82 { 0x96, 0x94, 0x1c, 0xf6, 0x4f, 0xba, 0xa8, 0x1c } }
84 class nsPIDOMWindow : public nsIDOMWindowInternal
86 public:
87 NS_DECLARE_STATIC_IID_ACCESSOR(NS_PIDOMWINDOW_IID)
89 virtual nsPIDOMWindow* GetPrivateRoot() = 0;
91 virtual void ActivateOrDeactivate(PRBool aActivate) = 0;
93 // this is called GetTopWindowRoot to avoid conflicts with nsIDOMWindow2::GetWindowRoot
94 virtual already_AddRefed<nsPIWindowRoot> GetTopWindowRoot() = 0;
96 virtual void SetActive(PRBool aActive)
98 mIsActive = aActive;
101 PRBool IsActive()
103 return mIsActive;
106 nsPIDOMEventTarget* GetChromeEventHandler() const
108 return mChromeEventHandler;
111 virtual void SetChromeEventHandler(nsPIDOMEventTarget* aChromeEventHandler) = 0;
113 nsPIDOMEventTarget* GetParentTarget()
115 if (!mParentTarget) {
116 UpdateParentTarget();
118 return mParentTarget;
121 PRBool HasMutationListeners(PRUint32 aMutationEventType) const
123 const nsPIDOMWindow *win;
125 if (IsOuterWindow()) {
126 win = GetCurrentInnerWindow();
128 if (!win) {
129 NS_ERROR("No current inner window available!");
131 return PR_FALSE;
133 } else {
134 if (!mOuterWindow) {
135 NS_ERROR("HasMutationListeners() called on orphan inner window!");
137 return PR_FALSE;
140 win = this;
143 return (win->mMutationBits & aMutationEventType) != 0;
146 void SetMutationListeners(PRUint32 aType)
148 nsPIDOMWindow *win;
150 if (IsOuterWindow()) {
151 win = GetCurrentInnerWindow();
153 if (!win) {
154 NS_ERROR("No inner window available to set mutation bits on!");
156 return;
158 } else {
159 if (!mOuterWindow) {
160 NS_ERROR("HasMutationListeners() called on orphan inner window!");
162 return;
165 win = this;
168 win->mMutationBits |= aType;
171 // GetExtantDocument provides a backdoor to the DOM GetDocument accessor
172 nsIDOMDocument* GetExtantDocument() const
174 return mDocument;
177 // Internal getter/setter for the frame element, this version of the
178 // getter crosses chrome boundaries whereas the public scriptable
179 // one doesn't for security reasons.
180 nsIDOMElement* GetFrameElementInternal() const
182 if (mOuterWindow) {
183 return mOuterWindow->GetFrameElementInternal();
186 NS_ASSERTION(!IsInnerWindow(),
187 "GetFrameElementInternal() called on orphan inner window");
189 return mFrameElement;
192 void SetFrameElementInternal(nsIDOMElement *aFrameElement)
194 if (IsOuterWindow()) {
195 mFrameElement = aFrameElement;
197 return;
200 if (!mOuterWindow) {
201 NS_ERROR("frameElement set on inner window with no outer!");
203 return;
206 mOuterWindow->SetFrameElementInternal(aFrameElement);
209 PRBool IsLoadingOrRunningTimeout() const
211 const nsPIDOMWindow *win = GetCurrentInnerWindow();
213 if (!win) {
214 win = this;
217 return !win->mIsDocumentLoaded || win->mRunningTimeout;
220 // Check whether a document is currently loading
221 PRBool IsLoading() const
223 const nsPIDOMWindow *win;
225 if (IsOuterWindow()) {
226 win = GetCurrentInnerWindow();
228 if (!win) {
229 NS_ERROR("No current inner window available!");
231 return PR_FALSE;
233 } else {
234 if (!mOuterWindow) {
235 NS_ERROR("IsLoading() called on orphan inner window!");
237 return PR_FALSE;
240 win = this;
243 return !win->mIsDocumentLoaded;
246 PRBool IsHandlingResizeEvent() const
248 const nsPIDOMWindow *win;
250 if (IsOuterWindow()) {
251 win = GetCurrentInnerWindow();
253 if (!win) {
254 NS_ERROR("No current inner window available!");
256 return PR_FALSE;
258 } else {
259 if (!mOuterWindow) {
260 NS_ERROR("IsHandlingResizeEvent() called on orphan inner window!");
262 return PR_FALSE;
265 win = this;
268 return win->mIsHandlingResizeEvent;
271 // Tell this window who opened it. This only has an effect if there is
272 // either no document currently in the window or if the document is the
273 // original document this window came with (an about:blank document either
274 // preloaded into it when it was created, or created by
275 // CreateAboutBlankContentViewer()).
276 virtual void SetOpenerScriptPrincipal(nsIPrincipal* aPrincipal) = 0;
277 // Ask this window who opened it.
278 virtual nsIPrincipal* GetOpenerScriptPrincipal() = 0;
280 virtual PopupControlState PushPopupControlState(PopupControlState aState,
281 PRBool aForce) const = 0;
282 virtual void PopPopupControlState(PopupControlState state) const = 0;
283 virtual PopupControlState GetPopupControlState() const = 0;
285 // Returns an object containing the window's state. This also suspends
286 // all running timeouts in the window.
287 virtual nsresult SaveWindowState(nsISupports **aState) = 0;
289 // Restore the window state from aState.
290 virtual nsresult RestoreWindowState(nsISupports *aState) = 0;
292 // Suspend timeouts in this window and in child windows.
293 virtual void SuspendTimeouts(PRUint32 aIncrease = 1,
294 PRBool aFreezeChildren = PR_TRUE) = 0;
296 // Resume suspended timeouts in this window and in child windows.
297 virtual nsresult ResumeTimeouts(PRBool aThawChildren = PR_TRUE) = 0;
299 virtual PRUint32 TimeoutSuspendCount() = 0;
301 // Fire any DOM notification events related to things that happened while
302 // the window was frozen.
303 virtual nsresult FireDelayedDOMEvents() = 0;
305 virtual PRBool IsFrozen() const = 0;
307 // Add a timeout to this window.
308 virtual nsresult SetTimeoutOrInterval(nsIScriptTimeoutHandler *aHandler,
309 PRInt32 interval,
310 PRBool aIsInterval, PRInt32 *aReturn) = 0;
312 // Clear a timeout from this window.
313 virtual nsresult ClearTimeoutOrInterval(PRInt32 aTimerID) = 0;
315 nsPIDOMWindow *GetOuterWindow()
317 return mIsInnerWindow ? mOuterWindow : this;
320 nsPIDOMWindow *GetCurrentInnerWindow() const
322 return mInnerWindow;
325 nsPIDOMWindow *EnsureInnerWindow()
327 NS_ASSERTION(IsOuterWindow(), "EnsureInnerWindow called on inner window");
328 // GetDocument forces inner window creation if there isn't one already
329 nsCOMPtr<nsIDOMDocument> doc;
330 GetDocument(getter_AddRefs(doc));
331 return GetCurrentInnerWindow();
334 PRBool IsInnerWindow() const
336 return mIsInnerWindow;
339 PRBool IsOuterWindow() const
341 return !IsInnerWindow();
344 virtual PRBool WouldReuseInnerWindow(nsIDocument *aNewDocument) = 0;
347 * Get the docshell in this window.
349 nsIDocShell *GetDocShell()
351 if (mOuterWindow) {
352 return mOuterWindow->mDocShell;
355 return mDocShell;
359 * Set or unset the docshell in the window.
361 virtual void SetDocShell(nsIDocShell *aDocShell) = 0;
364 * Set a new document in the window. Calling this method will in
365 * most cases create a new inner window. If this method is called on
366 * an inner window the call will be forewarded to the outer window,
367 * if the inner window is not the current inner window an
368 * NS_ERROR_NOT_AVAILABLE error code will be returned. This may be
369 * called with a pointer to the current document, in that case the
370 * document remains unchanged, but a new inner window will be
371 * created.
373 virtual nsresult SetNewDocument(nsIDocument *aDocument,
374 nsISupports *aState) = 0;
377 * Set the opener window. aOriginalOpener is true if and only if this is the
378 * original opener for the window. That is, it can only be true at most once
379 * during the life cycle of a window, and then only the first time
380 * SetOpenerWindow is called. It might never be true, of course, if the
381 * window does not have an opener when it's created.
383 virtual void SetOpenerWindow(nsIDOMWindowInternal *aOpener,
384 PRBool aOriginalOpener) = 0;
386 virtual void EnsureSizeUpToDate() = 0;
389 * Callback for notifying a window about a modal dialog being
390 * opened/closed with the window as a parent.
392 virtual void EnterModalState() = 0;
393 virtual void LeaveModalState() = 0;
395 virtual PRBool CanClose() = 0;
396 virtual nsresult ForceClose() = 0;
398 void SetModalContentWindow(PRBool aIsModalContentWindow)
400 mIsModalContentWindow = aIsModalContentWindow;
403 PRBool IsModalContentWindow() const
405 return mIsModalContentWindow;
409 * Call this to indicate that some node (this window, its document,
410 * or content in that document) has a paint event listener.
412 void SetHasPaintEventListeners()
414 mMayHavePaintEventListener = PR_TRUE;
418 * Call this to check whether some node (this window, its document,
419 * or content in that document) has a paint event listener.
421 PRBool HasPaintEventListeners()
423 return mMayHavePaintEventListener;
427 * Initialize window.java and window.Packages.
429 virtual void InitJavaProperties() = 0;
431 virtual void* GetCachedXBLPrototypeHandler(nsXBLPrototypeHandler* aKey) = 0;
432 virtual void CacheXBLPrototypeHandler(nsXBLPrototypeHandler* aKey,
433 nsScriptObjectHolder& aHandler) = 0;
436 * Get and set the currently focused element within the document. If
437 * aNeedsFocus is true, then set mNeedsFocus to true to indicate that a
438 * document focus event is needed.
440 * DO NOT CALL EITHER OF THESE METHODS DIRECTLY. USE THE FOCUS MANAGER
441 * INSTEAD.
443 nsIContent* GetFocusedNode()
445 if (IsOuterWindow()) {
446 return mInnerWindow ? mInnerWindow->mFocusedNode.get() : nsnull;
448 return mFocusedNode;
450 virtual void SetFocusedNode(nsIContent* aNode,
451 PRUint32 aFocusMethod = 0,
452 PRBool aNeedsFocus = PR_FALSE) = 0;
455 * Retrieves the method that was used to focus the current node.
457 virtual PRUint32 GetFocusMethod() = 0;
460 * Tells the window that it now has focus or has lost focus, based on the
461 * state of aFocus. If this method returns true, then the document loaded
462 * in the window has never received a focus event and expects to receive
463 * one. If false is returned, the document has received a focus event before
464 * and should only receive one if the window is being focused.
466 * aFocusMethod may be set to one of the focus method constants in
467 * nsIFocusManager to indicate how focus was set.
469 virtual PRBool TakeFocus(PRBool aFocus, PRUint32 aFocusMethod) = 0;
472 * Indicates that the window may now accept a document focus event. This
473 * should be called once a document has been loaded into the window.
475 virtual void SetReadyForFocus() = 0;
478 * Whether the focused content within the window should show a focus ring.
480 virtual PRBool ShouldShowFocusRing() = 0;
483 * Set the keyboard indicator state for accelerators and focus rings.
485 virtual void SetKeyboardIndicators(UIStateChangeType aShowAccelerators,
486 UIStateChangeType aShowFocusRings) = 0;
489 * Get the keyboard indicator state for accelerators and focus rings.
491 virtual void GetKeyboardIndicators(PRBool* aShowAccelerators,
492 PRBool* aShowFocusRings) = 0;
495 * Indicates that the page in the window has been hidden. This is used to
496 * reset the focus state.
498 virtual void PageHidden() = 0;
501 * Instructs this window to synchronously dispatch a hashchange event.
503 virtual nsresult DispatchSyncHashchange() = 0;
506 * Instructs this window to synchronously dispatch a popState event.
508 virtual nsresult DispatchSyncPopState() = 0;
511 * Tell this window that there is an observer for orientation changes
513 virtual void SetHasOrientationEventListener() = 0;
516 * Set a arguments for this window. This will be set on the window
517 * right away (if there's an existing document) and it will also be
518 * installed on the window when the next document is loaded. Each
519 * language impl is responsible for converting to an array of args
520 * as appropriate for that language.
522 virtual nsresult SetArguments(nsIArray *aArguments, nsIPrincipal *aOrigin) = 0;
524 protected:
525 // The nsPIDOMWindow constructor. The aOuterWindow argument should
526 // be null if and only if the created window itself is an outer
527 // window. In all other cases aOuterWindow should be the outer
528 // window for the inner window that is being created.
529 nsPIDOMWindow(nsPIDOMWindow *aOuterWindow);
531 ~nsPIDOMWindow();
533 void SetChromeEventHandlerInternal(nsPIDOMEventTarget* aChromeEventHandler) {
534 mChromeEventHandler = aChromeEventHandler;
535 // mParentTarget will be set when the next event is dispatched.
536 mParentTarget = nsnull;
539 virtual void UpdateParentTarget() = 0;
541 // These two variables are special in that they're set to the same
542 // value on both the outer window and the current inner window. Make
543 // sure you keep them in sync!
544 nsCOMPtr<nsPIDOMEventTarget> mChromeEventHandler; // strong
545 nsCOMPtr<nsIDOMDocument> mDocument; // strong
547 nsCOMPtr<nsPIDOMEventTarget> mParentTarget; // strong
549 // These members are only used on outer windows.
550 nsCOMPtr<nsIDOMElement> mFrameElement;
551 nsIDocShell *mDocShell; // Weak Reference
553 PRUint32 mModalStateDepth;
555 // These variables are only used on inner windows.
556 nsTimeout *mRunningTimeout;
558 PRUint32 mMutationBits;
560 PRPackedBool mIsDocumentLoaded;
561 PRPackedBool mIsHandlingResizeEvent;
562 PRPackedBool mIsInnerWindow;
563 PRPackedBool mMayHavePaintEventListener;
565 // This variable is used on both inner and outer windows (and they
566 // should match).
567 PRPackedBool mIsModalContentWindow;
569 // Tracks activation state that's used for :-moz-window-inactive.
570 PRPackedBool mIsActive;
572 // And these are the references between inner and outer windows.
573 nsPIDOMWindow *mInnerWindow;
574 nsPIDOMWindow *mOuterWindow;
576 // the element within the document that is currently focused when this
577 // window is active
578 nsCOMPtr<nsIContent> mFocusedNode;
582 NS_DEFINE_STATIC_IID_ACCESSOR(nsPIDOMWindow, NS_PIDOMWINDOW_IID)
584 #ifdef _IMPL_NS_LAYOUT
585 PopupControlState
586 PushPopupControlState(PopupControlState aState, PRBool aForce);
588 void
589 PopPopupControlState(PopupControlState aState);
591 #define NS_AUTO_POPUP_STATE_PUSHER nsAutoPopupStatePusherInternal
592 #else
593 #define NS_AUTO_POPUP_STATE_PUSHER nsAutoPopupStatePusherExternal
594 #endif
596 // Helper class that helps with pushing and popping popup control
597 // state. Note that this class looks different from within code that's
598 // part of the layout library than it does in code outside the layout
599 // library. We give the two object layouts different names so the symbols
600 // don't conflict, but code should always use the name
601 // |nsAutoPopupStatePusher|.
602 class NS_AUTO_POPUP_STATE_PUSHER
604 public:
605 #ifdef _IMPL_NS_LAYOUT
606 NS_AUTO_POPUP_STATE_PUSHER(PopupControlState aState, PRBool aForce = PR_FALSE)
607 : mOldState(::PushPopupControlState(aState, aForce))
611 ~NS_AUTO_POPUP_STATE_PUSHER()
613 PopPopupControlState(mOldState);
615 #else
616 NS_AUTO_POPUP_STATE_PUSHER(nsPIDOMWindow *aWindow, PopupControlState aState)
617 : mWindow(aWindow), mOldState(openAbused)
619 if (aWindow) {
620 mOldState = aWindow->PushPopupControlState(aState, PR_FALSE);
624 ~NS_AUTO_POPUP_STATE_PUSHER()
626 if (mWindow) {
627 mWindow->PopPopupControlState(mOldState);
630 #endif
632 protected:
633 #ifndef _IMPL_NS_LAYOUT
634 nsCOMPtr<nsPIDOMWindow> mWindow;
635 #endif
636 PopupControlState mOldState;
638 private:
639 // Hide so that this class can only be stack-allocated
640 static void* operator new(size_t /*size*/) CPP_THROW_NEW { return nsnull; }
641 static void operator delete(void* /*memory*/) {}
644 #define nsAutoPopupStatePusher NS_AUTO_POPUP_STATE_PUSHER
646 #endif // nsPIDOMWindow_h__