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