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