Bug 827065 - Simplify selection update code and use new focus text update offsets; r=cpeterson
authorJim Chen <nchen@mozilla.com>
Wed, 09 Jan 2013 11:57:48 -0500
changeset 118250 50752f340d6cdf7b34552cfd2add4729ac393b9a
parent 118249 efddd115fd76af1e2e0c1273814db0177b7e37b1
child 118251 bdddc01216a4413fd028d63b100dc6f84dfa44bd
push id20949
push usernchen@mozilla.com
push dateWed, 09 Jan 2013 16:58:37 +0000
treeherdermozilla-inbound@50752f340d6c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscpeterson
bugs827065
milestone21.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 827065 - Simplify selection update code and use new focus text update offsets; r=cpeterson
widget/android/nsWindow.cpp
widget/android/nsWindow.h
--- a/widget/android/nsWindow.cpp
+++ b/widget/android/nsWindow.cpp
@@ -157,17 +157,18 @@ nsWindow::DumpWindows(const nsTArray<nsW
 
 nsWindow::nsWindow() :
     mIsVisible(false),
     mParent(nullptr),
     mFocus(nullptr),
     mIMEComposing(false),
     mIMEMaskSelectionUpdate(false),
     mIMEMaskTextUpdate(false),
-    mIMEMaskEventsCount(1) // Mask IME events since there's no focus yet
+    mIMEMaskEventsCount(1), // Mask IME events since there's no focus yet
+    mIMESelectionChanged(false)
 {
 }
 
 nsWindow::~nsWindow()
 {
     gTopLevelWindows.RemoveElement(this);
     nsWindow *top = FindTopLevel();
     if (top->mFocus == this)
@@ -2059,19 +2060,22 @@ nsWindow::CancelIMEComposition()
 
 NS_IMETHODIMP
 nsWindow::OnIMEFocusChange(bool aFocus)
 {
     ALOGIME("IME: OnIMEFocusChange: f=%d", aFocus);
 
     if (aFocus) {
         mIMETextChanges.Clear();
-        mIMESelectionChange = IMEChange();
+        mIMESelectionChanged = false;
         // OnIMETextChange also notifies selection
-        OnIMETextChange(0, INT32_MAX, INT32_MAX);
+        // Use 'INT32_MAX / 2' here because subsequent text changes might
+        // combine with this text change, and overflow might occur if
+        // we just use INT32_MAX
+        OnIMETextChange(0, INT32_MAX / 2, INT32_MAX / 2);
         FlushIMEChanges();
     } else {
         // Mask events because we lost focus. On the next focus event, Gecko will notify
         // Java, and Java will send an acknowledge focus event back to Gecko. That is
         // where we unmask event handling
         mIMEMaskEventsCount++;
         mIMEComposing = false;
         mIMEComposingText.Truncate();
@@ -2081,32 +2085,31 @@ nsWindow::OnIMEFocusChange(bool aFocus)
                              int(aFocus));
 
     return NS_OK;
 }
 
 void
 nsWindow::PostFlushIMEChanges()
 {
-    if (!mIMETextChanges.IsEmpty() || !mIMESelectionChange.IsEmpty()) {
+    if (!mIMETextChanges.IsEmpty() || mIMESelectionChanged) {
         // Already posted
         return;
     }
     AndroidGeckoEvent *event = new AndroidGeckoEvent(
             AndroidGeckoEvent::IME_EVENT, AndroidGeckoEvent::IME_FLUSH_CHANGES);
     nsAppShell::gAppShell->PostEvent(event);
 }
 
 void
 nsWindow::FlushIMEChanges()
 {
     nsRefPtr<nsWindow> kungFuDeathGrip(this);
     for (uint32_t i = 0; i < mIMETextChanges.Length(); i++) {
         IMEChange &change = mIMETextChanges[i];
-        MOZ_ASSERT(change.IsTextChange());
 
         nsQueryContentEvent event(true, NS_QUERY_TEXT_CONTENT, this);
         InitEvent(event, nullptr);
         event.InitForQueryTextContent(change.mStart,
                                       change.mNewEnd - change.mStart);
         DispatchEvent(&event);
         if (!event.mSucceeded)
             return;
@@ -2114,36 +2117,42 @@ nsWindow::FlushIMEChanges()
         AndroidBridge::NotifyIMEChange(event.mReply.mString.get(),
                                        event.mReply.mString.Length(),
                                        change.mStart,
                                        change.mOldEnd,
                                        change.mNewEnd);
     }
     mIMETextChanges.Clear();
 
-    if (!mIMESelectionChange.IsEmpty()) {
-        MOZ_ASSERT(!mIMESelectionChange.IsTextChange());
+    if (mIMESelectionChanged) {
+        nsQueryContentEvent event(true, NS_QUERY_SELECTED_TEXT, this);
+        InitEvent(event, nullptr);
+
+        DispatchEvent(&event);
+        if (!event.mSucceeded)
+            return;
+
         AndroidBridge::NotifyIMEChange(nullptr, 0,
-                                       mIMESelectionChange.mStart,
-                                       mIMESelectionChange.mOldEnd, -1);
-        mIMESelectionChange = IMEChange();
+                                       event.GetSelectionStart(),
+                                       event.GetSelectionEnd(), -1);
+        mIMESelectionChanged = false;
     }
 }
 
 NS_IMETHODIMP
 nsWindow::OnIMETextChange(uint32_t aStart, uint32_t aOldEnd, uint32_t aNewEnd)
 {
     if (mIMEMaskTextUpdate)
         return NS_OK;
 
     ALOGIME("IME: OnIMETextChange: s=%d, oe=%d, ne=%d",
             aStart, aOldEnd, aNewEnd);
 
     /* Make sure Java's selection is up-to-date */
-    mIMESelectionChange = IMEChange();
+    mIMESelectionChanged = false;
     OnIMESelectionChange();
     PostFlushIMEChanges();
 
     mIMETextChanges.AppendElement(IMEChange(aStart, aOldEnd, aNewEnd));
     // Now that we added a new range we need to go back and
     // update all the ranges before that.
     // Ranges that have offsets which follow this new range
     // need to be updated to reflect new offsets
@@ -2204,27 +2213,18 @@ nsWindow::OnIMETextChange(uint32_t aStar
 NS_IMETHODIMP
 nsWindow::OnIMESelectionChange(void)
 {
     if (mIMEMaskSelectionUpdate)
         return NS_OK;
 
     ALOGIME("IME: OnIMESelectionChange");
 
-    nsRefPtr<nsWindow> kungFuDeathGrip(this);
-    nsQueryContentEvent event(true, NS_QUERY_SELECTED_TEXT, this);
-    InitEvent(event, nullptr);
-
-    DispatchEvent(&event);
-    if (!event.mSucceeded)
-        return NS_OK;
-
     PostFlushIMEChanges();
-    mIMESelectionChange = IMEChange((int32_t)event.GetSelectionStart(),
-                                    (int32_t)event.GetSelectionEnd());
+    mIMESelectionChanged = true;
     return NS_OK;
 }
 
 nsIMEUpdatePreference
 nsWindow::GetIMEUpdatePreference()
 {
     return nsIMEUpdatePreference(true, true);
 }
--- a/widget/android/nsWindow.h
+++ b/widget/android/nsWindow.h
@@ -196,31 +196,23 @@ protected:
         IMEChange() :
             mStart(-1), mOldEnd(-1), mNewEnd(-1)
         {
         }
         IMEChange(int32_t start, int32_t oldEnd, int32_t newEnd) :
             mStart(start), mOldEnd(oldEnd), mNewEnd(newEnd)
         {
         }
-        IMEChange(int32_t start, int32_t end) :
-            mStart(start), mOldEnd(end), mNewEnd(-1)
-        {
-        }
         bool IsEmpty()
         {
             return mStart < 0;
         }
-        bool IsTextChange()
-        {
-            return mNewEnd >= 0;
-        }
     };
     nsAutoTArray<IMEChange, 4> mIMETextChanges;
-    IMEChange mIMESelectionChange;
+    bool mIMESelectionChanged;
 
     InputContext mInputContext;
 
     static void DumpWindows();
     static void DumpWindows(const nsTArray<nsWindow*>& wins, int indent = 0);
     static void LogWindow(nsWindow *win, int index, int indent);
 
 private: