Bug 860940 - Add static factory-style methods to create AndroidGeckoEvent instances in widget code. r=cpeterson
authorKartikaya Gupta <kgupta@mozilla.com>
Wed, 17 Apr 2013 17:39:12 -0400
changeset 129132 63203f42c4b8
parent 129131 beaf93963e65
child 129133 e351053c0ef8
push id24559
push userryanvm@gmail.com
push date2013-04-18 14:56 +0000
treeherdermozilla-central@fa5d5fccbc11 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscpeterson
bugs860940
milestone23.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 860940 - Add static factory-style methods to create AndroidGeckoEvent instances in widget code. r=cpeterson
widget/android/AndroidJNI.cpp
widget/android/AndroidJavaWrappers.cpp
widget/android/AndroidJavaWrappers.h
widget/android/nsAppShell.cpp
widget/android/nsWindow.cpp
--- a/widget/android/AndroidJNI.cpp
+++ b/widget/android/AndroidJNI.cpp
@@ -56,17 +56,17 @@ Java_org_mozilla_gecko_GeckoAppShell_nat
     AndroidBridge::ConstructBridge(jenv, jc);
 }
 
 NS_EXPORT void JNICALL
 Java_org_mozilla_gecko_GeckoAppShell_notifyGeckoOfEvent(JNIEnv *jenv, jclass jc, jobject event)
 {
     // poke the appshell
     if (nsAppShell::gAppShell)
-        nsAppShell::gAppShell->PostEvent(new AndroidGeckoEvent(jenv, event));
+        nsAppShell::gAppShell->PostEvent(AndroidGeckoEvent::MakeFromJavaObject(jenv, event));
 }
 
 NS_EXPORT void JNICALL
 Java_org_mozilla_gecko_GeckoAppShell_processNextNativeEvent(JNIEnv *jenv, jclass)
 {
     // poke the appshell
     if (nsAppShell::gAppShell)
         nsAppShell::gAppShell->ProcessNextNativeEvent(false);
--- a/widget/android/AndroidJavaWrappers.cpp
+++ b/widget/android/AndroidJavaWrappers.cpp
@@ -493,21 +493,19 @@ AndroidGeckoEvent::ReadCharactersExtraFi
     }
 
     int len = jenv->GetStringLength(s);
     mCharactersExtra.SetLength(len);
     jenv->GetStringRegion(s, 0, len, mCharactersExtra.BeginWriting());
 }
 
 void
