OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_RENDERER_HOST_JAVA_JAVA_BOUND_OBJECT_H_ | 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BOUND_OBJECT_H_ |
6 #define CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BOUND_OBJECT_H_ | 6 #define CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BOUND_OBJECT_H_ |
7 | 7 |
8 #include <jni.h> | 8 #include <jni.h> |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
11 | 11 |
| 12 #include "base/android/jni_helper.h" |
12 #include "base/android/scoped_java_ref.h" | 13 #include "base/android/scoped_java_ref.h" |
13 #include "base/memory/linked_ptr.h" | 14 #include "base/memory/linked_ptr.h" |
14 #include "content/browser/renderer_host/java/java_method.h" | 15 #include "content/browser/renderer_host/java/java_method.h" |
15 #include "third_party/npapi/bindings/npruntime.h" | 16 #include "third_party/npapi/bindings/npruntime.h" |
16 | 17 |
17 namespace content { | 18 namespace content { |
18 | 19 |
19 // Wrapper around a Java object. | 20 // Wrapper around a Java object. |
20 // | 21 // |
21 // Represents a Java object for use in the Java bridge. Holds a global ref to | 22 // Represents a Java object for use in the Java bridge. Holds a global ref to |
22 // the Java object and provides the ability to invoke methods on it. | 23 // the Java object and provides the ability to invoke methods on it. |
23 // Interrogation of the Java object for its methods is done lazily. This class | 24 // Interrogation of the Java object for its methods is done lazily. This class |
24 // is not generally threadsafe. However, it does allow for instances to be | 25 // is not generally threadsafe. However, it does allow for instances to be |
25 // created and destroyed on different threads. | 26 // created and destroyed on different threads. |
26 class JavaBoundObject { | 27 class JavaBoundObject { |
27 public: | 28 public: |
28 // Takes a Java object and creates a JavaBoundObject around it. The | 29 // Takes a Java object and creates a JavaBoundObject around it. The |
29 // |require_annotation| flag specifies whether or not only methods with the | 30 // |require_annotation| flag specifies whether or not only methods with the |
30 // JavascriptInterface annotation are exposed to JavaScript. This property | 31 // JavascriptInterface annotation are exposed to JavaScript. This property |
31 // propagates to all Objects that get implicitly exposed as return values as | 32 // propagates to all Objects that get implicitly exposed as return values as |
32 // well. Returns an NPObject with a ref count of one which owns the | 33 // well. Returns an NPObject with a ref count of one which owns the |
33 // JavaBoundObject. | 34 // JavaBoundObject. |
34 static NPObject* Create( | 35 static NPObject* Create( |
35 const base::android::JavaRef<jobject>& object, | 36 const base::android::JavaRef<jobject>& object, |
36 base::android::JavaRef<jclass>& safe_annotation_clazz); | 37 base::android::JavaRef<jclass>& safe_annotation_clazz); |
37 | 38 |
38 virtual ~JavaBoundObject(); | 39 virtual ~JavaBoundObject(); |
39 | 40 |
40 // Gets a global ref to the underlying JavaObject from a JavaBoundObject | 41 // Gets a local ref to the underlying JavaObject from a JavaBoundObject |
41 // wrapped as an NPObject. Ownership of the global ref is retained by the | 42 // wrapped as an NPObject. May return null if the underlying object has |
42 // JavaBoundObject: the caller must NOT release it. | 43 // been garbage collected. |
43 static jobject GetJavaObject(NPObject* object); | 44 static base::android::ScopedJavaLocalRef<jobject> GetJavaObject( |
| 45 NPObject* object); |
44 | 46 |
45 // Methods to implement the NPObject callbacks. | 47 // Methods to implement the NPObject callbacks. |
46 bool HasMethod(const std::string& name) const; | 48 bool HasMethod(const std::string& name) const; |
47 bool Invoke(const std::string& name, const NPVariant* args, size_t arg_count, | 49 bool Invoke(const std::string& name, const NPVariant* args, size_t arg_count, |
48 NPVariant* result); | 50 NPVariant* result); |
49 | 51 |
50 private: | 52 private: |
51 explicit JavaBoundObject( | 53 explicit JavaBoundObject( |
52 const base::android::JavaRef<jobject>& object, | 54 const base::android::JavaRef<jobject>& object, |
53 base::android::JavaRef<jclass>& safe_annotation_clazz); | 55 base::android::JavaRef<jclass>& safe_annotation_clazz); |
54 | 56 |
55 void EnsureMethodsAreSetUp() const; | 57 void EnsureMethodsAreSetUp() const; |
56 | 58 |
57 // The global ref to the underlying Java object that this JavaBoundObject | 59 // The weak ref to the underlying Java object that this JavaBoundObject |
58 // instance represents. | 60 // instance represents. |
59 base::android::ScopedJavaGlobalRef<jobject> java_object_; | 61 JavaObjectWeakGlobalRef java_object_; |
60 | |
61 // Map of public methods, from method name to Method instance. Multiple | 62 // Map of public methods, from method name to Method instance. Multiple |
62 // entries will be present for overloaded methods. Note that we can't use | 63 // entries will be present for overloaded methods. Note that we can't use |
63 // scoped_ptr in STL containers as we can't copy it. | 64 // scoped_ptr in STL containers as we can't copy it. |
64 typedef std::multimap<std::string, linked_ptr<JavaMethod> > JavaMethodMap; | 65 typedef std::multimap<std::string, linked_ptr<JavaMethod> > JavaMethodMap; |
65 mutable JavaMethodMap methods_; | 66 mutable JavaMethodMap methods_; |
66 mutable bool are_methods_set_up_; | 67 mutable bool are_methods_set_up_; |
67 | 68 |
68 base::android::ScopedJavaGlobalRef<jclass> safe_annotation_clazz_; | 69 base::android::ScopedJavaGlobalRef<jclass> safe_annotation_clazz_; |
69 | 70 |
70 DISALLOW_IMPLICIT_CONSTRUCTORS(JavaBoundObject); | 71 DISALLOW_IMPLICIT_CONSTRUCTORS(JavaBoundObject); |
71 }; | 72 }; |
72 | 73 |
73 } // namespace content | 74 } // namespace content |
74 | 75 |
75 #endif // CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BOUND_OBJECT_H_ | 76 #endif // CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BOUND_OBJECT_H_ |
OLD | NEW |