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_BOUND_OBJECT_H_ | 5 #ifndef CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_BOUND_OBJECT_H_ |
6 #define CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_BOUND_OBJECT_H_ | 6 #define CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_BOUND_OBJECT_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 | 10 |
11 #include "base/android/jni_weak_ref.h" | 11 #include "base/android/jni_weak_ref.h" |
12 #include "base/android/scoped_java_ref.h" | 12 #include "base/android/scoped_java_ref.h" |
13 #include "base/id_map.h" | |
14 #include "base/memory/linked_ptr.h" | 13 #include "base/memory/linked_ptr.h" |
15 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
16 #include "base/memory/weak_ptr.h" | |
17 #include "base/values.h" | 15 #include "base/values.h" |
18 #include "content/browser/android/java/java_method.h" | 16 #include "content/browser/android/java/java_method.h" |
19 | 17 |
20 namespace content { | 18 namespace content { |
21 | 19 |
22 class RenderFrameHost; | |
23 | |
24 class GinJavaBoundObject | 20 class GinJavaBoundObject |
25 : public base::RefCountedThreadSafe<GinJavaBoundObject> { | 21 : public base::RefCountedThreadSafe<GinJavaBoundObject> { |
26 public: | 22 public: |
27 // Using scoped_refptr<> as a value type is somewhat not IDMap had been | 23 typedef int32 ObjectID; |
28 // designed for (it returns T* from lookups, so we have to de-ref it), but on | |
29 // the other hand, this allows us to manage lifetime of GinJavaBoundObject | |
30 // automatically. | |
31 typedef IDMap<scoped_refptr<GinJavaBoundObject>, IDMapOwnPointer> ObjectMap; | |
32 typedef ObjectMap::KeyType ObjectID; | |
33 | 24 |
34 static GinJavaBoundObject* CreateNamed( | 25 static GinJavaBoundObject* CreateNamed( |
35 const JavaObjectWeakGlobalRef& ref, | 26 const JavaObjectWeakGlobalRef& ref, |
36 const base::android::JavaRef<jclass>& safe_annotation_clazz); | 27 const base::android::JavaRef<jclass>& safe_annotation_clazz); |
37 static GinJavaBoundObject* CreateTransient( | 28 static GinJavaBoundObject* CreateTransient( |
38 const JavaObjectWeakGlobalRef& ref, | 29 const JavaObjectWeakGlobalRef& ref, |
39 const base::android::JavaRef<jclass>& safe_annotation_clazz, | 30 const base::android::JavaRef<jclass>& safe_annotation_clazz, |
40 RenderFrameHost* holder); | 31 int32 holder); |
41 | 32 |
33 // The following methods can be called on any thread. | |
42 JavaObjectWeakGlobalRef& GetWeakRef() { return ref_; } | 34 JavaObjectWeakGlobalRef& GetWeakRef() { return ref_; } |
43 base::android::ScopedJavaLocalRef<jobject> GetLocalRef(JNIEnv* env) { | 35 base::android::ScopedJavaLocalRef<jobject> GetLocalRef(JNIEnv* env) { |
44 return ref_.get(env); | 36 return ref_.get(env); |
45 } | 37 } |
46 | 38 |
47 bool IsNamed() { return names_count_ > 0; } | 39 bool IsNamed() { return names_count_ > 0; } |
48 void AddName() { ++names_count_; } | 40 void AddName() { ++names_count_; } |
49 void RemoveName() { --names_count_; } | 41 void RemoveName() { --names_count_; } |
50 | 42 |
51 bool HasHolders() { return !holders_.empty(); } | 43 bool HasHolders() { return !holders_.empty(); } |
52 void AddHolder(RenderFrameHost* holder) { holders_.insert(holder); } | 44 void AddHolder(int32 holder) { holders_.insert(holder); } |
53 void RemoveHolder(RenderFrameHost* holder) { holders_.erase(holder); } | 45 void RemoveHolder(int32 holder) { holders_.erase(holder); } |
54 | 46 |
55 // The following methods are called on the background thread. | 47 // The following methods are called on the background thread. |
56 std::set<std::string> GetMethodNames(); | 48 std::set<std::string> GetMethodNames(); |
57 bool HasMethod(const std::string& method_name); | 49 bool HasMethod(const std::string& method_name); |
58 const JavaMethod* FindMethod(const std::string& method_name, | 50 const JavaMethod* FindMethod(const std::string& method_name, |
59 size_t num_parameters); | 51 size_t num_parameters); |
60 bool IsObjectGetClassMethod(const JavaMethod* method); | 52 bool IsObjectGetClassMethod(const JavaMethod* method); |
61 const base::android::JavaRef<jclass>& GetSafeAnnotationClass(); | 53 const base::android::JavaRef<jclass>& GetSafeAnnotationClass(); |
62 base::android::ScopedJavaLocalRef<jclass> GetLocalClassRef(JNIEnv* env); | 54 base::android::ScopedJavaLocalRef<jclass> GetLocalClassRef(JNIEnv* env); |
63 | 55 |
64 private: | 56 private: |
65 friend class base::RefCountedThreadSafe<GinJavaBoundObject>; | 57 friend class base::RefCountedThreadSafe<GinJavaBoundObject>; |
66 | 58 |
67 GinJavaBoundObject( | 59 GinJavaBoundObject( |
68 const JavaObjectWeakGlobalRef& ref, | 60 const JavaObjectWeakGlobalRef& ref, |
69 const base::android::JavaRef<jclass>& safe_annotation_clazz); | 61 const base::android::JavaRef<jclass>& safe_annotation_clazz); |
70 GinJavaBoundObject( | 62 GinJavaBoundObject( |
71 const JavaObjectWeakGlobalRef& ref, | 63 const JavaObjectWeakGlobalRef& ref, |
72 const base::android::JavaRef<jclass>& safe_annotation_clazz, | 64 const base::android::JavaRef<jclass>& safe_annotation_clazz, |
73 const std::set<RenderFrameHost*> holders); | 65 const std::set<int32> holders); |
Sami
2014/12/08 18:52:31
nit: Could this be a const reference instead of a
mnaganov (inactive)
2014/12/09 10:23:55
Done.
| |
74 ~GinJavaBoundObject(); | 66 ~GinJavaBoundObject(); |
75 | 67 |
76 // The following methods are called on the background thread. | 68 // The following methods are called on the background thread. |
77 void EnsureMethodsAreSetUp(); | 69 void EnsureMethodsAreSetUp(); |
78 | 70 |
79 JavaObjectWeakGlobalRef ref_; | 71 JavaObjectWeakGlobalRef ref_; |
80 | 72 |
81 // An object must be kept in retained_object_set_ either if it has | 73 // An object must be kept in retained_object_set_ either if it has |
82 // names or if it has a non-empty holders set. | 74 // names or if it has a non-empty holders set. |
83 int names_count_; | 75 int names_count_; |
84 std::set<RenderFrameHost*> holders_; | 76 std::set<int32> holders_; |
85 | 77 |
86 // The following fields are accessed on the background thread. | 78 // The following fields are accessed on the background thread. |
87 typedef std::multimap<std::string, linked_ptr<JavaMethod> > JavaMethodMap; | 79 typedef std::multimap<std::string, linked_ptr<JavaMethod> > JavaMethodMap; |
88 JavaMethodMap methods_; | 80 JavaMethodMap methods_; |
89 jmethodID object_get_class_method_id_; | 81 jmethodID object_get_class_method_id_; |
90 bool are_methods_set_up_; | 82 bool are_methods_set_up_; |
91 base::android::ScopedJavaGlobalRef<jclass> safe_annotation_clazz_; | 83 base::android::ScopedJavaGlobalRef<jclass> safe_annotation_clazz_; |
92 }; | 84 }; |
93 | 85 |
94 } // namespace content | 86 } // namespace content |
95 | 87 |
96 #endif // CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_BOUND_OBJECT_H_ | 88 #endif // CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_BOUND_OBJECT_H_ |
OLD | NEW |