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.
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BOUND_OBJECT_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BOUND_OBJECT_H_
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"
21 class JavaBridgeDispatcherHostManager
;
23 // Wrapper around a Java object.
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
{
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
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
);
42 virtual ~JavaBoundObject();
44 // Gets a local ref to the underlying JavaObject from a JavaBoundObject
45 // wrapped as an NPObject. May return null if the underlying object has
46 // been garbage collected.
47 static base::android::ScopedJavaLocalRef
<jobject
> GetJavaObject(
50 // Methods to implement the NPObject callbacks.
51 bool HasMethod(const std::string
& name
) const;
52 bool Invoke(const std::string
& name
, const NPVariant
* args
, size_t arg_count
,
56 explicit JavaBoundObject(
57 const base::android::JavaRef
<jobject
>& object
,
58 const base::android::JavaRef
<jclass
>& safe_annotation_clazz
,
59 const base::WeakPtr
<JavaBridgeDispatcherHostManager
>& manager_
);
61 void EnsureMethodsAreSetUp() const;
63 // The weak ref to the underlying Java object that this JavaBoundObject
64 // instance represents.
65 JavaObjectWeakGlobalRef java_object_
;
67 // Keep a pointer back to the JavaBridgeDispatcherHostManager so that we
68 // can notify it when this JavaBoundObject is destroyed. JavaBoundObjects
69 // may outlive the manager so keep a WeakPtr. Note the WeakPtr may only be
70 // dereferenced on the UI thread.
71 base::WeakPtr
<JavaBridgeDispatcherHostManager
> manager_
;
73 // Map of public methods, from method name to Method instance. Multiple
74 // entries will be present for overloaded methods. Note that we can't use
75 // scoped_ptr in STL containers as we can't copy it.
76 typedef std::multimap
<std::string
, linked_ptr
<JavaMethod
> > JavaMethodMap
;
77 mutable JavaMethodMap methods_
;
78 mutable bool are_methods_set_up_
;
80 base::android::ScopedJavaGlobalRef
<jclass
> safe_annotation_clazz_
;
82 DISALLOW_IMPLICIT_CONSTRUCTORS(JavaBoundObject
);
85 } // namespace content
87 #endif // CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BOUND_OBJECT_H_