2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2000-09-22 09:02:20 +04:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
2003-02-28 04:04:45 +03:00
|
|
|
#include "nsXBLPrototypeHandler.h"
|
2000-09-22 09:02:20 +04:00
|
|
|
#include "nsXBLWindowKeyHandler.h"
|
|
|
|
#include "nsIContent.h"
|
2017-10-03 01:05:19 +03:00
|
|
|
#include "nsAtom.h"
|
2000-09-22 09:02:20 +04:00
|
|
|
#include "nsIDOMKeyEvent.h"
|
|
|
|
#include "nsXBLService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
2007-01-30 03:06:41 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2010-07-15 05:53:11 +04:00
|
|
|
#include "nsXBLDocumentInfo.h"
|
2000-09-28 00:23:49 +04:00
|
|
|
#include "nsIDOMElement.h"
|
Bug 178324, refactor focus by moving all focus handling into one place and simplifying it, add many tests, fixes many other bugs too numerous to mention in this small checkin comment, r=josh,smichaud,ere,dbaron,marco,neil,gavin,smaug,sr=smaug (CLOSED TREE)
2009-06-10 22:00:39 +04:00
|
|
|
#include "nsFocusManager.h"
|
2007-01-09 10:33:18 +03:00
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsXBLPrototypeBinding.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsIDocShell.h"
|
2017-08-07 09:08:02 +03:00
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsISelectionController.h"
|
2007-01-09 10:33:18 +03:00
|
|
|
#include "nsIPresShell.h"
|
2016-03-16 04:58:28 +03:00
|
|
|
#include "mozilla/EventListenerManager.h"
|
2014-04-01 08:09:23 +04:00
|
|
|
#include "mozilla/EventStateManager.h"
|
2017-08-07 09:08:02 +03:00
|
|
|
#include "mozilla/HTMLEditor.h"
|
2017-06-06 02:29:04 +03:00
|
|
|
#include "mozilla/Move.h"
|
2011-05-28 11:03:00 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2017-06-06 02:29:04 +03:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2013-09-25 15:21:19 +04:00
|
|
|
#include "mozilla/TextEvents.h"
|
2011-07-20 23:18:54 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2014-03-18 19:16:47 +04:00
|
|
|
#include "mozilla/dom/Event.h"
|
2017-06-16 01:06:00 +03:00
|
|
|
#include "mozilla/layers/KeyboardMap.h"
|
2011-05-28 11:03:00 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2013-04-06 04:44:26 +04:00
|
|
|
using namespace mozilla::dom;
|
2017-06-06 02:29:04 +03:00
|
|
|
using namespace mozilla::layers;
|
2000-09-22 09:02:20 +04:00
|
|
|
|
2012-10-14 20:15:12 +04:00
|
|
|
class nsXBLSpecialDocInfo : public nsIObserver
|
2007-01-09 10:33:18 +03:00
|
|
|
{
|
|
|
|
public:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsXBLDocumentInfo> mHTMLBindings;
|
|
|
|
RefPtr<nsXBLDocumentInfo> mUserHTMLBindings;
|
2007-01-09 10:33:18 +03:00
|
|
|
|
|
|
|
static const char sHTMLBindingStr[];
|
|
|
|
static const char sUserHTMLBindingStr[];
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mInitialized;
|
2007-01-09 10:33:18 +03:00
|
|
|
|
|
|
|
public:
|
2012-10-14 20:15:12 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
2007-01-09 10:33:18 +03:00
|
|
|
void LoadDocInfo();
|
|
|
|
void GetAllHandlers(const char* aType,
|
|
|
|
nsXBLPrototypeHandler** handler,
|
|
|
|
nsXBLPrototypeHandler** userHandler);
|
2010-07-15 05:53:11 +04:00
|
|
|
void GetHandlers(nsXBLDocumentInfo* aInfo,
|
2007-01-09 10:33:18 +03:00
|
|
|
const nsACString& aRef,
|
|
|
|
nsXBLPrototypeHandler** aResult);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
nsXBLSpecialDocInfo() : mInitialized(false) {}
|
2012-10-14 20:15:12 +04:00
|
|
|
|
2014-06-23 23:56:07 +04:00
|
|
|
protected:
|
2012-10-14 20:15:12 +04:00
|
|
|
virtual ~nsXBLSpecialDocInfo() {}
|
|
|
|
|
2007-01-09 10:33:18 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const char nsXBLSpecialDocInfo::sHTMLBindingStr[] =
|
|
|
|
"chrome://global/content/platformHTMLBindings.xml";
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsXBLSpecialDocInfo, nsIObserver)
|
2012-10-14 20:15:12 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsXBLSpecialDocInfo::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* aData)
|
2012-10-14 20:15:12 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!strcmp(aTopic, "xpcom-shutdown"), "wrong topic");
|
|
|
|
|
|
|
|
// On shutdown, clear our fields to avoid an extra cycle collection.
|
|
|
|
mHTMLBindings = nullptr;
|
|
|
|
mUserHTMLBindings = nullptr;
|
|
|
|
mInitialized = false;
|
|
|
|
nsContentUtils::UnregisterShutdownObserver(this);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-01-09 10:33:18 +03:00
|
|
|
void nsXBLSpecialDocInfo::LoadDocInfo()
|
|
|
|
{
|
|
|
|
if (mInitialized)
|
|
|
|
return;
|
2011-10-17 18:59:28 +04:00
|
|
|
mInitialized = true;
|
2012-10-14 20:15:12 +04:00
|
|
|
nsContentUtils::RegisterShutdownObserver(this);
|
2007-01-09 10:33:18 +03:00
|
|
|
|
2012-05-23 22:46:04 +04:00
|
|
|
nsXBLService* xblService = nsXBLService::GetInstance();
|
|
|
|
if (!xblService)
|
2007-01-09 10:33:18 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Obtain the platform doc info
|
|
|
|
nsCOMPtr<nsIURI> bindingURI;
|
|
|
|
NS_NewURI(getter_AddRefs(bindingURI), sHTMLBindingStr);
|
|
|
|
if (!bindingURI) {
|
|
|
|
return;
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
xblService->LoadBindingDocumentInfo(nullptr, nullptr,
|
2007-01-09 10:33:18 +03:00
|
|
|
bindingURI,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr,
|
2017-05-10 06:45:11 +03:00
|
|
|
true,
|
2007-01-09 10:33:18 +03:00
|
|
|
getter_AddRefs(mHTMLBindings));
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// GetHandlers
|
|
|
|
//
|
2017-05-10 06:45:11 +03:00
|
|
|
//
|
2007-01-09 10:33:18 +03:00
|
|
|
void
|
2010-07-15 05:53:11 +04:00
|
|
|
nsXBLSpecialDocInfo::GetHandlers(nsXBLDocumentInfo* aInfo,
|
2007-01-09 10:33:18 +03:00
|
|
|
const nsACString& aRef,
|
|
|
|
nsXBLPrototypeHandler** aResult)
|
|
|
|
{
|
2010-07-15 05:55:54 +04:00
|
|
|
nsXBLPrototypeBinding* binding = aInfo->GetPrototypeBinding(aRef);
|
2017-05-10 06:45:11 +03:00
|
|
|
|
2007-01-09 10:33:18 +03:00
|
|
|
NS_ASSERTION(binding, "No binding found for the XBL window key handler.");
|
|
|
|
if (!binding)
|
|
|
|
return;
|
|
|
|
|
|
|
|
*aResult = binding->GetPrototypeHandlers();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsXBLSpecialDocInfo::GetAllHandlers(const char* aType,
|
|
|
|
nsXBLPrototypeHandler** aHandler,
|
|
|
|
nsXBLPrototypeHandler** aUserHandler)
|
|
|
|
{
|
|
|
|
if (mUserHTMLBindings) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString type(aType);
|
2014-05-22 07:48:51 +04:00
|
|
|
type.AppendLiteral("User");
|
2007-01-09 10:33:18 +03:00
|
|
|
GetHandlers(mUserHTMLBindings, type, aUserHandler);
|
|
|
|
}
|
|
|
|
if (mHTMLBindings) {
|
|
|
|
GetHandlers(mHTMLBindings, nsDependentCString(aType), aHandler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init statics
|
2017-06-06 02:29:04 +03:00
|
|
|
static StaticRefPtr<nsXBLSpecialDocInfo> sXBLSpecialDocInfo;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t nsXBLWindowKeyHandler::sRefCnt = 0;
|
2007-01-09 10:33:18 +03:00
|
|
|
|
2017-06-06 02:29:04 +03:00
|
|
|
/* static */ void
|
|
|
|
nsXBLWindowKeyHandler::EnsureSpecialDocInfo()
|
|
|
|
{
|
|
|
|
if (!sXBLSpecialDocInfo) {
|
|
|
|
sXBLSpecialDocInfo = new nsXBLSpecialDocInfo();
|
|
|
|
}
|
|
|
|
sXBLSpecialDocInfo->LoadDocInfo();
|
|
|
|
}
|
|
|
|
|
2007-05-14 13:11:38 +04:00
|
|
|
nsXBLWindowKeyHandler::nsXBLWindowKeyHandler(nsIDOMElement* aElement,
|
2013-04-14 22:27:33 +04:00
|
|
|
EventTarget* aTarget)
|
2007-05-14 13:11:38 +04:00
|
|
|
: mTarget(aTarget),
|
2012-07-30 18:20:58 +04:00
|
|
|
mHandler(nullptr),
|
|
|
|
mUserHandler(nullptr)
|
2000-09-22 09:02:20 +04:00
|
|
|
{
|
2007-01-09 10:33:18 +03:00
|
|
|
mWeakPtrForElement = do_GetWeakReference(aElement);
|
|
|
|
++sRefCnt;
|
2000-09-22 09:02:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsXBLWindowKeyHandler::~nsXBLWindowKeyHandler()
|
|
|
|
{
|
2006-11-15 09:11:42 +03:00
|
|
|
// If mWeakPtrForElement is non-null, we created a prototype handler.
|
|
|
|
if (mWeakPtrForElement)
|
2003-03-07 02:59:18 +03:00
|
|
|
delete mHandler;
|
2007-01-09 10:33:18 +03:00
|
|
|
|
|
|
|
--sRefCnt;
|
|
|
|
if (!sRefCnt) {
|
2017-06-06 02:29:04 +03:00
|
|
|
sXBLSpecialDocInfo = nullptr;
|
2007-01-09 10:33:18 +03:00
|
|
|
}
|
2000-09-22 09:02:20 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsXBLWindowKeyHandler,
|
|
|
|
nsIDOMEventListener)
|
2000-09-22 09:02:20 +04:00
|
|
|
|
2001-11-02 04:53:13 +03:00
|
|
|
static void
|
2003-02-28 04:04:45 +03:00
|
|
|
BuildHandlerChain(nsIContent* aContent, nsXBLPrototypeHandler** aResult)
|
2001-11-02 04:53:13 +03:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aResult = nullptr;
|
2003-09-27 08:18:26 +04:00
|
|
|
|
2006-05-13 02:58:10 +04:00
|
|
|
// Since we chain each handler onto the next handler,
|
|
|
|
// we'll enumerate them here in reverse so that when we
|
|
|
|
// walk the chain they'll come out in the original order
|
2011-09-27 11:54:58 +04:00
|
|
|
for (nsIContent* key = aContent->GetLastChild();
|
|
|
|
key;
|
|
|
|
key = key->GetPreviousSibling()) {
|
2003-09-27 08:18:26 +04:00
|
|
|
|
2007-01-30 03:06:41 +03:00
|
|
|
if (key->NodeInfo()->Equals(nsGkAtoms::key, kNameSpaceID_XUL)) {
|
2008-04-15 08:16:24 +04:00
|
|
|
// Check whether the key element has empty value at key/char attribute.
|
|
|
|
// Such element is used by localizers for alternative shortcut key
|
|
|
|
// definition on the locale. See bug 426501.
|
2008-04-23 12:04:08 +04:00
|
|
|
nsAutoString valKey, valCharCode, valKeyCode;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool attrExists =
|
2008-04-23 12:04:08 +04:00
|
|
|
key->GetAttr(kNameSpaceID_None, nsGkAtoms::key, valKey) ||
|
|
|
|
key->GetAttr(kNameSpaceID_None, nsGkAtoms::charcode, valCharCode) ||
|
|
|
|
key->GetAttr(kNameSpaceID_None, nsGkAtoms::keycode, valKeyCode);
|
|
|
|
if (attrExists &&
|
|
|
|
valKey.IsEmpty() && valCharCode.IsEmpty() && valKeyCode.IsEmpty())
|
2008-04-15 08:16:24 +04:00
|
|
|
continue;
|
|
|
|
|
2017-01-19 21:57:11 +03:00
|
|
|
bool reserved = key->AttrValueIs(kNameSpaceID_None, nsGkAtoms::reserved,
|
|
|
|
nsGkAtoms::_true, eCaseMatters);
|
|
|
|
nsXBLPrototypeHandler* handler = new nsXBLPrototypeHandler(key, reserved);
|
2003-09-27 08:18:26 +04:00
|
|
|
|
2006-05-13 02:58:10 +04:00
|
|
|
handler->SetNextHandler(*aResult);
|
|
|
|
*aResult = handler;
|
2001-11-02 04:53:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-09-28 00:23:49 +04:00
|
|
|
|
2000-11-29 09:01:33 +03:00
|
|
|
//
|
|
|
|
// EnsureHandlers
|
2017-05-10 06:45:11 +03:00
|
|
|
//
|
2000-11-29 09:01:33 +03:00
|
|
|
// Lazily load the XBL handlers. Overridden to handle being attached
|
|
|
|
// to a particular element rather than the document
|
|
|
|
//
|
2000-11-29 09:34:14 +03:00
|
|
|
nsresult
|
2014-01-23 07:18:51 +04:00
|
|
|
nsXBLWindowKeyHandler::EnsureHandlers()
|
2000-09-22 09:02:20 +04:00
|
|
|
{
|
2013-04-14 22:27:33 +04:00
|
|
|
nsCOMPtr<Element> el = GetElement();
|
2006-11-15 09:11:42 +03:00
|
|
|
NS_ENSURE_STATE(!mWeakPtrForElement || el);
|
2006-11-04 12:15:32 +03:00
|
|
|
if (el) {
|
2001-11-02 04:53:13 +03:00
|
|
|
// We are actually a XUL <keyset>.
|
2004-09-30 07:36:15 +04:00
|
|
|
if (mHandler)
|
2000-09-22 09:02:20 +04:00
|
|
|
return NS_OK;
|
2004-09-10 11:17:31 +04:00
|
|
|
|
2006-11-04 12:15:32 +03:00
|
|
|
nsCOMPtr<nsIContent> content(do_QueryInterface(el));
|
2003-02-28 04:04:45 +03:00
|
|
|
BuildHandlerChain(content, &mHandler);
|
2007-01-09 10:33:18 +03:00
|
|
|
} else { // We are an XBL file of handlers.
|
2017-06-06 02:29:04 +03:00
|
|
|
EnsureSpecialDocInfo();
|
2007-01-09 10:33:18 +03:00
|
|
|
|
|
|
|
// Now determine which handlers we should be using.
|
2014-01-23 07:18:51 +04:00
|
|
|
if (IsHTMLEditableFieldFocused()) {
|
2007-01-09 10:33:18 +03:00
|
|
|
sXBLSpecialDocInfo->GetAllHandlers("editor", &mHandler, &mUserHandler);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sXBLSpecialDocInfo->GetAllHandlers("browser", &mHandler, &mUserHandler);
|
|
|
|
}
|
2000-09-22 09:02:20 +04:00
|
|
|
}
|
2007-01-09 10:33:18 +03:00
|
|
|
|
2001-11-02 04:53:13 +03:00
|
|
|
return NS_OK;
|
2000-09-22 09:02:20 +04:00
|
|
|
}
|
|
|
|
|
2007-01-09 10:33:18 +03:00
|
|
|
nsresult
|
2017-10-03 01:05:19 +03:00
|
|
|
nsXBLWindowKeyHandler::WalkHandlers(nsIDOMKeyEvent* aKeyEvent, nsAtom* aEventType)
|
2000-09-22 09:02:20 +04:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool prevent;
|
2016-01-12 07:19:08 +03:00
|
|
|
aKeyEvent->AsEvent()->GetDefaultPrevented(&prevent);
|
2000-09-22 09:02:20 +04:00
|
|
|
if (prevent)
|
|
|
|
return NS_OK;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool trustedEvent = false;
|
2012-08-04 11:44:00 +04:00
|
|
|
// Don't process the event if it was not dispatched from a trusted source
|
2016-01-12 07:19:08 +03:00
|
|
|
aKeyEvent->AsEvent()->GetIsTrusted(&trustedEvent);
|
2001-11-20 11:40:54 +03:00
|
|
|
|
2004-09-02 19:25:39 +04:00
|
|
|
if (!trustedEvent)
|
|
|
|
return NS_OK;
|
|
|
|
|
2014-01-23 07:18:51 +04:00
|
|
|
nsresult rv = EnsureHandlers();
|
2006-11-04 12:15:32 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-04-14 22:27:33 +04:00
|
|
|
|
2014-03-18 19:16:47 +04:00
|
|
|
bool isDisabled;
|
|
|
|
nsCOMPtr<Element> el = GetElement(&isDisabled);
|
2006-11-04 12:15:32 +03:00
|
|
|
if (!el) {
|
2001-02-14 04:24:45 +03:00
|
|
|
if (mUserHandler) {
|
2014-03-18 19:16:47 +04:00
|
|
|
WalkHandlersInternal(aKeyEvent, aEventType, mUserHandler, true);
|
2016-01-12 07:19:08 +03:00
|
|
|
aKeyEvent->AsEvent()->GetDefaultPrevented(&prevent);
|
2001-02-14 04:24:45 +03:00
|
|
|
if (prevent)
|
2004-09-10 11:17:31 +04:00
|
|
|
return NS_OK; // Handled by the user bindings. Our work here is done.
|
2001-02-14 04:24:45 +03:00
|
|
|
}
|
2000-09-22 09:02:20 +04:00
|
|
|
}
|
|
|
|
|
2008-08-06 18:32:09 +04:00
|
|
|
// skip keysets that are disabled
|
2014-03-18 19:16:47 +04:00
|
|
|
if (el && isDisabled) {
|
2008-08-06 18:32:09 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-18 19:16:47 +04:00
|
|
|
WalkHandlersInternal(aKeyEvent, aEventType, mHandler, true);
|
2004-09-10 11:17:31 +04:00
|
|
|
|
2000-09-22 09:02:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-03-16 04:58:28 +03:00
|
|
|
void
|
|
|
|
nsXBLWindowKeyHandler::InstallKeyboardEventListenersTo(
|
|
|
|
EventListenerManager* aEventListenerManager)
|
|
|
|
{
|
|
|
|
// For marking each keyboard event as if it's reserved by chrome,
|
|
|
|
// nsXBLWindowKeyHandlers need to listen each keyboard events before
|
|
|
|
// web contents.
|
|
|
|
aEventListenerManager->AddEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("keydown"),
|
|
|
|
TrustedEventsAtCapture());
|
|
|
|
aEventListenerManager->AddEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("keyup"),
|
|
|
|
TrustedEventsAtCapture());
|
|
|
|
aEventListenerManager->AddEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("keypress"),
|
|
|
|
TrustedEventsAtCapture());
|
2016-04-22 20:12:54 +03:00
|
|
|
aEventListenerManager->AddEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("mozkeydownonplugin"),
|
|
|
|
TrustedEventsAtCapture());
|
|
|
|
aEventListenerManager->AddEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("mozkeyuponplugin"),
|
|
|
|
TrustedEventsAtCapture());
|
2016-03-16 04:58:28 +03:00
|
|
|
|
|
|
|
// For reducing the IPC cost, preventing to dispatch reserved keyboard
|
|
|
|
// events into the content process.
|
|
|
|
aEventListenerManager->AddEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("keydown"),
|
|
|
|
TrustedEventsAtSystemGroupCapture());
|
|
|
|
aEventListenerManager->AddEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("keyup"),
|
|
|
|
TrustedEventsAtSystemGroupCapture());
|
|
|
|
aEventListenerManager->AddEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("keypress"),
|
|
|
|
TrustedEventsAtSystemGroupCapture());
|
2016-04-22 20:12:54 +03:00
|
|
|
aEventListenerManager->AddEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("mozkeydownonplugin"),
|
|
|
|
TrustedEventsAtSystemGroupCapture());
|
|
|
|
aEventListenerManager->AddEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("mozkeyuponplugin"),
|
|
|
|
TrustedEventsAtSystemGroupCapture());
|
2016-03-16 04:58:28 +03:00
|
|
|
|
|
|
|
// Handle keyboard events in bubbling phase of the system event group.
|
|
|
|
aEventListenerManager->AddEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("keydown"),
|
|
|
|
TrustedEventsAtSystemGroupBubble());
|
|
|
|
aEventListenerManager->AddEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("keyup"),
|
|
|
|
TrustedEventsAtSystemGroupBubble());
|
|
|
|
aEventListenerManager->AddEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("keypress"),
|
|
|
|
TrustedEventsAtSystemGroupBubble());
|
2016-04-22 20:12:54 +03:00
|
|
|
aEventListenerManager->AddEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("mozkeydownonplugin"),
|
|
|
|
TrustedEventsAtSystemGroupBubble());
|
|
|
|
aEventListenerManager->AddEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("mozkeyuponplugin"),
|
|
|
|
TrustedEventsAtSystemGroupBubble());
|
2016-03-16 04:58:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsXBLWindowKeyHandler::RemoveKeyboardEventListenersFrom(
|
|
|
|
EventListenerManager* aEventListenerManager)
|
|
|
|
{
|
|
|
|
aEventListenerManager->RemoveEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("keydown"),
|
|
|
|
TrustedEventsAtCapture());
|
|
|
|
aEventListenerManager->RemoveEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("keyup"),
|
|
|
|
TrustedEventsAtCapture());
|
|
|
|
aEventListenerManager->RemoveEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("keypress"),
|
|
|
|
TrustedEventsAtCapture());
|
2016-04-22 20:12:54 +03:00
|
|
|
aEventListenerManager->RemoveEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("mozkeydownonplugin"),
|
|
|
|
TrustedEventsAtCapture());
|
|
|
|
aEventListenerManager->RemoveEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("mozkeyuponplugin"),
|
|
|
|
TrustedEventsAtCapture());
|
2016-03-16 04:58:28 +03:00
|
|
|
|
|
|
|
aEventListenerManager->RemoveEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("keydown"),
|
|
|
|
TrustedEventsAtSystemGroupCapture());
|
|
|
|
aEventListenerManager->RemoveEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("keyup"),
|
|
|
|
TrustedEventsAtSystemGroupCapture());
|
|
|
|
aEventListenerManager->RemoveEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("keypress"),
|
|
|
|
TrustedEventsAtSystemGroupCapture());
|
2016-04-22 20:12:54 +03:00
|
|
|
aEventListenerManager->RemoveEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("mozkeydownonplugin"),
|
|
|
|
TrustedEventsAtSystemGroupCapture());
|
|
|
|
aEventListenerManager->RemoveEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("mozkeyuponplugin"),
|
|
|
|
TrustedEventsAtSystemGroupCapture());
|
2016-03-16 04:58:28 +03:00
|
|
|
|
|
|
|
aEventListenerManager->RemoveEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("keydown"),
|
|
|
|
TrustedEventsAtSystemGroupBubble());
|
|
|
|
aEventListenerManager->RemoveEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("keyup"),
|
|
|
|
TrustedEventsAtSystemGroupBubble());
|
|
|
|
aEventListenerManager->RemoveEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("keypress"),
|
|
|
|
TrustedEventsAtSystemGroupBubble());
|
2016-04-22 20:12:54 +03:00
|
|
|
aEventListenerManager->RemoveEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("mozkeydownonplugin"),
|
|
|
|
TrustedEventsAtSystemGroupBubble());
|
|
|
|
aEventListenerManager->RemoveEventListenerByType(
|
|
|
|
this, NS_LITERAL_STRING("mozkeyuponplugin"),
|
|
|
|
TrustedEventsAtSystemGroupBubble());
|
|
|
|
}
|
|
|
|
|
2017-06-06 02:29:04 +03:00
|
|
|
/* static */ KeyboardMap
|
|
|
|
nsXBLWindowKeyHandler::CollectKeyboardShortcuts()
|
|
|
|
{
|
|
|
|
// Load the XBL handlers
|
|
|
|
EnsureSpecialDocInfo();
|
|
|
|
|
|
|
|
nsXBLPrototypeHandler* handlers = nullptr;
|
|
|
|
nsXBLPrototypeHandler* userHandlers = nullptr;
|
|
|
|
sXBLSpecialDocInfo->GetAllHandlers("browser", &handlers, &userHandlers);
|
|
|
|
|
|
|
|
// Convert the handlers into keyboard shortcuts, using an AutoTArray with
|
|
|
|
// the maximum amount of shortcuts used on any platform to minimize allocations
|
2017-06-28 23:06:47 +03:00
|
|
|
AutoTArray<KeyboardShortcut, 48> shortcuts;
|
|
|
|
|
|
|
|
// Append keyboard shortcuts for hardcoded actions like tab
|
|
|
|
KeyboardShortcut::AppendHardcodedShortcuts(shortcuts);
|
2017-06-06 02:29:04 +03:00
|
|
|
|
|
|
|
for (nsXBLPrototypeHandler* handler = handlers;
|
|
|
|
handler;
|
|
|
|
handler = handler->GetNextHandler()) {
|
|
|
|
KeyboardShortcut shortcut;
|
|
|
|
if (handler->TryConvertToKeyboardShortcut(&shortcut)) {
|
|
|
|
shortcuts.AppendElement(shortcut);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (nsXBLPrototypeHandler* handler = userHandlers;
|
|
|
|
handler;
|
|
|
|
handler = handler->GetNextHandler()) {
|
|
|
|
KeyboardShortcut shortcut;
|
|
|
|
if (handler->TryConvertToKeyboardShortcut(&shortcut)) {
|
|
|
|
shortcuts.AppendElement(shortcut);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return KeyboardMap(mozilla::Move(shortcuts));
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom*
|
2016-04-22 20:12:54 +03:00
|
|
|
nsXBLWindowKeyHandler::ConvertEventToDOMEventType(
|
|
|
|
const WidgetKeyboardEvent& aWidgetKeyboardEvent) const
|
|
|
|
{
|
|
|
|
if (aWidgetKeyboardEvent.IsKeyDownOrKeyDownOnPlugin()) {
|
|
|
|
return nsGkAtoms::keydown;
|
|
|
|
}
|
|
|
|
if (aWidgetKeyboardEvent.IsKeyUpOrKeyUpOnPlugin()) {
|
|
|
|
return nsGkAtoms::keyup;
|
|
|
|
}
|
|
|
|
if (aWidgetKeyboardEvent.mMessage == eKeyPress) {
|
|
|
|
return nsGkAtoms::keypress;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT_UNREACHABLE(
|
|
|
|
"All event messages which this instance listens to should be handled");
|
|
|
|
return nullptr;
|
2016-03-16 04:58:28 +03:00
|
|
|
}
|
|
|
|
|
2011-06-25 03:12:34 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsXBLWindowKeyHandler::HandleEvent(nsIDOMEvent* aEvent)
|
2000-09-22 09:02:20 +04:00
|
|
|
{
|
2008-09-16 05:37:13 +04:00
|
|
|
nsCOMPtr<nsIDOMKeyEvent> keyEvent(do_QueryInterface(aEvent));
|
|
|
|
NS_ENSURE_TRUE(keyEvent, NS_ERROR_INVALID_ARG);
|
2000-09-22 09:02:20 +04:00
|
|
|
|
2014-03-18 19:16:47 +04:00
|
|
|
uint16_t eventPhase;
|
|
|
|
aEvent->GetEventPhase(&eventPhase);
|
|
|
|
if (eventPhase == nsIDOMEvent::CAPTURING_PHASE) {
|
2016-03-16 04:58:28 +03:00
|
|
|
if (aEvent->WidgetEventPtr()->mFlags.mInSystemGroup) {
|
|
|
|
HandleEventOnCaptureInSystemEventGroup(keyEvent);
|
|
|
|
} else {
|
|
|
|
HandleEventOnCaptureInDefaultEventGroup(keyEvent);
|
|
|
|
}
|
2014-03-18 19:16:47 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-22 20:12:54 +03:00
|
|
|
WidgetKeyboardEvent* widgetKeyboardEvent =
|
|
|
|
aEvent->WidgetEventPtr()->AsKeyboardEvent();
|
|
|
|
if (widgetKeyboardEvent->IsKeyEventOnPlugin()) {
|
|
|
|
// key events on plugin shouldn't execute shortcut key handlers which are
|
|
|
|
// not reserved.
|
2017-07-05 07:58:41 +03:00
|
|
|
if (!widgetKeyboardEvent->IsReservedByChrome()) {
|
2016-04-22 20:12:54 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the event is untrusted event or was already consumed, do nothing.
|
|
|
|
if (!widgetKeyboardEvent->IsTrusted() ||
|
|
|
|
widgetKeyboardEvent->DefaultPrevented()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-09-22 09:02:20 +04:00
|
|
|
|
2016-04-22 20:12:54 +03:00
|
|
|
// XXX Don't check isReserved here because even if the handler in this
|
|
|
|
// instance isn't reserved but another instance reserves the key
|
|
|
|
// combination, it will be executed when the event is normal keyboard
|
|
|
|
// events...
|
|
|
|
bool isReserved = false;
|
|
|
|
if (!HasHandlerForEvent(keyEvent, &isReserved)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-30 20:08:17 +03:00
|
|
|
// If this event was handled by APZ then don't do the default action, and
|
|
|
|
// preventDefault to prevent any other listeners from handling the event.
|
|
|
|
if (widgetKeyboardEvent->mFlags.mHandledByAPZ) {
|
|
|
|
aEvent->PreventDefault();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> eventTypeAtom =
|
2016-04-22 20:12:54 +03:00
|
|
|
ConvertEventToDOMEventType(*widgetKeyboardEvent);
|
2014-03-18 19:16:47 +04:00
|
|
|
return WalkHandlers(keyEvent, eventTypeAtom);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-03-16 04:58:28 +03:00
|
|
|
nsXBLWindowKeyHandler::HandleEventOnCaptureInDefaultEventGroup(
|
|
|
|
nsIDOMKeyEvent* aEvent)
|
|
|
|
{
|
|
|
|
WidgetKeyboardEvent* widgetKeyboardEvent =
|
|
|
|
aEvent->AsEvent()->WidgetEventPtr()->AsKeyboardEvent();
|
|
|
|
|
2017-07-05 07:58:41 +03:00
|
|
|
if (widgetKeyboardEvent->IsReservedByChrome()) {
|
2016-03-16 04:58:28 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isReserved = false;
|
|
|
|
if (HasHandlerForEvent(aEvent, &isReserved) && isReserved) {
|
2017-07-05 07:58:41 +03:00
|
|
|
widgetKeyboardEvent->MarkAsReservedByChrome();
|
2016-03-16 04:58:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsXBLWindowKeyHandler::HandleEventOnCaptureInSystemEventGroup(
|
|
|
|
nsIDOMKeyEvent* aEvent)
|
2014-03-18 19:16:47 +04:00
|
|
|
{
|
|
|
|
WidgetKeyboardEvent* widgetEvent =
|
2016-02-12 18:40:07 +03:00
|
|
|
aEvent->AsEvent()->WidgetEventPtr()->AsKeyboardEvent();
|
2014-03-18 19:16:47 +04:00
|
|
|
|
2017-07-19 12:39:34 +03:00
|
|
|
// If the event won't be sent to remote process, this listener needs to do
|
|
|
|
// nothing.
|
|
|
|
if (widgetEvent->mFlags.mOnlySystemGroupDispatchInContent ||
|
|
|
|
!widgetEvent->WillBeSentToRemoteProcess()) {
|
2014-03-18 19:16:47 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-16 04:58:28 +03:00
|
|
|
if (!HasHandlerForEvent(aEvent)) {
|
2014-03-18 19:16:47 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-05 07:58:41 +03:00
|
|
|
// If this event wasn't marked as IsCrossProcessForwardingStopped,
|
2016-03-16 04:58:28 +03:00
|
|
|
// yet, it means it wasn't processed by content. We'll not call any
|
2017-07-05 07:58:41 +03:00
|
|
|
// of the handlers at this moment, and will wait the reply event.
|
|
|
|
// So, stop immediate propagation in this event first, then, mark it as
|
|
|
|
// waiting reply from remote process. Finally, when this process receives
|
|
|
|
// a reply from the remote process, it should be dispatched into this
|
|
|
|
// DOM tree again.
|
|
|
|
widgetEvent->StopImmediatePropagation();
|
|
|
|
widgetEvent->MarkAsWaitingReplyFromRemoteProcess();
|
2000-09-22 09:02:20 +04:00
|
|
|
}
|
2000-11-29 09:01:33 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2014-01-23 07:18:51 +04:00
|
|
|
nsXBLWindowKeyHandler::IsHTMLEditableFieldFocused()
|
2007-01-09 10:33:18 +03:00
|
|
|
{
|
Bug 178324, refactor focus by moving all focus handling into one place and simplifying it, add many tests, fixes many other bugs too numerous to mention in this small checkin comment, r=josh,smichaud,ere,dbaron,marco,neil,gavin,smaug,sr=smaug (CLOSED TREE)
2009-06-10 22:00:39 +04:00
|
|
|
nsIFocusManager* fm = nsFocusManager::GetFocusManager();
|
|
|
|
if (!fm)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-01-09 10:33:18 +03:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<mozIDOMWindowProxy> focusedWindow;
|
Bug 178324, refactor focus by moving all focus handling into one place and simplifying it, add many tests, fixes many other bugs too numerous to mention in this small checkin comment, r=josh,smichaud,ere,dbaron,marco,neil,gavin,smaug,sr=smaug (CLOSED TREE)
2009-06-10 22:00:39 +04:00
|
|
|
fm->GetFocusedWindow(getter_AddRefs(focusedWindow));
|
2007-01-09 10:33:18 +03:00
|
|
|
if (!focusedWindow)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
Bug 178324, refactor focus by moving all focus handling into one place and simplifying it, add many tests, fixes many other bugs too numerous to mention in this small checkin comment, r=josh,smichaud,ere,dbaron,marco,neil,gavin,smaug,sr=smaug (CLOSED TREE)
2009-06-10 22:00:39 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
auto* piwin = nsPIDOMWindowOuter::From(focusedWindow);
|
2007-01-09 10:33:18 +03:00
|
|
|
nsIDocShell *docShell = piwin->GetDocShell();
|
2014-01-23 07:18:51 +04:00
|
|
|
if (!docShell) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-01-09 10:33:18 +03:00
|
|
|
|
2017-08-07 09:08:02 +03:00
|
|
|
RefPtr<HTMLEditor> htmlEditor = docShell->GetHTMLEditor();
|
2014-01-23 07:18:51 +04:00
|
|
|
if (!htmlEditor) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-07 09:08:02 +03:00
|
|
|
nsCOMPtr<nsIDocument> doc = htmlEditor->GetDocument();
|
2014-02-10 23:55:01 +04:00
|
|
|
if (doc->HasFlag(NODE_IS_EDITABLE)) {
|
|
|
|
// Don't need to perform any checks in designMode documents.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-23 07:18:51 +04:00
|
|
|
nsCOMPtr<nsIDOMElement> focusedElement;
|
|
|
|
fm->GetFocusedElement(getter_AddRefs(focusedElement));
|
|
|
|
nsCOMPtr<nsINode> focusedNode = do_QueryInterface(focusedElement);
|
|
|
|
if (focusedNode) {
|
|
|
|
// If there is a focused element, make sure it's in the active editing host.
|
|
|
|
// Note that GetActiveEditingHost finds the current editing host based on
|
|
|
|
// the document's selection. Even though the document selection is usually
|
|
|
|
// collapsed to where the focus is, but the page may modify the selection
|
|
|
|
// without our knowledge, in which case this check will do something useful.
|
|
|
|
nsCOMPtr<Element> activeEditingHost = htmlEditor->GetActiveEditingHost();
|
|
|
|
if (!activeEditingHost) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return nsContentUtils::ContentIsDescendantOf(focusedNode, activeEditingHost);
|
2007-01-09 10:33:18 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-01-09 10:33:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2008-04-15 08:16:24 +04:00
|
|
|
// WalkHandlersInternal and WalkHandlersAndExecute
|
2007-01-09 10:33:18 +03:00
|
|
|
//
|
|
|
|
// Given a particular DOM event and a pointer to the first handler in the list,
|
2014-03-18 19:16:47 +04:00
|
|
|
// scan through the list to find something to handle the event. If aExecute = true,
|
|
|
|
// the handler will be executed; otherwise just return an answer telling if a handler
|
|
|
|
// for that event was found.
|
2007-01-09 10:33:18 +03:00
|
|
|
//
|
2014-03-18 19:16:47 +04:00
|
|
|
bool
|
2008-09-16 05:37:13 +04:00
|
|
|
nsXBLWindowKeyHandler::WalkHandlersInternal(nsIDOMKeyEvent* aKeyEvent,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* aEventType,
|
2014-03-18 19:16:47 +04:00
|
|
|
nsXBLPrototypeHandler* aHandler,
|
2015-03-12 18:03:08 +03:00
|
|
|
bool aExecute,
|
|
|
|
bool* aOutReservedForChrome)
|
2008-04-15 08:16:24 +04:00
|
|
|
{
|
2016-03-18 05:22:37 +03:00
|
|
|
WidgetKeyboardEvent* nativeKeyboardEvent =
|
|
|
|
aKeyEvent->AsEvent()->WidgetEventPtr()->AsKeyboardEvent();
|
|
|
|
MOZ_ASSERT(nativeKeyboardEvent);
|
2008-04-15 08:16:24 +04:00
|
|
|
|
2016-03-18 05:22:37 +03:00
|
|
|
AutoShortcutKeyCandidateArray shortcutKeys;
|
|
|
|
nativeKeyboardEvent->GetShortcutKeyCandidates(shortcutKeys);
|
|
|
|
|
|
|
|
if (shortcutKeys.IsEmpty()) {
|
2014-03-18 19:16:47 +04:00
|
|
|
return WalkHandlersAndExecute(aKeyEvent, aEventType, aHandler,
|
2015-03-12 18:03:08 +03:00
|
|
|
0, IgnoreModifierState(),
|
|
|
|
aExecute, aOutReservedForChrome);
|
2008-04-15 08:16:24 +04:00
|
|
|
}
|
|
|
|
|
2016-03-18 05:22:37 +03:00
|
|
|
for (uint32_t i = 0; i < shortcutKeys.Length(); ++i) {
|
|
|
|
ShortcutKeyCandidate& key = shortcutKeys[i];
|
2014-12-12 15:17:37 +03:00
|
|
|
IgnoreModifierState ignoreModifierState;
|
|
|
|
ignoreModifierState.mShift = key.mIgnoreShift;
|
2008-09-16 05:37:13 +04:00
|
|
|
if (WalkHandlersAndExecute(aKeyEvent, aEventType, aHandler,
|
2015-03-12 18:03:08 +03:00
|
|
|
key.mCharCode, ignoreModifierState,
|
|
|
|
aExecute, aOutReservedForChrome)) {
|
2014-03-18 19:16:47 +04:00
|
|
|
return true;
|
2014-12-12 15:17:37 +03:00
|
|
|
}
|
2008-04-15 08:16:24 +04:00
|
|
|
}
|
2014-03-18 19:16:47 +04:00
|
|
|
return false;
|
2008-04-15 08:16:24 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2014-12-12 15:17:37 +03:00
|
|
|
nsXBLWindowKeyHandler::WalkHandlersAndExecute(
|
|
|
|
nsIDOMKeyEvent* aKeyEvent,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* aEventType,
|
2016-03-18 05:25:08 +03:00
|
|
|
nsXBLPrototypeHandler* aFirstHandler,
|
2014-12-12 15:17:37 +03:00
|
|
|
uint32_t aCharCode,
|
|
|
|
const IgnoreModifierState& aIgnoreModifierState,
|
2015-03-12 18:03:08 +03:00
|
|
|
bool aExecute,
|
|
|
|
bool* aOutReservedForChrome)
|
2007-01-09 10:33:18 +03:00
|
|
|
{
|
2016-04-22 20:12:54 +03:00
|
|
|
if (aOutReservedForChrome) {
|
|
|
|
*aOutReservedForChrome = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
WidgetKeyboardEvent* widgetKeyboardEvent =
|
|
|
|
aKeyEvent->AsEvent()->WidgetEventPtr()->AsKeyboardEvent();
|
|
|
|
if (NS_WARN_IF(!widgetKeyboardEvent)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-01-09 10:33:18 +03:00
|
|
|
// Try all of the handlers until we find one that matches the event.
|
2016-03-18 05:25:08 +03:00
|
|
|
for (nsXBLPrototypeHandler* handler = aFirstHandler;
|
|
|
|
handler;
|
|
|
|
handler = handler->GetNextHandler()) {
|
2016-01-12 07:19:08 +03:00
|
|
|
bool stopped = aKeyEvent->AsEvent()->IsDispatchStopped();
|
2007-01-09 10:33:18 +03:00
|
|
|
if (stopped) {
|
|
|
|
// The event is finished, don't execute any more handlers
|
2012-08-07 19:11:35 +04:00
|
|
|
return false;
|
2007-01-09 10:33:18 +03:00
|
|
|
}
|
|
|
|
|
2016-03-22 09:05:25 +03:00
|
|
|
if (aExecute) {
|
2016-04-22 20:12:54 +03:00
|
|
|
// If the event is eKeyDownOnPlugin, it should execute either keydown
|
|
|
|
// handler or keypress handler because eKeyDownOnPlugin events are
|
|
|
|
// never followed by keypress events.
|
|
|
|
if (widgetKeyboardEvent->mMessage == eKeyDownOnPlugin) {
|
|
|
|
if (!handler->EventTypeEquals(nsGkAtoms::keydown) &&
|
|
|
|
!handler->EventTypeEquals(nsGkAtoms::keypress)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// The other event types should exactly be matched with the handler's
|
|
|
|
// event type.
|
|
|
|
} else if (!handler->EventTypeEquals(aEventType)) {
|
2016-03-22 09:05:25 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (handler->EventTypeEquals(nsGkAtoms::keypress)) {
|
|
|
|
// If the handler is a keypress event handler, we also need to check
|
|
|
|
// if coming keydown event is a preceding event of reserved key
|
|
|
|
// combination because if default action of a keydown event is
|
|
|
|
// prevented, following keypress event won't be fired. However, if
|
|
|
|
// following keypress event is reserved, we shouldn't allow web
|
|
|
|
// contents to prevent the default of the preceding keydown event.
|
|
|
|
if (aEventType != nsGkAtoms::keydown &&
|
|
|
|
aEventType != nsGkAtoms::keypress) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (!handler->EventTypeEquals(aEventType)) {
|
|
|
|
// Otherwise, aEventType should exactly be matched.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the keyboard event *may* execute the handler.
|
|
|
|
if (!handler->KeyEventMatched(aKeyEvent, aCharCode, aIgnoreModifierState)) {
|
2007-01-09 10:33:18 +03:00
|
|
|
continue; // try the next one
|
2014-12-12 15:17:37 +03:00
|
|
|
}
|
2007-01-09 10:33:18 +03:00
|
|
|
|
|
|
|
// Before executing this handler, check that it's not disabled,
|
|
|
|
// and that it has something to do (oncommand of the <key> or its
|
|
|
|
// <command> is non-empty).
|
2016-03-18 05:25:08 +03:00
|
|
|
nsCOMPtr<Element> commandElement;
|
2016-03-18 05:25:22 +03:00
|
|
|
if (!GetElementForHandler(handler, getter_AddRefs(commandElement))) {
|
|
|
|
continue;
|
2007-01-09 10:33:18 +03:00
|
|
|
}
|
|
|
|
|
2017-01-19 21:57:11 +03:00
|
|
|
bool isReserved = handler->GetIsReserved();
|
|
|
|
if (aOutReservedForChrome) {
|
|
|
|
*aOutReservedForChrome = isReserved;
|
|
|
|
}
|
|
|
|
|
2016-03-18 05:25:08 +03:00
|
|
|
if (commandElement) {
|
2016-05-06 12:38:44 +03:00
|
|
|
if (aExecute && !IsExecutableElement(commandElement)) {
|
2016-03-19 17:16:21 +03:00
|
|
|
continue;
|
2007-01-09 10:33:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-18 19:16:47 +04:00
|
|
|
if (!aExecute) {
|
2016-03-22 09:05:25 +03:00
|
|
|
if (handler->EventTypeEquals(aEventType)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// If the command is reserved and the event is keydown, check also if
|
|
|
|
// the handler is for keypress because if following keypress event is
|
|
|
|
// reserved, we shouldn't dispatch the event into web contents.
|
|
|
|
if (isReserved &&
|
|
|
|
aEventType == nsGkAtoms::keydown &&
|
|
|
|
handler->EventTypeEquals(nsGkAtoms::keypress)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Otherwise, we've not found a handler for the event yet.
|
|
|
|
continue;
|
2014-03-18 19:16:47 +04:00
|
|
|
}
|
|
|
|
|
2016-04-22 20:12:54 +03:00
|
|
|
// If it's not reserved and the event is a key event on a plugin,
|
|
|
|
// the handler shouldn't be executed.
|
|
|
|
if (!isReserved && widgetKeyboardEvent->IsKeyEventOnPlugin()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-18 05:25:08 +03:00
|
|
|
nsCOMPtr<EventTarget> target;
|
2016-03-18 05:25:22 +03:00
|
|
|
nsCOMPtr<Element> chromeHandlerElement = GetElement();
|
2016-03-18 05:25:08 +03:00
|
|
|
if (chromeHandlerElement) {
|
2016-03-19 17:16:21 +03:00
|
|
|
// XXX commandElement may be nullptr...
|
2016-03-18 05:25:08 +03:00
|
|
|
target = commandElement;
|
|
|
|
} else {
|
|
|
|
target = mTarget;
|
|
|
|
}
|
|
|
|
|
2016-03-22 09:05:25 +03:00
|
|
|
// XXX Do we execute only one handler even if the handler neither stops
|
|
|
|
// propagation nor prevents default of the event?
|
2016-03-18 05:25:08 +03:00
|
|
|
nsresult rv = handler->ExecuteHandler(target, aKeyEvent->AsEvent());
|
2007-01-09 10:33:18 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2007-01-09 10:33:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-12 15:17:37 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
// Windows native applications ignore Windows-Logo key state when checking
|
|
|
|
// shortcut keys even if the key is pressed. Therefore, if there is no
|
|
|
|
// shortcut key which exactly matches current modifier state, we should
|
|
|
|
// retry to look for a shortcut key without the Windows-Logo key press.
|
2016-04-22 20:12:54 +03:00
|
|
|
if (!aIgnoreModifierState.mOS && widgetKeyboardEvent->IsOS()) {
|
|
|
|
IgnoreModifierState ignoreModifierState(aIgnoreModifierState);
|
|
|
|
ignoreModifierState.mOS = true;
|
|
|
|
return WalkHandlersAndExecute(aKeyEvent, aEventType, aFirstHandler,
|
|
|
|
aCharCode, ignoreModifierState, aExecute);
|
2014-12-12 15:17:37 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-01-09 10:33:18 +03:00
|
|
|
}
|
|
|
|
|
2014-03-18 19:16:47 +04:00
|
|
|
bool
|
2015-03-12 18:03:08 +03:00
|
|
|
nsXBLWindowKeyHandler::HasHandlerForEvent(nsIDOMKeyEvent* aEvent,
|
|
|
|
bool* aOutReservedForChrome)
|
2014-03-18 19:16:47 +04:00
|
|
|
{
|
2016-04-22 20:12:54 +03:00
|
|
|
WidgetKeyboardEvent* widgetKeyboardEvent =
|
|
|
|
aEvent->AsEvent()->WidgetEventPtr()->AsKeyboardEvent();
|
|
|
|
if (NS_WARN_IF(!widgetKeyboardEvent) || !widgetKeyboardEvent->IsTrusted()) {
|
2014-03-18 19:16:47 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = EnsureHandlers();
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
bool isDisabled;
|
|
|
|
nsCOMPtr<Element> el = GetElement(&isDisabled);
|
|
|
|
if (el && isDisabled) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> eventTypeAtom =
|
2016-04-22 20:12:54 +03:00
|
|
|
ConvertEventToDOMEventType(*widgetKeyboardEvent);
|
2015-03-12 18:03:08 +03:00
|
|
|
return WalkHandlersInternal(aEvent, eventTypeAtom, mHandler, false,
|
|
|
|
aOutReservedForChrome);
|
2014-03-18 19:16:47 +04:00
|
|
|
}
|
|
|
|
|
2013-04-14 22:27:33 +04:00
|
|
|
already_AddRefed<Element>
|
2014-03-18 19:16:47 +04:00
|
|
|
nsXBLWindowKeyHandler::GetElement(bool* aIsDisabled)
|
2007-01-09 10:33:18 +03:00
|
|
|
{
|
2013-04-14 22:27:33 +04:00
|
|
|
nsCOMPtr<Element> element = do_QueryReferent(mWeakPtrForElement);
|
2014-03-18 19:16:47 +04:00
|
|
|
if (element && aIsDisabled) {
|
|
|
|
*aIsDisabled = element->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
|
|
|
|
nsGkAtoms::_true, eCaseMatters);
|
|
|
|
}
|
2013-04-14 22:27:33 +04:00
|
|
|
return element.forget();
|
2007-01-09 10:33:18 +03:00
|
|
|
}
|
|
|
|
|
2016-03-18 05:25:22 +03:00
|
|
|
bool
|
|
|
|
nsXBLWindowKeyHandler::GetElementForHandler(nsXBLPrototypeHandler* aHandler,
|
|
|
|
Element** aElementForHandler)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aElementForHandler);
|
|
|
|
*aElementForHandler = nullptr;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> keyContent = aHandler->GetHandlerElement();
|
2016-03-19 17:12:39 +03:00
|
|
|
if (!keyContent) {
|
|
|
|
return true; // XXX Even though no key element?
|
|
|
|
}
|
2016-03-18 05:25:22 +03:00
|
|
|
|
|
|
|
nsCOMPtr<Element> chromeHandlerElement = GetElement();
|
2016-03-19 17:12:39 +03:00
|
|
|
if (!chromeHandlerElement) {
|
2016-09-02 10:12:24 +03:00
|
|
|
NS_WARNING_ASSERTION(keyContent->IsInUncomposedDoc(), "uncomposed");
|
2016-03-19 17:12:39 +03:00
|
|
|
nsCOMPtr<Element> keyElement = do_QueryInterface(keyContent);
|
|
|
|
keyElement.swap(*aElementForHandler);
|
|
|
|
return true;
|
2016-03-18 05:25:22 +03:00
|
|
|
}
|
|
|
|
|
2016-03-19 17:12:39 +03:00
|
|
|
// We are in a XUL doc. Obtain our command attribute.
|
|
|
|
nsAutoString command;
|
|
|
|
keyContent->GetAttr(kNameSpaceID_None, nsGkAtoms::command, command);
|
|
|
|
if (command.IsEmpty()) {
|
|
|
|
// There is no command element associated with the key element.
|
2016-09-02 10:12:24 +03:00
|
|
|
NS_WARNING_ASSERTION(keyContent->IsInUncomposedDoc(), "uncomposed");
|
2016-03-18 05:25:22 +03:00
|
|
|
nsCOMPtr<Element> keyElement = do_QueryInterface(keyContent);
|
|
|
|
keyElement.swap(*aElementForHandler);
|
2016-03-19 17:12:39 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX Shouldn't we check this earlier?
|
2016-03-31 14:46:32 +03:00
|
|
|
nsIDocument* doc = keyContent->GetUncomposedDoc();
|
2016-03-19 17:12:39 +03:00
|
|
|
if (NS_WARN_IF(!doc)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<Element> commandElement =
|
|
|
|
do_QueryInterface(doc->GetElementById(command));
|
|
|
|
if (!commandElement) {
|
|
|
|
NS_ERROR("A XUL <key> is observing a command that doesn't exist. "
|
|
|
|
"Unable to execute key binding!");
|
|
|
|
return false;
|
2016-03-18 05:25:22 +03:00
|
|
|
}
|
|
|
|
|
2016-03-19 17:12:39 +03:00
|
|
|
commandElement.swap(*aElementForHandler);
|
2016-03-18 05:25:22 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-19 17:16:21 +03:00
|
|
|
bool
|
|
|
|
nsXBLWindowKeyHandler::IsExecutableElement(Element* aElement) const
|
|
|
|
{
|
|
|
|
if (!aElement) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString value;
|
|
|
|
aElement->GetAttribute(NS_LITERAL_STRING("disabled"), value);
|
|
|
|
if (value.EqualsLiteral("true")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aElement->GetAttribute(NS_LITERAL_STRING("oncommand"), value);
|
|
|
|
if (value.IsEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2000-09-22 09:02:20 +04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-04-14 22:27:33 +04:00
|
|
|
already_AddRefed<nsXBLWindowKeyHandler>
|
|
|
|
NS_NewXBLWindowKeyHandler(nsIDOMElement* aElement, EventTarget* aTarget)
|
2000-09-22 09:02:20 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsXBLWindowKeyHandler> result =
|
2013-04-14 22:27:33 +04:00
|
|
|
new nsXBLWindowKeyHandler(aElement, aTarget);
|
|
|
|
return result.forget();
|
2000-09-22 09:02:20 +04:00
|
|
|
}
|