diff -r 000000000000 -r 6474c204b198 dom/base/nsWindowRoot.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dom/base/nsWindowRoot.cpp Wed Dec 31 06:09:35 2014 +0100 @@ -0,0 +1,302 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/BasicEvents.h" +#include "mozilla/EventDispatcher.h" +#include "mozilla/EventListenerManager.h" +#include "nsCOMPtr.h" +#include "nsWindowRoot.h" +#include "nsPIDOMWindow.h" +#include "nsPresContext.h" +#include "nsLayoutCID.h" +#include "nsContentCID.h" +#include "nsString.h" +#include "nsGlobalWindow.h" +#include "nsFocusManager.h" +#include "nsIContent.h" +#include "nsIDOMHTMLInputElement.h" +#include "nsIDOMHTMLTextAreaElement.h" +#include "nsIControllers.h" +#include "nsIController.h" + +#include "nsCycleCollectionParticipant.h" + +#ifdef MOZ_XUL +#include "nsIDOMXULElement.h" +#endif + +using namespace mozilla; +using namespace mozilla::dom; + +nsWindowRoot::nsWindowRoot(nsPIDOMWindow* aWindow) +{ + mWindow = aWindow; +} + +nsWindowRoot::~nsWindowRoot() +{ + if (mListenerManager) { + mListenerManager->Disconnect(); + } +} + +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_4(nsWindowRoot, + mWindow, + mListenerManager, + mPopupNode, + mParent) + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsWindowRoot) + NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY + NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMEventTarget) + NS_INTERFACE_MAP_ENTRY(nsPIWindowRoot) + NS_INTERFACE_MAP_ENTRY(nsIDOMEventTarget) + NS_INTERFACE_MAP_ENTRY(mozilla::dom::EventTarget) +NS_INTERFACE_MAP_END + +NS_IMPL_CYCLE_COLLECTING_ADDREF(nsWindowRoot) +NS_IMPL_CYCLE_COLLECTING_RELEASE(nsWindowRoot) + +NS_IMPL_DOMTARGET_DEFAULTS(nsWindowRoot) + +NS_IMETHODIMP +nsWindowRoot::RemoveEventListener(const nsAString& aType, nsIDOMEventListener* aListener, bool aUseCapture) +{ + if (nsRefPtr elm = GetExistingListenerManager()) { + elm->RemoveEventListener(aType, aListener, aUseCapture); + } + return NS_OK; +} + +NS_IMPL_REMOVE_SYSTEM_EVENT_LISTENER(nsWindowRoot) + +NS_IMETHODIMP +nsWindowRoot::DispatchEvent(nsIDOMEvent* aEvt, bool *aRetVal) +{ + nsEventStatus status = nsEventStatus_eIgnore; + nsresult rv = EventDispatcher::DispatchDOMEvent( + static_cast(this), nullptr, aEvt, nullptr, &status); + *aRetVal = (status != nsEventStatus_eConsumeNoDefault); + return rv; +} + +nsresult +nsWindowRoot::DispatchDOMEvent(WidgetEvent* aEvent, + nsIDOMEvent* aDOMEvent, + nsPresContext* aPresContext, + nsEventStatus* aEventStatus) +{ + return EventDispatcher::DispatchDOMEvent(static_cast(this), + aEvent, aDOMEvent, + aPresContext, aEventStatus); +} + +NS_IMETHODIMP +nsWindowRoot::AddEventListener(const nsAString& aType, + nsIDOMEventListener *aListener, + bool aUseCapture, bool aWantsUntrusted, + uint8_t 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."); + + EventListenerManager* elm = GetOrCreateListenerManager(); + NS_ENSURE_STATE(elm); + elm->AddEventListener(aType, aListener, aUseCapture, aWantsUntrusted); + return NS_OK; +} + +void +nsWindowRoot::AddEventListener(const nsAString& aType, + EventListener* aListener, + bool aUseCapture, + const Nullable& aWantsUntrusted, + ErrorResult& aRv) +{ + bool wantsUntrusted = !aWantsUntrusted.IsNull() && aWantsUntrusted.Value(); + EventListenerManager* elm = GetOrCreateListenerManager(); + if (!elm) { + aRv.Throw(NS_ERROR_UNEXPECTED); + return; + } + elm->AddEventListener(aType, aListener, aUseCapture, wantsUntrusted); +} + + +NS_IMETHODIMP +nsWindowRoot::AddSystemEventListener(const nsAString& aType, + nsIDOMEventListener *aListener, + bool aUseCapture, + bool aWantsUntrusted, + uint8_t 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); +} + +EventListenerManager* +nsWindowRoot::GetOrCreateListenerManager() +{ + if (!mListenerManager) { + mListenerManager = + new EventListenerManager(static_cast(this)); + } + + return mListenerManager; +} + +EventListenerManager* +nsWindowRoot::GetExistingListenerManager() const +{ + return mListenerManager; +} + +nsIScriptContext* +nsWindowRoot::GetContextForEventHandlers(nsresult* aRv) +{ + *aRv = NS_OK; + return nullptr; +} + +nsresult +nsWindowRoot::PreHandleEvent(EventChainPreVisitor& aVisitor) +{ + aVisitor.mCanHandle = true; + aVisitor.mForceContentDispatch = true; //FIXME! Bug 329119 + // To keep mWindow alive + aVisitor.mItemData = static_cast(mWindow); + aVisitor.mParentTarget = mParent; + return NS_OK; +} + +nsresult +nsWindowRoot::PostHandleEvent(EventChainPostVisitor& aVisitor) +{ + return NS_OK; +} + +nsIDOMWindow* +nsWindowRoot::GetOwnerGlobal() +{ + return GetWindow(); +} + +nsPIDOMWindow* +nsWindowRoot::GetWindow() +{ + return mWindow; +} + +nsresult +nsWindowRoot::GetControllers(nsIControllers** aResult) +{ + *aResult = nullptr; + + // 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 focusedWindow; + nsIContent* focusedContent = + nsFocusManager::GetFocusedDescendant(mWindow, true, getter_AddRefs(focusedWindow)); + if (focusedContent) { +#ifdef MOZ_XUL + nsCOMPtr xulElement(do_QueryInterface(focusedContent)); + if (xulElement) + return xulElement->GetControllers(aResult); +#endif + + nsCOMPtr htmlTextArea = + do_QueryInterface(focusedContent); + if (htmlTextArea) + return htmlTextArea->GetControllers(aResult); + + nsCOMPtr htmlInputElement = + do_QueryInterface(focusedContent); + if (htmlInputElement) + return htmlInputElement->GetControllers(aResult); + + if (focusedContent->IsEditable() && focusedWindow) + return focusedWindow->GetControllers(aResult); + } + else { + nsCOMPtr domWindow = do_QueryInterface(focusedWindow); + if (domWindow) + return domWindow->GetControllers(aResult); + } + + return NS_OK; +} + +nsresult +nsWindowRoot::GetControllerForCommand(const char * aCommand, + nsIController** _retval) +{ + NS_ENSURE_ARG_POINTER(_retval); + *_retval = nullptr; + + { + nsCOMPtr controllers; + GetControllers(getter_AddRefs(controllers)); + if (controllers) { + nsCOMPtr controller; + controllers->GetControllerForCommand(aCommand, getter_AddRefs(controller)); + if (controller) { + controller.forget(_retval); + return NS_OK; + } + } + } + + nsCOMPtr focusedWindow; + nsFocusManager::GetFocusedDescendant(mWindow, true, getter_AddRefs(focusedWindow)); + while (focusedWindow) { + nsCOMPtr controllers; + focusedWindow->GetControllers(getter_AddRefs(controllers)); + if (controllers) { + nsCOMPtr controller; + controllers->GetControllerForCommand(aCommand, + getter_AddRefs(controller)); + if (controller) { + controller.forget(_retval); + return NS_OK; + } + } + + // XXXndeakin P3 is this casting safe? + nsGlobalWindow *win = static_cast(focusedWindow.get()); + focusedWindow = win->GetPrivateParent(); + } + + return NS_OK; +} + +nsIDOMNode* +nsWindowRoot::GetPopupNode() +{ + return mPopupNode; +} + +void +nsWindowRoot::SetPopupNode(nsIDOMNode* aNode) +{ + mPopupNode = aNode; +} + +/////////////////////////////////////////////////////////////////////////////////// + +already_AddRefed +NS_NewWindowRoot(nsPIDOMWindow* aWindow) +{ + nsCOMPtr result = new nsWindowRoot(aWindow); + return result.forget(); +}