Bug 1382861 - 1. Replace jni::AbstractCall with nsIRunnable; r=esawin

The native binding code used `jni::AbstractCall` as the interface
between `ProxyNativeCall` and `DispatchToGeckoPriorityQueue`. However,
we already make use of `nsIRunnable` for dispatching to the XPCOM queue,
so we should just use `nsIRunnable` for the priority queue as well.

MozReview-Commit-ID: KmuNMZZkXX3
This commit is contained in:
Jim Chen 2017-07-25 17:25:58 -04:00
parent ffa6f2f3c0
commit 454e868137
3 changed files with 23 additions and 30 deletions

View File

@ -352,7 +352,7 @@ template<class C> struct ProxyArg<LocalRef<C>> : ProxyArg<typename C::Ref> {};
template<class Impl, class Owner, bool IsStatic,
bool HasThisArg /* has instance/class local ref in the call */,
typename... Args>
class ProxyNativeCall : public AbstractCall
class ProxyNativeCall
{
// "this arg" refers to the Class::LocalRef (for static methods) or
// Owner::LocalRef (for instance methods) that we optionally (as indicated
@ -475,7 +475,7 @@ public:
void SetTarget(NativeCallType call) { mNativeCall = call; }
template<typename T> void SetTarget(T&&) const { MOZ_CRASH(); }
void operator()() override
void operator()()
{
JNIEnv* const env = GetEnvForThread();
typename ThisArgClass::LocalRef thisArg(env, mThisArg);
@ -514,10 +514,12 @@ struct Dispatcher
// For a static method, do not forward the "this arg" (i.e. the class
// local ref) if the implementation does not request it. This saves us
// a pair of calls to add/delete global ref.
DispatchToGeckoPriorityQueue(MakeUnique<ProxyNativeCall<
Impl, typename Traits::Owner, IsStatic, HasThisArg,
Args...>>(HasThisArg || !IsStatic ? thisArg : nullptr,
Forward<ProxyArgs>(args)...));
auto proxy = ProxyNativeCall<Impl, typename Traits::Owner, IsStatic,
HasThisArg, Args...>(
(HasThisArg || !IsStatic) ? thisArg : nullptr,
Forward<ProxyArgs>(args)...);
DispatchToGeckoPriorityQueue(
NS_NewRunnableFunction("PriorityNativeCall", Move(proxy)));
}
template<class Traits, bool IsStatic = Traits::isStatic,
@ -529,10 +531,12 @@ struct Dispatcher
// For a static method, do not forward the "this arg" (i.e. the class
// local ref) if the implementation does not request it. This saves us
// a pair of calls to add/delete global ref.
NS_DispatchToMainThread(NS_NewRunnableFunction("ProxyNativeCall", ProxyNativeCall<
Impl, typename Traits::Owner, IsStatic, HasThisArg,
Args...>(HasThisArg || !IsStatic ? thisArg : nullptr,
Forward<ProxyArgs>(args)...)));
auto proxy = ProxyNativeCall<Impl, typename Traits::Owner, IsStatic,
HasThisArg, Args...>(
(HasThisArg || !IsStatic) ? thisArg : nullptr,
Forward<ProxyArgs>(args)...);
NS_DispatchToMainThread(
NS_NewRunnableFunction("GeckoNativeCall", Move(proxy)));
}
template<class Traits, bool IsStatic = false, typename... ProxyArgs>

View File

@ -288,24 +288,17 @@ jclass GetClassRef(JNIEnv* aEnv, const char* aClassName)
return nullptr;
}
void DispatchToGeckoPriorityQueue(UniquePtr<AbstractCall>&& aCall)
void DispatchToGeckoPriorityQueue(already_AddRefed<nsIRunnable> aCall)
{
class AbstractCallEvent : public nsAppShell::Event
class RunnableEvent : public nsAppShell::Event
{
UniquePtr<AbstractCall> mCall;
nsCOMPtr<nsIRunnable> mCall;
public:
AbstractCallEvent(UniquePtr<AbstractCall>&& aCall)
: mCall(Move(aCall))
{}
void Run() override
{
(*mCall)();
}
RunnableEvent(already_AddRefed<nsIRunnable> aCall) : mCall(aCall) {}
void Run() override { NS_ENSURE_SUCCESS_VOID(mCall->Run()); }
};
nsAppShell::PostEvent(MakeUnique<AbstractCallEvent>(Move(aCall)));
nsAppShell::PostEvent(MakeUnique<RunnableEvent>(Move(aCall)));
}
bool IsFennec()

View File

@ -3,6 +3,8 @@
#include <jni.h>
#include "nsIRunnable.h"
#include "mozilla/UniquePtr.h"
#if defined(DEBUG) || !defined(RELEASE_OR_BETA)
@ -132,13 +134,7 @@ void SetNativeHandle(JNIEnv* env, jobject instance, uintptr_t handle);
jclass GetClassRef(JNIEnv* aEnv, const char* aClassName);
struct AbstractCall
{
virtual ~AbstractCall() {}
virtual void operator()() = 0;
};
void DispatchToGeckoPriorityQueue(UniquePtr<AbstractCall>&& aCall);
void DispatchToGeckoPriorityQueue(already_AddRefed<nsIRunnable> aCall);
/**
* Returns whether Gecko is running in a Fennec environment, as determined by