-AndroidGeckoEvent::Init(int aType, nsIntRect const& aRect)
+AndroidGeckoEvent::UnionRect(nsIntRect const& aRect)
 {
-    mType = aType;
-    mAckNeeded = false;
-    mRect = aRect;
+    mRect = aRect.Union(mRect);
 }
 
 uint32_t
 AndroidGeckoEvent::ReadDomKeyLocation(JNIEnv* jenv, jobject jGeckoEventObj)
 {
     jobject enumObject = jenv->GetObjectField(jGeckoEventObj,
                                              jDomKeyLocationField);
     MOZ_ASSERT(enumObject);
@@ -667,24 +665,16 @@ AndroidGeckoEvent::Init(JNIEnv *jenv, jo
 void
 AndroidGeckoEvent::Init(int aType)
 {
     mType = aType;
     mAckNeeded = false;
 }
 
 void
-AndroidGeckoEvent::Init(int aType, int aAction)
-{
-    mType = aType;
-    mAckNeeded = false;
-    mAction = aAction;
-}
-
-void
 AndroidGeckoEvent::Init(AndroidGeckoEvent *aResizeEvent)
 {
     NS_ASSERTION(aResizeEvent->Type() == SIZE_CHANGED, "Init called on non-SIZE_CHANGED event");
 
     mType = FORCED_RESIZE;
     mAckNeeded = false;
     mTime = aResizeEvent->mTime;
     mPoints = aResizeEvent->mPoints; // x,y coordinates
--- a/widget/android/AndroidJavaWrappers.h
+++ b/widget/android/AndroidJavaWrappers.h
@@ -584,41 +584,59 @@ public:
     static jmethodID jGetAccuracyMethod;
     static jmethodID jGetBearingMethod;
     static jmethodID jGetSpeedMethod;
     static jmethodID jGetTimeMethod;
 };
 
 class AndroidGeckoEvent : public WrappedJavaObject
 {
-public:
-    static void InitGeckoEventClass(JNIEnv *jEnv);
-
-    AndroidGeckoEvent(int aType) {
-        Init(aType);
-    }
-    AndroidGeckoEvent(int aType, int aAction) {
-        Init(aType, aAction);
-    }
-    AndroidGeckoEvent(int aType, const nsIntRect &aRect) {
-        Init(aType, aRect);
-    }
-    AndroidGeckoEvent(JNIEnv *jenv, jobject jobj) {
-        Init(jenv, jobj);
-    }
-    AndroidGeckoEvent(AndroidGeckoEvent *aResizeEvent) {
-        Init(aResizeEvent);
+private:
+    AndroidGeckoEvent() {
     }
 
     void Init(JNIEnv *jenv, jobject jobj);
     void Init(int aType);
-    void Init(int aType, int aAction);
-    void Init(int aType, const nsIntRect &aRect);
     void Init(AndroidGeckoEvent *aResizeEvent);
 
+public:
+    static void InitGeckoEventClass(JNIEnv *jEnv);
+
+    static AndroidGeckoEvent* MakeNativePoke() {
+        AndroidGeckoEvent *event = new AndroidGeckoEvent();
+        event->Init(NATIVE_POKE);
+        return event;
+    }
+
+    static AndroidGeckoEvent* MakeIMEEvent(int aAction) {
+        AndroidGeckoEvent *event = new AndroidGeckoEvent();
+        event->Init(IME_EVENT);
+        event->mAction = aAction;
+        return event;
+    }
+
+    static AndroidGeckoEvent* MakeDrawEvent(const nsIntRect& aRect) {
+        AndroidGeckoEvent *event = new AndroidGeckoEvent();
+        event->Init(DRAW);
+        event->mRect = aRect;
+        return event;
+    }
+
+    static AndroidGeckoEvent* MakeFromJavaObject(JNIEnv *jenv, jobject jobj) {
+        AndroidGeckoEvent *event = new AndroidGeckoEvent();
+        event->Init(jenv, jobj);
+        return event;
+    }
+
+    static AndroidGeckoEvent* CopyResizeEvent(AndroidGeckoEvent *aResizeEvent) {
+        AndroidGeckoEvent *event = new AndroidGeckoEvent();
+        event->Init(aResizeEvent);
+        return event;
+    }
+
     int Action() { return mAction; }
     int Type() { return mType; }
     bool AckNeeded() { return mAckNeeded; }
     int64_t Time() { return mTime; }
     const nsTArray<nsIntPoint>& Points() { return mPoints; }
     const nsTArray<int>& PointIndicies() { return mPointIndicies; }
     const nsTArray<float>& Pressures() { return mPressures; }
     const nsTArray<float>& Orientations() { return mOrientations; }
@@ -654,16 +672,17 @@ public:
     nsGeoPosition* GeoPosition() { return mGeoPosition; }
     double Bandwidth() { return mBandwidth; }
     bool CanBeMetered() { return mCanBeMetered; }
     short ScreenOrientation() { return mScreenOrientation; }
     RefCountedJavaObject* ByteBuffer() { return mByteBuffer; }
     int Width() { return mWidth; }
     int Height() { return mHeight; }
     nsTouchEvent MakeTouchEvent(nsIWidget* widget);
+    void UnionRect(nsIntRect const& aRect);
 
 protected:
     int mAction;
     int mType;
     bool mAckNeeded;
     int64_t mTime;
     nsTArray<nsIntPoint> mPoints;
     nsTArray<nsIntPoint> mPointRadii;
--- a/widget/android/nsAppShell.cpp
+++ b/widget/android/nsAppShell.cpp
@@ -264,17 +264,17 @@ nsAppShell::Observe(nsISupports* aSubjec
 }
 
 void
 nsAppShell::ScheduleNativeEventCallback()
 {
     EVLOG("nsAppShell::ScheduleNativeEventCallback pth: %p thread: %p main: %d", (void*) pthread_self(), (void*) NS_GetCurrentThread(), NS_IsMainThread());
 
     // this is valid to be called from any thread, so do so.
-    PostEvent(new AndroidGeckoEvent(AndroidGeckoEvent::NATIVE_POKE));
+    PostEvent(AndroidGeckoEvent::MakeNativePoke());
 }
 
 bool
 nsAppShell::ProcessNextNativeEvent(bool mayWait)
 {
     EVLOG("nsAppShell::ProcessNextNativeEvent %d", mayWait);
 
     PROFILER_LABEL("nsAppShell", "ProcessNextNativeEvent");
@@ -462,17 +462,17 @@ nsAppShell::ProcessNextNativeEvent(bool 
         if (flag)
             nsMemory::Free(flag);
         break;
     }
 
     case AndroidGeckoEvent::SIZE_CHANGED: {
         // store the last resize event to dispatch it to new windows with a FORCED_RESIZE event
         if (curEvent != gLastSizeChange) {
-            gLastSizeChange = new AndroidGeckoEvent(curEvent);
+            gLastSizeChange = AndroidGeckoEvent::CopyResizeEvent(curEvent);
         }
         nsWindow::OnGlobalAndroidEvent(curEvent);
         break;
     }
 
     case AndroidGeckoEvent::VISITED: {
 #ifdef MOZ_ANDROID_HISTORY
         nsCOMPtr<IHistory> history = services::GetHistoryService();
@@ -598,37 +598,37 @@ nsAppShell::PostEvent(AndroidGeckoEvent 
                 }
                 EVLOG("nsAppShell: Inserting compositor event %d at position %d to maintain priority order", ae->Type(), i);
                 mEventQueue.InsertElementAt(i, ae);
             }
             break;
 
         case AndroidGeckoEvent::DRAW:
             if (mQueuedDrawEvent) {
+#if defined(DEBUG) || defined(FORCE_ALOG)
                 // coalesce this new draw event with the one already in the queue
                 const nsIntRect& oldRect = mQueuedDrawEvent->Rect();
                 const nsIntRect& newRect = ae->Rect();
                 nsIntRect combinedRect = oldRect.Union(newRect);
 
-#if defined(DEBUG) || defined(FORCE_ALOG)
                 // XXX We may want to consider using regions instead of rectangles.
                 //     Print an error if we're upload a lot more than we would
                 //     if we handled this as two separate events.
                 int combinedArea = (oldRect.width * oldRect.height) +
                                    (newRect.width * newRect.height);
                 int boundsArea = combinedRect.width * combinedRect.height;
                 if (boundsArea > combinedArea * 8)
                     ALOG("nsAppShell: Area of bounds greatly exceeds combined area: %d > %d",
                          boundsArea, combinedArea);
 #endif
 
                 // coalesce into the new draw event rather than the queued one because
                 // it is not always safe to move draws earlier in the queue; there may
                 // be events between the two draws that affect scroll position or something.
-                ae->Init(AndroidGeckoEvent::DRAW, combinedRect);
+                ae->UnionRect(mQueuedDrawEvent->Rect());
 
                 EVLOG("nsAppShell: Coalescing previous DRAW event at %p into new DRAW event %p", mQueuedDrawEvent, ae);
                 mEventQueue.RemoveElement(mQueuedDrawEvent);
                 delete mQueuedDrawEvent;
             }
 
             if (!mAllowCoalescingNextDraw) {
                 // if we're not allowing coalescing of this draw event, then
--- a/widget/android/nsWindow.cpp
+++ b/widget/android/nsWindow.cpp
@@ -509,17 +509,17 @@ bool
 nsWindow::IsEnabled() const
 {
     return true;
 }
 
 NS_IMETHODIMP
 nsWindow::Invalidate(const nsIntRect &aRect)
 {
-    AndroidGeckoEvent *event = new AndroidGeckoEvent(AndroidGeckoEvent::DRAW, aRect);
+    AndroidGeckoEvent *event = AndroidGeckoEvent::MakeDrawEvent(aRect);
     nsAppShell::gAppShell->PostEvent(event);
     return NS_OK;
 }
 
 nsWindow*
 nsWindow::FindTopLevel()
 {
     nsWindow *toplevel = this;
@@ -2033,18 +2033,17 @@ nsWindow::SetInputContext(const InputCon
         enabled = IMEState::DISABLED;
     }
 
     mInputContext.mIMEState.mEnabled = enabled;
 
     if (mIMEUpdatingContext) {
         return;
     }
-    AndroidGeckoEvent *event = new AndroidGeckoEvent(
-            AndroidGeckoEvent::IME_EVENT,
+    AndroidGeckoEvent *event = AndroidGeckoEvent::MakeIMEEvent(
             AndroidGeckoEvent::IME_UPDATE_CONTEXT);
     nsAppShell::gAppShell->PostEvent(event);
     mIMEUpdatingContext = true;
 }
 
 NS_IMETHODIMP_(InputContext)
 nsWindow::GetInputContext()
 {
@@ -2063,18 +2062,18 @@ nsWindow::GetInputContext()
 
 void
 nsWindow::PostFlushIMEChanges()
 {
     if (!mIMETextChanges.IsEmpty() || mIMESelectionChanged) {
         // Already posted
         return;
     }
-    AndroidGeckoEvent *event = new AndroidGeckoEvent(
-            AndroidGeckoEvent::IME_EVENT, AndroidGeckoEvent::IME_FLUSH_CHANGES);
+    AndroidGeckoEvent *event = AndroidGeckoEvent::MakeIMEEvent(
+            AndroidGeckoEvent::IME_FLUSH_CHANGES);
     nsAppShell::gAppShell->PostEvent(event);
 }
 
 void
 nsWindow::FlushIMEChanges()
 {
     nsRefPtr<nsWindow> kungFuDeathGrip(this);
     for (uint32_t i = 0; i < mIMETextChanges.Length(); i++) {