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.
5 #ifndef CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_METHOD_INVOCATION_HELPER_H_
6 #define CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_METHOD_INVOCATION_HELPER_H_
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/android/java/gin_java_bound_object.h"
15 #include "content/browser/android/java/java_type.h"
16 #include "content/common/android/gin_java_bridge_errors.h"
17 #include "content/common/content_export.h"
23 // Instances of this class are created and initialized on the UI thread, do
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
27 : public base::RefCountedThreadSafe
<GinJavaMethodInvocationHelper
> {
29 // DispatcherDelegate is used on the UI thread
30 class DispatcherDelegate
{
32 DispatcherDelegate() {}
33 virtual ~DispatcherDelegate() {}
34 virtual JavaObjectWeakGlobalRef
GetObjectWeakRef(
35 GinJavaBoundObject::ObjectID object_id
) = 0;
38 DISALLOW_COPY_AND_ASSIGN(DispatcherDelegate
);
41 // ObjectDelegate is used in the background thread
42 class ObjectDelegate
{
45 virtual ~ObjectDelegate() {}
46 virtual base::android::ScopedJavaLocalRef
<jobject
> GetLocalRef(
48 virtual base::android::ScopedJavaLocalRef
<jclass
> GetLocalClassRef(
50 virtual const JavaMethod
* FindMethod(const std::string
& method_name
,
51 size_t num_parameters
) = 0;
52 virtual bool IsObjectGetClassMethod(const JavaMethod
* method
) = 0;
53 virtual const base::android::JavaRef
<jclass
>& GetSafeAnnotationClass() = 0;
56 DISALLOW_COPY_AND_ASSIGN(ObjectDelegate
);
59 GinJavaMethodInvocationHelper(scoped_ptr
<ObjectDelegate
> object
,
60 const std::string
& method_name
,
61 const base::ListValue
& arguments
);
62 void Init(DispatcherDelegate
* dispatcher
);
64 // Called on the background thread
67 // Called on the UI thread
68 bool HoldsPrimitiveResult();
69 const base::ListValue
& GetPrimitiveResult();
70 const base::android::JavaRef
<jobject
>& GetObjectResult();
71 const base::android::JavaRef
<jclass
>& GetSafeAnnotationClass();
72 const GinJavaBridgeError
GetInvocationError();
75 friend class base::RefCountedThreadSafe
<GinJavaMethodInvocationHelper
>;
76 ~GinJavaMethodInvocationHelper();
78 // Called on the UI thread
79 void BuildObjectRefsFromListValue(DispatcherDelegate
* dispatcher
,
80 const base::Value
* list_value
);
81 void BuildObjectRefsFromDictionaryValue(DispatcherDelegate
* dispatcher
,
82 const base::Value
* dict_value
);
84 bool AppendObjectRef(DispatcherDelegate
* dispatcher
,
85 const base::Value
* raw_value
);
87 // Called on the background thread.
88 void InvokeMethod(jobject object
,
90 const JavaType
& return_type
,
93 void SetInvocationError(GinJavaBridgeError error
);
94 void SetPrimitiveResult(const base::ListValue
& result_wrapper
);
96 const base::android::JavaRef
<jobject
>& object
,
97 const base::android::JavaRef
<jclass
>& safe_annotation_clazz
);
99 typedef std::map
<GinJavaBoundObject::ObjectID
,
100 JavaObjectWeakGlobalRef
> ObjectRefs
;
102 scoped_ptr
<ObjectDelegate
> object_
;
103 const std::string method_name_
;
104 scoped_ptr
<base::ListValue
> arguments_
;
105 ObjectRefs object_refs_
;
106 bool holds_primitive_result_
;
107 scoped_ptr
<base::ListValue
> primitive_result_
;
108 GinJavaBridgeError invocation_error_
;
109 base::android::ScopedJavaGlobalRef
<jobject
> object_result_
;
110 base::android::ScopedJavaGlobalRef
<jclass
> safe_annotation_clazz_
;
112 DISALLOW_COPY_AND_ASSIGN(GinJavaMethodInvocationHelper
);
115 } // namespace content
117 #endif // CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_METHOD_INVOCATION_HELPER_H_