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 } | |
37 | 35 |
38 AwMessagePortServiceImpl::~AwMessagePortServiceImpl() { | 36 AppWebMessagePortServiceImpl::~AppWebMessagePortServiceImpl() { |
39 JNIEnv* env = AttachCurrentThread(); | 37 JNIEnv* env = AttachCurrentThread(); |
40 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | 38 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
41 if (obj.is_null()) | 39 if (obj.is_null()) |
42 return; | 40 return; |
43 Java_AwMessagePortService_unregisterNativeAwMessagePortService(env, obj); | 41 Java_AppWebMessagePortService_unregisterNativeAppWebMessagePortService(env, |
| 42 obj); |
44 } | 43 } |
45 | 44 |
46 void AwMessagePortServiceImpl::Init(JNIEnv* env, jobject obj) { | 45 void AppWebMessagePortServiceImpl::Init(JNIEnv* env, jobject obj) { |
47 java_ref_ = JavaObjectWeakGlobalRef(env, obj); | 46 java_ref_ = JavaObjectWeakGlobalRef(env, obj); |
48 } | 47 } |
49 | 48 |
50 void AwMessagePortServiceImpl::CreateMessageChannel( | 49 void AppWebMessagePortServiceImpl::CreateMessageChannel( |
51 JNIEnv* env, | 50 JNIEnv* env, |
52 jobjectArray ports, | 51 jobjectArray ports, |
53 scoped_refptr<AwMessagePortMessageFilter> filter) { | 52 WebContents* web_contents) { |
54 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 53 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
55 | 54 RenderFrameHostImpl* rfh = |
| 55 static_cast<RenderFrameHostImpl*>(web_contents->GetMainFrame()); |
| 56 int routing_id = web_contents->GetMainFrame()->GetRoutingID(); |
| 57 scoped_refptr<AppWebMessagePortMessageFilter> filter = |
| 58 rfh->GetAppWebMessagePortMessageFilter(routing_id); |
56 ScopedJavaGlobalRef<jobjectArray>* j_ports = | 59 ScopedJavaGlobalRef<jobjectArray>* j_ports = |
57 new ScopedJavaGlobalRef<jobjectArray>(); | 60 new ScopedJavaGlobalRef<jobjectArray>(); |
58 j_ports->Reset(env, ports); | 61 j_ports->Reset(env, ports); |
59 | 62 |
60 int* portId1 = new int; | 63 int* portId1 = new int; |
61 int* portId2 = new int; | 64 int* portId2 = new int; |
62 BrowserThread::PostTaskAndReply( | 65 BrowserThread::PostTaskAndReply( |
63 BrowserThread::IO, | 66 BrowserThread::IO, FROM_HERE, |
64 FROM_HERE, | 67 base::Bind(&AppWebMessagePortServiceImpl::CreateMessageChannelOnIOThread, |
65 base::Bind(&AwMessagePortServiceImpl::CreateMessageChannelOnIOThread, | 68 base::Unretained(this), filter, portId1, portId2), |
66 base::Unretained(this), | 69 base::Bind(&AppWebMessagePortServiceImpl::OnMessageChannelCreated, |
67 filter, | 70 base::Unretained(this), base::Owned(j_ports), |
68 portId1, | 71 base::Owned(portId1), base::Owned(portId2))); |
69 portId2), | |
70 base::Bind(&AwMessagePortServiceImpl::OnMessageChannelCreated, | |
71 base::Unretained(this), | |
72 base::Owned(j_ports), | |
73 base::Owned(portId1), | |
74 base::Owned(portId2))); | |
75 } | 72 } |
76 | 73 |
77 void AwMessagePortServiceImpl::OnConvertedWebToAppMessage( | 74 void AppWebMessagePortServiceImpl::OnConvertedWebToAppMessage( |
78 int message_port_id, | 75 int message_port_id, |
79 const base::ListValue& message, | 76 const base::ListValue& message, |
80 const std::vector<int>& sent_message_port_ids) { | 77 const std::vector<int>& sent_message_port_ids) { |
81 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 78 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
82 JNIEnv* env = AttachCurrentThread(); | 79 JNIEnv* env = AttachCurrentThread(); |
83 ScopedJavaLocalRef<jobject> jobj = java_ref_.get(env); | 80 ScopedJavaLocalRef<jobject> jobj = java_ref_.get(env); |
84 if (jobj.is_null()) | 81 if (jobj.is_null()) |
85 return; | 82 return; |
86 | 83 |
87 base::string16 value; | 84 base::string16 value; |
88 if (!message.GetString(0, &value)) { | 85 if (!message.GetString(0, &value)) { |
89 LOG(WARNING) << "Converting post message to a string failed for port " | 86 LOG(WARNING) << "Converting post message to a string failed for port " |
90 << message_port_id; | 87 << message_port_id; |
91 return; | 88 return; |
92 } | 89 } |
93 | 90 |
94 if (message.GetSize() != 1) { | 91 if (message.GetSize() != 1) { |
95 NOTREACHED(); | 92 NOTREACHED(); |
96 return; | 93 return; |
97 } | 94 } |
98 | 95 |
99 // Add the ports to AwMessagePortService. | 96 // Add the ports to AppWebMessagePortService. |
100 for (const auto& iter : sent_message_port_ids) { | 97 for (const auto& iter : sent_message_port_ids) { |
101 AddPort(iter, ports_[message_port_id]); | 98 AddPort(iter, ports_[message_port_id]); |
102 } | 99 } |
103 | 100 |
104 ScopedJavaLocalRef<jstring> jmsg = ConvertUTF16ToJavaString(env, value); | 101 ScopedJavaLocalRef<jstring> jmsg = ConvertUTF16ToJavaString(env, value); |
105 ScopedJavaLocalRef<jintArray> jports = | 102 ScopedJavaLocalRef<jintArray> jports = |
106 ToJavaIntArray(env, sent_message_port_ids); | 103 ToJavaIntArray(env, sent_message_port_ids); |
107 Java_AwMessagePortService_onReceivedMessage(env, jobj, message_port_id, jmsg, | 104 Java_AppWebMessagePortService_onReceivedMessage(env, jobj, message_port_id, |
108 jports); | 105 jmsg, jports); |
109 } | 106 } |
110 | 107 |
111 void AwMessagePortServiceImpl::OnMessagePortMessageFilterClosing( | 108 void AppWebMessagePortServiceImpl::OnMessagePortMessageFilterClosing( |
112 AwMessagePortMessageFilter* filter) { | 109 AppWebMessagePortMessageFilter* filter) { |
113 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 110 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
114 for (MessagePorts::iterator iter = ports_.begin(); | 111 for (MessagePorts::iterator iter = ports_.begin(); iter != ports_.end(); |
115 iter != ports_.end(); iter++) { | 112 iter++) { |
116 if (iter->second == filter) { | 113 if (iter->second == filter) { |
117 ports_.erase(iter); | 114 ports_.erase(iter); |
118 } | 115 } |
119 } | 116 } |
120 } | 117 } |
121 | 118 |
122 void AwMessagePortServiceImpl::PostAppToWebMessage( | 119 void AppWebMessagePortServiceImpl::PostAppToWebMessage( |
123 JNIEnv* env, | 120 JNIEnv* env, |
124 const JavaParamRef<jobject>& obj, | 121 const JavaParamRef<jobject>& obj, |
125 int sender_id, | 122 int sender_id, |
126 const JavaParamRef<jstring>& message, | 123 const JavaParamRef<jstring>& message, |
127 const JavaParamRef<jintArray>& sent_ports) { | 124 const JavaParamRef<jintArray>& sent_ports) { |
128 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 125 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
129 base::string16* j_message = new base::string16; | 126 base::string16* j_message = new base::string16; |
130 ConvertJavaStringToUTF16(env, message, j_message); | 127 ConvertJavaStringToUTF16(env, message, j_message); |
131 std::vector<int>* j_sent_ports = new std::vector<int>; | 128 std::vector<int>* j_sent_ports = new std::vector<int>; |
132 if (sent_ports != nullptr) | 129 if (sent_ports != nullptr) |
133 base::android::JavaIntArrayToIntVector(env, sent_ports, j_sent_ports); | 130 base::android::JavaIntArrayToIntVector(env, sent_ports, j_sent_ports); |
134 | 131 |
135 BrowserThread::PostTask( | 132 BrowserThread::PostTask( |
136 BrowserThread::IO, | 133 BrowserThread::IO, FROM_HERE, |
137 FROM_HERE, | 134 base::Bind(&AppWebMessagePortServiceImpl::PostAppToWebMessageOnIOThread, |
138 base::Bind(&AwMessagePortServiceImpl::PostAppToWebMessageOnIOThread, | 135 base::Unretained(this), sender_id, base::Owned(j_message), |
139 base::Unretained(this), | |
140 sender_id, | |
141 base::Owned(j_message), | |
142 base::Owned(j_sent_ports))); | 136 base::Owned(j_sent_ports))); |
143 } | 137 } |
144 | 138 |
145 // The message port service cannot immediately close the port, because | 139 // The message port service cannot immediately close the port, because |
146 // it is possible that messages are still queued in the renderer process | 140 // it is possible that messages are still queued in the renderer process |
147 // waiting for a conversion. Instead, it sends a special message with | 141 // waiting for a conversion. Instead, it sends a special message with |
148 // a flag which indicates that this message port should be closed. | 142 // a flag which indicates that this message port should be closed. |
149 void AwMessagePortServiceImpl::ClosePort(JNIEnv* env, | 143 void AppWebMessagePortServiceImpl::ClosePort(JNIEnv* env, |
150 const JavaParamRef<jobject>& obj, | 144 const JavaParamRef<jobject>& obj, |
151 int message_port_id) { | 145 int message_port_id) { |
152 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 146 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
153 BrowserThread::PostTask( | 147 BrowserThread::PostTask( |
154 BrowserThread::IO, | 148 BrowserThread::IO, FROM_HERE, |
155 FROM_HERE, | 149 base::Bind(&AppWebMessagePortServiceImpl::PostClosePortMessage, |
156 base::Bind(&AwMessagePortServiceImpl::PostClosePortMessage, | 150 base::Unretained(this), message_port_id)); |
157 base::Unretained(this), | 151 } |
| 152 |
| 153 void AppWebMessagePortServiceImpl::ReleaseMessages( |
| 154 JNIEnv* env, |
| 155 const JavaParamRef<jobject>& obj, |
| 156 int message_port_id) { |
| 157 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 158 BrowserThread::PostTask( |
| 159 BrowserThread::IO, FROM_HERE, |
| 160 base::Bind(&MessagePortService::ReleaseMessages, |
| 161 base::Unretained(MessagePortService::GetInstance()), |
158 message_port_id)); | 162 message_port_id)); |
159 } | 163 } |
160 | 164 |
161 void AwMessagePortServiceImpl::ReleaseMessages(JNIEnv* env, | 165 void AppWebMessagePortServiceImpl::RemoveSentPorts( |
162 const JavaParamRef<jobject>& obj, | |
163 int message_port_id) { | |
164 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
165 BrowserThread::PostTask( | |
166 BrowserThread::IO, | |
167 FROM_HERE, | |
168 base::Bind(&MessagePortProvider::ReleaseMessages, message_port_id)); | |
169 } | |
170 | |
171 void AwMessagePortServiceImpl::RemoveSentPorts( | |
172 const std::vector<int>& sent_ports) { | 166 const std::vector<int>& sent_ports) { |
173 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 167 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
174 // Remove the filters that are associated with the transferred ports | 168 // Remove the filters that are associated with the transferred ports |
175 for (const auto& iter : sent_ports) | 169 for (const auto& iter : sent_ports) |
176 ports_.erase(iter); | 170 ports_.erase(iter); |
177 } | 171 } |
178 | 172 |
179 void AwMessagePortServiceImpl::PostAppToWebMessageOnIOThread( | 173 void AppWebMessagePortServiceImpl::PostAppToWebMessageOnIOThread( |
180 int sender_id, | 174 int sender_id, |
181 base::string16* message, | 175 base::string16* message, |
182 std::vector<int>* sent_ports) { | 176 std::vector<int>* sent_ports) { |
183 RemoveSentPorts(*sent_ports); | 177 RemoveSentPorts(*sent_ports); |
184 ports_[sender_id]->SendAppToWebMessage(sender_id, *message, *sent_ports); | 178 ports_[sender_id]->SendAppToWebMessage(sender_id, *message, *sent_ports); |
185 } | 179 } |
186 | 180 |
187 void AwMessagePortServiceImpl::PostClosePortMessage(int message_port_id) { | 181 void AppWebMessagePortServiceImpl::PostClosePortMessage(int message_port_id) { |
188 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 182 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
189 ports_[message_port_id]->SendClosePortMessage(message_port_id); | 183 ports_[message_port_id]->SendClosePortMessage(message_port_id); |
190 } | 184 } |
191 | 185 |
192 void AwMessagePortServiceImpl::CleanupPort(int message_port_id) { | 186 void AppWebMessagePortServiceImpl::CleanupPort(int message_port_id) { |
193 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 187 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
194 ports_.erase(message_port_id); | 188 ports_.erase(message_port_id); |
195 } | 189 } |
196 | 190 |
197 void AwMessagePortServiceImpl::CreateMessageChannelOnIOThread( | 191 void AppWebMessagePortServiceImpl::CreateMessageChannelOnIOThread( |
198 scoped_refptr<AwMessagePortMessageFilter> filter, | 192 scoped_refptr<AppWebMessagePortMessageFilter> filter, |
199 int* portId1, | 193 int* portId1, |
200 int* portId2) { | 194 int* portId2) { |
201 MessagePortProvider::CreateMessageChannel(filter.get(), portId1, portId2); | 195 *portId1 = 0; |
202 MessagePortProvider::HoldMessages(*portId1); | 196 *portId2 = 0; |
203 MessagePortProvider::HoldMessages(*portId2); | 197 MessagePortService* msp = MessagePortService::GetInstance(); |
| 198 msp->Create(MSG_ROUTING_NONE, filter.get(), portId1); |
| 199 msp->Create(MSG_ROUTING_NONE, filter.get(), portId2); |
| 200 // Update the routing number of the message ports to be equal to the message |
| 201 // port numbers. |
| 202 msp->UpdateMessagePort(*portId1, filter.get(), *portId1); |
| 203 msp->UpdateMessagePort(*portId2, filter.get(), *portId2); |
| 204 msp->Entangle(*portId1, *portId2); |
| 205 msp->Entangle(*portId2, *portId1); |
| 206 |
| 207 msp->HoldMessages(*portId1); |
| 208 msp->HoldMessages(*portId2); |
204 AddPort(*portId1, filter.get()); | 209 AddPort(*portId1, filter.get()); |
205 AddPort(*portId2, filter.get()); | 210 AddPort(*portId2, filter.get()); |
206 } | 211 } |
207 | 212 |
208 void AwMessagePortServiceImpl::OnMessageChannelCreated( | 213 void AppWebMessagePortServiceImpl::OnMessageChannelCreated( |
209 ScopedJavaGlobalRef<jobjectArray>* ports, | 214 ScopedJavaGlobalRef<jobjectArray>* ports, |
210 int* port1, | 215 int* port1, |
211 int* port2) { | 216 int* port2) { |
212 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 217 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
213 JNIEnv* env = AttachCurrentThread(); | 218 JNIEnv* env = AttachCurrentThread(); |
214 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | 219 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
215 if (obj.is_null()) | 220 if (obj.is_null()) |
216 return; | 221 return; |
217 Java_AwMessagePortService_onMessageChannelCreated(env, obj, *port1, *port2, | 222 Java_AppWebMessagePortService_onMessageChannelCreated(env, obj, *port1, |
218 *ports); | 223 *port2, *ports); |
219 } | 224 } |
220 | 225 |
221 // Adds a new port to the message port service. | 226 // Adds a new port to the message port service. |
222 void AwMessagePortServiceImpl::AddPort(int message_port_id, | 227 void AppWebMessagePortServiceImpl::AddPort( |
223 AwMessagePortMessageFilter* filter) { | 228 int message_port_id, |
| 229 AppWebMessagePortMessageFilter* filter) { |
224 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 230 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
225 if (ports_.count(message_port_id)) { | 231 if (ports_.count(message_port_id)) { |
226 NOTREACHED(); | 232 NOTREACHED(); |
227 return; | 233 return; |
228 } | 234 } |
229 ports_[message_port_id] = filter; | 235 ports_[message_port_id] = filter; |
230 } | 236 } |
231 | 237 |
232 bool RegisterAwMessagePortService(JNIEnv* env) { | 238 bool RegisterAppWebMessagePortService(JNIEnv* env) { |
233 return RegisterNativesImpl(env); | 239 return RegisterNativesImpl(env); |
234 } | 240 } |
235 | 241 |
236 // static | 242 // static |
237 jlong InitAwMessagePortService(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 243 jlong InitAppWebMessagePortService(JNIEnv* env, |
238 AwMessagePortServiceImpl* service = AwMessagePortServiceImpl::GetInstance(); | 244 const JavaParamRef<jobject>& obj) { |
| 245 AppWebMessagePortServiceImpl* service = |
| 246 AppWebMessagePortServiceImpl::GetInstance(); |
239 service->Init(env, obj); | 247 service->Init(env, obj); |
240 return reinterpret_cast<intptr_t>(service); | 248 return reinterpret_cast<intptr_t>(service); |
241 } | 249 } |
242 | 250 |
243 } // namespace android_webview | 251 } // namespace content |
OLD | NEW |