OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef ANDROID_WEBVIEW_NATIVE_JAVA_CALLBACK_BRIDGE_H_ | |
6 #define ANDROID_WEBVIEW_NATIVE_JAVA_CALLBACK_BRIDGE_H_ | |
7 | |
8 #include "base/android/jni_android.h" | |
9 #include "base/android/scoped_java_ref.h" | |
10 #include "base/bind.h" | |
11 #include "base/callback.h" | |
12 | |
13 namespace android_webview { | |
14 | |
15 // This class facilitates delegating callback method invocations across the JNI | |
16 // boundary. | |
17 // For more details and examples please see the Java class documentation. | |
18 class CallbackJNIBridge { | |
19 private: | |
mkosiba (inactive)
2013/02/25 23:43:44
uh.. torn between this and an 'internal' namespace
| |
20 typedef base::android::ScopedJavaLocalRef<jobject> ScopedJobject; | |
21 | |
22 template <typename TTO, typename TFROM> | |
23 static void CallbackConverterHelper(base::Callback<void(TTO)> callback, | |
24 base::Callback<TTO(TFROM)> converter, | |
25 TFROM value) { | |
26 callback.Run(converter.Run(value)); | |
27 } | |
28 | |
29 public: | |
30 // Creates a base::Callback the Run() method of which will delegate to the | |
31 // onReceiveValue method of the Java-side |jvalue_callback| object. | |
32 // The argument passed to the Java |jvalue_callback| onReceiveValue method | |
33 // will be derived by running the |converter| on the value supplied to the | |
34 // Run() method of the returned callback. | |
35 // | |
36 // NOTES: | |
37 // | |
38 // The returned callback will hold a global reference to the Java object, so | |
39 // the return value should not be stored for as short a time as possible. | |
40 // | |
41 // The |converter| callback's .Run method will be invoked at the same time as | |
42 // the returned callback's .Run method. | |
43 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
| |
44 static base::Callback<void(N)> FromJavaCallback( | |
45 JNIEnv* env, | |
46 const ScopedJobject& jvalue_callback, | |
47 base::Callback<base::android::ScopedJavaLocalRef<J>(N)> converter) { | |
48 return base::Bind(&CallbackJNIBridge::CallbackConverterHelper< | |
49 base::android::ScopedJavaLocalRef<J>, | |
50 N>, | |
51 FromJavaCallbackObj(env, jvalue_callback), | |
52 converter); | |
53 } | |
54 | |
55 // Specializations for basic JNI types. | |
56 static base::Callback<void(int)> FromJavaCallbackInt( | |
57 JNIEnv* env, | |
58 const ScopedJobject& jvalue_callback); | |
59 static base::Callback<void(long)> FromJavaCallbackLong( | |
60 JNIEnv* env, | |
61 const ScopedJobject& jvalue_callback); | |
62 static base::Callback<void(bool)> FromJavaCallbackBool( | |
63 JNIEnv* env, | |
64 const ScopedJobject& jvalue_callback); | |
65 static base::Callback<void(ScopedJobject)> FromJavaCallbackObj( | |
66 JNIEnv* env, | |
67 const ScopedJobject& jvalue_callback); | |
68 | |
69 // Creates a Java object (an instance that implements the ValueCallback | |
70 // interface) the onReceiveValue method of which will delegate to the | |
71 // supplied |callback|. The input value provided to |callback|.Run is derived | |
72 // by applying |covnerter| to the Java object. | |
73 // | |
74 // NOTES: | |
75 // | |
76 // The |callback| will normally be deleted after onReceiveValue is called | |
77 // Java-side meaning that the returned ValueCallback is "one-shot". | |
78 // | |
79 // If the Java-side callback's onReceiveValue method is never invoked | |
80 // and the Java-side callback is GC'd |callback| will be deleted as a | |
81 // consequence of that. | |
82 template <typename N> | |
83 static ScopedJobject ToJavaCallback( | |
84 JNIEnv* env, | |
85 const base::Callback<void(N)>& callback, | |
86 base::Callback<N(ScopedJobject)> converter) { | |
mkosiba (inactive)
2013/02/25 23:43:44
now this is going to be fun - since we always get
| |
87 return ToJavaCallbackObj( | |
88 env, | |
89 base::Bind(&CallbackJNIBridge::CallbackConverterHelper<N, | |
90 ScopedJobject>, | |
91 callback, | |
92 converter)); | |
93 } | |
94 | |
95 // Specializations for basic JNI types. | |
96 static ScopedJobject ToJavaCallbackInt( | |
97 JNIEnv* env, | |
98 const base::Callback<void(int)>& callback); | |
99 static ScopedJobject ToJavaCallbackLong( | |
100 JNIEnv* env, | |
101 const base::Callback<void(long)>& callback); | |
102 static ScopedJobject ToJavaCallbackBool( | |
103 JNIEnv* env, | |
104 const base::Callback<void(bool)>& callback); | |
105 static ScopedJobject ToJavaCallbackObj( | |
106 JNIEnv* env, | |
107 const base::Callback<void(ScopedJobject)>& callback); | |
108 | |
109 private: | |
110 CallbackJNIBridge(); | |
111 | |
112 DISALLOW_COPY_AND_ASSIGN(CallbackJNIBridge); | |
113 }; | |
114 | |
115 bool RegisterCallbackJNIBridge(JNIEnv* env); | |
116 | |
117 } // namespace android_webview | |
118 | |
119 #endif // ANDROID_WEBVIEW_NATIVE_JAVA_CALLBACK_BRIDGE_H_ | |
OLD | NEW |