2011-10-15 00:27:57 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2007-03-22 10:30:00 -07:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is the Mozilla browser.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications, Inc.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1999
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* David W. Hyatt <hyatt@netscape.com> (Original Author)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsWindowRoot.h"
|
2010-02-20 08:07:03 -08:00
|
|
|
#include "nsPIDOMWindow.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIDocument.h"
|
2011-06-23 19:18:01 -07:00
|
|
|
#include "nsEventListenerManager.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsLayoutCID.h"
|
|
|
|
#include "nsContentCID.h"
|
|
|
|
#include "nsIPrivateDOMEvent.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsEventDispatcher.h"
|
2009-04-01 14:59:02 -07:00
|
|
|
#include "nsGUIEvent.h"
|
2010-02-20 08:07:03 -08:00
|
|
|
#include "nsGlobalWindow.h"
|
|
|
|
#include "nsFocusManager.h"
|
2010-08-04 19:40:18 -07:00
|
|
|
#include "nsIDOMHTMLInputElement.h"
|
2011-06-11 00:52:24 -07:00
|
|
|
#include "nsIDOMHTMLTextAreaElement.h"
|
2010-02-20 08:07:03 -08:00
|
|
|
#include "nsIControllers.h"
|
2007-09-21 01:54:36 -07:00
|
|
|
|
2007-09-23 09:10:12 -07:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2007-09-23 07:05:27 -07:00
|
|
|
|
2010-02-20 08:07:03 -08:00
|
|
|
#ifdef MOZ_XUL
|
|
|
|
#include "nsIDOMXULElement.h"
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static NS_DEFINE_CID(kEventListenerManagerCID, NS_EVENTLISTENERMANAGER_CID);
|
|
|
|
|
2010-02-20 08:07:03 -08:00
|
|
|
nsWindowRoot::nsWindowRoot(nsPIDOMWindow* aWindow)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mWindow = aWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsWindowRoot::~nsWindowRoot()
|
|
|
|
{
|
|
|
|
if (mListenerManager) {
|
|
|
|
mListenerManager->Disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-23 19:18:01 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsWindowRoot)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsWindowRoot)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NATIVE_MEMBER(mListenerManager,
|
|
|
|
nsEventListenerManager)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mPopupNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsWindowRoot)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mListenerManager)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mPopupNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-25 09:35:27 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsWindowRoot)
|
2007-05-14 02:11:38 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMEventTarget)
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsPIWindowRoot)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMEventTarget)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2011-03-06 03:11:31 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsWindowRoot)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsWindowRoot)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-06-23 19:17:58 -07:00
|
|
|
NS_IMPL_DOMTARGET_DEFAULTS(nsWindowRoot)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsWindowRoot::RemoveEventListener(const nsAString& aType, nsIDOMEventListener* aListener, bool aUseCapture)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
nsRefPtr<nsEventListenerManager> elm = GetListenerManager(false);
|
2011-06-23 19:18:02 -07:00
|
|
|
if (elm) {
|
|
|
|
elm->RemoveEventListener(aType, aListener, aUseCapture);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-11-24 17:09:58 -08:00
|
|
|
NS_IMPL_REMOVE_SYSTEM_EVENT_LISTENER(nsWindowRoot)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsWindowRoot::DispatchEvent(nsIDOMEvent* aEvt, bool *aRetVal)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
nsresult rv = nsEventDispatcher::DispatchDOMEvent(
|
2011-06-23 19:18:00 -07:00
|
|
|
static_cast<nsIDOMEventTarget*>(this), nsnull, aEvt, nsnull, &status);
|
2011-06-23 19:17:59 -07:00
|
|
|
*aRetVal = (status != nsEventStatus_eConsumeNoDefault);
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsWindowRoot::DispatchDOMEvent(nsEvent* aEvent,
|
|
|
|
nsIDOMEvent* aDOMEvent,
|
|
|
|
nsPresContext* aPresContext,
|
|
|
|
nsEventStatus* aEventStatus)
|
|
|
|
{
|
2011-06-23 19:18:00 -07:00
|
|
|
return nsEventDispatcher::DispatchDOMEvent(static_cast<nsIDOMEventTarget*>(this),
|
2007-03-22 10:30:00 -07:00
|
|
|
aEvent, aDOMEvent,
|
|
|
|
aPresContext, aEventStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsWindowRoot::AddEventListener(const nsAString& aType,
|
|
|
|
nsIDOMEventListener *aListener,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aUseCapture, bool aWantsUntrusted,
|
2011-06-23 19:17:59 -07:00
|
|
|
PRUint8 aOptionalArgc)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-06-23 19:17:59 -07:00
|
|
|
NS_ASSERTION(!aWantsUntrusted || aOptionalArgc > 1,
|
2010-01-13 05:50:01 -08:00
|
|
|
"Won't check if this is chrome, you want to set "
|
2011-10-17 07:59:28 -07:00
|
|
|
"aWantsUntrusted to false or make the aWantsUntrusted "
|
2010-01-13 05:50:01 -08:00
|
|
|
"explicit by making optional_argc non-zero.");
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
nsEventListenerManager* elm = GetListenerManager(true);
|
2011-06-23 19:18:02 -07:00
|
|
|
NS_ENSURE_STATE(elm);
|
2011-08-18 02:45:00 -07:00
|
|
|
elm->AddEventListener(aType, aListener, aUseCapture, aWantsUntrusted);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-11-24 17:09:58 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsWindowRoot::AddSystemEventListener(const nsAString& aType,
|
|
|
|
nsIDOMEventListener *aListener,
|
|
|
|
bool aUseCapture,
|
|
|
|
bool aWantsUntrusted,
|
|
|
|
PRUint8 aOptionalArgc)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!aWantsUntrusted || aOptionalArgc > 1,
|
|
|
|
"Won't check if this is chrome, you want to set "
|
|
|
|
"aWantsUntrusted to false or make the aWantsUntrusted "
|
|
|
|
"explicit by making optional_argc non-zero.");
|
|
|
|
|
|
|
|
return NS_AddSystemEventListener(this, aType, aListener, aUseCapture,
|
|
|
|
aWantsUntrusted);
|
|
|
|
}
|
|
|
|
|
2011-06-23 19:18:01 -07:00
|
|
|
nsEventListenerManager*
|
2011-09-28 23:19:26 -07:00
|
|
|
nsWindowRoot::GetListenerManager(bool aCreateIfNotFound)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-06-23 19:18:01 -07:00
|
|
|
if (!mListenerManager && aCreateIfNotFound) {
|
|
|
|
mListenerManager =
|
|
|
|
new nsEventListenerManager(static_cast<nsIDOMEventTarget*>(this));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-06-23 04:23:52 -07:00
|
|
|
return mListenerManager;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-06-23 19:17:58 -07:00
|
|
|
nsIScriptContext*
|
|
|
|
nsWindowRoot::GetContextForEventHandlers(nsresult* aRv)
|
|
|
|
{
|
|
|
|
*aRv = NS_OK;
|
|
|
|
return nsnull;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsWindowRoot::PreHandleEvent(nsEventChainPreVisitor& aVisitor)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
aVisitor.mCanHandle = true;
|
|
|
|
aVisitor.mForceContentDispatch = true; //FIXME! Bug 329119
|
2007-03-22 10:30:00 -07:00
|
|
|
// To keep mWindow alive
|
2010-02-20 08:07:03 -08:00
|
|
|
aVisitor.mItemData = static_cast<nsISupports *>(mWindow);
|
2010-02-20 09:05:20 -08:00
|
|
|
aVisitor.mParentTarget = mParent;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsWindowRoot::PostHandleEvent(nsEventChainPostVisitor& aVisitor)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-02-20 08:07:03 -08:00
|
|
|
nsPIDOMWindow*
|
2007-07-06 06:47:50 -07:00
|
|
|
nsWindowRoot::GetWindow()
|
|
|
|
{
|
|
|
|
return mWindow;
|
|
|
|
}
|
|
|
|
|
2010-02-20 08:07:03 -08:00
|
|
|
nsresult
|
|
|
|
nsWindowRoot::GetControllers(nsIControllers** aResult)
|
|
|
|
{
|
|
|
|
*aResult = nsnull;
|
|
|
|
|
|
|
|
// XXX: we should fix this so there's a generic interface that
|
|
|
|
// describes controllers, so this code would have no special
|
|
|
|
// knowledge of what object might have controllers.
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> focusedWindow;
|
|
|
|
nsIContent* focusedContent =
|
2011-10-17 07:59:28 -07:00
|
|
|
nsFocusManager::GetFocusedDescendant(mWindow, true, getter_AddRefs(focusedWindow));
|
2010-02-20 08:07:03 -08:00
|
|
|
if (focusedContent) {
|
|
|
|
#ifdef MOZ_XUL
|
|
|
|
nsCOMPtr<nsIDOMXULElement> xulElement(do_QueryInterface(focusedContent));
|
|
|
|
if (xulElement)
|
|
|
|
return xulElement->GetControllers(aResult);
|
|
|
|
#endif
|
|
|
|
|
2011-06-11 00:52:24 -07:00
|
|
|
nsCOMPtr<nsIDOMHTMLTextAreaElement> htmlTextArea =
|
2010-02-20 08:07:03 -08:00
|
|
|
do_QueryInterface(focusedContent);
|
|
|
|
if (htmlTextArea)
|
|
|
|
return htmlTextArea->GetControllers(aResult);
|
|
|
|
|
2010-08-04 19:40:18 -07:00
|
|
|
nsCOMPtr<nsIDOMHTMLInputElement> htmlInputElement =
|
2010-02-20 08:07:03 -08:00
|
|
|
do_QueryInterface(focusedContent);
|
|
|
|
if (htmlInputElement)
|
|
|
|
return htmlInputElement->GetControllers(aResult);
|
|
|
|
|
|
|
|
if (focusedContent->IsEditable() && focusedWindow)
|
|
|
|
return focusedWindow->GetControllers(aResult);
|
|
|
|
}
|
|
|
|
else {
|
2011-07-15 03:31:34 -07:00
|
|
|
nsCOMPtr<nsIDOMWindow> domWindow = do_QueryInterface(focusedWindow);
|
2010-02-20 08:07:03 -08:00
|
|
|
if (domWindow)
|
|
|
|
return domWindow->GetControllers(aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsWindowRoot::GetControllerForCommand(const char * aCommand,
|
|
|
|
nsIController** _retval)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
*_retval = nsnull;
|
|
|
|
|
2011-10-15 00:27:57 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIControllers> controllers;
|
|
|
|
GetControllers(getter_AddRefs(controllers));
|
|
|
|
if (controllers) {
|
|
|
|
nsCOMPtr<nsIController> controller;
|
|
|
|
controllers->GetControllerForCommand(aCommand, getter_AddRefs(controller));
|
|
|
|
if (controller) {
|
|
|
|
controller.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-02-20 08:07:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> focusedWindow;
|
2011-10-17 07:59:28 -07:00
|
|
|
nsFocusManager::GetFocusedDescendant(mWindow, true, getter_AddRefs(focusedWindow));
|
2010-02-20 08:07:03 -08:00
|
|
|
while (focusedWindow) {
|
2011-10-15 00:27:57 -07:00
|
|
|
nsCOMPtr<nsIControllers> controllers;
|
|
|
|
focusedWindow->GetControllers(getter_AddRefs(controllers));
|
|
|
|
if (controllers) {
|
|
|
|
nsCOMPtr<nsIController> controller;
|
|
|
|
controllers->GetControllerForCommand(aCommand,
|
|
|
|
getter_AddRefs(controller));
|
2010-02-20 08:07:03 -08:00
|
|
|
if (controller) {
|
2011-10-15 00:27:57 -07:00
|
|
|
controller.forget(_retval);
|
2010-02-20 08:07:03 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXXndeakin P3 is this casting safe?
|
2011-10-15 00:27:57 -07:00
|
|
|
nsGlobalWindow *win = static_cast<nsGlobalWindow*>(focusedWindow.get());
|
2010-02-20 08:07:03 -08:00
|
|
|
focusedWindow = win->GetPrivateParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-08-09 09:17:19 -07:00
|
|
|
nsIDOMNode*
|
|
|
|
nsWindowRoot::GetPopupNode()
|
2010-02-20 08:07:03 -08:00
|
|
|
{
|
2010-08-09 09:17:19 -07:00
|
|
|
return mPopupNode;
|
2010-02-20 08:07:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWindowRoot::SetPopupNode(nsIDOMNode* aNode)
|
|
|
|
{
|
|
|
|
mPopupNode = aNode;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsresult
|
2011-06-23 19:18:00 -07:00
|
|
|
NS_NewWindowRoot(nsPIDOMWindow* aWindow, nsIDOMEventTarget** aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*aResult = new nsWindowRoot(aWindow);
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|