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_helper.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. The 33 // |require_annotation| flag specifies whether or not only methods with the 34 // JavascriptInterface annotation are exposed to JavaScript. This property 35 // propagates to all Objects that get implicitly exposed as return values as 36 // well. Returns an NPObject with a ref count of one which owns the 37 // JavaBoundObject. 38 // See also comment below for |manager_|. 39 static NPObject* Create( 40 const base::android::JavaRef<jobject>& object, 41 const base::android::JavaRef<jclass>& safe_annotation_clazz, 42 const base::WeakPtr<JavaBridgeDispatcherHostManager>& manager); 43 44 virtual ~JavaBoundObject(); 45 46 // Gets a local ref to the underlying JavaObject from a JavaBoundObject 47 // wrapped as an NPObject. May return null if the underlying object has 48 // been garbage collected. 49 static base::android::ScopedJavaLocalRef<jobject> GetJavaObject( 50 NPObject* object); 51 52 // Methods to implement the NPObject callbacks. 53 bool HasMethod(const std::string& name) const; 54 bool Invoke(const std::string& name, const NPVariant* args, size_t arg_count, 55 NPVariant* result); 56 57 private: 58 explicit JavaBoundObject( 59 const base::android::JavaRef<jobject>& object, 60 const base::android::JavaRef<jclass>& safe_annotation_clazz, 61 const base::WeakPtr<JavaBridgeDispatcherHostManager>& manager_); 62 63 void EnsureMethodsAreSetUp() const; 64 65 // The weak ref to the underlying Java object that this JavaBoundObject 66 // instance represents. 67 JavaObjectWeakGlobalRef java_object_; 68 69 // Keep a pointer back to the JavaBridgeDispatcherHostManager so that we 70 // can notify it when this JavaBoundObject is destroyed. JavaBoundObjects 71 // may outlive the manager so keep a WeakPtr. Note the WeakPtr may only be 72 // dereferenced on the UI thread. 73 base::WeakPtr<JavaBridgeDispatcherHostManager> manager_; 74 75 // Map of public methods, from method name to Method instance. Multiple 76 // entries will be present for overloaded methods. Note that we can't use 77 // scoped_ptr in STL containers as we can't copy it. 78 typedef std::multimap<std::string, linked_ptr<JavaMethod> > JavaMethodMap; 79 mutable JavaMethodMap methods_; 80 mutable bool are_methods_set_up_; 81 82 base::android::ScopedJavaGlobalRef<jclass> safe_annotation_clazz_; 83 84 DISALLOW_IMPLICIT_CONSTRUCTORS(JavaBoundObject); 85 }; 86 87 } // namespace content 88 89 #endif // CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BOUND_OBJECT_H_ 90