From 2c4f8e6d64a4bae3a6b752c5df7ef26860c5e11e Mon Sep 17 00:00:00 2001 From: Jim Chen Date: Fri, 25 Sep 2015 15:03:34 -0400 Subject: [PATCH] Bug 1200343 - Add native calls for pref events; r=snorp This patch adds two native calls to PrefsHelper to avoid using GeckoEvent for getting prefs. --- build/mobile/robocop/FennecNativeActions.java | 6 +-- mobile/android/base/PrefsHelper.java | 20 ++++--- widget/android/GeneratedJNINatives.h | 19 +++++++ widget/android/GeneratedJNIWrappers.cpp | 8 +++ widget/android/GeneratedJNIWrappers.h | 52 +++++++++++++++++++ 5 files changed, 96 insertions(+), 9 deletions(-) diff --git a/build/mobile/robocop/FennecNativeActions.java b/build/mobile/robocop/FennecNativeActions.java index 07fdc9461010..84a44b373756 100644 --- a/build/mobile/robocop/FennecNativeActions.java +++ b/build/mobile/robocop/FennecNativeActions.java @@ -191,15 +191,15 @@ public class FennecNativeActions implements Actions { } public void sendPreferencesGetEvent(int requestId, String[] prefNames) { - GeckoAppShell.sendEventToGecko(GeckoEvent.createPreferencesGetEvent(requestId, prefNames)); + PrefsHelper.getPrefsById(requestId, prefNames, /* observe */ false); } public void sendPreferencesObserveEvent(int requestId, String[] prefNames) { - GeckoAppShell.sendEventToGecko(GeckoEvent.createPreferencesObserveEvent(requestId, prefNames)); + PrefsHelper.getPrefsById(requestId, prefNames, /* observe */ true); } public void sendPreferencesRemoveObserversEvent(int requestId) { - GeckoAppShell.sendEventToGecko(GeckoEvent.createPreferencesRemoveObserversEvent(requestId)); + PrefsHelper.removePrefsObserver(requestId); } class PaintExpecter implements RepeatedEventExpecter { diff --git a/mobile/android/base/PrefsHelper.java b/mobile/android/base/PrefsHelper.java index f3495b4cf08e..f87fbdb0b3d0 100644 --- a/mobile/android/base/PrefsHelper.java +++ b/mobile/android/base/PrefsHelper.java @@ -5,6 +5,8 @@ package org.mozilla.gecko; +import org.mozilla.gecko.annotation.RobocopTarget; +import org.mozilla.gecko.annotation.WrapForJNI; import org.mozilla.gecko.util.GeckoEventListener; import org.json.JSONArray; @@ -26,6 +28,11 @@ public final class PrefsHelper { private static int sUniqueRequestId = 1; static final SparseArray sCallbacks = new SparseArray(); + @WrapForJNI @RobocopTarget + /* package */ static native void getPrefsById(int requestId, String[] prefNames, boolean observe); + @WrapForJNI @RobocopTarget + /* package */ static native void removePrefsObserver(int requestId); + public static int getPref(String prefName, PrefHandler callback) { return getPrefsInternal(new String[] { prefName }, callback); } @@ -47,14 +54,15 @@ public final class PrefsHelper { sCallbacks.put(requestId, callback); } - GeckoEvent event; - if (callback.isObserver()) { - event = GeckoEvent.createPreferencesObserveEvent(requestId, prefNames); + // Because we use JS to handle pref events, we need to wait until the RUNNING state. + // If we ever convert that to native code, we can switch to using the JNI_READY state. + if (GeckoThread.isStateAtLeast(GeckoThread.State.RUNNING)) { + getPrefsById(requestId, prefNames, callback.isObserver()); } else { - event = GeckoEvent.createPreferencesGetEvent(requestId, prefNames); + GeckoThread.queueNativeCallUntil( + GeckoThread.State.RUNNING, PrefsHelper.class, "getPrefsById", + requestId, prefNames, callback.isObserver()); } - GeckoAppShell.sendEventToGecko(event); - return requestId; } diff --git a/widget/android/GeneratedJNINatives.h b/widget/android/GeneratedJNINatives.h index cec0a747ae0c..aa9aa027ff04 100644 --- a/widget/android/GeneratedJNINatives.h +++ b/widget/android/GeneratedJNINatives.h @@ -85,6 +85,25 @@ public: template constexpr JNINativeMethod GeckoView::Window::Natives::methods[]; +template +class PrefsHelper::Natives : public mozilla::jni::NativeImpl +{ +public: + static constexpr JNINativeMethod methods[] = { + + mozilla::jni::MakeNativeMethod( + mozilla::jni::NativeStub + ::template Wrap<&Impl::GetPrefsById>), + + mozilla::jni::MakeNativeMethod( + mozilla::jni::NativeStub + ::template Wrap<&Impl::RemovePrefsObserver>) + }; +}; + +template +constexpr JNINativeMethod PrefsHelper::Natives::methods[]; + template class NativeJSContainer::Natives : public mozilla::jni::NativeImpl { diff --git a/widget/android/GeneratedJNIWrappers.cpp b/widget/android/GeneratedJNIWrappers.cpp index 21a967de8341..c498b3fed61c 100644 --- a/widget/android/GeneratedJNIWrappers.cpp +++ b/widget/android/GeneratedJNIWrappers.cpp @@ -956,6 +956,14 @@ constexpr char GeckoView::Window::DisposeNative_t::signature[]; constexpr char GeckoView::Window::Open_t::name[]; constexpr char GeckoView::Window::Open_t::signature[]; +constexpr char PrefsHelper::name[]; + +constexpr char PrefsHelper::GetPrefsById_t::name[]; +constexpr char PrefsHelper::GetPrefsById_t::signature[]; + +constexpr char PrefsHelper::RemovePrefsObserver_t::name[]; +constexpr char PrefsHelper::RemovePrefsObserver_t::signature[]; + constexpr char RestrictedProfiles::name[]; constexpr char RestrictedProfiles::IsAllowed_t::name[]; diff --git a/widget/android/GeneratedJNIWrappers.h b/widget/android/GeneratedJNIWrappers.h index 2a0b340cd0cc..87044849b6c5 100644 --- a/widget/android/GeneratedJNIWrappers.h +++ b/widget/android/GeneratedJNIWrappers.h @@ -2312,6 +2312,58 @@ public: template class Natives; }; +class PrefsHelper : public mozilla::jni::Class +{ +public: + typedef mozilla::jni::Ref Ref; + typedef mozilla::jni::LocalRef LocalRef; + typedef mozilla::jni::GlobalRef GlobalRef; + typedef const mozilla::jni::Param& Param; + + static constexpr char name[] = + "org/mozilla/gecko/PrefsHelper"; + +protected: + PrefsHelper(jobject instance) : Class(instance) {} + +public: + struct GetPrefsById_t { + typedef PrefsHelper Owner; + typedef void ReturnType; + typedef void SetterType; + typedef mozilla::jni::Args< + int32_t, + mozilla::jni::ObjectArray::Param, + bool> Args; + static constexpr char name[] = "getPrefsById"; + static constexpr char signature[] = + "(I[Ljava/lang/String;Z)V"; + static const bool isStatic = true; + static const bool isMultithreaded = false; + static const mozilla::jni::ExceptionMode exceptionMode = + mozilla::jni::ExceptionMode::ABORT; + }; + +public: + struct RemovePrefsObserver_t { + typedef PrefsHelper Owner; + typedef void ReturnType; + typedef void SetterType; + typedef mozilla::jni::Args< + int32_t> Args; + static constexpr char name[] = "removePrefsObserver"; + static constexpr char signature[] = + "(I)V"; + static const bool isStatic = true; + static const bool isMultithreaded = false; + static const mozilla::jni::ExceptionMode exceptionMode = + mozilla::jni::ExceptionMode::ABORT; + }; + +public: + template class Natives; +}; + class RestrictedProfiles : public mozilla::jni::Class { public: