| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BOUND_OBJECT_H_ | |
| 6 #define CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BOUND_OBJECT_H_ | |
| 7 | |
| 8 #include <jni.h> | |
| 9 #include <map> | |
| 10 #include <string> | |
| 11 | |
| 12 #include "base/android/jni_weak_ref.h" | |
| 13 #include "base/android/scoped_java_ref.h" | |
| 14 #include "base/memory/linked_ptr.h" | |
| 15 #include "base/memory/weak_ptr.h" | |
| 16 #include "content/browser/renderer_host/java/java_method.h" | |
| 17 #include "third_party/npapi/bindings/npruntime.h" | |
| 18 | |
| 19 namespace content { | |
| 20 | |
| 21 class JavaBridgeDispatcherHostManager; | |
| 22 | |
| 23 // Wrapper around a Java object. | |
| 24 // | |
| 25 // Represents a Java object for use in the Java bridge. Holds a global ref to | |
| 26 // the Java object and provides the ability to invoke methods on it. | |
| 27 // Interrogation of the Java object for its methods is done lazily. This class | |
| 28 // is not generally threadsafe. However, it does allow for instances to be | |
| 29 // created and destroyed on different threads. | |
| 30 class JavaBoundObject { | |
| 31 public: | |
| 32 // Takes a Java object and creates a JavaBoundObject around it. If | |
| 33 // |safe_annotation_clazz| annotation is non-null, the method is exposed | |
| 34 // to JavaScript. Returns an NPObject with a ref count of one which owns the | |
| 35 // JavaBoundObject. | |
| 36 // See also comment below for |manager_|. | |
| 37 static NPObject* Create( | |
| 38 const base::android::JavaRef<jobject>& object, | |
| 39 const base::android::JavaRef<jclass>& safe_annotation_clazz, | |
| 40 const base::WeakPtr<JavaBridgeDispatcherHostManager>& manager, | |
| 41 bool can_enumerate_methods); | |
| 42 | |
| 43 virtual ~JavaBoundObject(); | |
| 44 | |
| 45 // Gets a local ref to the underlying JavaObject from a JavaBoundObject | |
| 46 // wrapped as an NPObject. May return null if the underlying object has | |
| 47 // been garbage collected. | |
| 48 static base::android::ScopedJavaLocalRef<jobject> GetJavaObject( | |
| 49 NPObject* object); | |
| 50 | |
| 51 // Methods to implement the NPObject callbacks. | |
| 52 bool CanEnumerateMethods() const { return can_enumerate_methods_; } | |
| 53 std::vector<std::string> GetMethodNames() const; | |
| 54 bool HasMethod(const std::string& name) const; | |
| 55 bool Invoke(const std::string& name, const NPVariant* args, size_t arg_count, | |
| 56 NPVariant* result); | |
| 57 | |
| 58 private: | |
| 59 JavaBoundObject( | |
| 60 const base::android::JavaRef<jobject>& object, | |
| 61 const base::android::JavaRef<jclass>& safe_annotation_clazz, | |
| 62 const base::WeakPtr<JavaBridgeDispatcherHostManager>& manager, | |
| 63 bool can_enumerate_methods); | |
| 64 | |
| 65 void EnsureMethodsAreSetUp() const; | |
| 66 base::android::ScopedJavaLocalRef<jclass> GetLocalClassRef(JNIEnv* env) const; | |
| 67 | |
| 68 static void ThrowSecurityException(const char* message); | |
| 69 | |
| 70 // The weak ref to the underlying Java object that this JavaBoundObject | |
| 71 // instance represents. | |
| 72 JavaObjectWeakGlobalRef java_object_; | |
| 73 | |
| 74 // Keep a pointer back to the JavaBridgeDispatcherHostManager so that we | |
| 75 // can notify it when this JavaBoundObject is destroyed. JavaBoundObjects | |
| 76 // may outlive the manager so keep a WeakPtr. Note the WeakPtr may only be | |
| 77 // dereferenced on the UI thread. | |
| 78 base::WeakPtr<JavaBridgeDispatcherHostManager> manager_; | |
| 79 | |
| 80 // Map of public methods, from method name to Method instance. Multiple | |
| 81 // entries will be present for overloaded methods. Note that we can't use | |
| 82 // scoped_ptr in STL containers as we can't copy it. | |
| 83 typedef std::multimap<std::string, linked_ptr<JavaMethod> > JavaMethodMap; | |
| 84 mutable JavaMethodMap methods_; | |
| 85 mutable bool are_methods_set_up_; | |
| 86 mutable jmethodID object_get_class_method_id_; | |
| 87 const bool can_enumerate_methods_; | |
| 88 | |
| 89 base::android::ScopedJavaGlobalRef<jclass> safe_annotation_clazz_; | |
| 90 | |
| 91 DISALLOW_IMPLICIT_CONSTRUCTORS(JavaBoundObject); | |
| 92 }; | |
| 93 | |
| 94 } // namespace content | |
| 95 | |
| 96 #endif // CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BOUND_OBJECT_H_ | |
| OLD | NEW |