| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_METHOD_INVOCATION_HELPER_H_ | 5 #ifndef CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_METHOD_INVOCATION_HELPER_H_ |
| 6 #define CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_METHOD_INVOCATION_HELPER_H_ | 6 #define CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_METHOD_INVOCATION_HELPER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/android/jni_weak_ref.h" | 10 #include "base/android/jni_weak_ref.h" |
| 11 #include "base/android/scoped_java_ref.h" | 11 #include "base/android/scoped_java_ref.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "content/browser/android/java/gin_java_bound_object.h" | 14 #include "content/browser/android/java/gin_java_bound_object.h" |
| 15 #include "content/browser/android/java/java_type.h" | 15 #include "content/browser/android/java/java_type.h" |
| 16 #include "content/common/android/gin_java_bridge_errors.h" | 16 #include "content/common/android/gin_java_bridge_errors.h" |
| 17 #include "content/common/content_export.h" | 17 #include "content/common/content_export.h" |
| 18 | 18 |
| 19 namespace content { | 19 namespace content { |
| 20 | 20 |
| 21 class JavaMethod; | 21 class JavaMethod; |
| 22 | 22 |
| 23 // Instances of this class are created and initialized on the UI thread, do | 23 // Instances of this class are created and used on the background thread. |
| 24 // their work on the background thread, and then again examined on the UI | |
| 25 // thread. They don't work on both threads simultaneously, though. | |
| 26 class CONTENT_EXPORT GinJavaMethodInvocationHelper | 24 class CONTENT_EXPORT GinJavaMethodInvocationHelper |
| 27 : public base::RefCountedThreadSafe<GinJavaMethodInvocationHelper> { | 25 : public base::RefCountedThreadSafe<GinJavaMethodInvocationHelper> { |
| 28 public: | 26 public: |
| 29 // DispatcherDelegate is used on the UI thread | |
| 30 class DispatcherDelegate { | 27 class DispatcherDelegate { |
| 31 public: | 28 public: |
| 32 DispatcherDelegate() {} | 29 DispatcherDelegate() {} |
| 33 virtual ~DispatcherDelegate() {} | 30 virtual ~DispatcherDelegate() {} |
| 34 virtual JavaObjectWeakGlobalRef GetObjectWeakRef( | 31 virtual JavaObjectWeakGlobalRef GetObjectWeakRef( |
| 35 GinJavaBoundObject::ObjectID object_id) = 0; | 32 GinJavaBoundObject::ObjectID object_id) = 0; |
| 36 | 33 |
| 37 private: | 34 private: |
| 38 DISALLOW_COPY_AND_ASSIGN(DispatcherDelegate); | 35 DISALLOW_COPY_AND_ASSIGN(DispatcherDelegate); |
| 39 }; | 36 }; |
| 40 | 37 |
| 41 // ObjectDelegate is used in the background thread | |
| 42 class ObjectDelegate { | 38 class ObjectDelegate { |
| 43 public: | 39 public: |
| 44 ObjectDelegate() {} | 40 ObjectDelegate() {} |
| 45 virtual ~ObjectDelegate() {} | 41 virtual ~ObjectDelegate() {} |
| 46 virtual base::android::ScopedJavaLocalRef<jobject> GetLocalRef( | 42 virtual base::android::ScopedJavaLocalRef<jobject> GetLocalRef( |
| 47 JNIEnv* env) = 0; | 43 JNIEnv* env) = 0; |
| 48 virtual base::android::ScopedJavaLocalRef<jclass> GetLocalClassRef( | 44 virtual base::android::ScopedJavaLocalRef<jclass> GetLocalClassRef( |
| 49 JNIEnv* env) = 0; | 45 JNIEnv* env) = 0; |
| 50 virtual const JavaMethod* FindMethod(const std::string& method_name, | 46 virtual const JavaMethod* FindMethod(const std::string& method_name, |
| 51 size_t num_parameters) = 0; | 47 size_t num_parameters) = 0; |
| 52 virtual bool IsObjectGetClassMethod(const JavaMethod* method) = 0; | 48 virtual bool IsObjectGetClassMethod(const JavaMethod* method) = 0; |
| 53 virtual const base::android::JavaRef<jclass>& GetSafeAnnotationClass() = 0; | 49 virtual const base::android::JavaRef<jclass>& GetSafeAnnotationClass() = 0; |
| 54 | 50 |
| 55 private: | 51 private: |
| 56 DISALLOW_COPY_AND_ASSIGN(ObjectDelegate); | 52 DISALLOW_COPY_AND_ASSIGN(ObjectDelegate); |
| 57 }; | 53 }; |
| 58 | 54 |
| 59 GinJavaMethodInvocationHelper(scoped_ptr<ObjectDelegate> object, | 55 GinJavaMethodInvocationHelper(scoped_ptr<ObjectDelegate> object, |
| 60 const std::string& method_name, | 56 const std::string& method_name, |
| 61 const base::ListValue& arguments); | 57 const base::ListValue& arguments); |
| 62 void Init(DispatcherDelegate* dispatcher); | 58 void Init(DispatcherDelegate* dispatcher); |
| 63 | 59 |
| 64 // Called on the background thread | |
| 65 void Invoke(); | 60 void Invoke(); |
| 66 | 61 |
| 67 // Called on the UI thread | |
| 68 bool HoldsPrimitiveResult(); | 62 bool HoldsPrimitiveResult(); |
| 69 const base::ListValue& GetPrimitiveResult(); | 63 const base::ListValue& GetPrimitiveResult(); |
| 70 const base::android::JavaRef<jobject>& GetObjectResult(); | 64 const base::android::JavaRef<jobject>& GetObjectResult(); |
| 71 const base::android::JavaRef<jclass>& GetSafeAnnotationClass(); | 65 const base::android::JavaRef<jclass>& GetSafeAnnotationClass(); |
| 72 const GinJavaBridgeError GetInvocationError(); | 66 const GinJavaBridgeError GetInvocationError(); |
| 73 | 67 |
| 74 private: | 68 private: |
| 75 friend class base::RefCountedThreadSafe<GinJavaMethodInvocationHelper>; | 69 friend class base::RefCountedThreadSafe<GinJavaMethodInvocationHelper>; |
| 76 ~GinJavaMethodInvocationHelper(); | 70 ~GinJavaMethodInvocationHelper(); |
| 77 | 71 |
| 78 // Called on the UI thread | |
| 79 void BuildObjectRefsFromListValue(DispatcherDelegate* dispatcher, | 72 void BuildObjectRefsFromListValue(DispatcherDelegate* dispatcher, |
| 80 const base::Value* list_value); | 73 const base::Value* list_value); |
| 81 void BuildObjectRefsFromDictionaryValue(DispatcherDelegate* dispatcher, | 74 void BuildObjectRefsFromDictionaryValue(DispatcherDelegate* dispatcher, |
| 82 const base::Value* dict_value); | 75 const base::Value* dict_value); |
| 83 | 76 |
| 84 bool AppendObjectRef(DispatcherDelegate* dispatcher, | 77 bool AppendObjectRef(DispatcherDelegate* dispatcher, |
| 85 const base::Value* raw_value); | 78 const base::Value* raw_value); |
| 86 | 79 |
| 87 // Called on the background thread. | |
| 88 void InvokeMethod(jobject object, | 80 void InvokeMethod(jobject object, |
| 89 jclass clazz, | 81 jclass clazz, |
| 90 const JavaType& return_type, | 82 const JavaType& return_type, |
| 91 jmethodID id, | 83 jmethodID id, |
| 92 jvalue* parameters); | 84 jvalue* parameters); |
| 93 void SetInvocationError(GinJavaBridgeError error); | 85 void SetInvocationError(GinJavaBridgeError error); |
| 94 void SetPrimitiveResult(const base::ListValue& result_wrapper); | 86 void SetPrimitiveResult(const base::ListValue& result_wrapper); |
| 95 void SetObjectResult( | 87 void SetObjectResult( |
| 96 const base::android::JavaRef<jobject>& object, | 88 const base::android::JavaRef<jobject>& object, |
| 97 const base::android::JavaRef<jclass>& safe_annotation_clazz); | 89 const base::android::JavaRef<jclass>& safe_annotation_clazz); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 108 GinJavaBridgeError invocation_error_; | 100 GinJavaBridgeError invocation_error_; |
| 109 base::android::ScopedJavaGlobalRef<jobject> object_result_; | 101 base::android::ScopedJavaGlobalRef<jobject> object_result_; |
| 110 base::android::ScopedJavaGlobalRef<jclass> safe_annotation_clazz_; | 102 base::android::ScopedJavaGlobalRef<jclass> safe_annotation_clazz_; |
| 111 | 103 |
| 112 DISALLOW_COPY_AND_ASSIGN(GinJavaMethodInvocationHelper); | 104 DISALLOW_COPY_AND_ASSIGN(GinJavaMethodInvocationHelper); |
| 113 }; | 105 }; |
| 114 | 106 |
| 115 } // namespace content | 107 } // namespace content |
| 116 | 108 |
| 117 #endif // CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_METHOD_INVOCATION_HELPER_H_ | 109 #endif // CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_METHOD_INVOCATION_HELPER_H_ |
| OLD | NEW |