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 // Wrapper around a Java object. |
| 18 // |
| 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. |
| 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 |
| 23 // created and destroyed on different threads. |
| 24 class JavaBoundObject { |
| 25 public: |
| 26 // Takes a Java object and creates a JavaBoundObject around it. Returns an |
| 27 // NPObject with a ref count of one which owns the JavaBoundObject. |
| 28 static NPObject* Create(const base::android::JavaRef<jobject>& object); |
| 29 |
| 30 virtual ~JavaBoundObject(); |
| 31 |
| 32 // Gets the underlying JavaObject from a JavaBoundObject wrapped as an |
| 33 // NPObject. |
| 34 static jobject GetJavaObject(NPObject* object); |
| 35 |
| 36 // Methods to implement the NPObject callbacks. |
| 37 bool HasMethod(const std::string& name) const; |
| 38 bool Invoke(const std::string& name, const NPVariant* args, size_t arg_count, |
| 39 NPVariant* result); |
| 40 |
| 41 private: |
| 42 explicit JavaBoundObject(const base::android::JavaRef<jobject>& object); |
| 43 |
| 44 void EnsureMethodsAreSetUp() const; |
| 45 |
| 46 // Global ref to the underlying Java object. We use a naked jobject, rather |
| 47 // than a ScopedJavaGlobalRef, as the global ref will be added and dropped on |
| 48 // different threads. |
| 49 jobject java_object_; |
| 50 |
| 51 // Map of public methods, from method name to Method instance. Multiple |
| 52 // entries will be present for overloaded methods. Note that we can't use |
| 53 // scoped_ptr in STL containers as we can't copy it. |
| 54 typedef std::multimap<std::string, linked_ptr<JavaMethod> > JavaMethodMap; |
| 55 mutable JavaMethodMap methods_; |
| 56 |
| 57 DISALLOW_IMPLICIT_CONSTRUCTORS(JavaBoundObject); |
| 58 }; |
| 59 |
| 60 #endif // CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BOUND_OBJECT_H_ |
OLD | NEW |