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: */
|
2014-03-08 05:20:07 +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/. */
|
|
|
|
|
2015-09-08 06:54:14 +03:00
|
|
|
#include "mozilla/Logging.h"
|
|
|
|
|
2014-03-11 09:08:02 +04:00
|
|
|
#include "ContentEventHandler.h"
|
2014-03-08 05:20:07 +04:00
|
|
|
#include "IMEContentObserver.h"
|
2014-03-17 10:56:54 +04:00
|
|
|
#include "mozilla/AsyncEventDispatcher.h"
|
2014-08-13 15:25:14 +04:00
|
|
|
#include "mozilla/AutoRestore.h"
|
2014-04-24 04:36:15 +04:00
|
|
|
#include "mozilla/EventStateManager.h"
|
2014-03-08 05:20:07 +04:00
|
|
|
#include "mozilla/IMEStateManager.h"
|
2014-08-29 14:08:43 +04:00
|
|
|
#include "mozilla/MouseEvents.h"
|
2014-04-03 08:18:37 +04:00
|
|
|
#include "mozilla/TextComposition.h"
|
2014-08-29 14:08:43 +04:00
|
|
|
#include "mozilla/TextEvents.h"
|
2014-03-08 05:20:07 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsIAtom.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIDOMRange.h"
|
2015-10-27 01:21:37 +03:00
|
|
|
#include "nsIEditorIMESupport.h"
|
2014-03-08 05:20:07 +04:00
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsINode.h"
|
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsISelectionController.h"
|
|
|
|
#include "nsISelectionPrivate.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsIWidget.h"
|
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsWeakReference.h"
|
2015-05-15 04:18:08 +03:00
|
|
|
#include "WritingModes.h"
|
2014-03-08 05:20:07 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace widget;
|
|
|
|
|
2015-11-23 22:09:25 +03:00
|
|
|
LazyLogModule sIMECOLog("IMEContentObserver");
|
2015-09-08 06:54:14 +03:00
|
|
|
|
|
|
|
static const char*
|
|
|
|
ToChar(bool aBool)
|
|
|
|
{
|
|
|
|
return aBool ? "true" : "false";
|
|
|
|
}
|
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
static const char*
|
|
|
|
ToChar(IMEMessage aIMEMessage)
|
|
|
|
{
|
|
|
|
switch (aIMEMessage) {
|
|
|
|
case NOTIFY_IME_OF_NOTHING:
|
|
|
|
return "NOTIFY_IME_OF_NOTHING";
|
|
|
|
case NOTIFY_IME_OF_FOCUS:
|
|
|
|
return "NOTIFY_IME_OF_FOCUS";
|
|
|
|
case NOTIFY_IME_OF_BLUR:
|
|
|
|
return "NOTIFY_IME_OF_BLUR";
|
|
|
|
case NOTIFY_IME_OF_SELECTION_CHANGE:
|
|
|
|
return "NOTIFY_IME_OF_SELECTION_CHANGE";
|
|
|
|
case NOTIFY_IME_OF_TEXT_CHANGE:
|
|
|
|
return "NOTIFY_IME_OF_TEXT_CHANGE";
|
|
|
|
case NOTIFY_IME_OF_COMPOSITION_UPDATE:
|
|
|
|
return "NOTIFY_IME_OF_COMPOSITION_UPDATE";
|
|
|
|
case NOTIFY_IME_OF_POSITION_CHANGE:
|
|
|
|
return "NOTIFY_IME_OF_POSITION_CHANGE";
|
|
|
|
case NOTIFY_IME_OF_MOUSE_BUTTON_EVENT:
|
|
|
|
return "NOTIFY_IME_OF_MOUSE_BUTTON_EVENT";
|
|
|
|
case REQUEST_TO_COMMIT_COMPOSITION:
|
|
|
|
return "REQUEST_TO_COMMIT_COMPOSITION";
|
|
|
|
case REQUEST_TO_CANCEL_COMPOSITION:
|
|
|
|
return "REQUEST_TO_CANCEL_COMPOSITION";
|
|
|
|
default:
|
|
|
|
return "Unexpected value";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-08 06:54:14 +03:00
|
|
|
class WritingModeToString final : public nsAutoCString
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit WritingModeToString(const WritingMode& aWritingMode)
|
|
|
|
{
|
|
|
|
if (!aWritingMode.IsVertical()) {
|
|
|
|
AssignLiteral("Horizontal");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (aWritingMode.IsVerticalLR()) {
|
|
|
|
AssignLiteral("Vertical (LR)");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
AssignLiteral("Vertical (RL)");
|
|
|
|
}
|
|
|
|
virtual ~WritingModeToString() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SelectionChangeDataToString final : public nsAutoCString
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit SelectionChangeDataToString(
|
|
|
|
const IMENotification::SelectionChangeDataBase& aData)
|
|
|
|
{
|
|
|
|
if (!aData.IsValid()) {
|
|
|
|
AppendLiteral("{ IsValid()=false }");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
AppendPrintf("{ mOffset=%u, ", aData.mOffset);
|
|
|
|
if (aData.mString->Length() > 20) {
|
|
|
|
AppendPrintf("mString.Length()=%u, ", aData.mString->Length());
|
|
|
|
} else {
|
|
|
|
AppendPrintf("mString=\"%s\" (Length()=%u), ",
|
|
|
|
NS_ConvertUTF16toUTF8(*aData.mString).get(),
|
|
|
|
aData.mString->Length());
|
|
|
|
}
|
|
|
|
AppendPrintf("GetWritingMode()=%s, mReversed=%s, mCausedByComposition=%s, "
|
|
|
|
"mCausedBySelectionEvent=%s }",
|
|
|
|
WritingModeToString(aData.GetWritingMode()).get(),
|
|
|
|
ToChar(aData.mReversed),
|
|
|
|
ToChar(aData.mCausedByComposition),
|
|
|
|
ToChar(aData.mCausedBySelectionEvent));
|
|
|
|
}
|
|
|
|
virtual ~SelectionChangeDataToString() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TextChangeDataToString final : public nsAutoCString
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit TextChangeDataToString(
|
|
|
|
const IMENotification::TextChangeDataBase& aData)
|
|
|
|
{
|
|
|
|
if (!aData.IsValid()) {
|
|
|
|
AppendLiteral("{ IsValid()=false }");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
AppendPrintf("{ mStartOffset=%u, mRemovedEndOffset=%u, mAddedEndOffset=%u, "
|
2016-01-28 07:28:53 +03:00
|
|
|
"mCausedOnlyByComposition=%s, "
|
2016-01-28 07:28:54 +03:00
|
|
|
"mIncludingChangesDuringComposition=%s, "
|
|
|
|
"mIncludingChangesWithoutComposition=%s }",
|
2016-01-28 07:28:53 +03:00
|
|
|
aData.mStartOffset, aData.mRemovedEndOffset,
|
|
|
|
aData.mAddedEndOffset,
|
|
|
|
ToChar(aData.mCausedOnlyByComposition),
|
2016-01-28 07:28:54 +03:00
|
|
|
ToChar(aData.mIncludingChangesDuringComposition),
|
|
|
|
ToChar(aData.mIncludingChangesWithoutComposition));
|
2015-09-08 06:54:14 +03:00
|
|
|
}
|
|
|
|
virtual ~TextChangeDataToString() {}
|
|
|
|
};
|
|
|
|
|
2015-06-17 04:03:57 +03:00
|
|
|
/******************************************************************************
|
|
|
|
* mozilla::IMEContentObserver
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-07-31 08:37:59 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(IMEContentObserver)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(IMEContentObserver)
|
|
|
|
nsAutoScriptBlocker scriptBlocker;
|
|
|
|
|
2015-05-26 10:45:26 +03:00
|
|
|
tmp->NotifyIMEOfBlur();
|
2015-05-26 10:45:26 +03:00
|
|
|
tmp->UnregisterObservers();
|
2014-07-31 08:37:59 +04:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mSelection)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mRootContent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mEditableNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocShell)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mEditor)
|
2014-07-31 08:38:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mEndOfAddedTextCache.mContainerNode)
|
2014-07-31 08:38:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mStartOfRemovingTextRangeCache.mContainerNode)
|
2014-07-31 08:37:59 +04:00
|
|
|
|
|
|
|
tmp->mUpdatePreference.mWantUpdates = nsIMEUpdatePreference::NOTIFY_NOTHING;
|
|
|
|
tmp->mESM = nullptr;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(IMEContentObserver)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWidget)
|
2015-09-08 06:54:14 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFocusedWidget)
|
2014-07-31 08:37:59 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSelection)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRootContent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEditableNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocShell)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEditor)
|
2014-07-31 08:38:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEndOfAddedTextCache.mContainerNode)
|
2014-07-31 08:38:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(
|
|
|
|
mStartOfRemovingTextRangeCache.mContainerNode)
|
2014-07-31 08:37:59 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2014-04-24 04:36:14 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IMEContentObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISelectionListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIMutationObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIReflowObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIScrollObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
2014-07-31 08:37:59 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIEditorObserver)
|
2014-04-24 04:36:14 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsISelectionListener)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(IMEContentObserver)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(IMEContentObserver)
|
2014-03-08 05:20:07 +04:00
|
|
|
|
|
|
|
IMEContentObserver::IMEContentObserver()
|
2014-04-24 04:36:15 +04:00
|
|
|
: mESM(nullptr)
|
2015-06-04 20:06:10 +03:00
|
|
|
, mSuppressNotifications(0)
|
2014-07-31 08:38:01 +04:00
|
|
|
, mPreCharacterDataChangeLength(-1)
|
2015-09-16 11:48:24 +03:00
|
|
|
, mSendingNotification(NOTIFY_IME_OF_NOTHING)
|
2015-05-26 10:45:26 +03:00
|
|
|
, mIsObserving(false)
|
2015-06-17 04:03:57 +03:00
|
|
|
, mIMEHasFocus(false)
|
2015-09-16 11:48:24 +03:00
|
|
|
, mNeedsToNotifyIMEOfFocusSet(false)
|
|
|
|
, mNeedsToNotifyIMEOfTextChange(false)
|
|
|
|
, mNeedsToNotifyIMEOfSelectionChange(false)
|
|
|
|
, mNeedsToNotifyIMEOfPositionChange(false)
|
2015-09-16 11:48:24 +03:00
|
|
|
, mIsHandlingQueryContentEvent(false)
|
2014-03-08 05:20:07 +04:00
|
|
|
{
|
2014-07-31 08:37:59 +04:00
|
|
|
#ifdef DEBUG
|
2015-07-11 04:53:56 +03:00
|
|
|
mTextChangeData.Test();
|
2014-07-31 08:37:59 +04:00
|
|
|
#endif
|
2014-03-08 05:20:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IMEContentObserver::Init(nsIWidget* aWidget,
|
|
|
|
nsPresContext* aPresContext,
|
2014-11-05 16:12:44 +03:00
|
|
|
nsIContent* aContent,
|
|
|
|
nsIEditor* aEditor)
|
2014-03-08 05:20:07 +04:00
|
|
|
{
|
2015-05-26 10:45:26 +03:00
|
|
|
State state = GetState();
|
|
|
|
if (NS_WARN_IF(state == eState_Observing)) {
|
|
|
|
return; // Nothing to do.
|
|
|
|
}
|
|
|
|
|
|
|
|
bool firstInitialization = state != eState_StoppedObserving;
|
2015-05-26 10:45:26 +03:00
|
|
|
if (!firstInitialization) {
|
|
|
|
// If this is now trying to initialize with new contents, all observers
|
|
|
|
// should be registered again for simpler implementation.
|
|
|
|
UnregisterObservers();
|
|
|
|
// Clear members which may not be initialized again.
|
2015-10-10 04:21:01 +03:00
|
|
|
Clear();
|
2015-05-26 10:45:26 +03:00
|
|
|
}
|
|
|
|
|
2014-04-24 04:36:15 +04:00
|
|
|
mESM = aPresContext->EventStateManager();
|
|
|
|
mESM->OnStartToObserveContent(this);
|
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
mWidget = aWidget;
|
2015-05-26 10:45:26 +03:00
|
|
|
|
2015-10-10 04:21:01 +03:00
|
|
|
if (aWidget->GetInputContext().mIMEState.mEnabled == IMEState::PLUGIN) {
|
|
|
|
if (!InitWithPlugin(aPresContext, aContent)) {
|
|
|
|
Clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!InitWithEditor(aPresContext, aContent, aEditor)) {
|
|
|
|
Clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (firstInitialization) {
|
2015-11-10 05:49:04 +03:00
|
|
|
// Now, try to send NOTIFY_IME_OF_FOCUS to IME via the widget.
|
2015-10-10 04:21:01 +03:00
|
|
|
MaybeNotifyIMEOfFocusSet();
|
2015-11-10 05:49:04 +03:00
|
|
|
// When this is called first time, IME has not received NOTIFY_IME_OF_FOCUS
|
|
|
|
// yet since NOTIFY_IME_OF_FOCUS will be sent to widget asynchronously.
|
|
|
|
// So, we need to do nothing here. After NOTIFY_IME_OF_FOCUS has been
|
|
|
|
// sent, OnIMEReceivedFocus() will be called and content, selection and/or
|
|
|
|
// position changes will be observed
|
|
|
|
return;
|
|
|
|
}
|
2015-10-10 04:21:01 +03:00
|
|
|
|
2015-11-10 05:49:04 +03:00
|
|
|
// When this is called after editor reframing (i.e., the root editable node
|
|
|
|
// is also recreated), IME has usually received NOTIFY_IME_OF_FOCUS. In this
|
|
|
|
// case, we need to restart to observe content, selection and/or position
|
|
|
|
// changes in new root editable node.
|
|
|
|
ObserveEditableNode();
|
2015-10-10 04:21:01 +03:00
|
|
|
|
2015-11-10 05:49:04 +03:00
|
|
|
if (!NeedsToNotifyIMEOfSomething()) {
|
|
|
|
return;
|
2015-10-10 04:21:01 +03:00
|
|
|
}
|
|
|
|
|
2015-11-10 05:49:04 +03:00
|
|
|
// Some change events may wait to notify IME because this was being
|
|
|
|
// initialized. It is the time to flush them.
|
|
|
|
FlushMergeableNotifications();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IMEContentObserver::OnIMEReceivedFocus()
|
|
|
|
{
|
|
|
|
// While Init() notifies IME of focus, pending layout may be flushed
|
|
|
|
// because the notification may cause querying content. Then, recursive
|
|
|
|
// call of Init() with the latest content may occur. In such case, we
|
|
|
|
// shouldn't keep first initialization which notified IME of focus.
|
|
|
|
if (GetState() != eState_Initializing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTIFY_IME_OF_FOCUS might cause recreating IMEContentObserver
|
|
|
|
// instance via IMEStateManager::UpdateIMEState(). So, this
|
|
|
|
// instance might already have been destroyed, check it.
|
|
|
|
if (!mRootContent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start to observe which is needed by IME when IME actually has focus.
|
2015-10-10 04:21:01 +03:00
|
|
|
ObserveEditableNode();
|
|
|
|
|
2015-11-10 05:49:04 +03:00
|
|
|
if (!NeedsToNotifyIMEOfSomething()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-10 04:21:01 +03:00
|
|
|
// Some change events may wait to notify IME because this was being
|
|
|
|
// initialized. It is the time to flush them.
|
|
|
|
FlushMergeableNotifications();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IMEContentObserver::InitWithEditor(nsPresContext* aPresContext,
|
|
|
|
nsIContent* aContent,
|
|
|
|
nsIEditor* aEditor)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aEditor);
|
|
|
|
|
2015-05-26 10:45:26 +03:00
|
|
|
mEditableNode =
|
|
|
|
IMEStateManager::GetRootEditableNode(aPresContext, aContent);
|
2015-10-10 04:21:01 +03:00
|
|
|
if (NS_WARN_IF(!mEditableNode)) {
|
|
|
|
return false;
|
2014-03-08 05:20:07 +04:00
|
|
|
}
|
|
|
|
|
2014-11-05 16:12:44 +03:00
|
|
|
mEditor = aEditor;
|
2015-10-10 04:21:01 +03:00
|
|
|
if (NS_WARN_IF(!mEditor)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-07-31 08:37:59 +04:00
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
nsIPresShell* presShell = aPresContext->PresShell();
|
|
|
|
|
|
|
|
// get selection and root content
|
|
|
|
nsCOMPtr<nsISelectionController> selCon;
|
|
|
|
if (mEditableNode->IsNodeOfType(nsINode::eCONTENT)) {
|
|
|
|
nsIFrame* frame =
|
|
|
|
static_cast<nsIContent*>(mEditableNode.get())->GetPrimaryFrame();
|
2015-10-10 04:21:01 +03:00
|
|
|
if (NS_WARN_IF(!frame)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-08 05:20:07 +04:00
|
|
|
|
|
|
|
frame->GetSelectionController(aPresContext,
|
|
|
|
getter_AddRefs(selCon));
|
|
|
|
} else {
|
|
|
|
// mEditableNode is a document
|
|
|
|
selCon = do_QueryInterface(presShell);
|
|
|
|
}
|
2015-10-10 04:21:01 +03:00
|
|
|
|
|
|
|
if (NS_WARN_IF(!selCon)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-08 05:20:07 +04:00
|
|
|
|
|
|
|
selCon->GetSelection(nsISelectionController::SELECTION_NORMAL,
|
2014-03-08 05:20:07 +04:00
|
|
|
getter_AddRefs(mSelection));
|
2015-10-10 04:21:01 +03:00
|
|
|
if (NS_WARN_IF(!mSelection)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-08 05:20:07 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMRange> selDomRange;
|
2014-03-08 05:20:07 +04:00
|
|
|
if (NS_SUCCEEDED(mSelection->GetRangeAt(0, getter_AddRefs(selDomRange)))) {
|
2014-03-08 05:20:07 +04:00
|
|
|
nsRange* selRange = static_cast<nsRange*>(selDomRange.get());
|
2015-10-10 04:21:01 +03:00
|
|
|
if (NS_WARN_IF(!selRange) || NS_WARN_IF(!selRange->GetStartParent())) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-08 05:20:07 +04:00
|
|
|
|
|
|
|
mRootContent = selRange->GetStartParent()->
|
|
|
|
GetSelectionRootContent(presShell);
|
|
|
|
} else {
|
|
|
|
mRootContent = mEditableNode->GetSelectionRootContent(presShell);
|
|
|
|
}
|
|
|
|
if (!mRootContent && mEditableNode->IsNodeOfType(nsINode::eDOCUMENT)) {
|
|
|
|
// The document node is editable, but there are no contents, this document
|
|
|
|
// is not editable.
|
2015-10-10 04:21:01 +03:00
|
|
|
return false;
|
2014-03-08 05:20:07 +04:00
|
|
|
}
|
|
|
|
|
2015-10-10 04:21:01 +03:00
|
|
|
if (NS_WARN_IF(!mRootContent)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-24 07:40:21 +03:00
|
|
|
|
2015-10-10 04:21:01 +03:00
|
|
|
mDocShell = aPresContext->GetDocShell();
|
|
|
|
if (NS_WARN_IF(!mDocShell)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-26 10:45:26 +03:00
|
|
|
|
2015-10-10 04:21:01 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IMEContentObserver::InitWithPlugin(nsPresContext* aPresContext,
|
|
|
|
nsIContent* aContent)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!aContent) ||
|
|
|
|
NS_WARN_IF(aContent->GetDesiredIMEState().mEnabled != IMEState::PLUGIN)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
nsIFrame* frame = aContent->GetPrimaryFrame();
|
|
|
|
if (NS_WARN_IF(!frame)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsISelectionController> selCon;
|
|
|
|
frame->GetSelectionController(aPresContext, getter_AddRefs(selCon));
|
|
|
|
if (NS_WARN_IF(!selCon)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
selCon->GetSelection(nsISelectionController::SELECTION_NORMAL,
|
|
|
|
getter_AddRefs(mSelection));
|
|
|
|
if (NS_WARN_IF(!mSelection)) {
|
|
|
|
return false;
|
2014-03-08 05:20:07 +04:00
|
|
|
}
|
|
|
|
|
2015-10-10 04:21:01 +03:00
|
|
|
mEditor = nullptr;
|
|
|
|
mEditableNode = aContent;
|
|
|
|
mRootContent = aContent;
|
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
mDocShell = aPresContext->GetDocShell();
|
2015-10-10 04:21:01 +03:00
|
|
|
if (NS_WARN_IF(!mDocShell)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-08 05:20:07 +04:00
|
|
|
|
2015-10-10 04:21:01 +03:00
|
|
|
return true;
|
|
|
|
}
|
2015-06-17 04:03:57 +03:00
|
|
|
|
2015-10-10 04:21:01 +03:00
|
|
|
void
|
|
|
|
IMEContentObserver::Clear()
|
|
|
|
{
|
|
|
|
mEditor = nullptr;
|
|
|
|
mSelection = nullptr;
|
|
|
|
mEditableNode = nullptr;
|
|
|
|
mRootContent = nullptr;
|
|
|
|
mDocShell = nullptr;
|
2014-03-08 05:20:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IMEContentObserver::ObserveEditableNode()
|
|
|
|
{
|
2015-05-26 10:45:26 +03:00
|
|
|
MOZ_RELEASE_ASSERT(mSelection);
|
|
|
|
MOZ_RELEASE_ASSERT(mRootContent);
|
|
|
|
MOZ_RELEASE_ASSERT(GetState() != eState_Observing);
|
|
|
|
|
2015-11-10 05:49:04 +03:00
|
|
|
// If this is called before sending NOTIFY_IME_OF_FOCUS (it's possible when
|
|
|
|
// the editor is reframed before sending NOTIFY_IME_OF_FOCUS asynchronously),
|
|
|
|
// the update preference of mWidget may be different from after the widget
|
|
|
|
// receives NOTIFY_IME_OF_FOCUS. So, this should be called again by
|
|
|
|
// OnIMEReceivedFocus() which is called after sending NOTIFY_IME_OF_FOCUS.
|
|
|
|
if (!mIMEHasFocus) {
|
|
|
|
MOZ_ASSERT(!mWidget || mNeedsToNotifyIMEOfFocusSet ||
|
|
|
|
mSendingNotification == NOTIFY_IME_OF_FOCUS,
|
|
|
|
"Wow, OnIMEReceivedFocus() won't be called?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-26 10:45:26 +03:00
|
|
|
mIsObserving = true;
|
2015-10-10 04:21:01 +03:00
|
|
|
if (mEditor) {
|
|
|
|
mEditor->AddEditorObserver(this);
|
|
|
|
}
|
2014-03-08 05:20:07 +04:00
|
|
|
|
|
|
|
mUpdatePreference = mWidget->GetIMEUpdatePreference();
|
|
|
|
if (mUpdatePreference.WantSelectionChange()) {
|
|
|
|
// add selection change listener
|
2014-03-08 05:20:07 +04:00
|
|
|
nsCOMPtr<nsISelectionPrivate> selPrivate(do_QueryInterface(mSelection));
|
2014-03-08 05:20:07 +04:00
|
|
|
NS_ENSURE_TRUE_VOID(selPrivate);
|
|
|
|
nsresult rv = selPrivate->AddSelectionListener(this);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mUpdatePreference.WantTextChange()) {
|
|
|
|
// add text change observer
|
|
|
|
mRootContent->AddMutationObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mUpdatePreference.WantPositionChanged() && mDocShell) {
|
|
|
|
// Add scroll position listener and reflow observer to detect position and
|
|
|
|
// size changes
|
|
|
|
mDocShell->AddWeakScrollObserver(this);
|
|
|
|
mDocShell->AddWeakReflowObserver(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-05-26 10:45:26 +03:00
|
|
|
IMEContentObserver::NotifyIMEOfBlur()
|
2015-05-24 04:11:37 +03:00
|
|
|
{
|
2015-06-17 04:03:57 +03:00
|
|
|
// Prevent any notifications to be sent IME.
|
|
|
|
nsCOMPtr<nsIWidget> widget;
|
|
|
|
mWidget.swap(widget);
|
|
|
|
|
|
|
|
// If we hasn't been set focus, we shouldn't send blur notification to IME.
|
|
|
|
if (!mIMEHasFocus) {
|
2015-05-26 10:45:26 +03:00
|
|
|
return;
|
2014-03-08 05:20:07 +04:00
|
|
|
}
|
2014-07-31 08:37:59 +04:00
|
|
|
|
2015-06-17 04:03:57 +03:00
|
|
|
// mWidget must have been non-nullptr if IME has focus.
|
|
|
|
MOZ_RELEASE_ASSERT(widget);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IMEContentObserver> kungFuDeathGrip(this);
|
2015-09-08 06:54:14 +03:00
|
|
|
|
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Info,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::NotifyIMEOfBlur(), "
|
|
|
|
"sending NOTIFY_IME_OF_BLUR", this));
|
|
|
|
|
2015-06-17 04:03:57 +03:00
|
|
|
// For now, we need to send blur notification in any condition because
|
|
|
|
// we don't have any simple ways to send blur notification asynchronously.
|
|
|
|
// After this call, Destroy() or Unlink() will stop observing the content
|
|
|
|
// and forget everything. Therefore, if it's not safe to send notification
|
|
|
|
// when script blocker is unlocked, we cannot send blur notification after
|
|
|
|
// that and before next focus notification.
|
|
|
|
// Anyway, as far as we know, IME doesn't try to query content when it loses
|
|
|
|
// focus. So, this may not cause any problem.
|
|
|
|
mIMEHasFocus = false;
|
2015-06-27 03:23:31 +03:00
|
|
|
IMEStateManager::NotifyIME(IMENotification(NOTIFY_IME_OF_BLUR), widget);
|
2015-09-08 06:54:14 +03:00
|
|
|
|
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::NotifyIMEOfBlur(), "
|
|
|
|
"sent NOTIFY_IME_OF_BLUR", this));
|
2015-05-26 10:45:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IMEContentObserver::UnregisterObservers()
|
|
|
|
{
|
2015-05-26 10:45:26 +03:00
|
|
|
if (!mIsObserving) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mIsObserving = false;
|
|
|
|
|
2015-05-26 10:45:26 +03:00
|
|
|
if (mEditor) {
|
|
|
|
mEditor->RemoveEditorObserver(this);
|
2015-05-24 07:40:21 +03:00
|
|
|
}
|
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
if (mUpdatePreference.WantSelectionChange() && mSelection) {
|
|
|
|
nsCOMPtr<nsISelectionPrivate> selPrivate(do_QueryInterface(mSelection));
|
|
|
|
if (selPrivate) {
|
2014-03-08 05:20:07 +04:00
|
|
|
selPrivate->RemoveSelectionListener(this);
|
2014-03-08 05:20:07 +04:00
|
|
|
}
|
2015-09-08 06:54:14 +03:00
|
|
|
mSelectionData.Clear();
|
|
|
|
mFocusedWidget = nullptr;
|
2014-03-08 05:20:07 +04:00
|
|
|
}
|
2014-07-31 08:37:59 +04:00
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
if (mUpdatePreference.WantTextChange() && mRootContent) {
|
|
|
|
mRootContent->RemoveMutationObserver(this);
|
|
|
|
}
|
2014-07-31 08:37:59 +04:00
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
if (mUpdatePreference.WantPositionChanged() && mDocShell) {
|
|
|
|
mDocShell->RemoveWeakScrollObserver(this);
|
|
|
|
mDocShell->RemoveWeakReflowObserver(this);
|
|
|
|
}
|
2014-07-31 08:37:59 +04:00
|
|
|
}
|
|
|
|
|
2015-05-15 04:18:08 +03:00
|
|
|
nsPresContext*
|
|
|
|
IMEContentObserver::GetPresContext() const
|
|
|
|
{
|
|
|
|
return mESM ? mESM->GetPresContext() : nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-31 08:37:59 +04:00
|
|
|
void
|
|
|
|
IMEContentObserver::Destroy()
|
|
|
|
{
|
|
|
|
// WARNING: When you change this method, you have to check Unlink() too.
|
|
|
|
|
2015-05-26 10:45:26 +03:00
|
|
|
NotifyIMEOfBlur();
|
2015-05-26 10:45:26 +03:00
|
|
|
UnregisterObservers();
|
2015-10-10 04:21:01 +03:00
|
|
|
Clear();
|
2014-07-31 08:37:59 +04:00
|
|
|
|
|
|
|
mWidget = nullptr;
|
2014-03-08 05:20:07 +04:00
|
|
|
mUpdatePreference.mWantUpdates = nsIMEUpdatePreference::NOTIFY_NOTHING;
|
2014-04-24 04:36:15 +04:00
|
|
|
|
|
|
|
if (mESM) {
|
|
|
|
mESM->OnStopObservingContent(this);
|
|
|
|
mESM = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IMEContentObserver::DisconnectFromEventStateManager()
|
|
|
|
{
|
|
|
|
mESM = nullptr;
|
2014-03-08 05:20:07 +04:00
|
|
|
}
|
|
|
|
|
2015-05-26 10:45:26 +03:00
|
|
|
bool
|
|
|
|
IMEContentObserver::MaybeReinitialize(nsIWidget* aWidget,
|
|
|
|
nsPresContext* aPresContext,
|
|
|
|
nsIContent* aContent,
|
|
|
|
nsIEditor* aEditor)
|
|
|
|
{
|
|
|
|
if (!IsObservingContent(aPresContext, aContent)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetState() == eState_StoppedObserving) {
|
|
|
|
Init(aWidget, aPresContext, aContent, aEditor);
|
|
|
|
}
|
|
|
|
return IsManaging(aPresContext, aContent);
|
|
|
|
}
|
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
bool
|
|
|
|
IMEContentObserver::IsManaging(nsPresContext* aPresContext,
|
|
|
|
nsIContent* aContent)
|
2015-05-26 10:45:26 +03:00
|
|
|
{
|
|
|
|
return GetState() == eState_Observing &&
|
|
|
|
IsObservingContent(aPresContext, aContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
IMEContentObserver::State
|
|
|
|
IMEContentObserver::GetState() const
|
2014-03-08 05:20:07 +04:00
|
|
|
{
|
2014-03-08 05:20:07 +04:00
|
|
|
if (!mSelection || !mRootContent || !mEditableNode) {
|
2015-05-26 10:45:26 +03:00
|
|
|
return eState_NotObserving; // failed to initialize or finalized.
|
2014-03-08 05:20:07 +04:00
|
|
|
}
|
2014-08-06 03:23:02 +04:00
|
|
|
if (!mRootContent->IsInComposedDoc()) {
|
2015-05-26 10:45:26 +03:00
|
|
|
// the focused editor has already been reframed.
|
|
|
|
return eState_StoppedObserving;
|
2014-03-08 05:20:07 +04:00
|
|
|
}
|
2015-05-26 10:45:26 +03:00
|
|
|
return mIsObserving ? eState_Observing : eState_Initializing;
|
2015-05-26 10:45:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IMEContentObserver::IsObservingContent(nsPresContext* aPresContext,
|
|
|
|
nsIContent* aContent) const
|
|
|
|
{
|
2015-10-10 04:21:01 +03:00
|
|
|
return IsInitializedWithPlugin() ?
|
|
|
|
mRootContent == aContent && mRootContent != nullptr :
|
|
|
|
mEditableNode == IMEStateManager::GetRootEditableNode(aPresContext,
|
|
|
|
aContent);
|
2014-03-08 05:20:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IMEContentObserver::IsEditorHandlingEventForComposition() const
|
|
|
|
{
|
|
|
|
if (!mWidget) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<TextComposition> composition =
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::GetTextCompositionFor(mWidget);
|
2014-03-08 05:20:07 +04:00
|
|
|
if (!composition) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return composition->IsEditorHandlingEvent();
|
|
|
|
}
|
|
|
|
|
2015-10-27 01:21:37 +03:00
|
|
|
bool
|
|
|
|
IMEContentObserver::IsEditorComposing() const
|
|
|
|
{
|
|
|
|
// Note that don't use TextComposition here. The important thing is,
|
|
|
|
// whether the editor already started to handle composition because
|
|
|
|
// web contents can change selection, text content and/or something from
|
|
|
|
// compositionstart event listener which is run before nsEditor handles it.
|
|
|
|
nsCOMPtr<nsIEditorIMESupport> editorIMESupport = do_QueryInterface(mEditor);
|
|
|
|
if (NS_WARN_IF(!editorIMESupport)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool isComposing = false;
|
|
|
|
nsresult rv = editorIMESupport->GetComposing(&isComposing);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return isComposing;
|
|
|
|
}
|
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
nsresult
|
|
|
|
IMEContentObserver::GetSelectionAndRoot(nsISelection** aSelection,
|
|
|
|
nsIContent** aRootContent) const
|
|
|
|
{
|
|
|
|
if (!mEditableNode || !mSelection) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mSelection && mRootContent, "uninitialized content observer");
|
|
|
|
NS_ADDREF(*aSelection = mSelection);
|
|
|
|
NS_ADDREF(*aRootContent = mRootContent);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
nsresult
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEContentObserver::NotifySelectionChanged(nsIDOMDocument* aDOMDocument,
|
|
|
|
nsISelection* aSelection,
|
2014-03-08 05:20:07 +04:00
|
|
|
int16_t aReason)
|
|
|
|
{
|
|
|
|
int32_t count = 0;
|
2014-03-08 05:20:07 +04:00
|
|
|
nsresult rv = aSelection->GetRangeCount(&count);
|
2014-03-08 05:20:07 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (count > 0 && mWidget) {
|
2015-08-21 19:43:42 +03:00
|
|
|
bool causedByComposition = IsEditorHandlingEventForComposition();
|
2015-07-17 05:25:00 +03:00
|
|
|
bool causedBySelectionEvent = TextComposition::IsHandlingSelectionEvent();
|
2015-10-27 01:21:37 +03:00
|
|
|
bool duringComposition = IsEditorComposing();
|
2015-07-17 05:25:00 +03:00
|
|
|
MaybeNotifyIMEOfSelectionChange(causedByComposition,
|
2015-10-27 01:21:37 +03:00
|
|
|
causedBySelectionEvent,
|
|
|
|
duringComposition);
|
2014-03-08 05:20:07 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IMEContentObserver::ScrollPositionChanged()
|
|
|
|
{
|
2014-07-31 08:38:00 +04:00
|
|
|
MaybeNotifyIMEOfPositionChange();
|
2014-03-08 05:20:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IMEContentObserver::Reflow(DOMHighResTimeStamp aStart,
|
|
|
|
DOMHighResTimeStamp aEnd)
|
|
|
|
{
|
2014-07-31 08:38:00 +04:00
|
|
|
MaybeNotifyIMEOfPositionChange();
|
2014-03-08 05:20:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IMEContentObserver::ReflowInterruptible(DOMHighResTimeStamp aStart,
|
|
|
|
DOMHighResTimeStamp aEnd)
|
|
|
|
{
|
2014-07-31 08:38:00 +04:00
|
|
|
MaybeNotifyIMEOfPositionChange();
|
2014-03-08 05:20:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-08 06:54:14 +03:00
|
|
|
nsresult
|
|
|
|
IMEContentObserver::HandleQueryContentEvent(WidgetQueryContentEvent* aEvent)
|
|
|
|
{
|
2015-09-08 06:54:14 +03:00
|
|
|
// If the instance has cache, it should use the cached selection which was
|
2015-11-10 05:49:05 +03:00
|
|
|
// sent to the widget. However, if this instance has already received new
|
|
|
|
// selection change notification but hasn't updated the cache yet (i.e.,
|
|
|
|
// not sending selection change notification to IME, don't use the cached
|
|
|
|
// value. Note that don't update selection cache here since if you update
|
|
|
|
// selection cache here, IMENotificationSender won't notify IME of selection
|
|
|
|
// change because it looks like that the selection isn't actually changed.
|
2015-09-10 04:40:05 +03:00
|
|
|
if (aEvent->mMessage == eQuerySelectedText && aEvent->mUseNativeLineBreak &&
|
2015-11-10 05:49:05 +03:00
|
|
|
mSelectionData.IsValid() && !mNeedsToNotifyIMEOfSelectionChange) {
|
2015-09-08 06:54:14 +03:00
|
|
|
aEvent->mReply.mContentsRoot = mRootContent;
|
|
|
|
aEvent->mReply.mHasSelection = !mSelectionData.IsCollapsed();
|
|
|
|
aEvent->mReply.mOffset = mSelectionData.mOffset;
|
|
|
|
aEvent->mReply.mString = mSelectionData.String();
|
|
|
|
aEvent->mReply.mWritingMode = mSelectionData.GetWritingMode();
|
|
|
|
aEvent->mReply.mReversed = mSelectionData.mReversed;
|
|
|
|
aEvent->mSucceeded = true;
|
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::HandleQueryContentEvent(aEvent={ "
|
|
|
|
"mMessage=%s })", this, ToChar(aEvent->mMessage)));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::HandleQueryContentEvent(aEvent={ "
|
|
|
|
"mMessage=%s })", this, ToChar(aEvent->mMessage)));
|
|
|
|
|
|
|
|
AutoRestore<bool> handling(mIsHandlingQueryContentEvent);
|
|
|
|
mIsHandlingQueryContentEvent = true;
|
2015-09-08 06:54:14 +03:00
|
|
|
ContentEventHandler handler(GetPresContext());
|
2015-09-08 06:54:14 +03:00
|
|
|
nsresult rv = handler.HandleQueryContentEvent(aEvent);
|
2016-03-16 21:20:30 +03:00
|
|
|
|
|
|
|
if (!IsInitializedWithPlugin() &&
|
|
|
|
NS_WARN_IF(aEvent->mReply.mContentsRoot != mRootContent)) {
|
|
|
|
// Focus has changed unexpectedly, so make the query fail.
|
|
|
|
aEvent->mSucceeded = false;
|
2015-09-08 06:54:14 +03:00
|
|
|
}
|
|
|
|
return rv;
|
2015-09-08 06:54:14 +03:00
|
|
|
}
|
|
|
|
|
2014-08-29 14:08:43 +04:00
|
|
|
bool
|
|
|
|
IMEContentObserver::OnMouseButtonEvent(nsPresContext* aPresContext,
|
|
|
|
WidgetMouseEvent* aMouseEvent)
|
|
|
|
{
|
|
|
|
if (!mUpdatePreference.WantMouseButtonEventOnChar()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!aMouseEvent->mFlags.mIsTrusted ||
|
|
|
|
aMouseEvent->mFlags.mDefaultPrevented ||
|
|
|
|
!aMouseEvent->widget) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Now, we need to notify only mouse down and mouse up event.
|
2015-08-22 04:34:51 +03:00
|
|
|
switch (aMouseEvent->mMessage) {
|
2015-08-29 02:58:30 +03:00
|
|
|
case eMouseUp:
|
2015-08-29 02:58:30 +03:00
|
|
|
case eMouseDown:
|
2014-08-29 14:08:43 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-17 16:20:08 +03:00
|
|
|
if (NS_WARN_IF(!mWidget) || NS_WARN_IF(mWidget->Destroyed())) {
|
2014-08-29 14:08:43 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IMEContentObserver> kungFuDeathGrip(this);
|
2015-02-17 16:20:08 +03:00
|
|
|
|
2015-09-10 04:40:06 +03:00
|
|
|
WidgetQueryContentEvent charAtPt(true, eQueryCharacterAtPoint,
|
2014-08-29 14:08:43 +04:00
|
|
|
aMouseEvent->widget);
|
|
|
|
charAtPt.refPoint = aMouseEvent->refPoint;
|
|
|
|
ContentEventHandler handler(aPresContext);
|
|
|
|
handler.OnQueryCharacterAtPoint(&charAtPt);
|
|
|
|
if (NS_WARN_IF(!charAtPt.mSucceeded) ||
|
|
|
|
charAtPt.mReply.mOffset == WidgetQueryContentEvent::NOT_FOUND) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-17 16:20:08 +03:00
|
|
|
// The widget might be destroyed during querying the content since it
|
|
|
|
// causes flushing layout.
|
|
|
|
if (!mWidget || NS_WARN_IF(mWidget->Destroyed())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-29 14:08:43 +04:00
|
|
|
// The result character rect is relative to the top level widget.
|
|
|
|
// We should notify it with offset in the widget.
|
|
|
|
nsIWidget* topLevelWidget = mWidget->GetTopLevelWidget();
|
|
|
|
if (topLevelWidget && topLevelWidget != mWidget) {
|
|
|
|
charAtPt.mReply.mRect.MoveBy(
|
2015-02-04 23:21:03 +03:00
|
|
|
topLevelWidget->WidgetToScreenOffset() -
|
|
|
|
mWidget->WidgetToScreenOffset());
|
2014-08-29 14:08:43 +04:00
|
|
|
}
|
|
|
|
// The refPt is relative to its widget.
|
|
|
|
// We should notify it with offset in the widget.
|
|
|
|
if (aMouseEvent->widget != mWidget) {
|
2015-02-04 23:21:03 +03:00
|
|
|
charAtPt.refPoint += aMouseEvent->widget->WidgetToScreenOffset() -
|
|
|
|
mWidget->WidgetToScreenOffset();
|
2014-08-29 14:08:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
IMENotification notification(NOTIFY_IME_OF_MOUSE_BUTTON_EVENT);
|
2015-08-22 04:34:51 +03:00
|
|
|
notification.mMouseButtonEventData.mEventMessage = aMouseEvent->mMessage;
|
2014-08-29 14:08:43 +04:00
|
|
|
notification.mMouseButtonEventData.mOffset = charAtPt.mReply.mOffset;
|
|
|
|
notification.mMouseButtonEventData.mCursorPos.Set(
|
2015-11-07 06:13:40 +03:00
|
|
|
charAtPt.refPoint.ToUnknownPoint());
|
2015-02-04 23:21:03 +03:00
|
|
|
notification.mMouseButtonEventData.mCharRect.Set(
|
2015-11-07 06:13:40 +03:00
|
|
|
charAtPt.mReply.mRect.ToUnknownRect());
|
2014-08-29 14:08:43 +04:00
|
|
|
notification.mMouseButtonEventData.mButton = aMouseEvent->button;
|
|
|
|
notification.mMouseButtonEventData.mButtons = aMouseEvent->buttons;
|
|
|
|
notification.mMouseButtonEventData.mModifiers = aMouseEvent->modifiers;
|
|
|
|
|
2015-06-27 03:23:31 +03:00
|
|
|
nsresult rv = IMEStateManager::NotifyIME(notification, mWidget);
|
2014-08-29 14:08:43 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool consumed = (rv == NS_SUCCESS_EVENT_CONSUMED);
|
|
|
|
aMouseEvent->mFlags.mDefaultPrevented = consumed;
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
2014-07-31 08:38:01 +04:00
|
|
|
void
|
|
|
|
IMEContentObserver::CharacterDataWillChange(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContent,
|
|
|
|
CharacterDataChangeInfo* aInfo)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aContent->IsNodeOfType(nsINode::eTEXT),
|
|
|
|
"character data changed for non-text node");
|
|
|
|
MOZ_ASSERT(mPreCharacterDataChangeLength < 0,
|
|
|
|
"CharacterDataChanged() should've reset "
|
|
|
|
"mPreCharacterDataChangeLength");
|
|
|
|
|
|
|
|
mEndOfAddedTextCache.Clear();
|
|
|
|
mStartOfRemovingTextRangeCache.Clear();
|
|
|
|
|
|
|
|
bool causedByComposition = IsEditorHandlingEventForComposition();
|
2015-07-11 04:53:56 +03:00
|
|
|
if (!mTextChangeData.IsValid() && causedByComposition &&
|
2014-07-31 08:38:01 +04:00
|
|
|
!mUpdatePreference.WantChangesCausedByComposition()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPreCharacterDataChangeLength =
|
|
|
|
ContentEventHandler::GetNativeTextLength(aContent, aInfo->mChangeStart,
|
|
|
|
aInfo->mChangeEnd);
|
|
|
|
MOZ_ASSERT(mPreCharacterDataChangeLength >=
|
|
|
|
aInfo->mChangeEnd - aInfo->mChangeStart,
|
|
|
|
"The computed length must be same as or larger than XP length");
|
|
|
|
}
|
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
void
|
|
|
|
IMEContentObserver::CharacterDataChanged(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContent,
|
|
|
|
CharacterDataChangeInfo* aInfo)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aContent->IsNodeOfType(nsINode::eTEXT),
|
|
|
|
"character data changed for non-text node");
|
|
|
|
|
2014-07-31 08:38:00 +04:00
|
|
|
mEndOfAddedTextCache.Clear();
|
2014-07-31 08:38:00 +04:00
|
|
|
mStartOfRemovingTextRangeCache.Clear();
|
2014-07-31 08:38:00 +04:00
|
|
|
|
2014-07-31 08:38:01 +04:00
|
|
|
int64_t removedLength = mPreCharacterDataChangeLength;
|
|
|
|
mPreCharacterDataChangeLength = -1;
|
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
bool causedByComposition = IsEditorHandlingEventForComposition();
|
2015-07-11 04:53:56 +03:00
|
|
|
if (!mTextChangeData.IsValid() && causedByComposition &&
|
2014-03-08 05:20:07 +04:00
|
|
|
!mUpdatePreference.WantChangesCausedByComposition()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-31 08:38:01 +04:00
|
|
|
MOZ_ASSERT(removedLength >= 0,
|
|
|
|
"mPreCharacterDataChangeLength should've been set by "
|
|
|
|
"CharacterDataWillChange()");
|
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
uint32_t offset = 0;
|
|
|
|
// get offsets of change and fire notification
|
2014-03-08 05:20:07 +04:00
|
|
|
nsresult rv =
|
2015-12-02 07:20:00 +03:00
|
|
|
ContentEventHandler::GetFlatTextLengthInRange(
|
|
|
|
NodePosition(mRootContent, 0),
|
|
|
|
NodePosition(aContent, aInfo->mChangeStart),
|
2015-12-02 07:20:00 +03:00
|
|
|
mRootContent, &offset, LINE_BREAK_TYPE_NATIVE);
|
2015-12-02 07:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-08 05:20:07 +04:00
|
|
|
|
2014-07-31 08:38:01 +04:00
|
|
|
uint32_t newLength =
|
|
|
|
ContentEventHandler::GetNativeTextLength(aContent, aInfo->mChangeStart,
|
|
|
|
aInfo->mChangeStart +
|
|
|
|
aInfo->mReplaceLength);
|
|
|
|
|
|
|
|
uint32_t oldEnd = offset + static_cast<uint32_t>(removedLength);
|
|
|
|
uint32_t newEnd = offset + newLength;
|
2014-03-08 05:20:07 +04:00
|
|
|
|
2015-10-27 01:21:37 +03:00
|
|
|
TextChangeData data(offset, oldEnd, newEnd, causedByComposition,
|
|
|
|
IsEditorComposing());
|
2014-07-31 08:38:00 +04:00
|
|
|
MaybeNotifyIMEOfTextChange(data);
|
2014-03-08 05:20:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IMEContentObserver::NotifyContentAdded(nsINode* aContainer,
|
|
|
|
int32_t aStartIndex,
|
|
|
|
int32_t aEndIndex)
|
|
|
|
{
|
2014-07-31 08:38:00 +04:00
|
|
|
mStartOfRemovingTextRangeCache.Clear();
|
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
bool causedByComposition = IsEditorHandlingEventForComposition();
|
2015-07-11 04:53:56 +03:00
|
|
|
if (!mTextChangeData.IsValid() && causedByComposition &&
|
2014-03-08 05:20:07 +04:00
|
|
|
!mUpdatePreference.WantChangesCausedByComposition()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
uint32_t offset = 0;
|
2014-07-31 08:38:00 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (!mEndOfAddedTextCache.Match(aContainer, aStartIndex)) {
|
|
|
|
mEndOfAddedTextCache.Clear();
|
2015-12-02 07:20:00 +03:00
|
|
|
rv = ContentEventHandler::GetFlatTextLengthInRange(
|
|
|
|
NodePosition(mRootContent, 0),
|
2015-12-02 07:20:00 +03:00
|
|
|
NodePositionBefore(aContainer, aStartIndex),
|
|
|
|
mRootContent, &offset, LINE_BREAK_TYPE_NATIVE);
|
2014-07-31 08:38:00 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED((rv)))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
offset = mEndOfAddedTextCache.mFlatTextLength;
|
|
|
|
}
|
2014-03-08 05:20:07 +04:00
|
|
|
|
|
|
|
// get offset at the end of the last added node
|
2014-03-08 05:20:07 +04:00
|
|
|
uint32_t addingLength = 0;
|
2015-12-02 07:20:00 +03:00
|
|
|
rv = ContentEventHandler::GetFlatTextLengthInRange(
|
2015-12-02 07:20:00 +03:00
|
|
|
NodePositionBefore(aContainer, aStartIndex),
|
2015-12-02 07:20:00 +03:00
|
|
|
NodePosition(aContainer, aEndIndex),
|
|
|
|
mRootContent, &addingLength,
|
|
|
|
LINE_BREAK_TYPE_NATIVE);
|
2014-07-31 08:38:00 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED((rv)))) {
|
|
|
|
mEndOfAddedTextCache.Clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If multiple lines are being inserted in an HTML editor, next call of
|
|
|
|
// NotifyContentAdded() is for adding next node. Therefore, caching the text
|
|
|
|
// length can skip to compute the text length before the adding node and
|
|
|
|
// before of it.
|
|
|
|
mEndOfAddedTextCache.Cache(aContainer, aEndIndex, offset + addingLength);
|
2014-03-08 05:20:07 +04:00
|
|
|
|
|
|
|
if (!addingLength) {
|
2014-03-08 05:20:07 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-03-08 05:20:07 +04:00
|
|
|
|
2014-07-31 08:37:59 +04:00
|
|
|
TextChangeData data(offset, offset, offset + addingLength,
|
2015-10-27 01:21:37 +03:00
|
|
|
causedByComposition, IsEditorComposing());
|
2014-07-31 08:38:00 +04:00
|
|
|
MaybeNotifyIMEOfTextChange(data);
|
2014-03-08 05:20:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IMEContentObserver::ContentAppended(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aFirstNewContent,
|
|
|
|
int32_t aNewIndexInContainer)
|
|
|
|
{
|
|
|
|
NotifyContentAdded(aContainer, aNewIndexInContainer,
|
|
|
|
aContainer->GetChildCount());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IMEContentObserver::ContentInserted(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
|
|
|
int32_t aIndexInContainer)
|
|
|
|
{
|
|
|
|
NotifyContentAdded(NODE_FROM(aContainer, aDocument),
|
|
|
|
aIndexInContainer, aIndexInContainer + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IMEContentObserver::ContentRemoved(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
|
|
|
int32_t aIndexInContainer,
|
|
|
|
nsIContent* aPreviousSibling)
|
|
|
|
{
|
2014-07-31 08:38:00 +04:00
|
|
|
mEndOfAddedTextCache.Clear();
|
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
bool causedByComposition = IsEditorHandlingEventForComposition();
|
2015-07-11 04:53:56 +03:00
|
|
|
if (!mTextChangeData.IsValid() && causedByComposition &&
|
2014-03-08 05:20:07 +04:00
|
|
|
!mUpdatePreference.WantChangesCausedByComposition()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-31 08:38:00 +04:00
|
|
|
nsINode* containerNode = NODE_FROM(aContainer, aDocument);
|
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
uint32_t offset = 0;
|
2014-07-31 08:38:00 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (!mStartOfRemovingTextRangeCache.Match(containerNode, aIndexInContainer)) {
|
2015-12-02 07:20:00 +03:00
|
|
|
// At removing a child node of aContainer, we need the line break caused
|
|
|
|
// by open tag of aContainer. Be careful when aIndexInContainer is 0.
|
2015-12-02 07:20:00 +03:00
|
|
|
rv = ContentEventHandler::GetFlatTextLengthInRange(
|
|
|
|
NodePosition(mRootContent, 0),
|
|
|
|
NodePosition(containerNode, aIndexInContainer),
|
2015-12-02 07:20:00 +03:00
|
|
|
mRootContent, &offset, LINE_BREAK_TYPE_NATIVE);
|
2014-07-31 08:38:00 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mStartOfRemovingTextRangeCache.Clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mStartOfRemovingTextRangeCache.Cache(containerNode, aIndexInContainer,
|
|
|
|
offset);
|
|
|
|
} else {
|
|
|
|
offset = mStartOfRemovingTextRangeCache.mFlatTextLength;
|
|
|
|
}
|
2014-03-08 05:20:07 +04:00
|
|
|
|
|
|
|
// get offset at the end of the deleted node
|
2014-03-08 05:20:07 +04:00
|
|
|
uint32_t textLength = 0;
|
2015-12-02 07:20:00 +03:00
|
|
|
if (aChild->IsNodeOfType(nsINode::eTEXT)) {
|
|
|
|
textLength = ContentEventHandler::GetNativeTextLength(aChild);
|
|
|
|
} else {
|
2015-12-02 07:20:00 +03:00
|
|
|
uint32_t nodeLength = static_cast<int32_t>(aChild->GetChildCount());
|
2015-12-02 07:20:00 +03:00
|
|
|
rv = ContentEventHandler::GetFlatTextLengthInRange(
|
2015-12-02 07:20:00 +03:00
|
|
|
NodePositionBefore(aChild, 0),
|
2015-12-02 07:20:00 +03:00
|
|
|
NodePosition(aChild, nodeLength),
|
|
|
|
mRootContent, &textLength,
|
2015-12-02 07:20:00 +03:00
|
|
|
LINE_BREAK_TYPE_NATIVE, true);
|
2015-12-02 07:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mStartOfRemovingTextRangeCache.Clear();
|
|
|
|
return;
|
|
|
|
}
|
2014-07-31 08:38:00 +04:00
|
|
|
}
|
2014-03-08 05:20:07 +04:00
|
|
|
|
|
|
|
if (!textLength) {
|
2014-03-08 05:20:07 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-03-08 05:20:07 +04:00
|
|
|
|
2015-10-27 01:21:37 +03:00
|
|
|
TextChangeData data(offset, offset + textLength, offset,
|
|
|
|
causedByComposition, IsEditorComposing());
|
2014-07-31 08:38:00 +04:00
|
|
|
MaybeNotifyIMEOfTextChange(data);
|
2014-03-08 05:20:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IMEContentObserver::AttributeWillChange(nsIDocument* aDocument,
|
|
|
|
dom::Element* aElement,
|
|
|
|
int32_t aNameSpaceID,
|
|
|
|
nsIAtom* aAttribute,
|
2015-07-25 09:05:19 +03:00
|
|
|
int32_t aModType,
|
|
|
|
const nsAttrValue* aNewValue)
|
2014-03-08 05:20:07 +04:00
|
|
|
{
|
2015-12-02 07:20:00 +03:00
|
|
|
mPreAttrChangeLength =
|
2015-12-02 07:20:00 +03:00
|
|
|
ContentEventHandler::GetNativeTextLengthBefore(aElement, mRootContent);
|
2014-03-08 05:20:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IMEContentObserver::AttributeChanged(nsIDocument* aDocument,
|
|
|
|
dom::Element* aElement,
|
|
|
|
int32_t aNameSpaceID,
|
|
|
|
nsIAtom* aAttribute,
|
2015-07-25 09:01:19 +03:00
|
|
|
int32_t aModType,
|
|
|
|
const nsAttrValue* aOldValue)
|
2014-03-08 05:20:07 +04:00
|
|
|
{
|
2014-07-31 08:38:00 +04:00
|
|
|
mEndOfAddedTextCache.Clear();
|
2014-07-31 08:38:00 +04:00
|
|
|
mStartOfRemovingTextRangeCache.Clear();
|
2014-07-31 08:38:00 +04:00
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
bool causedByComposition = IsEditorHandlingEventForComposition();
|
2015-07-11 04:53:56 +03:00
|
|
|
if (!mTextChangeData.IsValid() && causedByComposition &&
|
2014-03-08 05:20:07 +04:00
|
|
|
!mUpdatePreference.WantChangesCausedByComposition()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t postAttrChangeLength =
|
2015-12-02 07:20:00 +03:00
|
|
|
ContentEventHandler::GetNativeTextLengthBefore(aElement, mRootContent);
|
2014-03-08 05:20:07 +04:00
|
|
|
if (postAttrChangeLength == mPreAttrChangeLength) {
|
|
|
|
return;
|
2014-03-08 05:20:07 +04:00
|
|
|
}
|
2014-03-08 05:20:07 +04:00
|
|
|
uint32_t start;
|
|
|
|
nsresult rv =
|
2015-12-02 07:20:00 +03:00
|
|
|
ContentEventHandler::GetFlatTextLengthInRange(
|
|
|
|
NodePosition(mRootContent, 0),
|
|
|
|
NodePositionBefore(aElement, 0),
|
|
|
|
mRootContent, &start, LINE_BREAK_TYPE_NATIVE);
|
2015-12-02 07:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-08 05:20:07 +04:00
|
|
|
|
2014-07-31 08:37:59 +04:00
|
|
|
TextChangeData data(start, start + mPreAttrChangeLength,
|
2015-10-27 01:21:37 +03:00
|
|
|
start + postAttrChangeLength, causedByComposition,
|
|
|
|
IsEditorComposing());
|
2014-07-31 08:38:00 +04:00
|
|
|
MaybeNotifyIMEOfTextChange(data);
|
2014-07-31 08:37:59 +04:00
|
|
|
}
|
|
|
|
|
2015-09-08 06:54:14 +03:00
|
|
|
void
|
|
|
|
IMEContentObserver::SuppressNotifyingIME()
|
|
|
|
{
|
|
|
|
mSuppressNotifications++;
|
|
|
|
|
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::SuppressNotifyingIME(), "
|
|
|
|
"mSuppressNotifications=%u", this, mSuppressNotifications));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IMEContentObserver::UnsuppressNotifyingIME()
|
|
|
|
{
|
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::UnsuppressNotifyingIME(), "
|
|
|
|
"mSuppressNotifications=%u", this, mSuppressNotifications));
|
|
|
|
|
|
|
|
if (!mSuppressNotifications || --mSuppressNotifications) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FlushMergeableNotifications();
|
|
|
|
}
|
|
|
|
|
2014-07-31 08:37:59 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IMEContentObserver::EditAction()
|
|
|
|
{
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::EditAction()", this));
|
|
|
|
|
2014-07-31 08:38:00 +04:00
|
|
|
mEndOfAddedTextCache.Clear();
|
2014-07-31 08:38:00 +04:00
|
|
|
mStartOfRemovingTextRangeCache.Clear();
|
2014-07-31 08:37:59 +04:00
|
|
|
FlushMergeableNotifications();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IMEContentObserver::BeforeEditAction()
|
|
|
|
{
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::BeforeEditAction()", this));
|
|
|
|
|
2014-07-31 08:38:00 +04:00
|
|
|
mEndOfAddedTextCache.Clear();
|
2014-07-31 08:38:00 +04:00
|
|
|
mStartOfRemovingTextRangeCache.Clear();
|
2014-07-31 08:37:59 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IMEContentObserver::CancelEditAction()
|
|
|
|
{
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::CancelEditAction()", this));
|
|
|
|
|
2014-07-31 08:38:00 +04:00
|
|
|
mEndOfAddedTextCache.Clear();
|
2014-07-31 08:38:00 +04:00
|
|
|
mStartOfRemovingTextRangeCache.Clear();
|
2014-07-31 08:37:59 +04:00
|
|
|
FlushMergeableNotifications();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-06-17 04:03:57 +03:00
|
|
|
void
|
2015-06-17 04:03:57 +03:00
|
|
|
IMEContentObserver::PostFocusSetNotification()
|
2015-06-17 04:03:57 +03:00
|
|
|
{
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::PostFocusSetNotification()", this));
|
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
mNeedsToNotifyIMEOfFocusSet = true;
|
2015-06-17 04:03:57 +03:00
|
|
|
}
|
|
|
|
|
2014-07-31 08:38:00 +04:00
|
|
|
void
|
2015-09-16 11:48:23 +03:00
|
|
|
IMEContentObserver::PostTextChangeNotification()
|
2014-07-31 08:38:00 +04:00
|
|
|
{
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::PostTextChangeNotification("
|
2015-09-16 11:48:23 +03:00
|
|
|
"mTextChangeData=%s)",
|
|
|
|
this, TextChangeDataToString(mTextChangeData).get()));
|
2015-09-08 06:54:14 +03:00
|
|
|
|
2015-07-11 04:53:56 +03:00
|
|
|
MOZ_ASSERT(mTextChangeData.IsValid(),
|
2014-07-31 08:38:00 +04:00
|
|
|
"mTextChangeData must have text change data");
|
2015-09-16 11:48:24 +03:00
|
|
|
mNeedsToNotifyIMEOfTextChange = true;
|
2014-07-31 08:38:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-14 17:28:43 +03:00
|
|
|
IMEContentObserver::PostSelectionChangeNotification()
|
2014-07-31 08:38:00 +04:00
|
|
|
{
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
2015-09-14 17:28:43 +03:00
|
|
|
("IMECO: 0x%p IMEContentObserver::PostSelectionChangeNotification(), "
|
|
|
|
"mSelectionData={ mCausedByComposition=%s, mCausedBySelectionEvent=%s }",
|
|
|
|
this, ToChar(mSelectionData.mCausedByComposition),
|
|
|
|
ToChar(mSelectionData.mCausedBySelectionEvent)));
|
2015-09-08 06:54:14 +03:00
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
mNeedsToNotifyIMEOfSelectionChange = true;
|
2014-07-31 08:38:00 +04:00
|
|
|
}
|
|
|
|
|
2015-09-08 06:54:14 +03:00
|
|
|
void
|
|
|
|
IMEContentObserver::MaybeNotifyIMEOfFocusSet()
|
|
|
|
{
|
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::MaybeNotifyIMEOfFocusSet()", this));
|
|
|
|
|
|
|
|
PostFocusSetNotification();
|
|
|
|
FlushMergeableNotifications();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IMEContentObserver::MaybeNotifyIMEOfTextChange(
|
|
|
|
const TextChangeDataBase& aTextChangeData)
|
|
|
|
{
|
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::MaybeNotifyIMEOfTextChange("
|
|
|
|
"aTextChangeData=%s)",
|
|
|
|
this, TextChangeDataToString(aTextChangeData).get()));
|
|
|
|
|
2015-09-16 11:48:23 +03:00
|
|
|
mTextChangeData += aTextChangeData;
|
|
|
|
PostTextChangeNotification();
|
2015-09-08 06:54:14 +03:00
|
|
|
FlushMergeableNotifications();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IMEContentObserver::MaybeNotifyIMEOfSelectionChange(
|
|
|
|
bool aCausedByComposition,
|
2015-10-27 01:21:37 +03:00
|
|
|
bool aCausedBySelectionEvent,
|
|
|
|
bool aOccurredDuringComposition)
|
2015-09-08 06:54:14 +03:00
|
|
|
{
|
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::MaybeNotifyIMEOfSelectionChange("
|
2015-10-27 01:21:37 +03:00
|
|
|
"aCausedByComposition=%s, aCausedBySelectionEvent=%s, "
|
|
|
|
"aOccurredDuringComposition)",
|
2015-09-08 06:54:14 +03:00
|
|
|
this, ToChar(aCausedByComposition), ToChar(aCausedBySelectionEvent)));
|
|
|
|
|
2015-09-14 17:28:43 +03:00
|
|
|
mSelectionData.AssignReason(aCausedByComposition,
|
2015-10-27 01:21:37 +03:00
|
|
|
aCausedBySelectionEvent,
|
|
|
|
aOccurredDuringComposition);
|
2015-09-14 17:28:43 +03:00
|
|
|
PostSelectionChangeNotification();
|
2015-09-08 06:54:14 +03:00
|
|
|
FlushMergeableNotifications();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IMEContentObserver::MaybeNotifyIMEOfPositionChange()
|
|
|
|
{
|
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::MaybeNotifyIMEOfPositionChange()", this));
|
2015-09-16 11:48:24 +03:00
|
|
|
// If reflow is caused by ContentEventHandler during PositionChangeEvent
|
|
|
|
// sending NOTIFY_IME_OF_POSITION_CHANGE, we don't need to notify IME of it
|
|
|
|
// again since ContentEventHandler returns the result including this reflow's
|
|
|
|
// result.
|
|
|
|
if (mIsHandlingQueryContentEvent &&
|
|
|
|
mSendingNotification == NOTIFY_IME_OF_POSITION_CHANGE) {
|
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::MaybeNotifyIMEOfPositionChange(), "
|
|
|
|
"ignored since caused by ContentEventHandler during sending "
|
|
|
|
"NOTIY_IME_OF_POSITION_CHANGE", this));
|
|
|
|
return;
|
|
|
|
}
|
2015-09-08 06:54:14 +03:00
|
|
|
PostPositionChangeNotification();
|
|
|
|
FlushMergeableNotifications();
|
|
|
|
}
|
|
|
|
|
2015-08-21 19:43:41 +03:00
|
|
|
bool
|
|
|
|
IMEContentObserver::UpdateSelectionCache()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsSafeToNotifyIME());
|
|
|
|
|
|
|
|
if (!mUpdatePreference.WantSelectionChange()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-14 17:28:43 +03:00
|
|
|
mSelectionData.ClearSelectionData();
|
2015-08-21 19:43:41 +03:00
|
|
|
|
|
|
|
// XXX Cannot we cache some information for reducing the cost to compute
|
|
|
|
// selection offset and writing mode?
|
2015-09-10 04:40:05 +03:00
|
|
|
WidgetQueryContentEvent selection(true, eQuerySelectedText, mWidget);
|
2015-08-21 19:43:41 +03:00
|
|
|
ContentEventHandler handler(GetPresContext());
|
|
|
|
handler.OnQuerySelectedText(&selection);
|
2016-03-16 21:20:30 +03:00
|
|
|
if (NS_WARN_IF(!selection.mSucceeded) ||
|
|
|
|
NS_WARN_IF(selection.mReply.mContentsRoot != mRootContent)) {
|
2015-08-21 19:43:41 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-08 06:54:14 +03:00
|
|
|
mFocusedWidget = selection.mReply.mFocusedWidget;
|
2015-08-21 19:43:41 +03:00
|
|
|
mSelectionData.mOffset = selection.mReply.mOffset;
|
|
|
|
*mSelectionData.mString = selection.mReply.mString;
|
|
|
|
mSelectionData.SetWritingMode(selection.GetWritingMode());
|
|
|
|
mSelectionData.mReversed = selection.mReply.mReversed;
|
2015-09-14 17:28:43 +03:00
|
|
|
|
|
|
|
// WARNING: Don't modify the reason of selection change here.
|
2015-09-08 06:54:14 +03:00
|
|
|
|
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::UpdateSelectionCache(), "
|
|
|
|
"mSelectionData=%s",
|
|
|
|
this, SelectionChangeDataToString(mSelectionData).get()));
|
|
|
|
|
2015-08-21 19:43:41 +03:00
|
|
|
return mSelectionData.IsValid();
|
|
|
|
}
|
|
|
|
|
2014-07-31 08:38:00 +04:00
|
|
|
void
|
2015-06-17 04:03:57 +03:00
|
|
|
IMEContentObserver::PostPositionChangeNotification()
|
2014-07-31 08:38:00 +04:00
|
|
|
{
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::PostPositionChangeNotification()", this));
|
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
mNeedsToNotifyIMEOfPositionChange = true;
|
2014-07-31 08:38:00 +04:00
|
|
|
}
|
|
|
|
|
2015-06-17 04:03:57 +03:00
|
|
|
bool
|
|
|
|
IMEContentObserver::IsReflowLocked() const
|
|
|
|
{
|
|
|
|
nsPresContext* presContext = GetPresContext();
|
|
|
|
if (NS_WARN_IF(!presContext)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
nsIPresShell* presShell = presContext->GetPresShell();
|
|
|
|
if (NS_WARN_IF(!presShell)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// During reflow, we shouldn't notify IME because IME may query content
|
|
|
|
// synchronously. Then, it causes ContentEventHandler will try to flush
|
|
|
|
// pending notifications during reflow.
|
|
|
|
return presShell->IsReflowLocked();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IMEContentObserver::IsSafeToNotifyIME() const
|
2014-07-31 08:37:59 +04:00
|
|
|
{
|
2014-08-13 15:25:14 +04:00
|
|
|
// If this is already detached from the widget, this doesn't need to notify
|
|
|
|
// anything.
|
2014-08-19 15:54:08 +04:00
|
|
|
if (!mWidget) {
|
2015-06-17 04:03:57 +03:00
|
|
|
return false;
|
2014-08-19 15:54:08 +04:00
|
|
|
}
|
|
|
|
|
2015-06-04 20:06:10 +03:00
|
|
|
// Don't notify IME of anything if it's not good time to do it.
|
|
|
|
if (mSuppressNotifications) {
|
2015-06-17 04:03:57 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mESM || NS_WARN_IF(!GetPresContext())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's in reflow, we should wait to finish the reflow.
|
|
|
|
// FYI: This should be called again from Reflow() or ReflowInterruptible().
|
|
|
|
if (IsReflowLocked()) {
|
|
|
|
return false;
|
2015-06-04 20:06:10 +03:00
|
|
|
}
|
|
|
|
|
2014-08-19 15:54:08 +04:00
|
|
|
// If we're in handling an edit action, this method will be called later.
|
2014-08-28 18:33:35 +04:00
|
|
|
bool isInEditAction = false;
|
|
|
|
if (mEditor && NS_SUCCEEDED(mEditor->GetIsInEditAction(&isInEditAction)) &&
|
|
|
|
isInEditAction) {
|
2015-06-17 04:03:57 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IMEContentObserver::FlushMergeableNotifications()
|
|
|
|
{
|
|
|
|
if (!IsSafeToNotifyIME()) {
|
|
|
|
// So, if this is already called, this should do nothing.
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::FlushMergeableNotifications(), "
|
|
|
|
"FAILED, due to unsafe to notify IME", this));
|
2014-07-31 08:37:59 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-13 15:25:14 +04:00
|
|
|
// Notifying something may cause nested call of this method. For example,
|
|
|
|
// when somebody notified one of the notifications may dispatch query content
|
|
|
|
// event. Then, it causes flushing layout which may cause another layout
|
|
|
|
// change notification.
|
|
|
|
|
2015-11-10 05:49:05 +03:00
|
|
|
if (mQueuedSender) {
|
2014-08-13 15:25:14 +04:00
|
|
|
// So, if this is already called, this should do nothing.
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::FlushMergeableNotifications(), "
|
|
|
|
"FAILED, due to already flushing pending notifications", this));
|
2014-08-13 15:25:14 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
if (!NeedsToNotifyIMEOfSomething()) {
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
2015-09-16 11:48:24 +03:00
|
|
|
("IMECO: 0x%p IMEContentObserver::FlushMergeableNotifications(), "
|
|
|
|
"FAILED, due to no pending notifications", this));
|
2015-06-17 04:03:57 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
// NOTE: Reset each pending flag because sending notification may cause
|
|
|
|
// another change.
|
2014-07-31 08:38:00 +04:00
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::FlushMergeableNotifications(), "
|
|
|
|
"creating IMENotificationSender...", this));
|
2014-08-13 15:25:14 +04:00
|
|
|
|
2015-11-10 05:49:05 +03:00
|
|
|
// If contents in selection range is modified, the selection range still
|
|
|
|
// has removed node from the tree. In such case, nsContentIterator won't
|
|
|
|
// work well. Therefore, we shouldn't use AddScriptRunnder() here since
|
|
|
|
// it may kick runnable event immediately after DOM tree is changed but
|
|
|
|
// the selection range isn't modified yet.
|
|
|
|
mQueuedSender = new IMENotificationSender(this);
|
|
|
|
NS_DispatchToCurrentThread(mQueuedSender);
|
2015-09-08 06:54:14 +03:00
|
|
|
|
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::FlushMergeableNotifications(), "
|
|
|
|
"finished", this));
|
2014-07-31 08:37:59 +04:00
|
|
|
}
|
|
|
|
|
2015-11-10 05:49:05 +03:00
|
|
|
void
|
|
|
|
IMEContentObserver::TryToFlushPendingNotifications()
|
|
|
|
{
|
|
|
|
if (!mQueuedSender || mSendingNotification != NOTIFY_IME_OF_NOTHING) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::TryToFlushPendingNotifications(), "
|
|
|
|
"performing queued IMENotificationSender forcibly", this));
|
2015-11-12 18:56:01 +03:00
|
|
|
RefPtr<IMENotificationSender> queuedSender = mQueuedSender;
|
|
|
|
queuedSender->Run();
|
2015-11-10 05:49:05 +03:00
|
|
|
}
|
|
|
|
|
2015-06-17 04:03:57 +03:00
|
|
|
/******************************************************************************
|
|
|
|
* mozilla::IMEContentObserver::AChangeEvent
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
bool
|
2015-09-16 11:48:24 +03:00
|
|
|
IMEContentObserver::AChangeEvent::CanNotifyIME(
|
|
|
|
ChangeEventType aChangeEventType) const
|
2015-06-17 04:03:57 +03:00
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!mIMEContentObserver)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
State state = mIMEContentObserver->GetState();
|
|
|
|
// If it's not initialized, we should do nothing.
|
|
|
|
if (state == eState_NotObserving) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// If setting focus, just check the state.
|
2015-09-16 11:48:24 +03:00
|
|
|
if (aChangeEventType == eChangeEventType_Focus) {
|
2015-06-17 04:03:57 +03:00
|
|
|
return !NS_WARN_IF(mIMEContentObserver->mIMEHasFocus);
|
|
|
|
}
|
|
|
|
// If we've not notified IME of focus yet, we shouldn't notify anything.
|
|
|
|
if (!mIMEContentObserver->mIMEHasFocus) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If IME has focus, IMEContentObserver must hold the widget.
|
|
|
|
MOZ_ASSERT(mIMEContentObserver->mWidget);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-17 04:03:57 +03:00
|
|
|
bool
|
2015-09-16 11:48:24 +03:00
|
|
|
IMEContentObserver::AChangeEvent::IsSafeToNotifyIME(
|
|
|
|
ChangeEventType aChangeEventType) const
|
2015-06-17 04:03:57 +03:00
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!nsContentUtils::IsSafeToRunScript())) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-16 11:48:24 +03:00
|
|
|
// While we're sending a notification, we shouldn't send another notification
|
|
|
|
// recursively.
|
|
|
|
if (mIMEContentObserver->mSendingNotification != NOTIFY_IME_OF_NOTHING) {
|
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::AChangeEvent::IsSafeToNotifyIME(), "
|
|
|
|
"putting off sending notification due to detecting recursive call, "
|
|
|
|
"mIMEContentObserver={ mSendingNotification=%s }",
|
|
|
|
this, ToChar(mIMEContentObserver->mSendingNotification)));
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-17 04:03:57 +03:00
|
|
|
State state = mIMEContentObserver->GetState();
|
2015-09-16 11:48:24 +03:00
|
|
|
if (aChangeEventType == eChangeEventType_Focus) {
|
2015-06-17 04:03:57 +03:00
|
|
|
if (NS_WARN_IF(state != eState_Initializing && state != eState_Observing)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (state != eState_Observing) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return mIMEContentObserver->IsSafeToNotifyIME();
|
|
|
|
}
|
|
|
|
|
2015-06-17 04:03:57 +03:00
|
|
|
/******************************************************************************
|
2015-09-16 11:48:24 +03:00
|
|
|
* mozilla::IMEContentObserver::IMENotificationSender
|
2015-06-17 04:03:57 +03:00
|
|
|
******************************************************************************/
|
2015-09-16 11:48:24 +03:00
|
|
|
|
2015-06-17 04:03:57 +03:00
|
|
|
NS_IMETHODIMP
|
2015-09-16 11:48:24 +03:00
|
|
|
IMEContentObserver::IMENotificationSender::Run()
|
2015-06-17 04:03:57 +03:00
|
|
|
{
|
2015-11-10 05:49:05 +03:00
|
|
|
if (NS_WARN_IF(mIsRunning)) {
|
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Error,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::Run(), FAILED, "
|
|
|
|
"called recursively", this));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoRestore<bool> running(mIsRunning);
|
|
|
|
mIsRunning = true;
|
|
|
|
|
|
|
|
// This instance was already performed forcibly.
|
|
|
|
if (mIMEContentObserver->mQueuedSender != this) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-09-16 11:48:24 +03:00
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
// NOTE: Reset each pending flag because sending notification may cause
|
|
|
|
// another change.
|
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
if (mIMEContentObserver->mNeedsToNotifyIMEOfFocusSet) {
|
|
|
|
mIMEContentObserver->mNeedsToNotifyIMEOfFocusSet = false;
|
2015-09-16 11:48:24 +03:00
|
|
|
SendFocusSet();
|
2015-11-10 05:49:05 +03:00
|
|
|
mIMEContentObserver->mQueuedSender = nullptr;
|
2015-11-10 05:49:04 +03:00
|
|
|
// If it's not safe to notify IME of focus, SendFocusSet() sets
|
|
|
|
// mNeedsToNotifyIMEOfFocusSet true again. For guaranteeing to send the
|
|
|
|
// focus notification later, we should put a new sender into the queue but
|
|
|
|
// this case must be rare. Note that if mIMEContentObserver is already
|
|
|
|
// destroyed, mNeedsToNotifyIMEOfFocusSet is never set true again.
|
|
|
|
if (mIMEContentObserver->mNeedsToNotifyIMEOfFocusSet) {
|
|
|
|
MOZ_ASSERT(!mIMEContentObserver->mIMEHasFocus);
|
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::Run(), "
|
2015-11-10 05:49:05 +03:00
|
|
|
"posting IMENotificationSender to current thread", this));
|
|
|
|
mIMEContentObserver->mQueuedSender =
|
|
|
|
new IMENotificationSender(mIMEContentObserver);
|
|
|
|
NS_DispatchToCurrentThread(mIMEContentObserver->mQueuedSender);
|
2015-11-10 05:49:04 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-09-16 11:48:24 +03:00
|
|
|
// This is the first notification to IME. So, we don't need to notify
|
|
|
|
// anymore since IME starts to query content after it gets focus.
|
|
|
|
mIMEContentObserver->ClearPendingNotifications();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
if (mIMEContentObserver->mNeedsToNotifyIMEOfTextChange) {
|
|
|
|
mIMEContentObserver->mNeedsToNotifyIMEOfTextChange = false;
|
2015-09-16 11:48:24 +03:00
|
|
|
SendTextChange();
|
|
|
|
}
|
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
// If a text change notification causes another text change again, we should
|
|
|
|
// notify IME of that before sending a selection change notification.
|
2015-09-16 11:48:24 +03:00
|
|
|
if (!mIMEContentObserver->mNeedsToNotifyIMEOfTextChange) {
|
2015-09-16 11:48:24 +03:00
|
|
|
// Be aware, PuppetWidget depends on the order of this. A selection change
|
|
|
|
// notification should not be sent before a text change notification because
|
|
|
|
// PuppetWidget shouldn't query new text content every selection change.
|
2015-09-16 11:48:24 +03:00
|
|
|
if (mIMEContentObserver->mNeedsToNotifyIMEOfSelectionChange) {
|
|
|
|
mIMEContentObserver->mNeedsToNotifyIMEOfSelectionChange = false;
|
2015-09-16 11:48:24 +03:00
|
|
|
SendSelectionChange();
|
|
|
|
}
|
2015-09-16 11:48:24 +03:00
|
|
|
}
|
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
// If a text change notification causes another text change again or a
|
|
|
|
// selection change notification causes either a text change or another
|
|
|
|
// selection change, we should notify IME of those before sending a position
|
|
|
|
// change notification.
|
2015-09-16 11:48:24 +03:00
|
|
|
if (!mIMEContentObserver->mNeedsToNotifyIMEOfTextChange &&
|
|
|
|
!mIMEContentObserver->mNeedsToNotifyIMEOfSelectionChange) {
|
|
|
|
if (mIMEContentObserver->mNeedsToNotifyIMEOfPositionChange) {
|
|
|
|
mIMEContentObserver->mNeedsToNotifyIMEOfPositionChange = false;
|
2015-09-16 11:48:24 +03:00
|
|
|
SendPositionChange();
|
|
|
|
}
|
2015-09-16 11:48:24 +03:00
|
|
|
}
|
|
|
|
|
2015-11-10 05:49:05 +03:00
|
|
|
mIMEContentObserver->mQueuedSender = nullptr;
|
2015-11-10 05:49:04 +03:00
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
// If notifications caused some new change, we should notify them now.
|
2015-11-10 05:49:04 +03:00
|
|
|
if (mIMEContentObserver->NeedsToNotifyIMEOfSomething()) {
|
2015-09-16 11:48:24 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::Run(), "
|
2015-11-10 05:49:05 +03:00
|
|
|
"posting IMENotificationSender to current thread", this));
|
|
|
|
mIMEContentObserver->mQueuedSender =
|
|
|
|
new IMENotificationSender(mIMEContentObserver);
|
|
|
|
NS_DispatchToCurrentThread(mIMEContentObserver->mQueuedSender);
|
2015-09-16 11:48:24 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IMEContentObserver::IMENotificationSender::SendFocusSet()
|
|
|
|
{
|
|
|
|
if (!CanNotifyIME(eChangeEventType_Focus)) {
|
2015-06-17 04:03:57 +03:00
|
|
|
// If IMEContentObserver has already gone, we don't need to notify IME of
|
|
|
|
// focus.
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
2015-09-16 11:48:24 +03:00
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::"
|
|
|
|
"SendFocusSet(), FAILED, due to impossible to notify IME of focus",
|
|
|
|
this));
|
2015-06-17 04:03:57 +03:00
|
|
|
mIMEContentObserver->ClearPendingNotifications();
|
2015-09-16 11:48:24 +03:00
|
|
|
return;
|
2015-06-17 04:03:57 +03:00
|
|
|
}
|
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
if (!IsSafeToNotifyIME(eChangeEventType_Focus)) {
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
2015-09-16 11:48:24 +03:00
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::"
|
|
|
|
"SendFocusSet(), retrying to send NOTIFY_IME_OF_FOCUS...", this));
|
2015-06-17 04:03:57 +03:00
|
|
|
mIMEContentObserver->PostFocusSetNotification();
|
2015-09-16 11:48:24 +03:00
|
|
|
return;
|
2015-06-17 04:03:57 +03:00
|
|
|
}
|
|
|
|
|
2015-06-17 04:03:57 +03:00
|
|
|
mIMEContentObserver->mIMEHasFocus = true;
|
2015-08-21 19:43:42 +03:00
|
|
|
// Initialize selection cache with the first selection data.
|
|
|
|
mIMEContentObserver->UpdateSelectionCache();
|
2015-09-08 06:54:14 +03:00
|
|
|
|
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Info,
|
2015-09-16 11:48:24 +03:00
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::"
|
|
|
|
"SendFocusSet(), sending NOTIFY_IME_OF_FOCUS...", this));
|
2015-09-08 06:54:14 +03:00
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
MOZ_RELEASE_ASSERT(mIMEContentObserver->mSendingNotification ==
|
|
|
|
NOTIFY_IME_OF_NOTHING);
|
|
|
|
mIMEContentObserver->mSendingNotification = NOTIFY_IME_OF_FOCUS;
|
2015-06-27 03:23:31 +03:00
|
|
|
IMEStateManager::NotifyIME(IMENotification(NOTIFY_IME_OF_FOCUS),
|
|
|
|
mIMEContentObserver->mWidget);
|
2015-09-16 11:48:24 +03:00
|
|
|
mIMEContentObserver->mSendingNotification = NOTIFY_IME_OF_NOTHING;
|
2015-09-08 06:54:14 +03:00
|
|
|
|
2015-11-10 05:49:04 +03:00
|
|
|
// nsIMEUpdatePreference referred by ObserveEditableNode() may be different
|
|
|
|
// before or after widget receives NOTIFY_IME_OF_FOCUS. Therefore, we need
|
|
|
|
// to guarantee to call ObserveEditableNode() after sending
|
|
|
|
// NOTIFY_IME_OF_FOCUS.
|
|
|
|
mIMEContentObserver->OnIMEReceivedFocus();
|
|
|
|
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
2015-09-16 11:48:24 +03:00
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::"
|
|
|
|
"SendFocusSet(), sent NOTIFY_IME_OF_FOCUS", this));
|
2015-06-17 04:03:57 +03:00
|
|
|
}
|
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
void
|
|
|
|
IMEContentObserver::IMENotificationSender::SendSelectionChange()
|
2015-06-17 04:03:57 +03:00
|
|
|
{
|
2015-09-16 11:48:24 +03:00
|
|
|
if (!CanNotifyIME(eChangeEventType_Selection)) {
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
2015-09-16 11:48:24 +03:00
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::"
|
|
|
|
"SendSelectionChange(), FAILED, due to impossible to notify IME of "
|
|
|
|
"selection change", this));
|
|
|
|
return;
|
2015-06-17 04:03:57 +03:00
|
|
|
}
|
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
if (!IsSafeToNotifyIME(eChangeEventType_Selection)) {
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
2015-09-16 11:48:24 +03:00
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::"
|
|
|
|
"SendSelectionChange(), retrying to send "
|
|
|
|
"NOTIFY_IME_OF_SELECTION_CHANGE...", this));
|
2015-09-14 17:28:43 +03:00
|
|
|
mIMEContentObserver->PostSelectionChangeNotification();
|
2015-09-16 11:48:24 +03:00
|
|
|
return;
|
2015-06-17 04:03:57 +03:00
|
|
|
}
|
|
|
|
|
2015-08-21 19:43:42 +03:00
|
|
|
SelectionChangeData lastSelChangeData = mIMEContentObserver->mSelectionData;
|
2015-08-21 19:43:41 +03:00
|
|
|
if (NS_WARN_IF(!mIMEContentObserver->UpdateSelectionCache())) {
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Error,
|
2015-09-16 11:48:24 +03:00
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::"
|
|
|
|
"SendSelectionChange(), FAILED, due to UpdateSelectionCache() failure",
|
|
|
|
this));
|
|
|
|
return;
|
2015-06-17 04:03:57 +03:00
|
|
|
}
|
|
|
|
|
2015-08-21 19:43:42 +03:00
|
|
|
// If the IME doesn't want selection change notifications caused by
|
|
|
|
// composition, we should do nothing anymore.
|
2015-09-14 17:28:43 +03:00
|
|
|
SelectionChangeData& newSelChangeData = mIMEContentObserver->mSelectionData;
|
|
|
|
if (newSelChangeData.mCausedByComposition &&
|
2015-08-21 19:43:42 +03:00
|
|
|
!mIMEContentObserver->
|
|
|
|
mUpdatePreference.WantChangesCausedByComposition()) {
|
2015-09-16 11:48:24 +03:00
|
|
|
return;
|
2015-08-21 19:43:42 +03:00
|
|
|
}
|
|
|
|
|
2015-06-17 04:03:57 +03:00
|
|
|
// The state may be changed since querying content causes flushing layout.
|
2015-09-16 11:48:24 +03:00
|
|
|
if (!CanNotifyIME(eChangeEventType_Selection)) {
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
2015-09-16 11:48:24 +03:00
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::"
|
|
|
|
"SendSelectionChange(), FAILED, due to flushing layout having changed "
|
|
|
|
"something", this));
|
|
|
|
return;
|
2015-06-17 04:03:57 +03:00
|
|
|
}
|
|
|
|
|
2015-08-21 19:43:42 +03:00
|
|
|
// If the selection isn't changed actually, we shouldn't notify IME of
|
|
|
|
// selection change.
|
|
|
|
if (lastSelChangeData.IsValid() &&
|
|
|
|
lastSelChangeData.mOffset == newSelChangeData.mOffset &&
|
|
|
|
lastSelChangeData.String() == newSelChangeData.String() &&
|
|
|
|
lastSelChangeData.GetWritingMode() == newSelChangeData.GetWritingMode() &&
|
|
|
|
lastSelChangeData.mReversed == newSelChangeData.mReversed) {
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
2015-09-16 11:48:24 +03:00
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::"
|
|
|
|
"SendSelectionChange(), not notifying IME of "
|
|
|
|
"NOTIFY_IME_OF_SELECTION_CHANGE due to not changed actually", this));
|
|
|
|
return;
|
2015-08-21 19:43:42 +03:00
|
|
|
}
|
|
|
|
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Info,
|
2015-09-16 11:48:24 +03:00
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::"
|
|
|
|
"SendSelectionChange(), sending NOTIFY_IME_OF_SELECTION_CHANGE... "
|
|
|
|
"newSelChangeData=%s",
|
2015-09-08 06:54:14 +03:00
|
|
|
this, SelectionChangeDataToString(newSelChangeData).get()));
|
|
|
|
|
2015-06-17 04:03:57 +03:00
|
|
|
IMENotification notification(NOTIFY_IME_OF_SELECTION_CHANGE);
|
2015-09-14 17:28:43 +03:00
|
|
|
notification.SetData(mIMEContentObserver->mSelectionData);
|
2015-09-16 11:48:24 +03:00
|
|
|
|
|
|
|
MOZ_RELEASE_ASSERT(mIMEContentObserver->mSendingNotification ==
|
|
|
|
NOTIFY_IME_OF_NOTHING);
|
|
|
|
mIMEContentObserver->mSendingNotification = NOTIFY_IME_OF_SELECTION_CHANGE;
|
2015-06-27 03:23:31 +03:00
|
|
|
IMEStateManager::NotifyIME(notification, mIMEContentObserver->mWidget);
|
2015-09-16 11:48:24 +03:00
|
|
|
mIMEContentObserver->mSendingNotification = NOTIFY_IME_OF_NOTHING;
|
2015-09-08 06:54:14 +03:00
|
|
|
|
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
2015-09-16 11:48:24 +03:00
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::"
|
|
|
|
"SendSelectionChange(), sent NOTIFY_IME_OF_SELECTION_CHANGE", this));
|
2015-06-17 04:03:57 +03:00
|
|
|
}
|
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
void
|
|
|
|
IMEContentObserver::IMENotificationSender::SendTextChange()
|
2015-06-17 04:03:57 +03:00
|
|
|
{
|
2015-09-16 11:48:24 +03:00
|
|
|
if (!CanNotifyIME(eChangeEventType_Text)) {
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
2015-09-16 11:48:24 +03:00
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::"
|
|
|
|
"SendTextChange(), FAILED, due to impossible to notify IME of text "
|
|
|
|
"change", this));
|
|
|
|
return;
|
2015-06-17 04:03:57 +03:00
|
|
|
}
|
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
if (!IsSafeToNotifyIME(eChangeEventType_Text)) {
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
2015-09-16 11:48:24 +03:00
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::"
|
|
|
|
"SendTextChange(), retrying to send NOTIFY_IME_OF_TEXT_CHANGE...",
|
|
|
|
this));
|
2015-09-16 11:48:23 +03:00
|
|
|
mIMEContentObserver->PostTextChangeNotification();
|
2015-09-16 11:48:24 +03:00
|
|
|
return;
|
2015-06-17 04:03:57 +03:00
|
|
|
}
|
|
|
|
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Info,
|
2015-09-16 11:48:24 +03:00
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::"
|
|
|
|
"SendTextChange(), sending NOTIFY_IME_OF_TEXT_CHANGE... "
|
|
|
|
"mIMEContentObserver={ mTextChangeData=%s }",
|
2015-09-16 11:48:23 +03:00
|
|
|
this, TextChangeDataToString(mIMEContentObserver->mTextChangeData).get()));
|
2015-09-08 06:54:14 +03:00
|
|
|
|
2015-06-17 04:03:57 +03:00
|
|
|
IMENotification notification(NOTIFY_IME_OF_TEXT_CHANGE);
|
2015-09-16 11:48:23 +03:00
|
|
|
notification.SetData(mIMEContentObserver->mTextChangeData);
|
|
|
|
mIMEContentObserver->mTextChangeData.Clear();
|
2015-09-16 11:48:24 +03:00
|
|
|
|
|
|
|
MOZ_RELEASE_ASSERT(mIMEContentObserver->mSendingNotification ==
|
|
|
|
NOTIFY_IME_OF_NOTHING);
|
|
|
|
mIMEContentObserver->mSendingNotification = NOTIFY_IME_OF_TEXT_CHANGE;
|
2015-06-27 03:23:31 +03:00
|
|
|
IMEStateManager::NotifyIME(notification, mIMEContentObserver->mWidget);
|
2015-09-16 11:48:24 +03:00
|
|
|
mIMEContentObserver->mSendingNotification = NOTIFY_IME_OF_NOTHING;
|
2015-09-08 06:54:14 +03:00
|
|
|
|
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
2015-09-16 11:48:24 +03:00
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::"
|
|
|
|
"SendTextChange(), sent NOTIFY_IME_OF_TEXT_CHANGE", this));
|
2015-06-17 04:03:57 +03:00
|
|
|
}
|
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
void
|
|
|
|
IMEContentObserver::IMENotificationSender::SendPositionChange()
|
2015-06-17 04:03:57 +03:00
|
|
|
{
|
2015-09-16 11:48:24 +03:00
|
|
|
if (!CanNotifyIME(eChangeEventType_Position)) {
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
2015-09-16 11:48:24 +03:00
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::"
|
|
|
|
"SendPositionChange(), FAILED, due to impossible to notify IME of "
|
|
|
|
"position change", this));
|
|
|
|
return;
|
2015-06-17 04:03:57 +03:00
|
|
|
}
|
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
if (!IsSafeToNotifyIME(eChangeEventType_Position)) {
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
2015-09-16 11:48:24 +03:00
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::"
|
|
|
|
"SendPositionChange(), retrying to send "
|
|
|
|
"NOTIFY_IME_OF_POSITION_CHANGE...", this));
|
2015-06-17 04:03:57 +03:00
|
|
|
mIMEContentObserver->PostPositionChangeNotification();
|
2015-09-16 11:48:24 +03:00
|
|
|
return;
|
2015-06-17 04:03:57 +03:00
|
|
|
}
|
|
|
|
|
2015-09-08 06:54:14 +03:00
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Info,
|
2015-09-16 11:48:24 +03:00
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::"
|
|
|
|
"SendPositionChange(), sending NOTIFY_IME_OF_POSITION_CHANGE...", this));
|
2015-09-08 06:54:14 +03:00
|
|
|
|
2015-09-16 11:48:24 +03:00
|
|
|
MOZ_RELEASE_ASSERT(mIMEContentObserver->mSendingNotification ==
|
|
|
|
NOTIFY_IME_OF_NOTHING);
|
|
|
|
mIMEContentObserver->mSendingNotification = NOTIFY_IME_OF_POSITION_CHANGE;
|
2015-06-27 03:23:31 +03:00
|
|
|
IMEStateManager::NotifyIME(IMENotification(NOTIFY_IME_OF_POSITION_CHANGE),
|
|
|
|
mIMEContentObserver->mWidget);
|
2015-09-16 11:48:24 +03:00
|
|
|
mIMEContentObserver->mSendingNotification = NOTIFY_IME_OF_NOTHING;
|
2015-09-08 06:54:14 +03:00
|
|
|
|
|
|
|
MOZ_LOG(sIMECOLog, LogLevel::Debug,
|
2015-09-16 11:48:24 +03:00
|
|
|
("IMECO: 0x%p IMEContentObserver::IMENotificationSender::"
|
|
|
|
"SendPositionChange(), sent NOTIFY_IME_OF_POSITION_CHANGE", this));
|
2015-06-17 04:03:57 +03:00
|
|
|
}
|
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
} // namespace mozilla
|