Chromium Code Reviews| 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 |
|
joth
2013/01/07 19:32:41
actually, whether it's local or global is not so i
benm (inactive)
2013/01/08 14:43:05
Will change to SJLR, and add back the NewLocalRef
| |
| 41 // wrapped as an NPObject. Ownership of the global ref is retained by the | 42 // wrapped as an NPObject. |
|
joth
2013/01/07 19:32:41
Mention it may return NULL too, if the native side
benm (inactive)
2013/01/08 14:43:05
Done.
| |
| 42 // JavaBoundObject: the caller must NOT release it. | |
| 43 static jobject GetJavaObject(NPObject* object); | 43 static jobject GetJavaObject(NPObject* object); |
| 44 | 44 |
| 45 // Methods to implement the NPObject callbacks. | 45 // Methods to implement the NPObject callbacks. |
| 46 bool HasMethod(const std::string& name) const; | 46 bool HasMethod(const std::string& name) const; |
| 47 bool Invoke(const std::string& name, const NPVariant* args, size_t arg_count, | 47 bool Invoke(const std::string& name, const NPVariant* args, size_t arg_count, |
| 48 NPVariant* result); | 48 NPVariant* result); |
| 49 | 49 |
| 50 private: | 50 private: |
| 51 explicit JavaBoundObject( | 51 explicit JavaBoundObject( |
| 52 const base::android::JavaRef<jobject>& object, | 52 const base::android::JavaRef<jobject>& object, |
| 53 base::android::JavaRef<jclass>& safe_annotation_clazz); | 53 base::android::JavaRef<jclass>& safe_annotation_clazz); |
| 54 | 54 |
| 55 | |
|
joth
2013/01/07 19:32:41
\n nit
benm (inactive)
2013/01/08 14:43:05
Done.
| |
| 55 void EnsureMethodsAreSetUp() const; | 56 void EnsureMethodsAreSetUp() const; |
| 56 | 57 |
| 57 // The global ref to the underlying Java object that this JavaBoundObject | 58 // The weak ref to the underlying Java object that this JavaBoundObject |
| 58 // instance represents. | 59 // instance represents. |
| 59 base::android::ScopedJavaGlobalRef<jobject> java_object_; | 60 JavaObjectWeakGlobalRef java_object_; |
| 60 | |
| 61 // Map of public methods, from method name to Method instance. Multiple | 61 // 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 | 62 // 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. | 63 // scoped_ptr in STL containers as we can't copy it. |
| 64 typedef std::multimap<std::string, linked_ptr<JavaMethod> > JavaMethodMap; | 64 typedef std::multimap<std::string, linked_ptr<JavaMethod> > JavaMethodMap; |
| 65 mutable JavaMethodMap methods_; | 65 mutable JavaMethodMap methods_; |
| 66 mutable bool are_methods_set_up_; | 66 mutable bool are_methods_set_up_; |
| 67 | 67 |
| 68 base::android::ScopedJavaGlobalRef<jclass> safe_annotation_clazz_; | 68 base::android::ScopedJavaGlobalRef<jclass> safe_annotation_clazz_; |
| 69 | 69 |
| 70 DISALLOW_IMPLICIT_CONSTRUCTORS(JavaBoundObject); | 70 DISALLOW_IMPLICIT_CONSTRUCTORS(JavaBoundObject); |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 } // namespace content | 73 } // namespace content |
| 74 | 74 |
| 75 #endif // CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BOUND_OBJECT_H_ | 75 #endif // CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BOUND_OBJECT_H_ |
| OLD | NEW |