Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: content/browser/android/java/gin_java_bridge_dispatcher_host.h

Issue 772123002: [Android] Java Bridge: handle requests from Java Script on the background thread (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed benm's comment Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_BRIDGE_DISPATCHER_HOST_H_ 5 #ifndef CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_BRIDGE_DISPATCHER_HOST_H_
6 #define CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_BRIDGE_DISPATCHER_HOST_H_ 6 #define CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_BRIDGE_DISPATCHER_HOST_H_
7 7
8 #include <map> 8 #include <map>
9 #include <set> 9 #include <set>
10 10
11 #include "base/android/jni_weak_ref.h" 11 #include "base/android/jni_weak_ref.h"
12 #include "base/android/scoped_java_ref.h" 12 #include "base/android/scoped_java_ref.h"
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
15 #include "base/synchronization/lock.h"
15 #include "content/browser/android/java/gin_java_bound_object.h" 16 #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/browser/android/java/gin_java_method_invocation_helper.h"
18 #include "content/public/browser/browser_message_filter.h"
17 #include "content/public/browser/web_contents_observer.h" 19 #include "content/public/browser/web_contents_observer.h"
18 20
19 namespace base { 21 namespace base {
20 class ListValue; 22 class ListValue;
21 } 23 }
22 24
23 namespace IPC { 25 namespace IPC {
24 class Message; 26 class Message;
25 } 27 }
26 28
27 namespace content { 29 namespace content {
28 30
29 // This class handles injecting Java objects into a single RenderView. The Java 31 // This class handles injecting Java objects into a single RenderView. The Java
30 // object itself lives in the browser process on a background thread, while a 32 // object itself lives in the browser process on a background thread, while a
31 // proxy object is created in the renderer. An instance of this class exists 33 // proxy object is created in the renderer. An instance of this class exists
32 // for each RenderFrameHost. 34 // for each RenderFrameHost.
33 class GinJavaBridgeDispatcherHost 35 class GinJavaBridgeDispatcherHost
34 : public base::SupportsWeakPtr<GinJavaBridgeDispatcherHost>, 36 : public WebContentsObserver,
35 public WebContentsObserver, 37 public BrowserMessageFilter,
36 public GinJavaMethodInvocationHelper::DispatcherDelegate { 38 public GinJavaMethodInvocationHelper::DispatcherDelegate {
37 public: 39 public:
38 40
39 GinJavaBridgeDispatcherHost(WebContents* web_contents, 41 GinJavaBridgeDispatcherHost(WebContents* web_contents,
40 jobject retained_object_set); 42 jobject retained_object_set);
41 virtual ~GinJavaBridgeDispatcherHost();
42 43
43 void AddNamedObject( 44 void AddNamedObject(
44 const std::string& name, 45 const std::string& name,
45 const base::android::JavaRef<jobject>& object, 46 const base::android::JavaRef<jobject>& object,
46 const base::android::JavaRef<jclass>& safe_annotation_clazz); 47 const base::android::JavaRef<jclass>& safe_annotation_clazz);
47 void RemoveNamedObject(const std::string& name); 48 void RemoveNamedObject(const std::string& name);
48 void SetAllowObjectContentsInspection(bool allow); 49 void SetAllowObjectContentsInspection(bool allow);
49 50
50 // WebContentsObserver 51 // WebContentsObserver
51 virtual void RenderFrameCreated(RenderFrameHost* render_frame_host) override; 52 virtual void RenderFrameCreated(RenderFrameHost* render_frame_host) override;
52 virtual void RenderFrameDeleted(RenderFrameHost* render_frame_host) override; 53 virtual void RenderFrameDeleted(RenderFrameHost* render_frame_host) override;
53 virtual void DocumentAvailableInMainFrame() override; 54 virtual void DocumentAvailableInMainFrame() override;
54 virtual bool OnMessageReceived(const IPC::Message& message, 55
55 RenderFrameHost* render_frame_host) override; 56 // BrowserMessageFilter
57 using BrowserMessageFilter::Send;
58 void OnDestruct() const override;
59 bool OnMessageReceived(const IPC::Message& message) override;
60 base::TaskRunner* OverrideTaskRunnerForMessage(
61 const IPC::Message& message) override;
56 62
57 // GinJavaMethodInvocationHelper::DispatcherDelegate 63 // GinJavaMethodInvocationHelper::DispatcherDelegate
58 virtual JavaObjectWeakGlobalRef GetObjectWeakRef( 64 virtual JavaObjectWeakGlobalRef GetObjectWeakRef(
59 GinJavaBoundObject::ObjectID object_id) override; 65 GinJavaBoundObject::ObjectID object_id) override;
60 66
61 void OnGetMethods(RenderFrameHost* render_frame_host, 67 private:
62 GinJavaBoundObject::ObjectID object_id, 68 friend class BrowserThread;
63 IPC::Message* reply_msg); 69 friend class base::DeleteHelper<GinJavaBridgeDispatcherHost>;
64 void OnHasMethod(RenderFrameHost* render_frame_host,
65 GinJavaBoundObject::ObjectID object_id,
66 const std::string& method_name,
67 IPC::Message* reply_msg);
68 void OnInvokeMethod(RenderFrameHost* render_frame_host,
69 GinJavaBoundObject::ObjectID object_id,
70 const std::string& method_name,
71 const base::ListValue& arguments,
72 IPC::Message* reply_msg);
73 70
74 private: 71 typedef std::map<GinJavaBoundObject::ObjectID,
75 void OnObjectWrapperDeleted(RenderFrameHost* render_frame_host, 72 scoped_refptr<GinJavaBoundObject>> ObjectMap;
76 GinJavaBoundObject::ObjectID object_id);
77 73
78 bool IsValidRenderFrameHost(RenderFrameHost* render_frame_host); 74 virtual ~GinJavaBridgeDispatcherHost() override;
Sami 2014/12/08 18:52:31 nit: no need for virtual with override.
mnaganov (inactive) 2014/12/09 10:23:55 Thanks! Removed here and in other overrides above.
79 void SendMethods(RenderFrameHost* render_frame_host, 75
80 const std::set<std::string>& method_names); 76 void TryAddingBrowserFilter();
81 void SendHasMethodReply(RenderFrameHost* render_frame_host, 77
82 bool result); 78 // Run on any thread.
83 void ProcessMethodInvocationResult(
84 RenderFrameHost* render_frame_host,
85 scoped_refptr<GinJavaMethodInvocationHelper> result);
86 void ProcessMethodInvocationObjectResult(
87 RenderFrameHost* render_frame_host,
88 scoped_refptr<GinJavaMethodInvocationHelper> result);
89 GinJavaBoundObject::ObjectID AddObject( 79 GinJavaBoundObject::ObjectID AddObject(
90 const base::android::JavaRef<jobject>& object, 80 const base::android::JavaRef<jobject>& object,
91 const base::android::JavaRef<jclass>& safe_annotation_clazz, 81 const base::android::JavaRef<jclass>& safe_annotation_clazz,
92 bool is_named, 82 bool is_named,
93 RenderFrameHost* holder); 83 int32 holder);
84 scoped_refptr<GinJavaBoundObject> FindObject(
85 GinJavaBoundObject::ObjectID object_id);
94 bool FindObjectId(const base::android::JavaRef<jobject>& object, 86 bool FindObjectId(const base::android::JavaRef<jobject>& object,
95 GinJavaBoundObject::ObjectID* object_id); 87 GinJavaBoundObject::ObjectID* object_id);
96 void RemoveHolder(RenderFrameHost* holder, 88 void RemoveFromRetainedObjectSetLocked(const JavaObjectWeakGlobalRef& ref);
97 const GinJavaBoundObject::ObjectMap::iterator& from, 89 JavaObjectWeakGlobalRef RemoveHolderAndAdvanceLocked(
98 size_t count); 90 int32 holder,
99 bool HasPendingReply(RenderFrameHost* render_frame_host) const; 91 ObjectMap::iterator* iter_ptr);
100 IPC::Message* TakePendingReply(RenderFrameHost* render_frame_host); 92
93 // Run on the background thread.
94 void OnGetMethods(GinJavaBoundObject::ObjectID object_id,
95 std::set<std::string>* returned_method_names);
96 void OnHasMethod(GinJavaBoundObject::ObjectID object_id,
97 const std::string& method_name,
98 bool* result);
99 void OnInvokeMethod(GinJavaBoundObject::ObjectID object_id,
100 const std::string& method_name,
101 const base::ListValue& arguments,
102 base::ListValue* result,
103 content::GinJavaBridgeError* error_code);
104 void OnObjectWrapperDeleted(GinJavaBoundObject::ObjectID object_id);
105 int GetCurrentRoutingID();
106 void SetCurrentRoutingID(int routing_id);
107
108 bool browser_filter_added_;
109
110 typedef std::map<std::string, GinJavaBoundObject::ObjectID> NamedObjectMap;
111 NamedObjectMap named_objects_;
112
113 // The following objects are used on both threads, so locking must be used.
101 114
102 // Every time a GinJavaBoundObject backed by a real Java object is 115 // Every time a GinJavaBoundObject backed by a real Java object is
103 // created/destroyed, we insert/remove a strong ref to that Java object into 116 // created/destroyed, we insert/remove a strong ref to that Java object into
104 // this set so that it doesn't get garbage collected while it's still 117 // this set so that it doesn't get garbage collected while it's still
105 // potentially in use. Although the set is managed native side, it's owned 118 // potentially in use. Although the set is managed native side, it's owned
106 // and defined in Java so that pushing refs into it does not create new GC 119 // and defined in Java so that pushing refs into it does not create new GC
107 // roots that would prevent ContentViewCore from being garbage collected. 120 // roots that would prevent ContentViewCore from being garbage collected.
108 JavaObjectWeakGlobalRef retained_object_set_; 121 JavaObjectWeakGlobalRef retained_object_set_;
122 // Note that retained_object_set_ does not need to be consistent
123 // with objects_.
124 ObjectMap objects_;
125 base::Lock objects_lock_;
126
127 // The following objects are only used on the background thread.
109 bool allow_object_contents_inspection_; 128 bool allow_object_contents_inspection_;
110 GinJavaBoundObject::ObjectMap objects_; 129 // The routing id of the RenderFrameHost whose request we are processing.
111 typedef std::map<std::string, GinJavaBoundObject::ObjectID> NamedObjectMap; 130 int32 current_routing_id_;
112 NamedObjectMap named_objects_;
113
114 // Keep track of pending calls out to Java such that we can send a synchronous
115 // reply to the renderer waiting on the response should the RenderFrame be
116 // destroyed while the reply is pending.
117 // Only used on the UI thread.
118 typedef std::map<RenderFrameHost*, IPC::Message*> PendingReplyMap;
119 PendingReplyMap pending_replies_;
120 131
121 DISALLOW_COPY_AND_ASSIGN(GinJavaBridgeDispatcherHost); 132 DISALLOW_COPY_AND_ASSIGN(GinJavaBridgeDispatcherHost);
122 }; 133 };
123 134
124 } // namespace content 135 } // namespace content
125 136
126 #endif // CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_BRIDGE_DISPATCHER_HOST_H_ 137 #endif // CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_BRIDGE_DISPATCHER_HOST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698