OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_JAVA_GIN_JAVA_METHOD_INVOCATION_HELPER_H_ |
| 6 #define CONTENT_BROWSER_RENDERER_HOST_JAVA_GIN_JAVA_METHOD_INVOCATION_HELPER_H_ |
| 7 |
| 8 #include <map> |
| 9 |
| 10 #include "base/android/jni_weak_ref.h" |
| 11 #include "base/android/scoped_java_ref.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/values.h" |
| 14 #include "content/browser/renderer_host/java/gin_java_bound_object.h" |
| 15 #include "content/browser/renderer_host/java/java_type.h" |
| 16 #include "content/common/content_export.h" |
| 17 |
| 18 namespace content { |
| 19 |
| 20 class JavaMethod; |
| 21 |
| 22 // Instances of this class are created and initialized on the UI thread, do |
| 23 // their work on the background thread, and then again examined on the UI |
| 24 // thread. They don't work on both threads simultaneously, though. |
| 25 class CONTENT_EXPORT GinJavaMethodInvocationHelper |
| 26 : public base::RefCountedThreadSafe<GinJavaMethodInvocationHelper> { |
| 27 public: |
| 28 // DispatcherDelegate is used on the UI thread |
| 29 class DispatcherDelegate { |
| 30 public: |
| 31 DispatcherDelegate() {} |
| 32 virtual ~DispatcherDelegate() {} |
| 33 virtual JavaObjectWeakGlobalRef GetObjectWeakRef( |
| 34 GinJavaBoundObject::ObjectID object_id) = 0; |
| 35 |
| 36 private: |
| 37 DISALLOW_COPY_AND_ASSIGN(DispatcherDelegate); |
| 38 }; |
| 39 |
| 40 // ObjectDelegate is used in the background thread |
| 41 class ObjectDelegate { |
| 42 public: |
| 43 ObjectDelegate() {} |
| 44 virtual ~ObjectDelegate() {} |
| 45 virtual base::android::ScopedJavaLocalRef<jobject> GetLocalRef( |
| 46 JNIEnv* env) = 0; |
| 47 virtual const JavaMethod* FindMethod(const std::string& method_name, |
| 48 size_t num_parameters) = 0; |
| 49 virtual bool IsObjectGetClassMethod(const JavaMethod* method) = 0; |
| 50 virtual const base::android::JavaRef<jclass>& GetSafeAnnotationClass() = 0; |
| 51 |
| 52 private: |
| 53 DISALLOW_COPY_AND_ASSIGN(ObjectDelegate); |
| 54 }; |
| 55 |
| 56 GinJavaMethodInvocationHelper(scoped_ptr<ObjectDelegate> object, |
| 57 const std::string& method_name, |
| 58 const base::ListValue& arguments); |
| 59 void Init(DispatcherDelegate* dispatcher); |
| 60 |
| 61 // Called on the background thread |
| 62 void Invoke(); |
| 63 |
| 64 // Called on the UI thread |
| 65 bool HoldsPrimitiveResult(); |
| 66 const base::ListValue& GetPrimitiveResult(); |
| 67 const base::android::JavaRef<jobject>& GetObjectResult(); |
| 68 const base::android::JavaRef<jclass>& GetSafeAnnotationClass(); |
| 69 const std::string& GetErrorMessage(); |
| 70 |
| 71 private: |
| 72 friend class base::RefCountedThreadSafe<GinJavaMethodInvocationHelper>; |
| 73 ~GinJavaMethodInvocationHelper(); |
| 74 |
| 75 // Called on the UI thread |
| 76 void BuildObjectRefsFromListValue(DispatcherDelegate* dispatcher, |
| 77 const base::Value* list_value); |
| 78 void BuildObjectRefsFromDictionaryValue(DispatcherDelegate* dispatcher, |
| 79 const base::Value* dict_value); |
| 80 |
| 81 bool AppendObjectRef(DispatcherDelegate* dispatcher, |
| 82 const base::Value* raw_value); |
| 83 |
| 84 // Called on the background thread. |
| 85 void InvokeMethod(jobject object, |
| 86 const JavaType& return_type, |
| 87 jmethodID id, |
| 88 jvalue* parameters); |
| 89 void SetInvocationFailure(const char* error_message); |
| 90 void SetPrimitiveResult(const base::ListValue& result_wrapper); |
| 91 void SetObjectResult( |
| 92 const base::android::JavaRef<jobject>& object, |
| 93 const base::android::JavaRef<jclass>& safe_annotation_clazz); |
| 94 |
| 95 typedef std::map<GinJavaBoundObject::ObjectID, |
| 96 JavaObjectWeakGlobalRef> ObjectRefs; |
| 97 |
| 98 scoped_ptr<ObjectDelegate> object_; |
| 99 const std::string method_name_; |
| 100 scoped_ptr<base::ListValue> arguments_; |
| 101 ObjectRefs object_refs_; |
| 102 bool holds_primitive_result_; |
| 103 scoped_ptr<base::ListValue> primitive_result_; |
| 104 std::string error_message_; |
| 105 base::android::ScopedJavaGlobalRef<jobject> object_result_; |
| 106 base::android::ScopedJavaGlobalRef<jclass> safe_annotation_clazz_; |
| 107 |
| 108 DISALLOW_COPY_AND_ASSIGN(GinJavaMethodInvocationHelper); |
| 109 }; |
| 110 |
| 111 } // namespace content |
| 112 |
| 113 #endif // CONTENT_BROWSER_RENDERER_HOST_JAVA_GIN_JAVA_METHOD_INVOCATION_HELPER_
H_ |
OLD | NEW |