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.
This commit is contained in:
Jim Chen 2015-09-28 12:07:09 -04:00
parent e016c0a80e
commit f9d5091d8f
6 changed files with 97 additions and 9 deletions

View File

@ -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 {

View File

@ -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<PrefHandler> sCallbacks = new SparseArray<PrefHandler>();
@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;
}

View File

@ -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

View File

@ -85,6 +85,25 @@ public:
template<class Impl>
constexpr JNINativeMethod GeckoView::Window::Natives<Impl>::methods[];
template<class Impl>
class PrefsHelper::Natives : public mozilla::jni::NativeImpl<PrefsHelper, Impl>
{
public:
static constexpr JNINativeMethod methods[] = {
mozilla::jni::MakeNativeMethod<PrefsHelper::GetPrefsById_t>(
mozilla::jni::NativeStub<PrefsHelper::GetPrefsById_t, Impl>
::template Wrap<&Impl::GetPrefsById>),
mozilla::jni::MakeNativeMethod<PrefsHelper::RemovePrefsObserver_t>(
mozilla::jni::NativeStub<PrefsHelper::RemovePrefsObserver_t, Impl>
::template Wrap<&Impl::RemovePrefsObserver>)
};
};
template<class Impl>
constexpr JNINativeMethod PrefsHelper::Natives<Impl>::methods[];
template<class Impl>
class NativeJSContainer::Natives : public mozilla::jni::NativeImpl<NativeJSContainer, Impl>
{

View File

@ -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[];

View File

@ -2329,6 +2329,58 @@ public:
template<class Impl> class Natives;
};
class PrefsHelper : public mozilla::jni::Class<PrefsHelper>
{
public:
typedef mozilla::jni::Ref<PrefsHelper> Ref;
typedef mozilla::jni::LocalRef<PrefsHelper> LocalRef;
typedef mozilla::jni::GlobalRef<PrefsHelper> GlobalRef;
typedef const mozilla::jni::Param<PrefsHelper>& 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 Impl> class Natives;
};
class RestrictedProfiles : public mozilla::jni::Class<RestrictedProfiles>
{
public: