Files
palemoon27/widget/ContentCache.cpp
T
roytam1 d56ce7399f import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1232506: Make dom/devicestorage really work with e10s. r=alchen (7f95105c5e)
- Bug 1208944 - Part 1. Dispatch CompositionEvent to Plugin. r=masayuki (85c062b417)
- Bug 1208944 - Part 2-a. Handle CompositionEvent on plugin. r=masayuki (ea2cebfca9)
- Bug 1208944 - Part 2-b. Workaround for OSX. r=masayuki (ca401cbc04)
- Bug 1208944 - Part 3. Allow IME window messages on plugin process. r=jmathies (571fd75010)
- Bug 1192844: Accept 0xCC padding in WindowsDllDetourPatcher::CreateTrampoline. r=m_kato (b34b6173d3)
- Bug 1201205 part 1: Add an AutoVirtualProtect helper class to make the next patch easier. r=m_kato (b384bd2412)
- Bug 1201205 part 2: Restore protection on the nop space separately from the function. r=m_kato (a822b2414b)
- Bug 1208944 - Part 4. nsWindowsDllInterceptor supports IMM32 API hook. r=ehsan (9b409ff15a)
- Bug 1208944 - Part 5. Send PluginEvent to content process. r=jmathies (4ee0341190)
- Bug 1208944 - Part 6. Get vaild TextRangeArray on compositionupdate. r=masayuki (18f184931c)
- Bug 1208944 - Part 7. Don't post WM_IME_REQUEST on windowless plugin since we don't convert pointer over process. r=masayuki (6b1a9ce71f)
- Bug 1208944 - Part 8. Don't get selection on start compostion when plugin has foucs. r=masayuki (daf400620e)
- Add Telemetry for the drawing models that plugins use. (bug 1229961 part 1, r=aklotz,vladan) (c9645a68ec)
- Enable direct plugin drawing by default. (bug 1229961 part 2, r=aklotz) (080c9337cb)
- Bug 1213845 - enable osk support on windows 8, but hide it behind a preference, r=jaws (fcb8dfb10b)
- Bug 1192248 - Fix wchar_t/char16_t mismatch WinIMEHandler.cpp. r=masayuki (13e61f3f0c)
- Bug 1226145 - actually check whether the on-screen keyboard is up rather than relying on internal state, r=masayuki (da090b605d)
- Bug 1208944 - Part 9. Hook IMM32 APIs on plugin process. r=masayuki (f15d73721f)
- Bug 1208944 - Part 10-a. Call CallWindowProc when WidgetPluginEvent isn't handled by plugin. r=masayuki (c54eaa50ed)
- Bug 1208944 - Part 10-b. Call DefaultProc When CompositionEvent isn't handled correctly by plugin. r=masayuki (0e77eaa40f)
- Bug 1157046 - Remove ARRAY_LENGTH in favor of MOZ_ARRAY_LENGTH; r=Waldo (a7f8ce3684)
- Bug 1196834 - Add a test that confirms plugin windows are hidden after switching from a remote to local tab. r=roc (a09d4a32af)
- Bug 1208944 - Part 11. Add test. r=jmathies (ed8e4c87d2)
- Bug 1231378 - part 1 - Fix uninitialized members of classes in docshell/*, r=smaug (fcd7615d97)
- Bug 1231378 - part 2 - Fix uninitialized members of classes in dom/*,r=smaug (40b354438f)
- Bug 1231378 - part 3 - Fix uninitialized members of classes in module/libjar and mfbt, r=smaug (ee15d8739a)
- Bug 1231378 - part 4 - Fix uninitialized members of classes in netwerk/widget/storage/uriloader/memory/tools, r=smaug (6aec559dd8)
- Bug 1238082 - Fix mode lines in dom/ipc. r=baku (a49aa3555b)
- Bug 1024149 - Use element size for upload texture when using SVG image. r=jgilbert r=roc (531bdd2406)
- Bug 1233922 - Skip Camera preinit for browser elements. r=fabrice (cd8ffbb112)
- Bug 1159327 - Enable accessibility more broadly with e10s and add an e10s a11y blacklist for clients with known issues. r=tbsaunde (374f49a942)
- Bug 1198459 - Prevent accessibility from initializing in content processes when e10s is running. r=tbsaunde (33bdfccd56)
- Bug 1115956 - Improve notice string for when e10s was disabled for accessibility. r=mconley (e5ff276d9e)
- Bug 1172491 - Let e10s be enabled in safe mode. r=felipe (bd0a3fbbd5)
- Bug 1226487 - Allow e10s to run on Beta. r=mconley (05103c816d)
- Bug 634063 - Use gfxPrefs for some layers acceleration prefs. r=nical (9a20bcd8d2)
- Bug 1198459 - Add support for disabling e10s if a11y was run in the previous session, or run in a session over the previous week. r=felipe (7154a021bd)
- Bug 1171171 - Make sure the graphics preferences are initialized early enough. r=billm (34801701aa)
- Bug 1171171: Move nsAppRunner's gfxPrefs #include out of windows-specific section. r=milan a=KWierso (492fa00f99)
- Bug 1182048 - Part 1: Allow e10s to be enabled,r=vlad (628949ad83)
- Bug 1182048 - Part 2: Implement e10s support for WebVR,r=vlad (a77d280b48)
- Bug 1207221 - Do not prevent the system app from vibrating when it is hidden. r=bz r=dhylands (f3d2306f41)
- Bug 1233902 - Check the TP list in sendBeacon(). r=gcp (bc279755ab)
- Bug 1216207 - Modify navigator.hasFeature method to implement new (9b4571468e)
- Bug 1217187 - Modify navigator.hasFeature method to detect new (d9ea58e65c)
- Bug 1125477 - Part 1: Modify dom/tv mochitest in order to enable e10s test. r=seanlin (7b535fddaa)
- Bug 1125477 - Part 2: Modify permission check of TV Manager API in order to remove dom.testing.tv_enabled_for_hosted_apps. r=seanlin;r=smaug (5719fdf3a7)
- Bug 1125477 - Part3: Remove permission removing code. r=seanlin (614de5dfd1)
- Bug 1223672 - NSec package cannot use device storage API. r=kchen (f1fc6711ea)
- Bug 1224609 - Ensure the primary screen is always returned in RecvScreenForBrowser if all else fails. r=wmccloskey (d562935754)
- Bug 1180288 - Use native filepickers for Graphene. r=khuey (0b9c78ce13)
- Bug 1201394 - Use cached preferences value in ProcessPriorityManager. r=gsvelto (d211492014)
- Bug 1212833 - Delay the MemoryPressure when an application goes to background. r=gsvelto (ce33de4324)
- Bug 1186812 (part 1) - Replace nsBaseHashtable::EnumerateRead() calls in dom/{ipc,plugins}/. r=jimm. (c061bb9d1b)
- Bug 1212984 - HangMonitorChild should delete its Transport. r=billm (3331593b2d)
- Bug 1186812 (part 2) - Replace nsBaseHashtable::EnumerateRead() calls in dom/{ipc,plugins}/. r=jimm. (fdac3f52cb)
- Bug 1215239 - Do not set ownApp for a browser element. r=bholley (e3cc250c3f)
- Bug 1241278 - Change Notification.requestPermission() to return a promise. r=baku (b0a86eeeb5)
- Bug 1249102. Make overrides of WorkerRunnable::PostRun a bit more consistent. r=khuey (a23bf9468f)
- Bug 1209812 (part 6) - Convert all gfxImageFormat values to SurfaceFormat equivalents. r=jrmuizel. (9a5da3597f)
- Bug 1239225 - Remove unused args from TextureImage's constructor and related functions. r=mattwoodrow. (26ff005ecb)
- Bug 1240708 - Various trivial coverity warning fixes. r=kats (74a485c2b8)
- Bug 1240708 - Various trivial coverity warning fixes (part 2). r=kats (34bcd568dc)
- Bug 1219494 - Part 3 gfx/gl with gfxCrash. r=mchang (268968a3d9)
- Bug 1232456 - Create EGL surface through widget; r=snorp (13634bebc6)
- Bug 1232456 - Renew EGL surface using existing compositor widget; r=snorp (2649b088d3)
- Bug 1187322 - Don't require accelerated OpenGL contexts for BasicCompositor on OS X. r=jrmuizel (87f92af2c8)
- Bug 1238753 - Use StartRemoteDrawingInRegion on Mac. r=mattwoodrow (22870ed043)
- Bug 1238755 - Avoid a copy when uploading the BasicCompositor result to a texture. r=mattwoodrow (0b6663ab61)
- Bug 890156 - patch 0.2 - Remove the (unused) aRect parameter from nsBaseWidget::BaseCreate. r=kats (1120763eef)
- Bug 1241983 - Make DOM Identity observe inner-window-destroyed not dom-window-destroyed; r=ferjm (7cf4cf3211)
- Bug 1236282 - Clip color layer drawing in BasicCompositor so that unbounded operators don't erase stuff outside the layer. r=Bas (a7e73d1d8d)
- Bug 1238753 - Make BasicCompositor respect changes to mInvalidRegion through StartRemoteDrawingWithRegion properly. r=mattwoodrow (36e20bccce)
- Bug 1238753 - Don't skip the call to StartRemoteDrawing in from BasicCompositor::BeginFrame if the invalid region is empty. r=mattwoodrow (783b25775f)
- Bug 1239137 - Return early from BasicCompositor::DrawQuad if transformBounds is empty. r=mattwoodrow (0815cfc28a)
- Bug 1239530 (part 1) - Remove PuppetWidget::Scroll(), which is dead. r=kats. (63ab8c32b6)
- Bug 1239530 (part 2) - Use LayoutDevice coordinates in {Start,End}RemoteDrawingInRegion() and related functions. r=kats. (609001767d)
- Bug 1239537 - Remove Compositor::GetWidgetSize(), which is unused. r=mattwoodrow. (25732d37b1)
- Bug 1242293 - Don't call EndRemoteDrawingInRegion if StartRemoteDrawingInRegion returns a null DrawTarget. r=mstange (8e297c3b84)
- import changes from mozilla: Bug 1211642: Whitelist test plugin for async plugin init; r=jimm (f02deaaeb8) (6689ce99c8)
2023-08-15 16:18:14 +08:00

1108 lines
39 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: sw=2 ts=8 et :
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/ContentCache.h"
#include "mozilla/IMEStateManager.h"
#include "mozilla/Logging.h"
#include "mozilla/TextComposition.h"
#include "mozilla/TextEvents.h"
#include "nsIWidget.h"
#include "mozilla/RefPtr.h"
namespace mozilla {
using namespace widget;
static const char*
GetBoolName(bool aBool)
{
return aBool ? "true" : "false";
}
static const char*
GetNotificationName(const IMENotification* aNotification)
{
if (!aNotification) {
return "Not notification";
}
switch (aNotification->mMessage) {
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 "Unsupported notification";
}
}
class GetRectText : public nsAutoCString
{
public:
explicit GetRectText(const LayoutDeviceIntRect& aRect)
{
Assign("{ x=");
AppendInt(aRect.x);
Append(", y=");
AppendInt(aRect.y);
Append(", width=");
AppendInt(aRect.width);
Append(", height=");
AppendInt(aRect.height);
Append(" }");
}
virtual ~GetRectText() {}
};
class GetWritingModeName : public nsAutoCString
{
public:
explicit GetWritingModeName(const WritingMode& aWritingMode)
{
if (!aWritingMode.IsVertical()) {
Assign("Horizontal");
return;
}
if (aWritingMode.IsVerticalLR()) {
Assign("Vertical (LTR)");
return;
}
Assign("Vertical (RTL)");
}
virtual ~GetWritingModeName() {}
};
/*****************************************************************************
* mozilla::ContentCache
*****************************************************************************/
PRLogModuleInfo* sContentCacheLog = nullptr;
ContentCache::ContentCache()
{
if (!sContentCacheLog) {
sContentCacheLog = PR_NewLogModule("ContentCacheWidgets");
}
}
/*****************************************************************************
* mozilla::ContentCacheInChild
*****************************************************************************/
ContentCacheInChild::ContentCacheInChild()
: ContentCache()
{
}
void
ContentCacheInChild::Clear()
{
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInChild: 0x%p Clear()", this));
mText.Truncate();
mSelection.Clear();
mFirstCharRect.SetEmpty();
mCaret.Clear();
mTextRectArray.Clear();
mEditorRect.SetEmpty();
}
bool
ContentCacheInChild::CacheAll(nsIWidget* aWidget,
const IMENotification* aNotification)
{
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInChild: 0x%p CacheAll(aWidget=0x%p, "
"aNotification=%s)",
this, aWidget, GetNotificationName(aNotification)));
if (NS_WARN_IF(!CacheText(aWidget, aNotification)) ||
NS_WARN_IF(!CacheEditorRect(aWidget, aNotification))) {
return false;
}
return true;
}
bool
ContentCacheInChild::CacheSelection(nsIWidget* aWidget,
const IMENotification* aNotification)
{
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInChild: 0x%p CacheSelection(aWidget=0x%p, "
"aNotification=%s)",
this, aWidget, GetNotificationName(aNotification)));
mCaret.Clear();
mSelection.Clear();
nsEventStatus status = nsEventStatus_eIgnore;
WidgetQueryContentEvent selection(true, eQuerySelectedText, aWidget);
aWidget->DispatchEvent(&selection, status);
if (NS_WARN_IF(!selection.mSucceeded)) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("ContentCache: 0x%p CacheSelection(), FAILED, "
"couldn't retrieve the selected text", this));
return false;
}
if (selection.mReply.mReversed) {
mSelection.mAnchor =
selection.mReply.mOffset + selection.mReply.mString.Length();
mSelection.mFocus = selection.mReply.mOffset;
} else {
mSelection.mAnchor = selection.mReply.mOffset;
mSelection.mFocus =
selection.mReply.mOffset + selection.mReply.mString.Length();
}
mSelection.mWritingMode = selection.GetWritingMode();
return CacheCaret(aWidget, aNotification) &&
CacheTextRects(aWidget, aNotification);
}
bool
ContentCacheInChild::CacheCaret(nsIWidget* aWidget,
const IMENotification* aNotification)
{
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInChild: 0x%p CacheCaret(aWidget=0x%p, "
"aNotification=%s)",
this, aWidget, GetNotificationName(aNotification)));
mCaret.Clear();
if (NS_WARN_IF(!mSelection.IsValid())) {
return false;
}
// XXX Should be mSelection.mFocus?
mCaret.mOffset = mSelection.StartOffset();
nsEventStatus status = nsEventStatus_eIgnore;
WidgetQueryContentEvent caretRect(true, eQueryCaretRect, aWidget);
caretRect.InitForQueryCaretRect(mCaret.mOffset);
aWidget->DispatchEvent(&caretRect, status);
if (NS_WARN_IF(!caretRect.mSucceeded)) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("ContentCacheInChild: 0x%p CacheCaret(), FAILED, "
"couldn't retrieve the caret rect at offset=%u",
this, mCaret.mOffset));
mCaret.Clear();
return false;
}
mCaret.mRect = caretRect.mReply.mRect;
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInChild: 0x%p CacheCaret(), Succeeded, "
"mSelection={ mAnchor=%u, mFocus=%u, mWritingMode=%s }, "
"mCaret={ mOffset=%u, mRect=%s }",
this, mSelection.mAnchor, mSelection.mFocus,
GetWritingModeName(mSelection.mWritingMode).get(), mCaret.mOffset,
GetRectText(mCaret.mRect).get()));
return true;
}
bool
ContentCacheInChild::CacheEditorRect(nsIWidget* aWidget,
const IMENotification* aNotification)
{
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInChild: 0x%p CacheEditorRect(aWidget=0x%p, "
"aNotification=%s)",
this, aWidget, GetNotificationName(aNotification)));
nsEventStatus status = nsEventStatus_eIgnore;
WidgetQueryContentEvent editorRectEvent(true, eQueryEditorRect, aWidget);
aWidget->DispatchEvent(&editorRectEvent, status);
if (NS_WARN_IF(!editorRectEvent.mSucceeded)) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("ContentCacheInChild: 0x%p CacheEditorRect(), FAILED, "
"couldn't retrieve the editor rect", this));
return false;
}
mEditorRect = editorRectEvent.mReply.mRect;
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInChild: 0x%p CacheEditorRect(), Succeeded, "
"mEditorRect=%s", this, GetRectText(mEditorRect).get()));
return true;
}
bool
ContentCacheInChild::CacheText(nsIWidget* aWidget,
const IMENotification* aNotification)
{
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInChild: 0x%p CacheText(aWidget=0x%p, "
"aNotification=%s)",
this, aWidget, GetNotificationName(aNotification)));
nsEventStatus status = nsEventStatus_eIgnore;
WidgetQueryContentEvent queryText(true, eQueryTextContent, aWidget);
queryText.InitForQueryTextContent(0, UINT32_MAX);
aWidget->DispatchEvent(&queryText, status);
if (NS_WARN_IF(!queryText.mSucceeded)) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("ContentCacheInChild: 0x%p CacheText(), FAILED, "
"couldn't retrieve whole text", this));
mText.Truncate();
return false;
}
mText = queryText.mReply.mString;
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInChild: 0x%p CacheText(), Succeeded, "
"mText.Length()=%u", this, mText.Length()));
return CacheSelection(aWidget, aNotification);
}
bool
ContentCacheInChild::QueryCharRect(nsIWidget* aWidget,
uint32_t aOffset,
LayoutDeviceIntRect& aCharRect) const
{
aCharRect.SetEmpty();
nsEventStatus status = nsEventStatus_eIgnore;
WidgetQueryContentEvent textRect(true, eQueryTextRect, aWidget);
textRect.InitForQueryTextRect(aOffset, 1);
aWidget->DispatchEvent(&textRect, status);
if (NS_WARN_IF(!textRect.mSucceeded)) {
return false;
}
aCharRect = textRect.mReply.mRect;
// Guarantee the rect is not empty.
if (NS_WARN_IF(!aCharRect.height)) {
aCharRect.height = 1;
}
if (NS_WARN_IF(!aCharRect.width)) {
aCharRect.width = 1;
}
return true;
}
bool
ContentCacheInChild::CacheTextRects(nsIWidget* aWidget,
const IMENotification* aNotification)
{
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInChild: 0x%p CacheTextRects(aWidget=0x%p, "
"aNotification=%s), mCaret={ mOffset=%u, IsValid()=%s }",
this, aWidget, GetNotificationName(aNotification), mCaret.mOffset,
GetBoolName(mCaret.IsValid())));
mTextRectArray.Clear();
mSelection.mAnchorCharRect.SetEmpty();
mSelection.mFocusCharRect.SetEmpty();
mSelection.mRect.SetEmpty();
mFirstCharRect.SetEmpty();
if (NS_WARN_IF(!mSelection.IsValid())) {
return false;
}
// Retrieve text rects in composition string if there is.
RefPtr<TextComposition> textComposition =
IMEStateManager::GetTextCompositionFor(aWidget);
if (textComposition) {
// Note that TextComposition::String() may not be modified here because
// it's modified after all edit action listeners are performed but this
// is called while some of them are performed.
uint32_t length = textComposition->LastData().Length();
mTextRectArray.mRects.SetCapacity(length);
mTextRectArray.mStart = textComposition->NativeOffsetOfStartComposition();
uint32_t endOffset = mTextRectArray.mStart + length;
for (uint32_t i = mTextRectArray.mStart; i < endOffset; i++) {
LayoutDeviceIntRect charRect;
if (NS_WARN_IF(!QueryCharRect(aWidget, i, charRect))) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("ContentCacheInChild: 0x%p CacheTextRects(), FAILED, "
"couldn't retrieve text rect at offset=%u", this, i));
mTextRectArray.Clear();
return false;
}
mTextRectArray.mRects.AppendElement(charRect);
}
}
if (mTextRectArray.InRange(mSelection.mAnchor)) {
mSelection.mAnchorCharRect = mTextRectArray.GetRect(mSelection.mAnchor);
} else {
LayoutDeviceIntRect charRect;
if (NS_WARN_IF(!QueryCharRect(aWidget, mSelection.mAnchor, charRect))) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("ContentCacheInChild: 0x%p CacheTextRects(), FAILED, "
"couldn't retrieve text rect at anchor of selection (%u)",
this, mSelection.mAnchor));
}
mSelection.mAnchorCharRect = charRect;
}
if (mSelection.Collapsed()) {
mSelection.mFocusCharRect = mSelection.mAnchorCharRect;
} else if (mTextRectArray.InRange(mSelection.mFocus)) {
mSelection.mFocusCharRect = mTextRectArray.GetRect(mSelection.mFocus);
} else {
LayoutDeviceIntRect charRect;
if (NS_WARN_IF(!QueryCharRect(aWidget, mSelection.mFocus, charRect))) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("ContentCacheInChild: 0x%p CacheTextRects(), FAILED, "
"couldn't retrieve text rect at focus of selection (%u)",
this, mSelection.mFocus));
}
mSelection.mFocusCharRect = charRect;
}
if (!mSelection.Collapsed()) {
nsEventStatus status = nsEventStatus_eIgnore;
WidgetQueryContentEvent textRect(true, eQueryTextRect, aWidget);
textRect.InitForQueryTextRect(mSelection.StartOffset(),
mSelection.Length());
aWidget->DispatchEvent(&textRect, status);
if (NS_WARN_IF(!textRect.mSucceeded)) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("ContentCacheInChild: 0x%p CacheTextRects(), FAILED, "
"couldn't retrieve text rect of whole selected text", this));
} else {
mSelection.mRect = textRect.mReply.mRect;
}
}
if (!mSelection.mFocus) {
mFirstCharRect = mSelection.mFocusCharRect;
} else if (!mSelection.mAnchor) {
mFirstCharRect = mSelection.mAnchorCharRect;
} else if (mTextRectArray.InRange(0)) {
mFirstCharRect = mTextRectArray.GetRect(0);
} else {
LayoutDeviceIntRect charRect;
if (NS_WARN_IF(!QueryCharRect(aWidget, 0, charRect))) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("ContentCacheInChild: 0x%p CacheTextRects(), FAILED, "
"couldn't retrieve first char rect", this));
} else {
mFirstCharRect = charRect;
}
}
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInChild: 0x%p CacheTextRects(), Succeeded, "
"mText.Length()=%u, mTextRectArray={ mStart=%u, mRects.Length()=%u }, "
"mSelection={ mAnchor=%u, mAnchorCharRect=%s, mFocus=%u, "
"mFocusCharRect=%s, mRect=%s }, mFirstCharRect=%s",
this, mText.Length(), mTextRectArray.mStart,
mTextRectArray.mRects.Length(), mSelection.mAnchor,
GetRectText(mSelection.mAnchorCharRect).get(), mSelection.mFocus,
GetRectText(mSelection.mFocusCharRect).get(),
GetRectText(mSelection.mRect).get(), GetRectText(mFirstCharRect).get()));
return true;
}
void
ContentCacheInChild::SetSelection(nsIWidget* aWidget,
uint32_t aStartOffset,
uint32_t aLength,
bool aReversed,
const WritingMode& aWritingMode)
{
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInChild: 0x%p SetSelection(aStartOffset=%u, "
"aLength=%u, aReversed=%s, aWritingMode=%s), mText.Length()=%u",
this, aStartOffset, aLength, GetBoolName(aReversed),
GetWritingModeName(aWritingMode).get(), mText.Length()));
if (!aReversed) {
mSelection.mAnchor = aStartOffset;
mSelection.mFocus = aStartOffset + aLength;
} else {
mSelection.mAnchor = aStartOffset + aLength;
mSelection.mFocus = aStartOffset;
}
mSelection.mWritingMode = aWritingMode;
if (NS_WARN_IF(!CacheCaret(aWidget))) {
return;
}
NS_WARN_IF(!CacheTextRects(aWidget));
}
/*****************************************************************************
* mozilla::ContentCacheInParent
*****************************************************************************/
ContentCacheInParent::ContentCacheInParent()
: ContentCache()
, mCommitStringByRequest(nullptr)
, mCompositionStart(UINT32_MAX)
, mPendingEventsNeedingAck(0)
, mIsComposing(false)
{
}
void
ContentCacheInParent::AssignContent(const ContentCache& aOther,
const IMENotification* aNotification)
{
mText = aOther.mText;
mSelection = aOther.mSelection;
mFirstCharRect = aOther.mFirstCharRect;
mCaret = aOther.mCaret;
mTextRectArray = aOther.mTextRectArray;
mEditorRect = aOther.mEditorRect;
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInParent: 0x%p AssignContent(aNotification=%s), "
"Succeeded, mText.Length()=%u, mSelection={ mAnchor=%u, mFocus=%u, "
"mWritingMode=%s, mAnchorCharRect=%s, mFocusCharRect=%s, mRect=%s }, "
"mFirstCharRect=%s, mCaret={ mOffset=%u, mRect=%s }, mTextRectArray={ "
"mStart=%u, mRects.Length()=%u }, mEditorRect=%s",
this, GetNotificationName(aNotification),
mText.Length(), mSelection.mAnchor, mSelection.mFocus,
GetWritingModeName(mSelection.mWritingMode).get(),
GetRectText(mSelection.mAnchorCharRect).get(),
GetRectText(mSelection.mFocusCharRect).get(),
GetRectText(mSelection.mRect).get(), GetRectText(mFirstCharRect).get(),
mCaret.mOffset, GetRectText(mCaret.mRect).get(), mTextRectArray.mStart,
mTextRectArray.mRects.Length(), GetRectText(mEditorRect).get()));
}
bool
ContentCacheInParent::HandleQueryContentEvent(WidgetQueryContentEvent& aEvent,
nsIWidget* aWidget) const
{
MOZ_ASSERT(aWidget);
aEvent.mSucceeded = false;
aEvent.mReply.mFocusedWidget = aWidget;
switch (aEvent.mMessage) {
case eQuerySelectedText:
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInParent: 0x%p HandleQueryContentEvent("
"aEvent={ mMessage=eQuerySelectedText }, aWidget=0x%p)",
this, aWidget));
if (aWidget->PluginHasFocus()) {
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInParent: 0x%p HandleQueryContentEvent(), "
"return emtpy selection becasue plugin has focus",
this));
aEvent.mSucceeded = true;
aEvent.mReply.mOffset = 0;
aEvent.mReply.mReversed = false;
aEvent.mReply.mHasSelection = false;
return true;
}
if (NS_WARN_IF(!IsSelectionValid())) {
// If content cache hasn't been initialized properly, make the query
// failed.
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("ContentCacheInParent: 0x%p HandleQueryContentEvent(), "
"FAILED because mSelection is not valid", this));
return true;
}
aEvent.mReply.mOffset = mSelection.StartOffset();
if (mSelection.Collapsed()) {
aEvent.mReply.mString.Truncate(0);
} else {
if (NS_WARN_IF(mSelection.EndOffset() > mText.Length())) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("ContentCacheInParent: 0x%p HandleQueryContentEvent(), "
"FAILED because mSelection.EndOffset()=%u is larger than "
"mText.Length()=%u",
this, mSelection.EndOffset(), mText.Length()));
return false;
}
aEvent.mReply.mString =
Substring(mText, aEvent.mReply.mOffset, mSelection.Length());
}
aEvent.mReply.mReversed = mSelection.Reversed();
aEvent.mReply.mHasSelection = true;
aEvent.mReply.mWritingMode = mSelection.mWritingMode;
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInParent: 0x%p HandleQueryContentEvent(), "
"Succeeded, aEvent={ mReply={ mOffset=%u, mString=\"%s\", "
"mReversed=%s, mHasSelection=%s, mWritingMode=%s } }",
this, aEvent.mReply.mOffset,
NS_ConvertUTF16toUTF8(aEvent.mReply.mString).get(),
GetBoolName(aEvent.mReply.mReversed),
GetBoolName(aEvent.mReply.mHasSelection),
GetWritingModeName(aEvent.mReply.mWritingMode).get()));
break;
case eQueryTextContent: {
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInParent: 0x%p HandleQueryContentEvent("
"aEvent={ mMessage=eQueryTextContent, mInput={ mOffset=%u, "
"mLength=%u } }, aWidget=0x%p), mText.Length()=%u",
this, aEvent.mInput.mOffset,
aEvent.mInput.mLength, aWidget, mText.Length()));
uint32_t inputOffset = aEvent.mInput.mOffset;
uint32_t inputEndOffset =
std::min(aEvent.mInput.EndOffset(), mText.Length());
if (NS_WARN_IF(inputEndOffset < inputOffset)) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("ContentCacheInParent: 0x%p HandleQueryContentEvent(), "
"FAILED because inputOffset=%u is larger than inputEndOffset=%u",
this, inputOffset, inputEndOffset));
return false;
}
aEvent.mReply.mOffset = inputOffset;
aEvent.mReply.mString =
Substring(mText, inputOffset, inputEndOffset - inputOffset);
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInParent: 0x%p HandleQueryContentEvent(), "
"Succeeded, aEvent={ mReply={ mOffset=%u, mString.Length()=%u } }",
this, aEvent.mReply.mOffset, aEvent.mReply.mString.Length()));
break;
}
case eQueryTextRect:
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInParent: 0x%p HandleQueryContentEvent("
"aEvent={ mMessage=eQueryTextRect, mInput={ mOffset=%u, "
"mLength=%u } }, aWidget=0x%p), mText.Length()=%u",
this, aEvent.mInput.mOffset, aEvent.mInput.mLength, aWidget,
mText.Length()));
if (NS_WARN_IF(!IsSelectionValid())) {
// If content cache hasn't been initialized properly, make the query
// failed.
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("ContentCacheInParent: 0x%p HandleQueryContentEvent(), "
"FAILED because mSelection is not valid", this));
return true;
}
if (aEvent.mInput.mLength) {
if (NS_WARN_IF(!GetUnionTextRects(aEvent.mInput.mOffset,
aEvent.mInput.mLength,
aEvent.mReply.mRect))) {
// XXX We don't have cache for this request.
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("ContentCacheInParent: 0x%p HandleQueryContentEvent(), "
"FAILED to get union rect", this));
return false;
}
} else {
// If the length is 0, we should return caret rect instead.
if (NS_WARN_IF(!GetCaretRect(aEvent.mInput.mOffset,
aEvent.mReply.mRect))) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("ContentCacheInParent: 0x%p HandleQueryContentEvent(), "
"FAILED to get caret rect", this));
return false;
}
}
if (aEvent.mInput.mOffset < mText.Length()) {
aEvent.mReply.mString =
Substring(mText, aEvent.mInput.mOffset,
mText.Length() >= aEvent.mInput.EndOffset() ?
aEvent.mInput.mLength : UINT32_MAX);
} else {
aEvent.mReply.mString.Truncate(0);
}
aEvent.mReply.mOffset = aEvent.mInput.mOffset;
// XXX This may be wrong if storing range isn't in the selection range.
aEvent.mReply.mWritingMode = mSelection.mWritingMode;
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInParent: 0x%p HandleQueryContentEvent(), "
"Succeeded, aEvent={ mReply={ mOffset=%u, mString=\"%s\", "
"mWritingMode=%s, mRect=%s } }",
this, aEvent.mReply.mOffset,
NS_ConvertUTF16toUTF8(aEvent.mReply.mString).get(),
GetWritingModeName(aEvent.mReply.mWritingMode).get(),
GetRectText(aEvent.mReply.mRect).get()));
break;
case eQueryCaretRect:
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInParent: 0x%p HandleQueryContentEvent("
"aEvent={ mMessage=eQueryCaretRect, mInput={ mOffset=%u } }, "
"aWidget=0x%p), mText.Length()=%u",
this, aEvent.mInput.mOffset, aWidget, mText.Length()));
if (NS_WARN_IF(!IsSelectionValid())) {
// If content cache hasn't been initialized properly, make the query
// failed.
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("ContentCacheInParent: 0x%p HandleQueryContentEvent(), "
"FAILED because mSelection is not valid", this));
return true;
}
if (NS_WARN_IF(!GetCaretRect(aEvent.mInput.mOffset,
aEvent.mReply.mRect))) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("ContentCacheInParent: 0x%p HandleQueryContentEvent(), "
"FAILED to get caret rect", this));
return false;
}
aEvent.mReply.mOffset = aEvent.mInput.mOffset;
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInParent: 0x%p HandleQueryContentEvent(), "
"Succeeded, aEvent={ mReply={ mOffset=%u, mRect=%s } }",
this, aEvent.mReply.mOffset, GetRectText(aEvent.mReply.mRect).get()));
break;
case eQueryEditorRect:
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInParent: 0x%p HandleQueryContentEvent("
"aEvent={ mMessage=eQueryEditorRect }, aWidget=0x%p)",
this, aWidget));
aEvent.mReply.mRect = mEditorRect;
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInParent: 0x%p HandleQueryContentEvent(), "
"Succeeded, aEvent={ mReply={ mRect=%s } }",
this, GetRectText(aEvent.mReply.mRect).get()));
break;
default:
break;
}
aEvent.mSucceeded = true;
return true;
}
bool
ContentCacheInParent::GetTextRect(uint32_t aOffset,
LayoutDeviceIntRect& aTextRect) const
{
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInParent: 0x%p GetTextRect(aOffset=%u), "
"mTextRectArray={ mStart=%u, mRects.Length()=%u }, "
"mSelection={ mAnchor=%u, mFocus=%u }",
this, aOffset, mTextRectArray.mStart, mTextRectArray.mRects.Length(),
mSelection.mAnchor, mSelection.mFocus));
if (!aOffset) {
NS_WARN_IF(mFirstCharRect.IsEmpty());
aTextRect = mFirstCharRect;
return !aTextRect.IsEmpty();
}
if (aOffset == mSelection.mAnchor) {
NS_WARN_IF(mSelection.mAnchorCharRect.IsEmpty());
aTextRect = mSelection.mAnchorCharRect;
return !aTextRect.IsEmpty();
}
if (aOffset == mSelection.mFocus) {
NS_WARN_IF(mSelection.mFocusCharRect.IsEmpty());
aTextRect = mSelection.mFocusCharRect;
return !aTextRect.IsEmpty();
}
if (!mTextRectArray.InRange(aOffset)) {
aTextRect.SetEmpty();
return false;
}
aTextRect = mTextRectArray.GetRect(aOffset);
return true;
}
bool
ContentCacheInParent::GetUnionTextRects(
uint32_t aOffset,
uint32_t aLength,
LayoutDeviceIntRect& aUnionTextRect) const
{
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInParent: 0x%p GetUnionTextRects(aOffset=%u, "
"aLength=%u), mTextRectArray={ mStart=%u, mRects.Length()=%u }, "
"mSelection={ mAnchor=%u, mFocus=%u }",
this, aOffset, aLength, mTextRectArray.mStart,
mTextRectArray.mRects.Length(), mSelection.mAnchor, mSelection.mFocus));
CheckedInt<uint32_t> endOffset =
CheckedInt<uint32_t>(aOffset) + aLength;
if (!endOffset.isValid()) {
return false;
}
if (!mSelection.Collapsed() &&
aOffset == mSelection.StartOffset() && aLength == mSelection.Length()) {
NS_WARN_IF(mSelection.mRect.IsEmpty());
aUnionTextRect = mSelection.mRect;
return !aUnionTextRect.IsEmpty();
}
if (aLength == 1) {
if (!aOffset) {
NS_WARN_IF(mFirstCharRect.IsEmpty());
aUnionTextRect = mFirstCharRect;
return !aUnionTextRect.IsEmpty();
}
if (aOffset == mSelection.mAnchor) {
NS_WARN_IF(mSelection.mAnchorCharRect.IsEmpty());
aUnionTextRect = mSelection.mAnchorCharRect;
return !aUnionTextRect.IsEmpty();
}
if (aOffset == mSelection.mFocus) {
NS_WARN_IF(mSelection.mFocusCharRect.IsEmpty());
aUnionTextRect = mSelection.mFocusCharRect;
return !aUnionTextRect.IsEmpty();
}
}
// Even if some text rects are not cached of the queried range,
// we should return union rect when the first character's rect is cached
// since the first character rect is important and the others are not so
// in most cases.
if (!aOffset && aOffset != mSelection.mAnchor &&
aOffset != mSelection.mFocus && !mTextRectArray.InRange(aOffset)) {
// The first character rect isn't cached.
return false;
}
if (mTextRectArray.IsOverlappingWith(aOffset, aLength)) {
aUnionTextRect =
mTextRectArray.GetUnionRectAsFarAsPossible(aOffset, aLength);
} else {
aUnionTextRect.SetEmpty();
}
if (!aOffset) {
aUnionTextRect = aUnionTextRect.Union(mFirstCharRect);
}
if (aOffset <= mSelection.mAnchor && mSelection.mAnchor < endOffset.value()) {
aUnionTextRect = aUnionTextRect.Union(mSelection.mAnchorCharRect);
}
if (aOffset <= mSelection.mFocus && mSelection.mFocus < endOffset.value()) {
aUnionTextRect = aUnionTextRect.Union(mSelection.mFocusCharRect);
}
return !aUnionTextRect.IsEmpty();
}
bool
ContentCacheInParent::GetCaretRect(uint32_t aOffset,
LayoutDeviceIntRect& aCaretRect) const
{
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInParent: 0x%p GetCaretRect(aOffset=%u), "
"mCaret={ mOffset=%u, mRect=%s, IsValid()=%s }, mTextRectArray={ "
"mStart=%u, mRects.Length()=%u }, mSelection={ mAnchor=%u, mFocus=%u, "
"mWritingMode=%s, mAnchorCharRect=%s, mFocusCharRect=%s }, "
"mFirstCharRect=%s",
this, aOffset, mCaret.mOffset, GetRectText(mCaret.mRect).get(),
GetBoolName(mCaret.IsValid()), mTextRectArray.mStart,
mTextRectArray.mRects.Length(), mSelection.mAnchor, mSelection.mFocus,
GetWritingModeName(mSelection.mWritingMode).get(),
GetRectText(mSelection.mAnchorCharRect).get(),
GetRectText(mSelection.mFocusCharRect).get(),
GetRectText(mFirstCharRect).get()));
if (mCaret.IsValid() && mCaret.mOffset == aOffset) {
aCaretRect = mCaret.mRect;
return true;
}
// Guess caret rect from the text rect if it's stored.
if (!GetTextRect(aOffset, aCaretRect)) {
// There might be previous character rect in the cache. If so, we can
// guess the caret rect with it.
if (!aOffset || !GetTextRect(aOffset - 1, aCaretRect)) {
aCaretRect.SetEmpty();
return false;
}
if (mSelection.mWritingMode.IsVertical()) {
aCaretRect.y = aCaretRect.YMost();
} else {
// XXX bidi-unaware.
aCaretRect.x = aCaretRect.XMost();
}
}
// XXX This is not bidi aware because we don't cache each character's
// direction. However, this is usually used by IME, so, assuming the
// character is in LRT context must not cause any problem.
if (mSelection.mWritingMode.IsVertical()) {
aCaretRect.height = mCaret.IsValid() ? mCaret.mRect.height : 1;
} else {
aCaretRect.width = mCaret.IsValid() ? mCaret.mRect.width : 1;
}
return true;
}
bool
ContentCacheInParent::OnCompositionEvent(const WidgetCompositionEvent& aEvent)
{
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInParent: 0x%p OnCompositionEvent(aEvent={ "
"mMessage=%s, mData=\"%s\" (Length()=%u), mRanges->Length()=%u }), "
"mPendingEventsNeedingAck=%u, mIsComposing=%s, "
"mCommitStringByRequest=0x%p",
this, ToChar(aEvent.mMessage),
NS_ConvertUTF16toUTF8(aEvent.mData).get(), aEvent.mData.Length(),
aEvent.mRanges ? aEvent.mRanges->Length() : 0, mPendingEventsNeedingAck,
GetBoolName(mIsComposing), mCommitStringByRequest));
// We must be able to simulate the selection because
// we might not receive selection updates in time
if (!mIsComposing) {
if (aEvent.widget && aEvent.widget->PluginHasFocus()) {
// If focus is on plugin, we cannot get selection range
mCompositionStart = 0;
} else {
mCompositionStart = mSelection.StartOffset();
}
}
mIsComposing = !aEvent.CausesDOMCompositionEndEvent();
// During REQUEST_TO_COMMIT_COMPOSITION or REQUEST_TO_CANCEL_COMPOSITION,
// widget usually sends a eCompositionChange and/or eCompositionCommit event
// to finalize or clear the composition, respectively. In this time,
// we need to intercept all composition events here and pass the commit
// string for returning to the remote process as a result of
// RequestIMEToCommitComposition(). Then, eCommitComposition event will
// be dispatched with the committed string in the remote process internally.
if (mCommitStringByRequest) {
MOZ_ASSERT(aEvent.mMessage == eCompositionChange ||
aEvent.mMessage == eCompositionCommit);
*mCommitStringByRequest = aEvent.mData;
return false;
}
mPendingEventsNeedingAck++;
return true;
}
void
ContentCacheInParent::OnSelectionEvent(
const WidgetSelectionEvent& aSelectionEvent)
{
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInParent: 0x%p OnSelectionEvent(aEvent={ "
"mMessage=%s, mOffset=%u, mLength=%u, mReversed=%s, "
"mExpandToClusterBoundary=%s, mUseNativeLineBreak=%s }), "
"mPendingEventsNeedingAck=%u, mIsComposing=%s",
this, ToChar(aSelectionEvent.mMessage),
aSelectionEvent.mOffset, aSelectionEvent.mLength,
GetBoolName(aSelectionEvent.mReversed),
GetBoolName(aSelectionEvent.mExpandToClusterBoundary),
GetBoolName(aSelectionEvent.mUseNativeLineBreak), mPendingEventsNeedingAck,
GetBoolName(mIsComposing)));
mPendingEventsNeedingAck++;
}
void
ContentCacheInParent::OnEventNeedingAckHandled(nsIWidget* aWidget,
EventMessage aMessage)
{
// This is called when the child process receives WidgetCompositionEvent or
// WidgetSelectionEvent.
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInParent: 0x%p OnEventNeedingAckHandled(aWidget=0x%p, "
"aMessage=%s), mPendingEventsNeedingAck=%u",
this, aWidget, ToChar(aMessage), mPendingEventsNeedingAck));
MOZ_RELEASE_ASSERT(mPendingEventsNeedingAck > 0);
if (--mPendingEventsNeedingAck) {
return;
}
FlushPendingNotifications(aWidget);
}
bool
ContentCacheInParent::RequestIMEToCommitComposition(nsIWidget* aWidget,
bool aCancel,
nsAString& aCommittedString)
{
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("ContentCacheInParent: 0x%p RequestToCommitComposition(aWidget=%p, "
"aCancel=%s), mIsComposing=%s, mCommitStringByRequest=%p",
this, aWidget, GetBoolName(aCancel), GetBoolName(mIsComposing),
mCommitStringByRequest));
MOZ_ASSERT(!mCommitStringByRequest);
RefPtr<TextComposition> composition =
IMEStateManager::GetTextCompositionFor(aWidget);
if (NS_WARN_IF(!composition)) {
MOZ_LOG(sContentCacheLog, LogLevel::Warning,
(" ContentCacheInParent: 0x%p RequestToCommitComposition(), "
"does nothing due to no composition", this));
return false;
}
mCommitStringByRequest = &aCommittedString;
aWidget->NotifyIME(IMENotification(aCancel ? REQUEST_TO_CANCEL_COMPOSITION :
REQUEST_TO_COMMIT_COMPOSITION));
mCommitStringByRequest = nullptr;
MOZ_LOG(sContentCacheLog, LogLevel::Info,
(" ContentCacheInParent: 0x%p RequestToCommitComposition(), "
"mIsComposing=%s, the composition %s committed synchronously",
this, GetBoolName(mIsComposing),
composition->Destroyed() ? "WAS" : "has NOT been"));
if (!composition->Destroyed()) {
// When the composition isn't committed synchronously, the remote process's
// TextComposition instance will synthesize commit events and wait to
// receive delayed composition events. When TextComposition instances both
// in this process and the remote process will be destroyed when delayed
// composition events received. TextComposition instance in the parent
// process will dispatch following composition events and be destroyed
// normally. On the other hand, TextComposition instance in the remote
// process won't dispatch following composition events and will be
// destroyed by IMEStateManager::DispatchCompositionEvent().
return false;
}
// When the composition is committed synchronously, the commit string will be
// returned to the remote process. Then, PuppetWidget will dispatch
// eCompositionCommit event with the returned commit string (i.e., the value
// is aCommittedString of this method). Finally, TextComposition instance in
// the remote process will be destroyed by
// IMEStateManager::DispatchCompositionEvent() at receiving the
// eCompositionCommit event (Note that TextComposition instance in this
// process was already destroyed).
return true;
}
void
ContentCacheInParent::MaybeNotifyIME(nsIWidget* aWidget,
const IMENotification& aNotification)
{
if (!mPendingEventsNeedingAck) {
IMEStateManager::NotifyIME(aNotification, aWidget, true);
return;
}
switch (aNotification.mMessage) {
case NOTIFY_IME_OF_SELECTION_CHANGE:
mPendingSelectionChange.MergeWith(aNotification);
break;
case NOTIFY_IME_OF_TEXT_CHANGE:
mPendingTextChange.MergeWith(aNotification);
break;
case NOTIFY_IME_OF_POSITION_CHANGE:
mPendingLayoutChange.MergeWith(aNotification);
break;
case NOTIFY_IME_OF_COMPOSITION_UPDATE:
mPendingCompositionUpdate.MergeWith(aNotification);
break;
default:
MOZ_CRASH("Unsupported notification");
break;
}
}
void
ContentCacheInParent::FlushPendingNotifications(nsIWidget* aWidget)
{
MOZ_ASSERT(!mPendingEventsNeedingAck);
// New notifications which are notified during flushing pending notifications
// should be merged again.
mPendingEventsNeedingAck++;
nsCOMPtr<nsIWidget> kungFuDeathGrip(aWidget);
// First, text change notification should be sent because selection change
// notification notifies IME of current selection range in the latest content.
// So, IME may need the latest content before that.
if (mPendingTextChange.HasNotification()) {
IMENotification notification(mPendingTextChange);
if (!aWidget->Destroyed()) {
mPendingTextChange.Clear();
IMEStateManager::NotifyIME(notification, aWidget, true);
}
}
if (mPendingSelectionChange.HasNotification()) {
IMENotification notification(mPendingSelectionChange);
if (!aWidget->Destroyed()) {
mPendingSelectionChange.Clear();
IMEStateManager::NotifyIME(notification, aWidget, true);
}
}
// Layout change notification should be notified after selection change
// notification because IME may want to query position of new caret position.
if (mPendingLayoutChange.HasNotification()) {
IMENotification notification(mPendingLayoutChange);
if (!aWidget->Destroyed()) {
mPendingLayoutChange.Clear();
IMEStateManager::NotifyIME(notification, aWidget, true);
}
}
// Finally, send composition update notification because it notifies IME of
// finishing handling whole sending events.
if (mPendingCompositionUpdate.HasNotification()) {
IMENotification notification(mPendingCompositionUpdate);
if (!aWidget->Destroyed()) {
mPendingCompositionUpdate.Clear();
IMEStateManager::NotifyIME(notification, aWidget, true);
}
}
if (!--mPendingEventsNeedingAck && !aWidget->Destroyed() &&
(mPendingTextChange.HasNotification() ||
mPendingSelectionChange.HasNotification() ||
mPendingLayoutChange.HasNotification() ||
mPendingCompositionUpdate.HasNotification())) {
FlushPendingNotifications(aWidget);
}
}
/*****************************************************************************
* mozilla::ContentCache::TextRectArray
*****************************************************************************/
LayoutDeviceIntRect
ContentCache::TextRectArray::GetRect(uint32_t aOffset) const
{
LayoutDeviceIntRect rect;
if (InRange(aOffset)) {
rect = mRects[aOffset - mStart];
}
return rect;
}
LayoutDeviceIntRect
ContentCache::TextRectArray::GetUnionRect(uint32_t aOffset,
uint32_t aLength) const
{
LayoutDeviceIntRect rect;
if (!InRange(aOffset, aLength)) {
return rect;
}
for (uint32_t i = 0; i < aLength; i++) {
rect = rect.Union(mRects[aOffset - mStart + i]);
}
return rect;
}
LayoutDeviceIntRect
ContentCache::TextRectArray::GetUnionRectAsFarAsPossible(
uint32_t aOffset,
uint32_t aLength) const
{
LayoutDeviceIntRect rect;
if (!IsOverlappingWith(aOffset, aLength)) {
return rect;
}
uint32_t startOffset = std::max(aOffset, mStart);
uint32_t endOffset = std::min(aOffset + aLength, EndOffset());
for (uint32_t i = 0; i < endOffset - startOffset; i++) {
rect = rect.Union(mRects[startOffset - mStart + i]);
}
return rect;
}
} // namespace mozilla