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

Unified Diff: android_webview/java/src/org/chromium/android_webview/CallbackJNIBridge.java

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/java/src/org/chromium/android_webview/CallbackJNIBridge.java
diff --git a/android_webview/java/src/org/chromium/android_webview/CallbackJNIBridge.java b/android_webview/java/src/org/chromium/android_webview/CallbackJNIBridge.java
new file mode 100644
index 0000000000000000000000000000000000000000..f7827509dd9364d27fadccdd20b14a3bb2efd596
--- /dev/null
+++ b/android_webview/java/src/org/chromium/android_webview/CallbackJNIBridge.java
@@ -0,0 +1,84 @@
+// Copyright (c) 2012 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.
+
+package org.chromium.android_webview;
+
+import android.webkit.ValueCallback;
+
+import org.chromium.base.CalledByNative;
+import org.chromium.base.JNINamespace;
+
+/*
+ * Use case A:
+ *
+ * in Java:
+ *
+ * doSomethingInCpp(new ValueCallback<Integer>() {
+ * @Override
+ * public void onReceiveValue(int value) {
+ * ..stuff..
+ * }
+ * });
+ *
+ * in C++:
+ * void object::doSomethingInCpp(jobject intCallback) {
+ * base::Callback<int> callback = CallbackJNIBridge::FromJavaValueCallbackInt(intCallback);
+ * callback.Run(1) ----> anon_object.onReceiveValue(1); gets called Java-side
+ *
+ * ------------------------------------------------------------------------------------------------
+ * Use case B:
+ *
+ * in C++
+ * android::ScopedLocalJavaRef jcallback = JavaIntCallback(object, &Object::onIntValue);
mkosiba (inactive) 2013/02/21 19:28:24 uh.. didn't update this - it should be ... = Cal
+ * doSomethingInJava(jcallback.obj());
+ *
+ * in Java:
+ * private void doSomethingInJava(ValueCallback<Int> vc) {
+ * int value;
+ * ...
+ * value = 3;
+ * ...
+ * vc.onReceiveValue(value); // --> calls object->onIntValue(3);
+ * }
+ *
+ */
+
+@JNINamespace("android_webview")
+class CallbackJNIBridge {
+ @SuppressWarnings("unchecked")
mkosiba (inactive) 2013/02/21 19:26:42 I'll probably want to go the direction of having s
+ @CalledByNative
+ public static void invokeValueCallbackInt(ValueCallback callback, int value) {
+ callback.onReceiveValue(value);
+ }
+
+ @CalledByNative
+ public static ValueCallback createValueCallbackInt(final int nativeCallback) {
+ return new ValueCallback<Integer>() {
+ private int mNativeCallback = nativeCallback;
+
+ @Override
+ public void onReceiveValue(Integer value) {
+ assert mNativeCallback != 0;
+ if (mNativeCallback != 0) {
+ nativeOnReceiveValueInt(mNativeCallback, value);
+ clear();
mkosiba (inactive) 2013/02/21 19:26:42 the idea here is that these are one-shot callbacks
joth 2013/02/21 23:30:47 THe other pattern oft used is to couple the native
mkosiba (inactive) 2013/02/22 18:54:31 I was considering whether I should make the Callba
+ }
+ }
+
+ @Override
+ protected void finalize() throws Throwable {
joth 2013/02/21 23:30:47 use CleanupReference rather than finalizer
mkosiba (inactive) 2013/02/22 18:54:31 ok
+ clear();
+ }
+
+ private void clear() {
+ if (mNativeCallback != 0) {
+ nativeDestroyInt(mNativeCallback);
+ }
+ }
+ };
+ }
+
+ private static native void nativeOnReceiveValueInt(int callbackInt, int value);
+ private static native void nativeDestroyInt(int callbackInt);
+}

Powered by Google App Engine
This is Rietveld 408576698