Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / android / java / gin_java_bridge_dispatcher_host.h
blobf17fd50ae064a8337eefd79bf615fe61d9e4afc4
1 // Copyright 2014 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_ANDROID_JAVA_GIN_JAVA_BRIDGE_DISPATCHER_HOST_H_
6 #define CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_BRIDGE_DISPATCHER_HOST_H_
8 #include <map>
9 #include <set>
11 #include "base/android/jni_weak_ref.h"
12 #include "base/android/scoped_java_ref.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/synchronization/lock.h"
15 #include "content/browser/android/java/gin_java_bound_object.h"
16 #include "content/browser/android/java/gin_java_method_invocation_helper.h"
17 #include "content/public/browser/web_contents_observer.h"
19 namespace base {
20 class ListValue;
23 namespace content {
25 // This class handles injecting Java objects into a single ContentViewCore /
26 // WebView. The Java object itself lives in the browser process on a background
27 // thread, while multiple JavaScript wrapper objects (one per frame) are created
28 // on the renderer side. The injected Java objects are identified by ObjectID,
29 // while wrappers are identified by a pair of (ObjectID, frame_routing_id).
30 class GinJavaBridgeDispatcherHost
31 : public base::RefCountedThreadSafe<GinJavaBridgeDispatcherHost>,
32 public WebContentsObserver,
33 public GinJavaMethodInvocationHelper::DispatcherDelegate {
34 public:
36 GinJavaBridgeDispatcherHost(WebContents* web_contents,
37 jobject retained_object_set);
39 void AddNamedObject(
40 const std::string& name,
41 const base::android::JavaRef<jobject>& object,
42 const base::android::JavaRef<jclass>& safe_annotation_clazz);
43 void RemoveNamedObject(const std::string& name);
44 void SetAllowObjectContentsInspection(bool allow);
46 // WebContentsObserver
47 void RenderFrameCreated(RenderFrameHost* render_frame_host) override;
48 void DocumentAvailableInMainFrame() override;
49 void WebContentsDestroyed() override;
51 // GinJavaMethodInvocationHelper::DispatcherDelegate
52 JavaObjectWeakGlobalRef GetObjectWeakRef(
53 GinJavaBoundObject::ObjectID object_id) override;
55 // Run on the background thread.
56 void OnGetMethods(GinJavaBoundObject::ObjectID object_id,
57 std::set<std::string>* returned_method_names);
58 void OnHasMethod(GinJavaBoundObject::ObjectID object_id,
59 const std::string& method_name,
60 bool* result);
61 void OnInvokeMethod(int routing_id,
62 GinJavaBoundObject::ObjectID object_id,
63 const std::string& method_name,
64 const base::ListValue& arguments,
65 base::ListValue* result,
66 content::GinJavaBridgeError* error_code);
67 void OnObjectWrapperDeleted(int routing_id,
68 GinJavaBoundObject::ObjectID object_id);
70 private:
71 friend class base::RefCountedThreadSafe<GinJavaBridgeDispatcherHost>;
73 typedef std::map<GinJavaBoundObject::ObjectID,
74 scoped_refptr<GinJavaBoundObject>> ObjectMap;
76 ~GinJavaBridgeDispatcherHost() override;
78 // Run on the UI thread.
79 void InstallFilterAndRegisterAllRoutingIds();
81 // Run on any thread.
82 GinJavaBoundObject::ObjectID AddObject(
83 const base::android::JavaRef<jobject>& object,
84 const base::android::JavaRef<jclass>& safe_annotation_clazz,
85 bool is_named,
86 int32 holder);
87 scoped_refptr<GinJavaBoundObject> FindObject(
88 GinJavaBoundObject::ObjectID object_id);
89 bool FindObjectId(const base::android::JavaRef<jobject>& object,
90 GinJavaBoundObject::ObjectID* object_id);
91 void RemoveFromRetainedObjectSetLocked(const JavaObjectWeakGlobalRef& ref);
92 JavaObjectWeakGlobalRef RemoveHolderAndAdvanceLocked(
93 int32 holder,
94 ObjectMap::iterator* iter_ptr);
96 // The following objects are used only on the UI thread.
98 typedef std::map<std::string, GinJavaBoundObject::ObjectID> NamedObjectMap;
99 NamedObjectMap named_objects_;
101 // The following objects are used on both threads, so locking must be used.
103 GinJavaBoundObject::ObjectID next_object_id_;
104 // Every time a GinJavaBoundObject backed by a real Java object is
105 // created/destroyed, we insert/remove a strong ref to that Java object into
106 // this set so that it doesn't get garbage collected while it's still
107 // potentially in use. Although the set is managed native side, it's owned
108 // and defined in Java so that pushing refs into it does not create new GC
109 // roots that would prevent ContentViewCore from being garbage collected.
110 JavaObjectWeakGlobalRef retained_object_set_;
111 // Note that retained_object_set_ does not need to be consistent
112 // with objects_.
113 ObjectMap objects_;
114 base::Lock objects_lock_;
116 // The following objects are only used on the background thread.
117 bool allow_object_contents_inspection_;
119 DISALLOW_COPY_AND_ASSIGN(GinJavaBridgeDispatcherHost);
122 } // namespace content
124 #endif // CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_BRIDGE_DISPATCHER_HOST_H_