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

Unified Diff: android_webview/native/callback_jni_bridge.cc

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.cc
diff --git a/android_webview/native/callback_jni_bridge.cc b/android_webview/native/callback_jni_bridge.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ab81715fec89482e08581e09a28cd926a9f24761
--- /dev/null
+++ b/android_webview/native/callback_jni_bridge.cc
@@ -0,0 +1,81 @@
+// 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.
+
+#include "android_webview/native/callback_jni_bridge.h"
+
+#include "base/android/scoped_java_ref.h"
+#include "base/bind.h"
+#include "jni/CallbackJNIBridge_jni.h"
+
+using base::Callback;
+using base::android::AttachCurrentThread;
+using base::android::ScopedJavaLocalRef;
+using base::android::ScopedJavaGlobalRef;
+
+namespace android_webview {
+
+class JavaValueCallbackInt {
+ public:
+ JavaValueCallbackInt(JNIEnv* env,
+ const ScopedJavaLocalRef<jobject>& jvalue_callback) {
+ jvalue_callback_.Reset(env, jvalue_callback.obj());
+ }
+
+ void onReceiveValue(int value) {
+ DCHECK(!jvalue_callback_.is_null());
boliu 2013/02/21 23:11:12 Would be nice if it can automatically check thread
mkosiba (inactive) 2013/02/22 18:54:31 nope, the more auto-checking we have the better. I
+
+ if (!jvalue_callback_.is_null()) {
+ JNIEnv* env = AttachCurrentThread();
+ Java_CallbackJNIBridge_invokeValueCallbackInt(
+ env, jvalue_callback_.obj(), value);
boliu 2013/02/22 23:02:52 Also just realized, generic types must be an objec
mkosiba (inactive) 2013/02/25 23:43:43 no, you'd have to new up an Integer object if you
+ jvalue_callback_.Reset();
+ }
+ }
+
+ private:
+ ScopedJavaGlobalRef<jobject> jvalue_callback_;
+};
+
+Callback<void(int)> CallbackJNIBridge::FromJavaValueCallbackInt(
boliu 2013/02/21 23:11:12 There is no way to use templates to interpret the
mkosiba (inactive) 2013/02/22 18:54:31 I talked to Marcus and he'd prefer us to make the
boliu 2013/02/22 23:02:52 I'm gonna push back on this a bit at least for non
+ JNIEnv* env,
+ ScopedJavaLocalRef<jobject> jvalue_callback) {
+ JavaValueCallbackInt* value_callback =
+ new JavaValueCallbackInt(env, jvalue_callback);
+ return base::Bind(&JavaValueCallbackInt::onReceiveValue,
+ base::Owned(value_callback));
+}
+
+ScopedJavaLocalRef<jobject> CallbackJNIBridge::ToJavaValueCallbackInt(
+ JNIEnv* env,
+ const Callback<void(int)>& callback) {
+ Callback<void(int)>* native_callback = new Callback<void(int)>(callback);
boliu 2013/02/21 23:11:12 new vs copy by value, the native object only holds
mkosiba (inactive) 2013/02/22 18:54:31 The Callback has an internal scoped_refptr<BindSta
+ ScopedJavaLocalRef<jobject> jvalue_callback =
+ Java_CallbackJNIBridge_createValueCallbackInt(
+ env, reinterpret_cast<int>(native_callback));
+
+ return jvalue_callback;
+}
+
+// static methods -------------------------------------------------------------
+
+static void OnReceiveValueInt(JNIEnv* env,
+ jclass clazz,
+ jint callbackInt,
+ jint value) {
+ Callback<void(int)>* native_callback =
+ reinterpret_cast<Callback<void(int)>*>(callbackInt);
+ native_callback->Run(value);
+}
+
+static void DestroyInt(JNIEnv* env, jclass clazz, jint callbackInt) {
+ Callback<void(int)>* native_callback =
+ reinterpret_cast<Callback<void(int)>*>(callbackInt);
+ delete native_callback;
+}
+
+bool RegisterCallbackJNIBridge(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+} // namespace android_webview

Powered by Google App Engine
This is Rietveld 408576698