Chromium Code Reviews| 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 "base/android/jni_weak_ref.h" | |
| 9 #include "base/android/scoped_java_ref.h" | |
| 10 #include "base/memory/ref_counted.h" | |
| 11 #include "base/values.h" | |
| 12 #include "content/browser/renderer_host/java/gin_java_bound_object.h" | |
| 13 #include "content/browser/renderer_host/java/java_type.h" | |
| 14 #include "content/common/content_export.h" | |
| 15 | |
| 16 namespace content { | |
| 17 | |
| 18 class JavaMethod; | |
| 19 | |
| 20 // Instances of this class are created and initialized on the UI thread, do | |
| 21 // their work on the background thread, and then again examined on the UI | |
| 22 // thread. They don't work on both threads simultaneously, though. | |
| 23 class CONTENT_EXPORT GinJavaMethodInvocationHelper | |
| 24 : public base::RefCountedThreadSafe<GinJavaMethodInvocationHelper> { | |
| 25 public: | |
| 26 // DispatcherDelegate is used on the UI thread | |
| 27 class DispatcherDelegate { | |
| 28 public: | |
| 29 virtual ~DispatcherDelegate() {} | |
| 30 virtual JavaObjectWeakGlobalRef GetObjectWeakRef( | |
| 31 GinJavaBoundObject::ObjectID object_id) = 0; | |
|
bulach
2014/06/09 11:44:33
nit: private: DISALLOW_COPY_AND_ASSIGN?
mnaganov (inactive)
2014/06/11 13:50:44
A good idea! Done.
| |
| 32 }; | |
| 33 | |
| 34 // ObjectDelegate is used in the background thread | |
| 35 class ObjectDelegate { | |
| 36 public: | |
| 37 virtual ~ObjectDelegate() {} | |
| 38 virtual base::android::ScopedJavaLocalRef<jobject> GetLocalRef( | |
| 39 JNIEnv* env) = 0; | |
| 40 virtual const JavaMethod* FindMethod(const std::string& method_name, | |
| 41 size_t num_parameters) = 0; | |
| 42 virtual bool IsObjectGetClassMethod(const JavaMethod* method) = 0; | |
| 43 virtual const base::android::JavaRef<jclass>& GetSafeAnnotationClass() = 0; | |
|
bulach
2014/06/09 11:44:33
nit: I think we normally return by value rather th
mnaganov (inactive)
2014/06/11 13:50:44
It's not possible to return a JavaRef by value bec
| |
| 44 }; | |
|
bulach
2014/06/09 11:44:33
nit: private DISALLOW_COPY_AND_ASSIGN?
mnaganov (inactive)
2014/06/11 13:50:44
Done.
| |
| 45 | |
| 46 GinJavaMethodInvocationHelper(scoped_ptr<ObjectDelegate> object, | |
| 47 const std::string& method_name, | |
| 48 const base::ListValue& arguments); | |
| 49 void Init(DispatcherDelegate* dispatcher); | |
| 50 | |
| 51 // Called on the background thread | |
| 52 void Invoke(); | |
| 53 | |
| 54 // Called on the UI thread | |
| 55 bool HoldsPrimitiveResult(); | |
| 56 const base::ListValue& GetPrimitiveResult(); | |
| 57 const base::android::JavaRef<jobject>& GetObjectResult(); | |
| 58 const base::android::JavaRef<jclass>& GetSafeAnnotationClass(); | |
| 59 const std::string& GetErrorMessage(); | |
| 60 | |
| 61 private: | |
| 62 friend class base::RefCountedThreadSafe<GinJavaMethodInvocationHelper>; | |
| 63 ~GinJavaMethodInvocationHelper(); | |
| 64 | |
| 65 // Called on the UI thread | |
| 66 void BuildObjectRefsFromListValue(DispatcherDelegate* dispatcher, | |
| 67 const base::Value* list_value); | |
| 68 void BuildObjectRefsFromDictionaryValue(DispatcherDelegate* dispatcher, | |
| 69 const base::Value* dict_value); | |
| 70 bool AppendObjectRef(DispatcherDelegate* dispatcher, | |
| 71 const base::Value* raw_value); | |
| 72 | |
| 73 // Called on the background thread. | |
| 74 jvalue CoerceJavaScriptValueToJavaValue(const base::Value* value, | |
| 75 const JavaType& target_type, | |
| 76 bool coerce_to_string); | |
| 77 jvalue CoerceGinJavaBridgeValueToJavaValue(const base::Value* value, | |
| 78 const JavaType& target_type, | |
| 79 bool coerce_to_string); | |
| 80 jvalue CoerceJavaScriptIntegerToJavaValue(const base::Value* value, | |
| 81 const JavaType& target_type, | |
| 82 bool coerce_to_string); | |
| 83 jvalue CoerceJavaScriptDoubleToJavaValue(double double_value, | |
| 84 const JavaType& target_type, | |
| 85 bool coerce_to_string); | |
| 86 jvalue CoerceJavaScriptBooleanToJavaValue(const base::Value* value, | |
| 87 const JavaType& target_type, | |
| 88 bool coerce_to_string); | |
| 89 jvalue CoerceJavaScriptStringToJavaValue(const base::Value* value, | |
| 90 const JavaType& target_type); | |
| 91 jvalue CoerceJavaScriptObjectToJavaValue(const base::Value* value, | |
| 92 const JavaType& target_type, | |
| 93 bool coerce_to_string); | |
| 94 jobject CoerceJavaScriptListToArray(const base::Value* value, | |
| 95 const JavaType& target_type); | |
| 96 jobject CoerceJavaScriptDictionaryToArray( | |
| 97 const base::Value* value, | |
| 98 const JavaType& target_type); | |
| 99 jvalue CoerceJavaScriptNullOrUndefinedToJavaValue(const base::Value* value, | |
| 100 const JavaType& target_type, | |
| 101 bool coerce_to_string); | |
| 102 jobject CreateJavaArray(const JavaType& type, jsize length); | |
|
bulach
2014/06/09 11:44:34
question: could all these Coerce* methods go into
mnaganov (inactive)
2014/06/11 13:50:44
This makes sense, extracted.
We don't add any new
| |
| 103 void InvokeMethod(jobject object, | |
| 104 const JavaType& return_type, | |
| 105 jmethodID id, | |
| 106 jvalue* parameters); | |
| 107 void ReleaseJavaValueIfRequired(JNIEnv* env, | |
| 108 jvalue* value, | |
| 109 const JavaType& type); | |
| 110 void SetArrayElement(jobject array, | |
| 111 const JavaType& type, | |
| 112 jsize index, | |
| 113 const jvalue& value); | |
| 114 | |
| 115 void SetInvocationFailure(const char* error_message); | |
| 116 void SetPrimitiveResult(const base::ListValue& result_wrapper); | |
| 117 void SetObjectResult( | |
| 118 const base::android::JavaRef<jobject>& object, | |
| 119 const base::android::JavaRef<jclass>& safe_annotation_clazz); | |
| 120 | |
| 121 typedef std::map<GinJavaBoundObject::ObjectID, | |
| 122 JavaObjectWeakGlobalRef> ObjectRefs; | |
| 123 | |
| 124 scoped_ptr<ObjectDelegate> object_; | |
| 125 const std::string method_name_; | |
| 126 scoped_ptr<base::ListValue> arguments_; | |
| 127 ObjectRefs object_refs_; | |
| 128 bool holds_primitive_result_; | |
| 129 scoped_ptr<base::ListValue> primitive_result_; | |
| 130 std::string error_message_; | |
| 131 base::android::ScopedJavaGlobalRef<jobject> object_result_; | |
| 132 base::android::ScopedJavaGlobalRef<jclass> safe_annotation_clazz_; | |
| 133 | |
| 134 DISALLOW_COPY_AND_ASSIGN(GinJavaMethodInvocationHelper); | |
| 135 }; | |
| 136 | |
| 137 } // namespace content | |
| 138 | |
| 139 #endif // CONTENT_BROWSER_RENDERER_HOST_JAVA_GIN_JAVA_METHOD_INVOCATION_HELPER_ H_ | |
| OLD | NEW |