From 55b4f820a4c854e1881bb23775c2b0495a7a9e4d Mon Sep 17 00:00:00 2001 From: Kartikaya Gupta Date: Wed, 17 Apr 2013 17:39:12 -0400 Subject: [PATCH] Bug 860940 - Add static factory-style methods to create AndroidGeckoEvent instances in widget code. r=cpeterson --- widget/android/AndroidJNI.cpp | 2 +- widget/android/AndroidJavaWrappers.cpp | 14 +------ widget/android/AndroidJavaWrappers.h | 57 +++++++++++++++++--------- widget/android/nsAppShell.cpp | 8 ++-- widget/android/nsWindow.cpp | 9 ++-- 5 files changed, 49 insertions(+), 41 deletions(-) diff --git a/widget/android/AndroidJNI.cpp b/widget/android/AndroidJNI.cpp index b73dc5a8dc7..cf239d3e9ef 100644 --- a/widget/android/AndroidJNI.cpp +++ b/widget/android/AndroidJNI.cpp @@ -61,7 +61,7 @@ Java_org_mozilla_gecko_GeckoAppShell_notifyGeckoOfEvent(JNIEnv *jenv, jclass jc, { // poke the appshell if (nsAppShell::gAppShell) - nsAppShell::gAppShell->PostEvent(new AndroidGeckoEvent(jenv, event)); + nsAppShell::gAppShell->PostEvent(AndroidGeckoEvent::MakeFromJavaObject(jenv, event)); } NS_EXPORT void JNICALL diff --git a/widget/android/AndroidJavaWrappers.cpp b/widget/android/AndroidJavaWrappers.cpp index 380be278155..3bbb46c00e3 100644 --- a/widget/android/AndroidJavaWrappers.cpp +++ b/widget/android/AndroidJavaWrappers.cpp @@ -498,11 +498,9 @@ AndroidGeckoEvent::ReadCharactersExtraField(JNIEnv *jenv) } 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 @@ -671,14 +669,6 @@ AndroidGeckoEvent::Init(int aType) mAckNeeded = false; } -void -AndroidGeckoEvent::Init(int aType, int aAction) -{ - mType = aType; - mAckNeeded = false; - mAction = aAction; -} - void AndroidGeckoEvent::Init(AndroidGeckoEvent *aResizeEvent) { diff --git a/widget/android/AndroidJavaWrappers.h b/widget/android/AndroidJavaWrappers.h index f7251bfc343..8548ea8738c 100644 --- a/widget/android/AndroidJavaWrappers.h +++ b/widget/android/AndroidJavaWrappers.h @@ -589,31 +589,49 @@ public: 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; } @@ -659,6 +677,7 @@ public: int Width() { return mWidth; } int Height() { return mHeight; } nsTouchEvent MakeTouchEvent(nsIWidget* widget); + void UnionRect(nsIntRect const& aRect); protected: int mAction; diff --git a/widget/android/nsAppShell.cpp b/widget/android/nsAppShell.cpp index d0a27ee857a..eac90d84167 100644 --- a/widget/android/nsAppShell.cpp +++ b/widget/android/nsAppShell.cpp @@ -269,7 +269,7 @@ 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 @@ -467,7 +467,7 @@ nsAppShell::ProcessNextNativeEvent(bool mayWait) 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; @@ -603,12 +603,12 @@ nsAppShell::PostEvent(AndroidGeckoEvent *ae) 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. @@ -623,7 +623,7 @@ nsAppShell::PostEvent(AndroidGeckoEvent *ae) // 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); diff --git a/widget/android/nsWindow.cpp b/widget/android/nsWindow.cpp index c99200c4dc1..b5a25e282df 100644 --- a/widget/android/nsWindow.cpp +++ b/widget/android/nsWindow.cpp @@ -514,7 +514,7 @@ nsWindow::IsEnabled() const 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; } @@ -2038,8 +2038,7 @@ nsWindow::SetInputContext(const InputContext& aContext, if (mIMEUpdatingContext) { return; } - AndroidGeckoEvent *event = new AndroidGeckoEvent( - AndroidGeckoEvent::IME_EVENT, + AndroidGeckoEvent *event = AndroidGeckoEvent::MakeIMEEvent( AndroidGeckoEvent::IME_UPDATE_CONTEXT); nsAppShell::gAppShell->PostEvent(event); mIMEUpdatingContext = true; @@ -2068,8 +2067,8 @@ nsWindow::PostFlushIMEChanges() // 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); }