2015-01-27 22:27:32 -08:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
2015-01-27 22:27:32 -08:00
|
|
|
#include "mozilla/EventForwards.h"
|
2015-01-27 22:27:32 -08:00
|
|
|
#include "mozilla/TextEventDispatcher.h"
|
2015-02-18 22:50:19 -08:00
|
|
|
#include "mozilla/TextEvents.h"
|
2015-01-27 22:27:32 -08:00
|
|
|
#include "mozilla/TextInputProcessor.h"
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIWidget.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsPresContext.h"
|
|
|
|
|
2015-01-27 22:27:32 -08:00
|
|
|
using namespace mozilla::widget;
|
|
|
|
|
2015-01-27 22:27:32 -08:00
|
|
|
namespace mozilla {
|
|
|
|
|
2015-01-27 22:27:33 -08:00
|
|
|
/******************************************************************************
|
|
|
|
* TextInputProcessorNotification
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
class TextInputProcessorNotification MOZ_FINAL :
|
|
|
|
public nsITextInputProcessorNotification
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit TextInputProcessorNotification(const char* aType)
|
|
|
|
: mType(aType)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD GetType(nsACString& aType) MOZ_OVERRIDE MOZ_FINAL
|
|
|
|
{
|
|
|
|
aType = mType;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
~TextInputProcessorNotification() { }
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsAutoCString mType;
|
|
|
|
|
|
|
|
TextInputProcessorNotification() { }
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(TextInputProcessorNotification,
|
|
|
|
nsITextInputProcessorNotification)
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* TextInputProcessor
|
|
|
|
******************************************************************************/
|
|
|
|
|
2015-01-27 22:27:32 -08:00
|
|
|
NS_IMPL_ISUPPORTS(TextInputProcessor,
|
2015-01-27 22:27:32 -08:00
|
|
|
nsITextInputProcessor,
|
|
|
|
TextEventDispatcherListener,
|
|
|
|
nsISupportsWeakReference)
|
2015-01-27 22:27:32 -08:00
|
|
|
|
|
|
|
TextInputProcessor::TextInputProcessor()
|
|
|
|
: mDispatcher(nullptr)
|
|
|
|
, mForTests(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TextInputProcessor::~TextInputProcessor()
|
|
|
|
{
|
2015-01-27 22:27:33 -08:00
|
|
|
if (mDispatcher && mDispatcher->IsComposing()) {
|
|
|
|
// If this is composing and not canceling the composition, nobody can steal
|
|
|
|
// the rights of TextEventDispatcher from this instance. Therefore, this
|
|
|
|
// needs to cancel the composition here.
|
|
|
|
if (NS_SUCCEEDED(IsValidStateForComposition())) {
|
|
|
|
nsRefPtr<TextEventDispatcher> kungFuDeathGrip(mDispatcher);
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
mDispatcher->CommitComposition(status, &EmptyString());
|
|
|
|
}
|
|
|
|
}
|
2015-01-27 22:27:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-02-10 00:09:29 -08:00
|
|
|
TextInputProcessor::BeginInputTransaction(
|
|
|
|
nsIDOMWindow* aWindow,
|
|
|
|
nsITextInputProcessorCallback* aCallback,
|
|
|
|
bool* aSucceeded)
|
2015-01-27 22:27:32 -08:00
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(aSucceeded, "aSucceeded must not be nullptr");
|
|
|
|
MOZ_RELEASE_ASSERT(nsContentUtils::IsCallerChrome());
|
2015-01-27 22:27:33 -08:00
|
|
|
if (NS_WARN_IF(!aCallback)) {
|
|
|
|
*aSucceeded = false;
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2015-02-10 00:09:29 -08:00
|
|
|
return BeginInputTransactionInternal(aWindow, aCallback, false, *aSucceeded);
|
2015-01-27 22:27:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-02-10 00:09:29 -08:00
|
|
|
TextInputProcessor::BeginInputTransactionForTests(
|
|
|
|
nsIDOMWindow* aWindow,
|
|
|
|
nsITextInputProcessorCallback* aCallback,
|
|
|
|
uint8_t aOptionalArgc,
|
|
|
|
bool* aSucceeded)
|
2015-01-27 22:27:32 -08:00
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(aSucceeded, "aSucceeded must not be nullptr");
|
|
|
|
MOZ_RELEASE_ASSERT(nsContentUtils::IsCallerChrome());
|
2015-01-27 22:27:33 -08:00
|
|
|
nsITextInputProcessorCallback* callback =
|
|
|
|
aOptionalArgc >= 1 ? aCallback : nullptr;
|
2015-02-10 00:09:29 -08:00
|
|
|
return BeginInputTransactionInternal(aWindow, callback, true, *aSucceeded);
|
2015-01-27 22:27:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-02-10 00:09:29 -08:00
|
|
|
TextInputProcessor::BeginInputTransactionInternal(
|
|
|
|
nsIDOMWindow* aWindow,
|
|
|
|
nsITextInputProcessorCallback* aCallback,
|
|
|
|
bool aForTests,
|
|
|
|
bool& aSucceeded)
|
2015-01-27 22:27:32 -08:00
|
|
|
{
|
|
|
|
aSucceeded = false;
|
|
|
|
if (NS_WARN_IF(!aWindow)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsPIDOMWindow> pWindow(do_QueryInterface(aWindow));
|
|
|
|
if (NS_WARN_IF(!pWindow)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDocShell> docShell(pWindow->GetDocShell());
|
|
|
|
if (NS_WARN_IF(!docShell)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
nsRefPtr<nsPresContext> presContext;
|
|
|
|
nsresult rv = docShell->GetPresContext(getter_AddRefs(presContext));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (NS_WARN_IF(!presContext)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIWidget> widget = presContext->GetRootWidget();
|
|
|
|
if (NS_WARN_IF(!widget)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<TextEventDispatcher> dispatcher = widget->GetTextEventDispatcher();
|
|
|
|
MOZ_RELEASE_ASSERT(dispatcher, "TextEventDispatcher must not be null");
|
|
|
|
|
|
|
|
// If the instance was initialized and is being initialized for same
|
|
|
|
// dispatcher and same purpose, we don't need to initialize the dispatcher
|
|
|
|
// again.
|
2015-01-27 22:27:33 -08:00
|
|
|
if (mDispatcher && dispatcher == mDispatcher && aCallback == mCallback &&
|
|
|
|
aForTests == mForTests) {
|
2015-01-27 22:27:32 -08:00
|
|
|
aSucceeded = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-27 22:27:32 -08:00
|
|
|
// If this instance is composing, don't allow to initialize again.
|
|
|
|
if (mDispatcher && mDispatcher->IsComposing()) {
|
|
|
|
return NS_ERROR_ALREADY_INITIALIZED;
|
2015-01-27 22:27:32 -08:00
|
|
|
}
|
|
|
|
|
2015-01-27 22:27:32 -08:00
|
|
|
// And also if another instance is composing with the new dispatcher, it'll
|
|
|
|
// fail to steal its ownership. Then, we should not throw an exception,
|
|
|
|
// just return false.
|
|
|
|
if (dispatcher->IsComposing()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This instance has finished preparing to link to the dispatcher. Therefore,
|
|
|
|
// let's forget the old dispatcher and purpose.
|
|
|
|
UnlinkFromTextEventDispatcher();
|
|
|
|
|
|
|
|
if (aForTests) {
|
2015-02-10 00:09:29 -08:00
|
|
|
rv = dispatcher->BeginInputTransactionForTests(this);
|
2015-01-27 22:27:32 -08:00
|
|
|
} else {
|
2015-02-10 00:09:29 -08:00
|
|
|
rv = dispatcher->BeginInputTransaction(this);
|
2015-01-27 22:27:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mDispatcher = dispatcher;
|
2015-01-27 22:27:33 -08:00
|
|
|
mCallback = aCallback;
|
2015-01-27 22:27:32 -08:00
|
|
|
mForTests = aForTests;
|
2015-01-27 22:27:32 -08:00
|
|
|
aSucceeded = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-27 22:27:32 -08:00
|
|
|
void
|
|
|
|
TextInputProcessor::UnlinkFromTextEventDispatcher()
|
|
|
|
{
|
|
|
|
mDispatcher = nullptr;
|
|
|
|
mForTests = false;
|
2015-01-27 22:27:33 -08:00
|
|
|
if (mCallback) {
|
|
|
|
nsCOMPtr<nsITextInputProcessorCallback> callback(mCallback);
|
|
|
|
mCallback = nullptr;
|
|
|
|
|
|
|
|
nsRefPtr<TextInputProcessorNotification> notification =
|
2015-02-10 00:09:29 -08:00
|
|
|
new TextInputProcessorNotification("notify-end-input-transaction");
|
2015-01-27 22:27:33 -08:00
|
|
|
bool result = false;
|
|
|
|
callback->OnNotify(this, notification, &result);
|
|
|
|
}
|
2015-01-27 22:27:32 -08:00
|
|
|
}
|
|
|
|
|
2015-01-27 22:27:32 -08:00
|
|
|
nsresult
|
2015-01-27 22:27:32 -08:00
|
|
|
TextInputProcessor::IsValidStateForComposition()
|
2015-01-27 22:27:32 -08:00
|
|
|
{
|
2015-01-27 22:27:32 -08:00
|
|
|
if (NS_WARN_IF(!mDispatcher)) {
|
2015-01-27 22:27:32 -08:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = mDispatcher->GetState();
|
2015-01-27 22:27:32 -08:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2015-01-27 22:27:32 -08:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-01-27 22:27:32 -08:00
|
|
|
return NS_OK;
|
2015-01-27 22:27:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TextInputProcessor::StartComposition(bool* aSucceeded)
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(aSucceeded, "aSucceeded must not be nullptr");
|
|
|
|
MOZ_RELEASE_ASSERT(nsContentUtils::IsCallerChrome());
|
|
|
|
*aSucceeded = false;
|
|
|
|
nsRefPtr<TextEventDispatcher> kungfuDeathGrip(mDispatcher);
|
|
|
|
nsresult rv = IsValidStateForComposition();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
rv = mDispatcher->StartComposition(status);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
*aSucceeded = status != nsEventStatus_eConsumeNoDefault &&
|
|
|
|
mDispatcher && mDispatcher->IsComposing();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TextInputProcessor::SetPendingCompositionString(const nsAString& aString)
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(nsContentUtils::IsCallerChrome());
|
|
|
|
nsRefPtr<TextEventDispatcher> kungfuDeathGrip(mDispatcher);
|
|
|
|
nsresult rv = IsValidStateForComposition();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return mDispatcher->SetPendingCompositionString(aString);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TextInputProcessor::AppendClauseToPendingComposition(uint32_t aLength,
|
|
|
|
uint32_t aAttribute)
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(nsContentUtils::IsCallerChrome());
|
|
|
|
nsRefPtr<TextEventDispatcher> kungfuDeathGrip(mDispatcher);
|
|
|
|
switch (aAttribute) {
|
|
|
|
case ATTR_RAW_CLAUSE:
|
|
|
|
case ATTR_SELECTED_RAW_CLAUSE:
|
|
|
|
case ATTR_CONVERTED_CLAUSE:
|
|
|
|
case ATTR_SELECTED_CLAUSE:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
nsresult rv = IsValidStateForComposition();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return mDispatcher->AppendClauseToPendingComposition(aLength, aAttribute);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TextInputProcessor::SetCaretInPendingComposition(uint32_t aOffset)
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(nsContentUtils::IsCallerChrome());
|
|
|
|
nsRefPtr<TextEventDispatcher> kungfuDeathGrip(mDispatcher);
|
|
|
|
nsresult rv = IsValidStateForComposition();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return mDispatcher->SetCaretInPendingComposition(aOffset, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TextInputProcessor::FlushPendingComposition(bool* aSucceeded)
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(aSucceeded, "aSucceeded must not be nullptr");
|
|
|
|
MOZ_RELEASE_ASSERT(nsContentUtils::IsCallerChrome());
|
|
|
|
*aSucceeded = false;
|
|
|
|
nsRefPtr<TextEventDispatcher> kungfuDeathGrip(mDispatcher);
|
|
|
|
nsresult rv = IsValidStateForComposition();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
rv = mDispatcher->FlushPendingComposition(status);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
*aSucceeded = status != nsEventStatus_eConsumeNoDefault;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TextInputProcessor::CommitComposition(const nsAString& aCommitString,
|
|
|
|
uint8_t aOptionalArgc,
|
|
|
|
bool* aSucceeded)
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(aSucceeded, "aSucceeded must not be nullptr");
|
|
|
|
MOZ_RELEASE_ASSERT(nsContentUtils::IsCallerChrome());
|
2015-01-27 22:27:32 -08:00
|
|
|
const nsAString* commitString =
|
|
|
|
aOptionalArgc >= 1 ? &aCommitString : nullptr;
|
|
|
|
return CommitCompositionInternal(commitString, aSucceeded);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TextInputProcessor::CommitCompositionInternal(const nsAString* aCommitString,
|
|
|
|
bool* aSucceeded)
|
|
|
|
{
|
|
|
|
if (aSucceeded) {
|
|
|
|
*aSucceeded = false;
|
|
|
|
}
|
2015-01-27 22:27:32 -08:00
|
|
|
nsRefPtr<TextEventDispatcher> kungfuDeathGrip(mDispatcher);
|
|
|
|
nsresult rv = IsValidStateForComposition();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2015-01-27 22:27:32 -08:00
|
|
|
rv = mDispatcher->CommitComposition(status, aCommitString);
|
2015-01-27 22:27:32 -08:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2015-01-27 22:27:32 -08:00
|
|
|
if (aSucceeded) {
|
|
|
|
*aSucceeded = status != nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
2015-01-27 22:27:32 -08:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TextInputProcessor::CancelComposition()
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(nsContentUtils::IsCallerChrome());
|
2015-01-27 22:27:32 -08:00
|
|
|
return CancelCompositionInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TextInputProcessor::CancelCompositionInternal()
|
|
|
|
{
|
2015-01-27 22:27:32 -08:00
|
|
|
nsRefPtr<TextEventDispatcher> kungfuDeathGrip(mDispatcher);
|
|
|
|
nsresult rv = IsValidStateForComposition();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
return mDispatcher->CommitComposition(status, &EmptyString());
|
|
|
|
}
|
|
|
|
|
2015-01-27 22:27:32 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TextInputProcessor::NotifyIME(TextEventDispatcher* aTextEventDispatcher,
|
|
|
|
const IMENotification& aNotification)
|
|
|
|
{
|
|
|
|
// If This is called while this is being initialized, ignore the call.
|
|
|
|
if (!mDispatcher) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(aTextEventDispatcher == mDispatcher,
|
|
|
|
"Wrong TextEventDispatcher notifies this");
|
2015-01-27 22:27:33 -08:00
|
|
|
NS_ASSERTION(mForTests || mCallback,
|
|
|
|
"mCallback can be null only when IME is initialized for tests");
|
|
|
|
if (mCallback) {
|
|
|
|
nsRefPtr<TextInputProcessorNotification> notification;
|
|
|
|
switch (aNotification.mMessage) {
|
|
|
|
case REQUEST_TO_COMMIT_COMPOSITION: {
|
|
|
|
NS_ASSERTION(aTextEventDispatcher->IsComposing(),
|
|
|
|
"Why is this requested without composition?");
|
|
|
|
notification = new TextInputProcessorNotification("request-to-commit");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case REQUEST_TO_CANCEL_COMPOSITION: {
|
|
|
|
NS_ASSERTION(aTextEventDispatcher->IsComposing(),
|
|
|
|
"Why is this requested without composition?");
|
|
|
|
notification = new TextInputProcessorNotification("request-to-cancel");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NOTIFY_IME_OF_FOCUS:
|
|
|
|
notification = new TextInputProcessorNotification("notify-focus");
|
|
|
|
break;
|
|
|
|
case NOTIFY_IME_OF_BLUR:
|
|
|
|
notification = new TextInputProcessorNotification("notify-blur");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
MOZ_RELEASE_ASSERT(notification);
|
|
|
|
bool result = false;
|
|
|
|
nsresult rv = mCallback->OnNotify(this, notification, &result);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return result ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-01-27 22:27:32 -08:00
|
|
|
switch (aNotification.mMessage) {
|
|
|
|
case REQUEST_TO_COMMIT_COMPOSITION: {
|
|
|
|
NS_ASSERTION(aTextEventDispatcher->IsComposing(),
|
|
|
|
"Why is this requested without composition?");
|
|
|
|
CommitCompositionInternal();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
case REQUEST_TO_CANCEL_COMPOSITION: {
|
|
|
|
NS_ASSERTION(aTextEventDispatcher->IsComposing(),
|
|
|
|
"Why is this requested without composition?");
|
|
|
|
CancelCompositionInternal();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
TextInputProcessor::OnRemovedFrom(TextEventDispatcher* aTextEventDispatcher)
|
|
|
|
{
|
|
|
|
// If This is called while this is being initialized, ignore the call.
|
|
|
|
if (!mDispatcher) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(aTextEventDispatcher == mDispatcher,
|
|
|
|
"Wrong TextEventDispatcher notifies this");
|
|
|
|
UnlinkFromTextEventDispatcher();
|
|
|
|
}
|
|
|
|
|
2015-02-18 22:50:19 -08:00
|
|
|
nsresult
|
|
|
|
TextInputProcessor::PrepareKeyboardEventToDispatch(
|
|
|
|
WidgetKeyboardEvent& aKeyboardEvent,
|
|
|
|
uint32_t aKeyFlags)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(aKeyboardEvent.mCodeNameIndex == CODE_NAME_INDEX_USE_STRING)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
if ((aKeyFlags & KEY_NON_PRINTABLE_KEY) &&
|
|
|
|
NS_WARN_IF(aKeyboardEvent.mKeyNameIndex == KEY_NAME_INDEX_USE_STRING)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
if ((aKeyFlags & KEY_FORCE_PRINTABLE_KEY) &&
|
|
|
|
aKeyboardEvent.mKeyNameIndex != KEY_NAME_INDEX_USE_STRING) {
|
|
|
|
aKeyboardEvent.GetDOMKeyName(aKeyboardEvent.mKeyValue);
|
|
|
|
aKeyboardEvent.mKeyNameIndex = KEY_NAME_INDEX_USE_STRING;
|
|
|
|
}
|
2015-02-18 22:50:19 -08:00
|
|
|
if (aKeyFlags & KEY_KEEP_KEY_LOCATION_STANDARD) {
|
|
|
|
// If .location is initialized with specific value, using
|
|
|
|
// KEY_KEEP_KEY_LOCATION_STANDARD must be a bug of the caller.
|
|
|
|
// Let's throw an exception for notifying the developer of this bug.
|
|
|
|
if (NS_WARN_IF(aKeyboardEvent.location)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
} else if (!aKeyboardEvent.location) {
|
|
|
|
// If KeyboardEvent.location is 0, it may be uninitialized. If so, we
|
|
|
|
// should compute proper location value from its .code value.
|
|
|
|
aKeyboardEvent.location =
|
|
|
|
WidgetKeyboardEvent::ComputeLocationFromCodeValue(
|
|
|
|
aKeyboardEvent.mCodeNameIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aKeyFlags & KEY_KEEP_KEYCODE_ZERO) {
|
|
|
|
// If .keyCode is initialized with specific value, using
|
|
|
|
// KEY_KEEP_KEYCODE_ZERO must be a bug of the caller. Let's throw an
|
|
|
|
// exception for notifying the developer of such bug.
|
|
|
|
if (NS_WARN_IF(aKeyboardEvent.keyCode)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
} else if (!aKeyboardEvent.keyCode &&
|
|
|
|
aKeyboardEvent.mKeyNameIndex > KEY_NAME_INDEX_Unidentified &&
|
|
|
|
aKeyboardEvent.mKeyNameIndex < KEY_NAME_INDEX_USE_STRING) {
|
|
|
|
// If KeyboardEvent.keyCode is 0, it may be uninitialized. If so, we may
|
|
|
|
// be able to decide a good .keyCode value if the .key value is a
|
|
|
|
// non-printable key.
|
|
|
|
aKeyboardEvent.keyCode =
|
|
|
|
WidgetKeyboardEvent::ComputeKeyCodeFromKeyNameIndex(
|
|
|
|
aKeyboardEvent.mKeyNameIndex);
|
|
|
|
}
|
2015-02-18 22:50:19 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TextInputProcessor::Keydown(nsIDOMKeyEvent* aDOMKeyEvent,
|
|
|
|
uint32_t aKeyFlags,
|
|
|
|
uint8_t aOptionalArgc,
|
|
|
|
bool* aDoDefault)
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(aDoDefault, "aDoDefault must not be nullptr");
|
|
|
|
MOZ_RELEASE_ASSERT(nsContentUtils::IsCallerChrome());
|
|
|
|
if (!aOptionalArgc) {
|
|
|
|
aKeyFlags = 0;
|
|
|
|
}
|
|
|
|
*aDoDefault = false;
|
|
|
|
if (NS_WARN_IF(!aDOMKeyEvent)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
WidgetKeyboardEvent* originalKeyEvent =
|
|
|
|
aDOMKeyEvent->GetInternalNSEvent()->AsKeyboardEvent();
|
|
|
|
if (NS_WARN_IF(!originalKeyEvent)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
// We shouldn't modify the internal WidgetKeyboardEvent.
|
|
|
|
WidgetKeyboardEvent keyEvent(*originalKeyEvent);
|
|
|
|
nsresult rv = PrepareKeyboardEventToDispatch(keyEvent, aKeyFlags);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
*aDoDefault = !(aKeyFlags & KEY_DEFAULT_PREVENTED);
|
|
|
|
// XXX Ignore specified modifier flags for now
|
|
|
|
keyEvent.modifiers = MODIFIER_NONE;
|
|
|
|
|
|
|
|
nsRefPtr<TextEventDispatcher> kungfuDeathGrip(mDispatcher);
|
|
|
|
rv = IsValidStateForComposition();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus status = *aDoDefault ? nsEventStatus_eIgnore :
|
|
|
|
nsEventStatus_eConsumeNoDefault;
|
|
|
|
if (!mDispatcher->DispatchKeyboardEvent(NS_KEY_DOWN, keyEvent, status)) {
|
|
|
|
// If keydown event isn't dispatched, we don't need to dispatch keypress
|
|
|
|
// events.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mDispatcher->MaybeDispatchKeypressEvents(keyEvent, status);
|
|
|
|
|
|
|
|
*aDoDefault = (status != nsEventStatus_eConsumeNoDefault);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TextInputProcessor::Keyup(nsIDOMKeyEvent* aDOMKeyEvent,
|
|
|
|
uint32_t aKeyFlags,
|
|
|
|
uint8_t aOptionalArgc,
|
|
|
|
bool* aDoDefault)
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(aDoDefault, "aDoDefault must not be nullptr");
|
|
|
|
MOZ_RELEASE_ASSERT(nsContentUtils::IsCallerChrome());
|
|
|
|
if (!aOptionalArgc) {
|
|
|
|
aKeyFlags = 0;
|
|
|
|
}
|
|
|
|
*aDoDefault = false;
|
|
|
|
if (NS_WARN_IF(!aDOMKeyEvent)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
WidgetKeyboardEvent* originalKeyEvent =
|
|
|
|
aDOMKeyEvent->GetInternalNSEvent()->AsKeyboardEvent();
|
|
|
|
if (NS_WARN_IF(!originalKeyEvent)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
// We shouldn't modify the internal WidgetKeyboardEvent.
|
|
|
|
WidgetKeyboardEvent keyEvent(*originalKeyEvent);
|
|
|
|
nsresult rv = PrepareKeyboardEventToDispatch(keyEvent, aKeyFlags);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
*aDoDefault = !(aKeyFlags & KEY_DEFAULT_PREVENTED);
|
|
|
|
// XXX Ignore specified modifier flags for now
|
|
|
|
keyEvent.modifiers = MODIFIER_NONE;
|
|
|
|
|
|
|
|
nsRefPtr<TextEventDispatcher> kungfuDeathGrip(mDispatcher);
|
|
|
|
rv = IsValidStateForComposition();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus status = *aDoDefault ? nsEventStatus_eIgnore :
|
|
|
|
nsEventStatus_eConsumeNoDefault;
|
|
|
|
mDispatcher->DispatchKeyboardEvent(NS_KEY_UP, keyEvent, status);
|
|
|
|
*aDoDefault = (status != nsEventStatus_eConsumeNoDefault);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-27 22:27:32 -08:00
|
|
|
} // namespace mozilla
|