From f9d5091d8f27ebd66fb7ebf1ce6cb032b4606181 Mon Sep 17 00:00:00 2001 From: Jim Chen Date: Mon, 28 Sep 2015 12:07:09 -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 ++++--- .../tests/browser/robocop/BaseTest.java | 1 + widget/android/GeneratedJNINatives.h | 19 +++++++ widget/android/GeneratedJNIWrappers.cpp | 8 +++ widget/android/GeneratedJNIWrappers.h | 52 +++++++++++++++++++ 6 files changed, 97 insertions(+), 9 deletions(-) diff --git a/build/mobile/robocop/FennecNativeActions.java b/build/mobile/robocop/FennecNativeActions.java index 07fdc946101..84a44b37375 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 f3495b4cf08..f87fbdb0b3d 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/mobile/android/tests/browser/robocop/BaseTest.java b/mobile/android/tests/browser/robocop/BaseTest.java index e8fe3c4603e..af87e517863 100644 --- a/mobile/android/tests/browser/robocop/BaseTest.java +++ b/mobile/android/tests/browser/robocop/BaseTest.java @@ -959,6 +959,7 @@ abstract class BaseTest extends BaseRobocopTest { * Set the preference and wait for it to change before proceeding with the test. */ public void setPreferenceAndWaitForChange(final JSONObject jsonPref) { + blockForGeckoReady(); mActions.sendGeckoEvent("Preferences:Set", jsonPref.toString()); // Get the preference name from the json and store it in an array. This array diff --git a/widget/android/GeneratedJNINatives.h b/widget/android/GeneratedJNINatives.h index cec0a747ae0..aa9aa027ff0 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 5b5c9aa444b..d20fb670b50 100644 --- a/widget/android/GeneratedJNIWrappers.cpp +++ b/widget/android/GeneratedJNIWrappers.cpp @@ -964,6 +964,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 33517d8542b..cad7fab9c61 100644 --- a/widget/android/GeneratedJNIWrappers.h +++ b/widget/android/GeneratedJNIWrappers.h @@ -2329,6 +2329,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: