| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/android/app_web_message_port_service_impl.h" | |
| 6 | |
| 7 #include "base/android/jni_array.h" | |
| 8 #include "base/android/jni_string.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" | |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 #include "content/public/browser/message_port_provider.h" | |
| 16 #include "jni/AppWebMessagePortService_jni.h" | |
| 17 | |
| 18 namespace content { | |
| 19 | |
| 20 using base::android::AttachCurrentThread; | |
| 21 using base::android::ConvertJavaStringToUTF16; | |
| 22 using base::android::ConvertUTF16ToJavaString; | |
| 23 using base::android::JavaParamRef; | |
| 24 using base::android::JavaRef; | |
| 25 using base::android::ScopedJavaGlobalRef; | |
| 26 using base::android::ScopedJavaLocalRef; | |
| 27 using base::android::ToJavaIntArray; | |
| 28 using content::BrowserThread; | |
| 29 using content::MessagePortProvider; | |
| 30 | |
| 31 AppWebMessagePortServiceImpl* AppWebMessagePortServiceImpl::GetInstance() { | |
| 32 return base::Singleton<AppWebMessagePortServiceImpl>::get(); | |
| 33 } | |
| 34 | |
| 35 AppWebMessagePortServiceImpl::AppWebMessagePortServiceImpl() {} | |
| 36 | |
| 37 AppWebMessagePortServiceImpl::~AppWebMessagePortServiceImpl() { | |
| 38 JNIEnv* env = AttachCurrentThread(); | |
| 39 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | |
| 40 if (obj.is_null()) | |
| 41 return; | |
| 42 Java_AppWebMessagePortService_unregisterNativeAppWebMessagePortService(env, | |
| 43 obj); | |
| 44 } | |
| 45 | |
| 46 void AppWebMessagePortServiceImpl::Init(JNIEnv* env, jobject obj) { | |
| 47 java_ref_ = JavaObjectWeakGlobalRef(env, obj); | |
| 48 } | |
| 49 | |
| 50 void AppWebMessagePortServiceImpl::CreateMessageChannel( | |
| 51 JNIEnv* env, | |
| 52 const JavaRef<jobjectArray>& ports, | |
| 53 WebContents* web_contents) { | |
| 54 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 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 | |
| 61 int* portId1 = new int; | |
| 62 int* portId2 = new int; | |
| 63 BrowserThread::PostTaskAndReply( | |
| 64 BrowserThread::IO, FROM_HERE, | |
| 65 base::Bind(&AppWebMessagePortServiceImpl::CreateMessageChannelOnIOThread, | |
| 66 base::Unretained(this), filter, portId1, portId2), | |
| 67 base::Bind(&AppWebMessagePortServiceImpl::OnMessageChannelCreated, | |
| 68 base::Unretained(this), | |
| 69 ScopedJavaGlobalRef<jobjectArray>(ports), base::Owned(portId1), | |
| 70 base::Owned(portId2))); | |
| 71 } | |
| 72 | |
| 73 void AppWebMessagePortServiceImpl::OnConvertedWebToAppMessage( | |
| 74 int message_port_id, | |
| 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 if (iter->second == filter) { | |
| 112 iter = ports_.erase(iter); | |
| 113 } else { | |
| 114 ++iter; | |
| 115 } | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 void AppWebMessagePortServiceImpl::PostAppToWebMessage( | |
| 120 JNIEnv* env, | |
| 121 const JavaParamRef<jobject>& obj, | |
| 122 int sender_id, | |
| 123 const JavaParamRef<jstring>& message, | |
| 124 const JavaParamRef<jintArray>& sent_ports) { | |
| 125 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 126 base::string16* j_message = new base::string16; | |
| 127 ConvertJavaStringToUTF16(env, message, j_message); | |
| 128 std::vector<int>* j_sent_ports = new std::vector<int>; | |
| 129 if (sent_ports != nullptr) | |
| 130 base::android::JavaIntArrayToIntVector(env, sent_ports, j_sent_ports); | |
| 131 | |
| 132 BrowserThread::PostTask( | |
| 133 BrowserThread::IO, FROM_HERE, | |
| 134 base::Bind(&AppWebMessagePortServiceImpl::PostAppToWebMessageOnIOThread, | |
| 135 base::Unretained(this), sender_id, base::Owned(j_message), | |
| 136 base::Owned(j_sent_ports))); | |
| 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 AppWebMessagePortServiceImpl::ClosePort(JNIEnv* env, | |
| 144 const JavaParamRef<jobject>& obj, | |
| 145 int message_port_id) { | |
| 146 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 147 BrowserThread::PostTask( | |
| 148 BrowserThread::IO, FROM_HERE, | |
| 149 base::Bind(&AppWebMessagePortServiceImpl::PostClosePortMessage, | |
| 150 base::Unretained(this), message_port_id)); | |
| 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()), | |
| 162 message_port_id)); | |
| 163 } | |
| 164 | |
| 165 void AppWebMessagePortServiceImpl::RemoveSentPorts( | |
| 166 const std::vector<int>& sent_ports) { | |
| 167 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 168 // Remove the filters that are associated with the transferred ports | |
| 169 for (const auto& iter : sent_ports) | |
| 170 ports_.erase(iter); | |
| 171 } | |
| 172 | |
| 173 void AppWebMessagePortServiceImpl::PostAppToWebMessageOnIOThread( | |
| 174 int sender_id, | |
| 175 base::string16* message, | |
| 176 std::vector<int>* sent_ports) { | |
| 177 RemoveSentPorts(*sent_ports); | |
| 178 ports_[sender_id]->SendAppToWebMessage(sender_id, *message, *sent_ports); | |
| 179 } | |
| 180 | |
| 181 void AppWebMessagePortServiceImpl::PostClosePortMessage(int message_port_id) { | |
| 182 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 183 ports_[message_port_id]->SendClosePortMessage(message_port_id); | |
| 184 } | |
| 185 | |
| 186 void AppWebMessagePortServiceImpl::CleanupPort(int message_port_id) { | |
| 187 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 188 ports_.erase(message_port_id); | |
| 189 } | |
| 190 | |
| 191 void AppWebMessagePortServiceImpl::CreateMessageChannelOnIOThread( | |
| 192 scoped_refptr<AppWebMessagePortMessageFilter> filter, | |
| 193 int* portId1, | |
| 194 int* portId2) { | |
| 195 *portId1 = 0; | |
| 196 *portId2 = 0; | |
| 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); | |
| 209 AddPort(*portId1, filter.get()); | |
| 210 AddPort(*portId2, filter.get()); | |
| 211 } | |
| 212 | |
| 213 void AppWebMessagePortServiceImpl::OnMessageChannelCreated( | |
| 214 const JavaRef<jobjectArray>& ports, | |
| 215 int* port1, | |
| 216 int* port2) { | |
| 217 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 218 JNIEnv* env = AttachCurrentThread(); | |
| 219 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | |
| 220 if (obj.is_null()) | |
| 221 return; | |
| 222 Java_AppWebMessagePortService_onMessageChannelCreated(env, obj, *port1, | |
| 223 *port2, ports); | |
| 224 } | |
| 225 | |
| 226 // Adds a new port to the message port service. | |
| 227 void AppWebMessagePortServiceImpl::AddPort( | |
| 228 int message_port_id, | |
| 229 AppWebMessagePortMessageFilter* filter) { | |
| 230 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 231 if (ports_.count(message_port_id)) { | |
| 232 NOTREACHED(); | |
| 233 return; | |
| 234 } | |
| 235 ports_[message_port_id] = filter; | |
| 236 } | |
| 237 | |
| 238 bool RegisterAppWebMessagePortService(JNIEnv* env) { | |
| 239 return RegisterNativesImpl(env); | |
| 240 } | |
| 241 | |
| 242 // static | |
| 243 jlong InitAppWebMessagePortService(JNIEnv* env, | |
| 244 const JavaParamRef<jobject>& obj) { | |
| 245 AppWebMessagePortServiceImpl* service = | |
| 246 AppWebMessagePortServiceImpl::GetInstance(); | |
| 247 service->Init(env, obj); | |
| 248 return reinterpret_cast<intptr_t>(service); | |
| 249 } | |
| 250 | |
| 251 } // namespace content | |
| OLD | NEW |