Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2323)

Unified Diff: android_webview/native/callback_jni_bridge.h

Issue 12313042: [android_webview] Add a generic callback JNI bridge. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: android_webview/native/callback_jni_bridge.h
diff --git a/android_webview/native/callback_jni_bridge.h b/android_webview/native/callback_jni_bridge.h
new file mode 100644
index 0000000000000000000000000000000000000000..c8b14dea5d754cf03b2f70e660a59dc3f696da51
--- /dev/null
+++ b/android_webview/native/callback_jni_bridge.h
@@ -0,0 +1,119 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef ANDROID_WEBVIEW_NATIVE_JAVA_CALLBACK_BRIDGE_H_
+#define ANDROID_WEBVIEW_NATIVE_JAVA_CALLBACK_BRIDGE_H_
+
+#include "base/android/jni_android.h"
+#include "base/android/scoped_java_ref.h"
+#include "base/bind.h"
+#include "base/callback.h"
+
+namespace android_webview {
+
+// This class facilitates delegating callback method invocations across the JNI
+// boundary.
+// For more details and examples please see the Java class documentation.
+class CallbackJNIBridge {
+ private:
mkosiba (inactive) 2013/02/25 23:43:44 uh.. torn between this and an 'internal' namespace
+ typedef base::android::ScopedJavaLocalRef<jobject> ScopedJobject;
+
+ template <typename TTO, typename TFROM>
+ static void CallbackConverterHelper(base::Callback<void(TTO)> callback,
+ base::Callback<TTO(TFROM)> converter,
+ TFROM value) {
+ callback.Run(converter.Run(value));
+ }
+
+ public:
+ // Creates a base::Callback the Run() method of which will delegate to the
+ // onReceiveValue method of the Java-side |jvalue_callback| object.
+ // The argument passed to the Java |jvalue_callback| onReceiveValue method
+ // will be derived by running the |converter| on the value supplied to the
+ // Run() method of the returned callback.
+ //
+ // NOTES:
+ //
+ // The returned callback will hold a global reference to the Java object, so
+ // the return value should not be stored for as short a time as possible.
+ //
+ // The |converter| callback's .Run method will be invoked at the same time as
+ // the returned callback's .Run method.
+ template <typename N, typename J>
mkosiba (inactive) 2013/02/25 23:43:44 as you'll see in the unittest code converting to a
joth 2013/02/26 00:13:45 Name N and J something more descriptive like above
+ static base::Callback<void(N)> FromJavaCallback(
+ JNIEnv* env,
+ const ScopedJobject& jvalue_callback,
+ base::Callback<base::android::ScopedJavaLocalRef<J>(N)> converter) {
+ return base::Bind(&CallbackJNIBridge::CallbackConverterHelper<
+ base::android::ScopedJavaLocalRef<J>,
+ N>,
+ FromJavaCallbackObj(env, jvalue_callback),
+ converter);
+ }
+
+ // Specializations for basic JNI types.
+ static base::Callback<void(int)> FromJavaCallbackInt(
+ JNIEnv* env,
+ const ScopedJobject& jvalue_callback);
+ static base::Callback<void(long)> FromJavaCallbackLong(
+ JNIEnv* env,
+ const ScopedJobject& jvalue_callback);
+ static base::Callback<void(bool)> FromJavaCallbackBool(
+ JNIEnv* env,
+ const ScopedJobject& jvalue_callback);
+ static base::Callback<void(ScopedJobject)> FromJavaCallbackObj(
+ JNIEnv* env,
+ const ScopedJobject& jvalue_callback);
+
+ // Creates a Java object (an instance that implements the ValueCallback
+ // interface) the onReceiveValue method of which will delegate to the
+ // supplied |callback|. The input value provided to |callback|.Run is derived
+ // by applying |covnerter| to the Java object.
+ //
+ // NOTES:
+ //
+ // The |callback| will normally be deleted after onReceiveValue is called
+ // Java-side meaning that the returned ValueCallback is "one-shot".
+ //
+ // If the Java-side callback's onReceiveValue method is never invoked
+ // and the Java-side callback is GC'd |callback| will be deleted as a
+ // consequence of that.
+ template <typename N>
+ static ScopedJobject ToJavaCallback(
+ JNIEnv* env,
+ const base::Callback<void(N)>& callback,
+ base::Callback<N(ScopedJobject)> converter) {
mkosiba (inactive) 2013/02/25 23:43:44 now this is going to be fun - since we always get
+ return ToJavaCallbackObj(
+ env,
+ base::Bind(&CallbackJNIBridge::CallbackConverterHelper<N,
+ ScopedJobject>,
+ callback,
+ converter));
+ }
+
+ // Specializations for basic JNI types.
+ static ScopedJobject ToJavaCallbackInt(
+ JNIEnv* env,
+ const base::Callback<void(int)>& callback);
+ static ScopedJobject ToJavaCallbackLong(
+ JNIEnv* env,
+ const base::Callback<void(long)>& callback);
+ static ScopedJobject ToJavaCallbackBool(
+ JNIEnv* env,
+ const base::Callback<void(bool)>& callback);
+ static ScopedJobject ToJavaCallbackObj(
+ JNIEnv* env,
+ const base::Callback<void(ScopedJobject)>& callback);
+
+ private:
+ CallbackJNIBridge();
+
+ DISALLOW_COPY_AND_ASSIGN(CallbackJNIBridge);
+};
+
+bool RegisterCallbackJNIBridge(JNIEnv* env);
+
+} // namespace android_webview
+
+#endif // ANDROID_WEBVIEW_NATIVE_JAVA_CALLBACK_BRIDGE_H_

Powered by Google App Engine
This is Rietveld 408576698