| 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 "android_webview/native/aw_message_port_service_impl.h" |
| 6 | 6 |
| 7 #include "android_webview/browser/aw_browser_context.h" | 7 #include "android_webview/browser/aw_browser_context.h" |
| 8 #include "android_webview/browser/aw_message_port_message_filter.h" | 8 #include "android_webview/browser/aw_message_port_message_filter.h" |
| 9 #include "android_webview/native/aw_contents.h" | 9 #include "android_webview/native/aw_contents.h" |
| 10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 if (sent_ports != nullptr) | 125 if (sent_ports != nullptr) |
| 126 base::android::JavaIntArrayToIntVector(env, sent_ports, j_sent_ports); | 126 base::android::JavaIntArrayToIntVector(env, sent_ports, j_sent_ports); |
| 127 | 127 |
| 128 BrowserThread::PostTask( | 128 BrowserThread::PostTask( |
| 129 BrowserThread::IO, | 129 BrowserThread::IO, |
| 130 FROM_HERE, | 130 FROM_HERE, |
| 131 base::Bind(&AwMessagePortServiceImpl::PostAppToWebMessageOnIOThread, | 131 base::Bind(&AwMessagePortServiceImpl::PostAppToWebMessageOnIOThread, |
| 132 base::Unretained(this), | 132 base::Unretained(this), |
| 133 sender_id, | 133 sender_id, |
| 134 base::Owned(j_message), | 134 base::Owned(j_message), |
| 135 base::Owned(j_sent_ports))); | 135 base::Owned(j_sent_ports), |
| 136 false)); |
| 137 } |
| 138 |
| 139 // The message port service cannot immediately close the port, because |
| 140 // it is possible that messages are still queued in the renderer process |
| 141 // waiting for a conversion. Instead, it sends a special message with |
| 142 // a flag which indicates that this message port should be closed. |
| 143 void AwMessagePortServiceImpl::ClosePort(JNIEnv* env, jobject obj, |
| 144 int message_port_id) { |
| 145 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 146 base::string16* j_message = new base::string16; |
| 147 std::vector<int>* j_sent_ports = new std::vector<int>; |
| 148 BrowserThread::PostTask( |
| 149 BrowserThread::IO, |
| 150 FROM_HERE, |
| 151 base::Bind(&AwMessagePortServiceImpl::PostAppToWebMessageOnIOThread, |
| 152 base::Unretained(this), |
| 153 message_port_id, |
| 154 base::Owned(j_message), |
| 155 base::Owned(j_sent_ports), |
| 156 true /* close the port */)); |
| 136 } | 157 } |
| 137 | 158 |
| 138 void AwMessagePortServiceImpl::RemoveSentPorts( | 159 void AwMessagePortServiceImpl::RemoveSentPorts( |
| 139 const std::vector<int>& sent_ports) { | 160 const std::vector<int>& sent_ports) { |
| 140 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 161 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 141 // Remove the filters that are associated with the transferred ports | 162 // Remove the filters that are associated with the transferred ports |
| 142 for (const auto& iter : sent_ports) | 163 for (const auto& iter : sent_ports) |
| 143 ports_.erase(iter); | 164 ports_.erase(iter); |
| 144 } | 165 } |
| 145 | 166 |
| 146 void AwMessagePortServiceImpl::PostAppToWebMessageOnIOThread( | 167 void AwMessagePortServiceImpl::PostAppToWebMessageOnIOThread( |
| 147 int sender_id, | 168 int sender_id, |
| 148 base::string16* message, | 169 base::string16* message, |
| 149 std::vector<int>* sent_ports) { | 170 std::vector<int>* sent_ports, |
| 171 bool close_port) { |
| 150 RemoveSentPorts(*sent_ports); | 172 RemoveSentPorts(*sent_ports); |
| 151 ports_[sender_id]->SendAppToWebMessage(sender_id, *message, *sent_ports); | 173 if (!ports_[sender_id]) { |
| 174 NOTREACHED(); |
| 175 } |
| 176 ports_[sender_id]->SendAppToWebMessage(sender_id, *message, *sent_ports, |
| 177 close_port); |
| 152 } | 178 } |
| 153 | 179 |
| 180 void AwMessagePortServiceImpl::CleanupPort(int message_port_id) { |
| 181 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 182 ports_.erase(message_port_id); |
| 183 } |
| 154 | 184 |
| 155 void AwMessagePortServiceImpl::CreateMessageChannelOnIOThread( | 185 void AwMessagePortServiceImpl::CreateMessageChannelOnIOThread( |
| 156 scoped_refptr<AwMessagePortMessageFilter> filter, | 186 scoped_refptr<AwMessagePortMessageFilter> filter, |
| 157 int* portId1, | 187 int* portId1, |
| 158 int* portId2) { | 188 int* portId2) { |
| 159 MessagePortProvider::CreateMessageChannel(filter.get(), portId1, portId2); | 189 MessagePortProvider::CreateMessageChannel(filter.get(), portId1, portId2); |
| 160 AddPort(*portId1, filter.get()); | 190 AddPort(*portId1, filter.get()); |
| 161 AddPort(*portId2, filter.get()); | 191 AddPort(*portId2, filter.get()); |
| 162 } | 192 } |
| 163 | 193 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 189 } | 219 } |
| 190 | 220 |
| 191 // static | 221 // static |
| 192 jlong InitAwMessagePortService(JNIEnv* env, jobject obj) { | 222 jlong InitAwMessagePortService(JNIEnv* env, jobject obj) { |
| 193 AwMessagePortServiceImpl* service = AwMessagePortServiceImpl::GetInstance(); | 223 AwMessagePortServiceImpl* service = AwMessagePortServiceImpl::GetInstance(); |
| 194 service->Init(env, obj); | 224 service->Init(env, obj); |
| 195 return reinterpret_cast<intptr_t>(service); | 225 return reinterpret_cast<intptr_t>(service); |
| 196 } | 226 } |
| 197 | 227 |
| 198 } // namespace android_webview | 228 } // namespace android_webview |
| OLD | NEW |