| 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_message_filter.h" |
| 11 #include "content/browser/message_port_service.h" | 11 #include "content/browser/message_port_service.h" |
| 12 #include "content/browser/renderer_host/render_process_host_impl.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/browser/web_contents/web_contents_impl.h" |
| 14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/message_port_provider.h" | 15 #include "content/public/browser/message_port_provider.h" |
| 16 #include "jni/AppWebMessagePortService_jni.h" | 16 #include "jni/AppWebMessagePortService_jni.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::ScopedJavaGlobalRef; | 25 using base::android::ScopedJavaGlobalRef; |
| 25 using base::android::ScopedJavaLocalRef; | 26 using base::android::ScopedJavaLocalRef; |
| 26 using base::android::ToJavaIntArray; | 27 using base::android::ToJavaIntArray; |
| 27 using content::BrowserThread; | 28 using content::BrowserThread; |
| 28 using content::MessagePortProvider; | 29 using content::MessagePortProvider; |
| 29 | 30 |
| 30 AppWebMessagePortServiceImpl* AppWebMessagePortServiceImpl::GetInstance() { | 31 AppWebMessagePortServiceImpl* AppWebMessagePortServiceImpl::GetInstance() { |
| 31 return base::Singleton<AppWebMessagePortServiceImpl>::get(); | 32 return base::Singleton<AppWebMessagePortServiceImpl>::get(); |
| 32 } | 33 } |
| 33 | 34 |
| 34 AppWebMessagePortServiceImpl::AppWebMessagePortServiceImpl() {} | 35 AppWebMessagePortServiceImpl::AppWebMessagePortServiceImpl() {} |
| 35 | 36 |
| 36 AppWebMessagePortServiceImpl::~AppWebMessagePortServiceImpl() { | 37 AppWebMessagePortServiceImpl::~AppWebMessagePortServiceImpl() { |
| 37 JNIEnv* env = AttachCurrentThread(); | 38 JNIEnv* env = AttachCurrentThread(); |
| 38 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | 39 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| 39 if (obj.is_null()) | 40 if (obj.is_null()) |
| 40 return; | 41 return; |
| 41 Java_AppWebMessagePortService_unregisterNativeAppWebMessagePortService(env, | 42 Java_AppWebMessagePortService_unregisterNativeAppWebMessagePortService(env, |
| 42 obj); | 43 obj); |
| 43 } | 44 } |
| 44 | 45 |
| 45 void AppWebMessagePortServiceImpl::Init(JNIEnv* env, jobject obj) { | 46 void AppWebMessagePortServiceImpl::Init(JNIEnv* env, jobject obj) { |
| 46 java_ref_ = JavaObjectWeakGlobalRef(env, obj); | 47 java_ref_ = JavaObjectWeakGlobalRef(env, obj); |
| 47 } | 48 } |
| 48 | 49 |
| 49 void AppWebMessagePortServiceImpl::CreateMessageChannel( | 50 void AppWebMessagePortServiceImpl::CreateMessageChannel( |
| 50 JNIEnv* env, | 51 JNIEnv* env, |
| 51 jobjectArray ports, | 52 const JavaRef<jobjectArray>& ports, |
| 52 WebContents* web_contents) { | 53 WebContents* web_contents) { |
| 53 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 54 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 54 RenderFrameHostImpl* rfh = | 55 RenderFrameHostImpl* rfh = |
| 55 static_cast<RenderFrameHostImpl*>(web_contents->GetMainFrame()); | 56 static_cast<RenderFrameHostImpl*>(web_contents->GetMainFrame()); |
| 56 int routing_id = web_contents->GetMainFrame()->GetRoutingID(); | 57 int routing_id = web_contents->GetMainFrame()->GetRoutingID(); |
| 57 scoped_refptr<AppWebMessagePortMessageFilter> filter = | 58 scoped_refptr<AppWebMessagePortMessageFilter> filter = |
| 58 rfh->GetAppWebMessagePortMessageFilter(routing_id); | 59 rfh->GetAppWebMessagePortMessageFilter(routing_id); |
| 59 ScopedJavaGlobalRef<jobjectArray>* j_ports = | |
| 60 new ScopedJavaGlobalRef<jobjectArray>(); | |
| 61 j_ports->Reset(env, ports); | |
| 62 | 60 |
| 63 int* portId1 = new int; | 61 int* portId1 = new int; |
| 64 int* portId2 = new int; | 62 int* portId2 = new int; |
| 65 BrowserThread::PostTaskAndReply( | 63 BrowserThread::PostTaskAndReply( |
| 66 BrowserThread::IO, FROM_HERE, | 64 BrowserThread::IO, FROM_HERE, |
| 67 base::Bind(&AppWebMessagePortServiceImpl::CreateMessageChannelOnIOThread, | 65 base::Bind(&AppWebMessagePortServiceImpl::CreateMessageChannelOnIOThread, |
| 68 base::Unretained(this), filter, portId1, portId2), | 66 base::Unretained(this), filter, portId1, portId2), |
| 69 base::Bind(&AppWebMessagePortServiceImpl::OnMessageChannelCreated, | 67 base::Bind(&AppWebMessagePortServiceImpl::OnMessageChannelCreated, |
| 70 base::Unretained(this), base::Owned(j_ports), | 68 base::Unretained(this), |
| 71 base::Owned(portId1), base::Owned(portId2))); | 69 ScopedJavaGlobalRef<jobjectArray>(ports), base::Owned(portId1), |
| 70 base::Owned(portId2))); |
| 72 } | 71 } |
| 73 | 72 |
| 74 void AppWebMessagePortServiceImpl::OnConvertedWebToAppMessage( | 73 void AppWebMessagePortServiceImpl::OnConvertedWebToAppMessage( |
| 75 int message_port_id, | 74 int message_port_id, |
| 76 const base::ListValue& message, | 75 const base::ListValue& message, |
| 77 const std::vector<int>& sent_message_port_ids) { | 76 const std::vector<int>& sent_message_port_ids) { |
| 78 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 77 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 79 JNIEnv* env = AttachCurrentThread(); | 78 JNIEnv* env = AttachCurrentThread(); |
| 80 ScopedJavaLocalRef<jobject> jobj = java_ref_.get(env); | 79 ScopedJavaLocalRef<jobject> jobj = java_ref_.get(env); |
| 81 if (jobj.is_null()) | 80 if (jobj.is_null()) |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 msp->Entangle(*portId1, *portId2); | 203 msp->Entangle(*portId1, *portId2); |
| 205 msp->Entangle(*portId2, *portId1); | 204 msp->Entangle(*portId2, *portId1); |
| 206 | 205 |
| 207 msp->HoldMessages(*portId1); | 206 msp->HoldMessages(*portId1); |
| 208 msp->HoldMessages(*portId2); | 207 msp->HoldMessages(*portId2); |
| 209 AddPort(*portId1, filter.get()); | 208 AddPort(*portId1, filter.get()); |
| 210 AddPort(*portId2, filter.get()); | 209 AddPort(*portId2, filter.get()); |
| 211 } | 210 } |
| 212 | 211 |
| 213 void AppWebMessagePortServiceImpl::OnMessageChannelCreated( | 212 void AppWebMessagePortServiceImpl::OnMessageChannelCreated( |
| 214 ScopedJavaGlobalRef<jobjectArray>* ports, | 213 const JavaRef<jobjectArray>& ports, |
| 215 int* port1, | 214 int* port1, |
| 216 int* port2) { | 215 int* port2) { |
| 217 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 216 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 218 JNIEnv* env = AttachCurrentThread(); | 217 JNIEnv* env = AttachCurrentThread(); |
| 219 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | 218 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| 220 if (obj.is_null()) | 219 if (obj.is_null()) |
| 221 return; | 220 return; |
| 222 Java_AppWebMessagePortService_onMessageChannelCreated(env, obj, *port1, | 221 Java_AppWebMessagePortService_onMessageChannelCreated(env, obj, *port1, |
| 223 *port2, *ports); | 222 *port2, ports); |
| 224 } | 223 } |
| 225 | 224 |
| 226 // Adds a new port to the message port service. | 225 // Adds a new port to the message port service. |
| 227 void AppWebMessagePortServiceImpl::AddPort( | 226 void AppWebMessagePortServiceImpl::AddPort( |
| 228 int message_port_id, | 227 int message_port_id, |
| 229 AppWebMessagePortMessageFilter* filter) { | 228 AppWebMessagePortMessageFilter* filter) { |
| 230 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 229 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 231 if (ports_.count(message_port_id)) { | 230 if (ports_.count(message_port_id)) { |
| 232 NOTREACHED(); | 231 NOTREACHED(); |
| 233 return; | 232 return; |
| 234 } | 233 } |
| 235 ports_[message_port_id] = filter; | 234 ports_[message_port_id] = filter; |
| 236 } | 235 } |
| 237 | 236 |
| 238 bool RegisterAppWebMessagePortService(JNIEnv* env) { | 237 bool RegisterAppWebMessagePortService(JNIEnv* env) { |
| 239 return RegisterNativesImpl(env); | 238 return RegisterNativesImpl(env); |
| 240 } | 239 } |
| 241 | 240 |
| 242 // static | 241 // static |
| 243 jlong InitAppWebMessagePortService(JNIEnv* env, | 242 jlong InitAppWebMessagePortService(JNIEnv* env, |
| 244 const JavaParamRef<jobject>& obj) { | 243 const JavaParamRef<jobject>& obj) { |
| 245 AppWebMessagePortServiceImpl* service = | 244 AppWebMessagePortServiceImpl* service = |
| 246 AppWebMessagePortServiceImpl::GetInstance(); | 245 AppWebMessagePortServiceImpl::GetInstance(); |
| 247 service->Init(env, obj); | 246 service->Init(env, obj); |
| 248 return reinterpret_cast<intptr_t>(service); | 247 return reinterpret_cast<intptr_t>(service); |
| 249 } | 248 } |
| 250 | 249 |
| 251 } // namespace content | 250 } // namespace content |
| OLD | NEW |