OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #include "android_webview/native/aw_message_port_service_impl.h" | 5 #include "content/browser/android/app_web_message_port_service_impl.h" |
6 | 6 |
7 #include "android_webview/browser/aw_browser_context.h" | |
8 #include "android_webview/browser/aw_message_port_message_filter.h" | |
9 #include "android_webview/native/aw_contents.h" | |
10 #include "base/android/jni_array.h" | 7 #include "base/android/jni_array.h" |
11 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
12 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "content/browser/android/app_web_message_port_message_filter.h" | |
11 #include "content/browser/message_port_service.h" | |
12 #include "content/browser/renderer_host/render_process_host_impl.h" | |
13 #include "content/browser/web_contents/web_contents_impl.h" | |
13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/browser/message_port_provider.h" | 15 #include "content/public/browser/message_port_provider.h" |
15 #include "jni/AwMessagePortService_jni.h" | 16 #include "jni/AppWebMessagePortService_jni.h" |
16 | 17 |
17 namespace android_webview { | 18 namespace content { |
18 | 19 |
19 using base::android::AttachCurrentThread; | 20 using base::android::AttachCurrentThread; |
20 using base::android::ConvertJavaStringToUTF16; | 21 using base::android::ConvertJavaStringToUTF16; |
21 using base::android::ConvertUTF16ToJavaString; | 22 using base::android::ConvertUTF16ToJavaString; |
22 using base::android::JavaParamRef; | 23 using base::android::JavaParamRef; |
23 using base::android::ScopedJavaGlobalRef; | 24 using base::android::ScopedJavaGlobalRef; |
24 using base::android::ScopedJavaLocalRef; | 25 using base::android::ScopedJavaLocalRef; |
25 using base::android::ToJavaIntArray; | 26 using base::android::ToJavaIntArray; |
26 using content::BrowserThread; | 27 using content::BrowserThread; |
27 using content::MessagePortProvider; | 28 using content::MessagePortProvider; |
28 | 29 |
29 // static | 30 AppWebMessagePortServiceImpl* AppWebMessagePortServiceImpl::GetInstance() { |
30 AwMessagePortServiceImpl* AwMessagePortServiceImpl::GetInstance() { | 31 return base::Singleton<AppWebMessagePortServiceImpl>::get(); |
31 return static_cast<AwMessagePortServiceImpl*>( | |
32 AwBrowserContext::GetDefault()->GetMessagePortService()); | |
33 } | 32 } |
34 | 33 |
35 AwMessagePortServiceImpl::AwMessagePortServiceImpl() { | 34 AppWebMessagePortServiceImpl::AppWebMessagePortServiceImpl() { |
36 } | 35 } |
37 | 36 |
38 AwMessagePortServiceImpl::~AwMessagePortServiceImpl() { | 37 AppWebMessagePortServiceImpl::~AppWebMessagePortServiceImpl() { |
39 JNIEnv* env = AttachCurrentThread(); | 38 JNIEnv* env = AttachCurrentThread(); |
40 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | 39 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
41 if (obj.is_null()) | 40 if (obj.is_null()) |
42 return; | 41 return; |
43 Java_AwMessagePortService_unregisterNativeAwMessagePortService(env, obj); | 42 Java_AppWebMessagePortService_unregisterNativeAppWebMessagePortService(env, |
43 obj); | |
44 } | 44 } |
45 | 45 |
46 void AwMessagePortServiceImpl::Init(JNIEnv* env, jobject obj) { | 46 void AppWebMessagePortServiceImpl::Init(JNIEnv* env, jobject obj) { |
47 java_ref_ = JavaObjectWeakGlobalRef(env, obj); | 47 java_ref_ = JavaObjectWeakGlobalRef(env, obj); |
48 } | 48 } |
49 | 49 |
50 void AwMessagePortServiceImpl::CreateMessageChannel( | 50 void AppWebMessagePortServiceImpl::CreateMessageChannel( |
51 JNIEnv* env, | 51 JNIEnv* env, |
52 jobjectArray ports, | 52 jobjectArray ports, |
53 scoped_refptr<AwMessagePortMessageFilter> filter) { | 53 WebContents* web_contents) { |
54 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 54 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
55 | 55 AppWebMessagePortMessageFilter* filter = new AppWebMessagePortMessageFilter( |
sgurun-gerrit only
2016/10/06 22:57:39
I think this is wrong.
The logic in aw_contents k
Yusuf
2016/10/07 21:20:27
Oops, you are right.
Moved the ownership to rende
| |
56 web_contents->GetMainFrame()->GetRoutingID()); | |
57 static_cast<RenderFrameHostImpl*>(web_contents->GetMainFrame()) | |
58 ->set_app_web_message_port_delegate(filter); | |
59 RenderProcessHostImpl* rph = static_cast<RenderProcessHostImpl*>( | |
60 web_contents->GetRenderProcessHost()); | |
61 rph->AddFilter(filter); | |
56 ScopedJavaGlobalRef<jobjectArray>* j_ports = | 62 ScopedJavaGlobalRef<jobjectArray>* j_ports = |
57 new ScopedJavaGlobalRef<jobjectArray>(); | 63 new ScopedJavaGlobalRef<jobjectArray>(); |
58 j_ports->Reset(env, ports); | 64 j_ports->Reset(env, ports); |
59 | 65 |
60 int* portId1 = new int; | 66 int* portId1 = new int; |
61 int* portId2 = new int; | 67 int* portId2 = new int; |
62 BrowserThread::PostTaskAndReply( | 68 BrowserThread::PostTaskAndReply( |
63 BrowserThread::IO, | 69 BrowserThread::IO, |
64 FROM_HERE, | 70 FROM_HERE, |
65 base::Bind(&AwMessagePortServiceImpl::CreateMessageChannelOnIOThread, | 71 base::Bind(&AppWebMessagePortServiceImpl::CreateMessageChannelOnIOThread, |
66 base::Unretained(this), | 72 base::Unretained(this), |
67 filter, | 73 filter, |
68 portId1, | 74 portId1, |
69 portId2), | 75 portId2), |
70 base::Bind(&AwMessagePortServiceImpl::OnMessageChannelCreated, | 76 base::Bind(&AppWebMessagePortServiceImpl::OnMessageChannelCreated, |
71 base::Unretained(this), | 77 base::Unretained(this), |
72 base::Owned(j_ports), | 78 base::Owned(j_ports), |
73 base::Owned(portId1), | 79 base::Owned(portId1), |
74 base::Owned(portId2))); | 80 base::Owned(portId2))); |
75 } | 81 } |
76 | 82 |
77 void AwMessagePortServiceImpl::OnConvertedWebToAppMessage( | 83 void AppWebMessagePortServiceImpl::OnConvertedWebToAppMessage( |
78 int message_port_id, | 84 int message_port_id, |
79 const base::ListValue& message, | 85 const base::ListValue& message, |
80 const std::vector<int>& sent_message_port_ids) { | 86 const std::vector<int>& sent_message_port_ids) { |
87 | |
81 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 88 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
82 JNIEnv* env = AttachCurrentThread(); | 89 JNIEnv* env = AttachCurrentThread(); |
83 ScopedJavaLocalRef<jobject> jobj = java_ref_.get(env); | 90 ScopedJavaLocalRef<jobject> jobj = java_ref_.get(env); |
84 if (jobj.is_null()) | 91 if (jobj.is_null()) |
85 return; | 92 return; |
86 | 93 |
87 base::string16 value; | 94 base::string16 value; |
88 if (!message.GetString(0, &value)) { | 95 if (!message.GetString(0, &value)) { |
89 LOG(WARNING) << "Converting post message to a string failed for port " | 96 LOG(WARNING) << "Converting post message to a string failed for port " |
90 << message_port_id; | 97 << message_port_id; |
91 return; | 98 return; |
92 } | 99 } |
93 | 100 |
94 if (message.GetSize() != 1) { | 101 if (message.GetSize() != 1) { |
95 NOTREACHED(); | 102 NOTREACHED(); |
96 return; | 103 return; |
97 } | 104 } |
98 | 105 |
99 // Add the ports to AwMessagePortService. | 106 // Add the ports to AppWebMessagePortService. |
100 for (const auto& iter : sent_message_port_ids) { | 107 for (const auto& iter : sent_message_port_ids) { |
101 AddPort(iter, ports_[message_port_id]); | 108 AddPort(iter, ports_[message_port_id]); |
102 } | 109 } |
103 | 110 |
104 ScopedJavaLocalRef<jstring> jmsg = ConvertUTF16ToJavaString(env, value); | 111 ScopedJavaLocalRef<jstring> jmsg = ConvertUTF16ToJavaString(env, value); |
105 ScopedJavaLocalRef<jintArray> jports = | 112 ScopedJavaLocalRef<jintArray> jports = |
106 ToJavaIntArray(env, sent_message_port_ids); | 113 ToJavaIntArray(env, sent_message_port_ids); |
107 Java_AwMessagePortService_onReceivedMessage(env, jobj, message_port_id, jmsg, | 114 Java_AppWebMessagePortService_onReceivedMessage(env, |
115 jobj, | |
116 message_port_id, | |
117 jmsg, | |
108 jports); | 118 jports); |
109 } | 119 } |
110 | 120 |
111 void AwMessagePortServiceImpl::OnMessagePortMessageFilterClosing( | 121 void AppWebMessagePortServiceImpl::OnMessagePortMessageFilterClosing( |
112 AwMessagePortMessageFilter* filter) { | 122 AppWebMessagePortMessageFilter* filter) { |
113 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 123 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
114 for (MessagePorts::iterator iter = ports_.begin(); | 124 for (MessagePorts::iterator iter = ports_.begin(); |
115 iter != ports_.end(); iter++) { | 125 iter != ports_.end(); iter++) { |
116 if (iter->second == filter) { | 126 if (iter->second == filter) { |
117 ports_.erase(iter); | 127 ports_.erase(iter); |
118 } | 128 } |
119 } | 129 } |
120 } | 130 } |
121 | 131 |
122 void AwMessagePortServiceImpl::PostAppToWebMessage( | 132 void AppWebMessagePortServiceImpl::PostAppToWebMessage( |
123 JNIEnv* env, | 133 JNIEnv* env, |
124 const JavaParamRef<jobject>& obj, | 134 const JavaParamRef<jobject>& obj, |
125 int sender_id, | 135 int sender_id, |
126 const JavaParamRef<jstring>& message, | 136 const JavaParamRef<jstring>& message, |
127 const JavaParamRef<jintArray>& sent_ports) { | 137 const JavaParamRef<jintArray>& sent_ports) { |
128 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 138 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
129 base::string16* j_message = new base::string16; | 139 base::string16* j_message = new base::string16; |
130 ConvertJavaStringToUTF16(env, message, j_message); | 140 ConvertJavaStringToUTF16(env, message, j_message); |
131 std::vector<int>* j_sent_ports = new std::vector<int>; | 141 std::vector<int>* j_sent_ports = new std::vector<int>; |
132 if (sent_ports != nullptr) | 142 if (sent_ports != nullptr) |
133 base::android::JavaIntArrayToIntVector(env, sent_ports, j_sent_ports); | 143 base::android::JavaIntArrayToIntVector(env, sent_ports, j_sent_ports); |
134 | 144 |
135 BrowserThread::PostTask( | 145 BrowserThread::PostTask( |
136 BrowserThread::IO, | 146 BrowserThread::IO, |
137 FROM_HERE, | 147 FROM_HERE, |
138 base::Bind(&AwMessagePortServiceImpl::PostAppToWebMessageOnIOThread, | 148 base::Bind(&AppWebMessagePortServiceImpl::PostAppToWebMessageOnIOThread, |
139 base::Unretained(this), | 149 base::Unretained(this), |
140 sender_id, | 150 sender_id, |
141 base::Owned(j_message), | 151 base::Owned(j_message), |
142 base::Owned(j_sent_ports))); | 152 base::Owned(j_sent_ports))); |
143 } | 153 } |
144 | 154 |
145 // The message port service cannot immediately close the port, because | 155 // The message port service cannot immediately close the port, because |
146 // it is possible that messages are still queued in the renderer process | 156 // it is possible that messages are still queued in the renderer process |
147 // waiting for a conversion. Instead, it sends a special message with | 157 // waiting for a conversion. Instead, it sends a special message with |
148 // a flag which indicates that this message port should be closed. | 158 // a flag which indicates that this message port should be closed. |
149 void AwMessagePortServiceImpl::ClosePort(JNIEnv* env, | 159 void AppWebMessagePortServiceImpl::ClosePort(JNIEnv* env, |
150 const JavaParamRef<jobject>& obj, | 160 const JavaParamRef<jobject>& obj, |
151 int message_port_id) { | 161 int message_port_id) { |
152 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 162 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
153 BrowserThread::PostTask( | 163 BrowserThread::PostTask( |
154 BrowserThread::IO, | 164 BrowserThread::IO, |
155 FROM_HERE, | 165 FROM_HERE, |
156 base::Bind(&AwMessagePortServiceImpl::PostClosePortMessage, | 166 base::Bind(&AppWebMessagePortServiceImpl::PostClosePortMessage, |
157 base::Unretained(this), | 167 base::Unretained(this), |
158 message_port_id)); | 168 message_port_id)); |
159 } | 169 } |
160 | 170 |
161 void AwMessagePortServiceImpl::ReleaseMessages(JNIEnv* env, | 171 void AppWebMessagePortServiceImpl::ReleaseMessages(JNIEnv* env, |
162 const JavaParamRef<jobject>& obj, | 172 const JavaParamRef<jobject>& obj, |
163 int message_port_id) { | 173 int message_port_id) { |
164 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 174 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
165 BrowserThread::PostTask( | 175 BrowserThread::PostTask( |
166 BrowserThread::IO, | 176 BrowserThread::IO, |
167 FROM_HERE, | 177 FROM_HERE, |
168 base::Bind(&MessagePortProvider::ReleaseMessages, message_port_id)); | 178 base::Bind(&MessagePortService::ReleaseMessages, |
179 base::Unretained( | |
180 MessagePortService::GetInstance()), message_port_id)); | |
169 } | 181 } |
170 | 182 |
171 void AwMessagePortServiceImpl::RemoveSentPorts( | 183 void AppWebMessagePortServiceImpl::RemoveSentPorts( |
172 const std::vector<int>& sent_ports) { | 184 const std::vector<int>& sent_ports) { |
173 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 185 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
174 // Remove the filters that are associated with the transferred ports | 186 // Remove the filters that are associated with the transferred ports |
175 for (const auto& iter : sent_ports) | 187 for (const auto& iter : sent_ports) |
176 ports_.erase(iter); | 188 ports_.erase(iter); |
177 } | 189 } |
178 | 190 |
179 void AwMessagePortServiceImpl::PostAppToWebMessageOnIOThread( | 191 void AppWebMessagePortServiceImpl::PostAppToWebMessageOnIOThread( |
180 int sender_id, | 192 int sender_id, |
181 base::string16* message, | 193 base::string16* message, |
182 std::vector<int>* sent_ports) { | 194 std::vector<int>* sent_ports) { |
183 RemoveSentPorts(*sent_ports); | 195 RemoveSentPorts(*sent_ports); |
184 ports_[sender_id]->SendAppToWebMessage(sender_id, *message, *sent_ports); | 196 ports_[sender_id]->SendAppToWebMessage(sender_id, *message, *sent_ports); |
185 } | 197 } |
186 | 198 |
187 void AwMessagePortServiceImpl::PostClosePortMessage(int message_port_id) { | 199 void AppWebMessagePortServiceImpl::PostClosePortMessage(int message_port_id) { |
188 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 200 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
189 ports_[message_port_id]->SendClosePortMessage(message_port_id); | 201 ports_[message_port_id]->SendClosePortMessage(message_port_id); |
190 } | 202 } |
191 | 203 |
192 void AwMessagePortServiceImpl::CleanupPort(int message_port_id) { | 204 void AppWebMessagePortServiceImpl::CleanupPort(int message_port_id) { |
193 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 205 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
194 ports_.erase(message_port_id); | 206 ports_.erase(message_port_id); |
195 } | 207 } |
196 | 208 |
197 void AwMessagePortServiceImpl::CreateMessageChannelOnIOThread( | 209 void AppWebMessagePortServiceImpl::CreateMessageChannelOnIOThread( |
198 scoped_refptr<AwMessagePortMessageFilter> filter, | 210 scoped_refptr<AppWebMessagePortMessageFilter> filter, |
199 int* portId1, | 211 int* portId1, |
200 int* portId2) { | 212 int* portId2) { |
201 MessagePortProvider::CreateMessageChannel(filter.get(), portId1, portId2); | 213 *portId1 = 0; |
202 MessagePortProvider::HoldMessages(*portId1); | 214 *portId2 = 0; |
203 MessagePortProvider::HoldMessages(*portId2); | 215 MessagePortService* msp = MessagePortService::GetInstance(); |
216 msp->Create(MSG_ROUTING_NONE, filter.get(), portId1); | |
217 msp->Create(MSG_ROUTING_NONE, filter.get(), portId2); | |
218 // Update the routing number of the message ports to be equal to the message | |
219 // port numbers. | |
220 msp->UpdateMessagePort(*portId1, filter.get(), *portId1); | |
221 msp->UpdateMessagePort(*portId2, filter.get(), *portId2); | |
222 msp->Entangle(*portId1, *portId2); | |
223 msp->Entangle(*portId2, *portId1); | |
224 | |
225 MessagePortService::GetInstance()->HoldMessages(*portId1); | |
sgurun-gerrit only
2016/10/06 22:57:39
use msp here and below.
Yusuf
2016/10/07 21:20:27
Done.
| |
226 MessagePortService::GetInstance()->HoldMessages(*portId2); | |
204 AddPort(*portId1, filter.get()); | 227 AddPort(*portId1, filter.get()); |
205 AddPort(*portId2, filter.get()); | 228 AddPort(*portId2, filter.get()); |
206 } | 229 } |
207 | 230 |
208 void AwMessagePortServiceImpl::OnMessageChannelCreated( | 231 void AppWebMessagePortServiceImpl::OnMessageChannelCreated( |
209 ScopedJavaGlobalRef<jobjectArray>* ports, | 232 ScopedJavaGlobalRef<jobjectArray>* ports, |
210 int* port1, | 233 int* port1, |
211 int* port2) { | 234 int* port2) { |
212 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 235 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
213 JNIEnv* env = AttachCurrentThread(); | 236 JNIEnv* env = AttachCurrentThread(); |
214 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | 237 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
215 if (obj.is_null()) | 238 if (obj.is_null()) |
216 return; | 239 return; |
217 Java_AwMessagePortService_onMessageChannelCreated(env, obj, *port1, *port2, | 240 Java_AppWebMessagePortService_onMessageChannelCreated(env, obj, |
218 *ports); | 241 *port1, *port2, |
242 *ports); | |
219 } | 243 } |
220 | 244 |
221 // Adds a new port to the message port service. | 245 // Adds a new port to the message port service. |
222 void AwMessagePortServiceImpl::AddPort(int message_port_id, | 246 void AppWebMessagePortServiceImpl::AddPort( |
223 AwMessagePortMessageFilter* filter) { | 247 int message_port_id, AppWebMessagePortMessageFilter* filter) { |
224 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 248 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
225 if (ports_.count(message_port_id)) { | 249 if (ports_.count(message_port_id)) { |
226 NOTREACHED(); | 250 NOTREACHED(); |
227 return; | 251 return; |
228 } | 252 } |
229 ports_[message_port_id] = filter; | 253 ports_[message_port_id] = filter; |
230 } | 254 } |
231 | 255 |
232 bool RegisterAwMessagePortService(JNIEnv* env) { | 256 bool RegisterAppWebMessagePortService(JNIEnv* env) { |
233 return RegisterNativesImpl(env); | 257 return RegisterNativesImpl(env); |
234 } | 258 } |
235 | 259 |
236 // static | 260 // static |
237 jlong InitAwMessagePortService(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 261 jlong InitAppWebMessagePortService( |
238 AwMessagePortServiceImpl* service = AwMessagePortServiceImpl::GetInstance(); | 262 JNIEnv* env, const JavaParamRef<jobject>& obj) { |
263 AppWebMessagePortServiceImpl* service = | |
264 AppWebMessagePortServiceImpl::GetInstance(); | |
239 service->Init(env, obj); | 265 service->Init(env, obj); |
240 return reinterpret_cast<intptr_t>(service); | 266 return reinterpret_cast<intptr_t>(service); |
241 } | 267 } |
242 | 268 |
243 } // namespace android_webview | 269 } // namespace content |
OLD | NEW |