| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/public/browser/message_port_provider.h" | 5 #include "content/public/browser/message_port_provider.h" |
| 6 | 6 |
| 7 #include "content/browser/browser_thread_impl.h" | 7 #include "content/browser/browser_thread_impl.h" |
| 8 #include "content/browser/message_port_message_filter.h" | |
| 9 #include "content/browser/message_port_service.h" | |
| 10 #include "content/browser/renderer_host/render_process_host_impl.h" | 8 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 11 #include "content/browser/renderer_host/render_view_host_impl.h" | 9 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 12 #include "content/browser/web_contents/web_contents_impl.h" | 10 #include "content/browser/web_contents/web_contents_impl.h" |
| 13 #include "content/common/frame_messages.h" | 11 #include "content/common/frame_messages.h" |
| 14 #include "content/public/browser/message_port_delegate.h" | |
| 15 | 12 |
| 16 #if defined(OS_ANDROID) | 13 #if defined(OS_ANDROID) |
| 17 #include "content/browser/android/app_web_message_port_service_impl.h" | 14 #include "base/android/jni_string.h" |
| 15 #include "content/browser/android/app_web_message_port.h" |
| 18 #endif | 16 #endif |
| 19 | 17 |
| 20 namespace content { | 18 namespace content { |
| 19 namespace { |
| 20 |
| 21 void PostMessageToFrameInternal( |
| 22 WebContents* web_contents, |
| 23 const base::string16& source_origin, |
| 24 const base::string16& target_origin, |
| 25 const base::string16& data, |
| 26 std::vector<MessagePort> ports) { |
| 27 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 28 |
| 29 FrameMsg_PostMessage_Params params; |
| 30 params.is_data_raw_string = true; |
| 31 params.data = data; |
| 32 params.source_routing_id = MSG_ROUTING_NONE; |
| 33 params.source_origin = source_origin; |
| 34 params.target_origin = target_origin; |
| 35 params.message_ports = std::move(ports); |
| 36 |
| 37 RenderFrameHost* rfh = web_contents->GetMainFrame(); |
| 38 rfh->Send(new FrameMsg_PostMessageEvent(rfh->GetRoutingID(), params)); |
| 39 } |
| 40 |
| 41 } // namespace |
| 21 | 42 |
| 22 // static | 43 // static |
| 23 void MessagePortProvider::PostMessageToFrame( | 44 void MessagePortProvider::PostMessageToFrame( |
| 24 WebContents* web_contents, | 45 WebContents* web_contents, |
| 25 const base::string16& source_origin, | 46 const base::string16& source_origin, |
| 26 const base::string16& target_origin, | 47 const base::string16& target_origin, |
| 27 const base::string16& data, | 48 const base::string16& data) { |
| 28 const std::vector<int>& ports) { | 49 PostMessageToFrameInternal(web_contents, source_origin, target_origin, data, |
| 29 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 50 std::vector<MessagePort>()); |
| 30 | |
| 31 #if defined(OS_ANDROID) | |
| 32 BrowserThread::PostTask( | |
| 33 BrowserThread::IO, FROM_HERE, | |
| 34 base::Bind(&content::AppWebMessagePortServiceImpl::RemoveSentPorts, | |
| 35 base::Unretained(AppWebMessagePortServiceImpl::GetInstance()), | |
| 36 ports)); | |
| 37 #endif | |
| 38 FrameMsg_PostMessage_Params params; | |
| 39 params.is_data_raw_string = true; | |
| 40 params.data = data; | |
| 41 params.source_routing_id = MSG_ROUTING_NONE; | |
| 42 params.source_origin = source_origin; | |
| 43 params.target_origin = target_origin; | |
| 44 params.message_ports = ports; | |
| 45 | |
| 46 RenderProcessHostImpl* rph = | |
| 47 static_cast<RenderProcessHostImpl*>(web_contents->GetRenderProcessHost()); | |
| 48 BrowserThread::PostTask( | |
| 49 BrowserThread::IO, FROM_HERE, | |
| 50 base::Bind(&MessagePortMessageFilter::RouteMessageEventWithMessagePorts, | |
| 51 rph->message_port_message_filter(), | |
| 52 web_contents->GetMainFrame()->GetRoutingID(), params)); | |
| 53 } | 51 } |
| 54 | 52 |
| 55 #if defined(OS_ANDROID) | 53 #if defined(OS_ANDROID) |
| 56 // static | 54 void MessagePortProvider::PostMessageToFrame( |
| 57 AppWebMessagePortService* MessagePortProvider::GetAppWebMessagePortService() { | 55 WebContents* web_contents, |
| 58 return AppWebMessagePortServiceImpl::GetInstance(); | 56 JNIEnv* env, |
| 57 const base::android::JavaParamRef<jstring>& source_origin, |
| 58 const base::android::JavaParamRef<jstring>& target_origin, |
| 59 const base::android::JavaParamRef<jstring>& data, |
| 60 const base::android::JavaParamRef<jobjectArray>& ports) { |
| 61 PostMessageToFrameInternal( |
| 62 web_contents, |
| 63 base::android::ConvertJavaStringToUTF16(env, source_origin), |
| 64 base::android::ConvertJavaStringToUTF16(env, target_origin), |
| 65 base::android::ConvertJavaStringToUTF16(env, data), |
| 66 AppWebMessagePort::UnwrapJavaArray(env, ports)); |
| 59 } | 67 } |
| 60 #endif | 68 #endif |
| 61 | 69 |
| 62 } // namespace content | 70 } // namespace content |
| OLD | NEW |