OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/scoped_java_ref.h" |
| 13 #include "base/memory/linked_ptr.h" |
| 14 #include "content/browser/renderer_host/java/java_method.h" |
| 15 #include "third_party/npapi/bindings/npruntime.h" |
| 16 |
| 17 // Represents a Java object for use in the Java bridge. Holds a global ref to |
| 18 // the Java object and provides the ability to invoke methods on it. |
| 19 // Interrogation of the Java object for its methods is done lazily. This class |
| 20 // is not generally threadsafe. However, it does allow for instances to be |
| 21 // created and destroyed on different threads. |
| 22 class JavaBoundObject { |
| 23 public: |
| 24 // Takes a Java object and creates a JavaBoundObject around it. Returns an |
| 25 // NPObject with a ref count of one which owns the JavaBoundObject. |
| 26 static NPObject* Create(const base::android::JavaRef<jobject>& object); |
| 27 |
| 28 virtual ~JavaBoundObject(); |
| 29 |
| 30 // Gets the underlying JavaObject from a JavaBoundObject wrapped as an |
| 31 // NPObject. |
| 32 static jobject GetJavaObject(NPObject* object); |
| 33 |
| 34 // Methods to implement the NPObject callbacks. |
| 35 bool HasMethod(const std::string& name) const; |
| 36 bool Invoke(const std::string& name, const NPVariant* args, size_t arg_count, |
| 37 NPVariant* result); |
| 38 |
| 39 private: |
| 40 explicit JavaBoundObject(const base::android::JavaRef<jobject>& object); |
| 41 |
| 42 void EnsureMethodsAreSetUp(); |
| 43 |
| 44 base::android::ScopedJavaGlobalRef<jobject> java_object_; |
| 45 bool are_methods_set_up_; |
| 46 |
| 47 // A map containing the public methods of this Java object. We map from |
| 48 // method name to Method instance. We support overloaded methods, so multiple |
| 49 // entries in the map may have the same key. Note that we can't use |
| 50 // scoped_ptr in STL containers as we can't copy it. |
| 51 typedef std::multimap<std::string, linked_ptr<JavaMethod> > JavaMethodMap; |
| 52 JavaMethodMap methods_; |
| 53 |
| 54 DISALLOW_IMPLICIT_CONSTRUCTORS(JavaBoundObject); |
| 55 }; |
| 56 |
| 57 #endif // CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BOUND_OBJECT_H_ |
OLD | NEW |