| Index: content/browser/android/app_web_message_port_service_impl.cc
|
| diff --git a/android_webview/native/aw_message_port_service_impl.cc b/content/browser/android/app_web_message_port_service_impl.cc
|
| similarity index 57%
|
| rename from android_webview/native/aw_message_port_service_impl.cc
|
| rename to content/browser/android/app_web_message_port_service_impl.cc
|
| index bc55c23bb74386ff764ca033a575f2e2d045af6c..d5591502023b4835a21d2aa3671032d00bdfef46 100644
|
| --- a/android_webview/native/aw_message_port_service_impl.cc
|
| +++ b/content/browser/android/app_web_message_port_service_impl.cc
|
| @@ -2,19 +2,20 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "android_webview/native/aw_message_port_service_impl.h"
|
| +#include "content/browser/android/app_web_message_port_service_impl.h"
|
|
|
| -#include "android_webview/browser/aw_browser_context.h"
|
| -#include "android_webview/browser/aw_message_port_message_filter.h"
|
| -#include "android_webview/native/aw_contents.h"
|
| #include "base/android/jni_array.h"
|
| #include "base/android/jni_string.h"
|
| #include "base/bind.h"
|
| +#include "content/browser/android/app_web_message_port_message_filter.h"
|
| +#include "content/browser/message_port_service.h"
|
| +#include "content/browser/renderer_host/render_process_host_impl.h"
|
| +#include "content/browser/web_contents/web_contents_impl.h"
|
| #include "content/public/browser/browser_thread.h"
|
| #include "content/public/browser/message_port_provider.h"
|
| -#include "jni/AwMessagePortService_jni.h"
|
| +#include "jni/AppWebMessagePortService_jni.h"
|
|
|
| -namespace android_webview {
|
| +namespace content {
|
|
|
| using base::android::AttachCurrentThread;
|
| using base::android::ConvertJavaStringToUTF16;
|
| @@ -26,33 +27,38 @@ using base::android::ToJavaIntArray;
|
| using content::BrowserThread;
|
| using content::MessagePortProvider;
|
|
|
| -// static
|
| -AwMessagePortServiceImpl* AwMessagePortServiceImpl::GetInstance() {
|
| - return static_cast<AwMessagePortServiceImpl*>(
|
| - AwBrowserContext::GetDefault()->GetMessagePortService());
|
| +AppWebMessagePortServiceImpl* AppWebMessagePortServiceImpl::GetInstance() {
|
| + return base::Singleton<AppWebMessagePortServiceImpl>::get();
|
| }
|
|
|
| -AwMessagePortServiceImpl::AwMessagePortServiceImpl() {
|
| +AppWebMessagePortServiceImpl::AppWebMessagePortServiceImpl() {
|
| }
|
|
|
| -AwMessagePortServiceImpl::~AwMessagePortServiceImpl() {
|
| +AppWebMessagePortServiceImpl::~AppWebMessagePortServiceImpl() {
|
| JNIEnv* env = AttachCurrentThread();
|
| ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
|
| if (obj.is_null())
|
| return;
|
| - Java_AwMessagePortService_unregisterNativeAwMessagePortService(env, obj);
|
| + Java_AppWebMessagePortService_unregisterNativeAppWebMessagePortService(env,
|
| + obj);
|
| }
|
|
|
| -void AwMessagePortServiceImpl::Init(JNIEnv* env, jobject obj) {
|
| +void AppWebMessagePortServiceImpl::Init(JNIEnv* env, jobject obj) {
|
| java_ref_ = JavaObjectWeakGlobalRef(env, obj);
|
| }
|
|
|
| -void AwMessagePortServiceImpl::CreateMessageChannel(
|
| +void AppWebMessagePortServiceImpl::CreateMessageChannel(
|
| JNIEnv* env,
|
| jobjectArray ports,
|
| - scoped_refptr<AwMessagePortMessageFilter> filter) {
|
| + WebContents* web_contents) {
|
| DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
| -
|
| + AppWebMessagePortMessageFilter* filter = new AppWebMessagePortMessageFilter(
|
| + web_contents->GetMainFrame()->GetRoutingID());
|
| + static_cast<RenderFrameHostImpl*>(web_contents->GetMainFrame())
|
| + ->set_app_web_message_port_delegate(filter);
|
| + RenderProcessHostImpl* rph = static_cast<RenderProcessHostImpl*>(
|
| + web_contents->GetRenderProcessHost());
|
| + rph->AddFilter(filter);
|
| ScopedJavaGlobalRef<jobjectArray>* j_ports =
|
| new ScopedJavaGlobalRef<jobjectArray>();
|
| j_ports->Reset(env, ports);
|
| @@ -62,22 +68,23 @@ void AwMessagePortServiceImpl::CreateMessageChannel(
|
| BrowserThread::PostTaskAndReply(
|
| BrowserThread::IO,
|
| FROM_HERE,
|
| - base::Bind(&AwMessagePortServiceImpl::CreateMessageChannelOnIOThread,
|
| + base::Bind(&AppWebMessagePortServiceImpl::CreateMessageChannelOnIOThread,
|
| base::Unretained(this),
|
| filter,
|
| portId1,
|
| portId2),
|
| - base::Bind(&AwMessagePortServiceImpl::OnMessageChannelCreated,
|
| + base::Bind(&AppWebMessagePortServiceImpl::OnMessageChannelCreated,
|
| base::Unretained(this),
|
| base::Owned(j_ports),
|
| base::Owned(portId1),
|
| base::Owned(portId2)));
|
| }
|
|
|
| -void AwMessagePortServiceImpl::OnConvertedWebToAppMessage(
|
| +void AppWebMessagePortServiceImpl::OnConvertedWebToAppMessage(
|
| int message_port_id,
|
| const base::ListValue& message,
|
| const std::vector<int>& sent_message_port_ids) {
|
| +
|
| DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
| JNIEnv* env = AttachCurrentThread();
|
| ScopedJavaLocalRef<jobject> jobj = java_ref_.get(env);
|
| @@ -96,7 +103,7 @@ void AwMessagePortServiceImpl::OnConvertedWebToAppMessage(
|
| return;
|
| }
|
|
|
| - // Add the ports to AwMessagePortService.
|
| + // Add the ports to AppWebMessagePortService.
|
| for (const auto& iter : sent_message_port_ids) {
|
| AddPort(iter, ports_[message_port_id]);
|
| }
|
| @@ -104,12 +111,15 @@ void AwMessagePortServiceImpl::OnConvertedWebToAppMessage(
|
| ScopedJavaLocalRef<jstring> jmsg = ConvertUTF16ToJavaString(env, value);
|
| ScopedJavaLocalRef<jintArray> jports =
|
| ToJavaIntArray(env, sent_message_port_ids);
|
| - Java_AwMessagePortService_onReceivedMessage(env, jobj, message_port_id, jmsg,
|
| + Java_AppWebMessagePortService_onReceivedMessage(env,
|
| + jobj,
|
| + message_port_id,
|
| + jmsg,
|
| jports);
|
| }
|
|
|
| -void AwMessagePortServiceImpl::OnMessagePortMessageFilterClosing(
|
| - AwMessagePortMessageFilter* filter) {
|
| +void AppWebMessagePortServiceImpl::OnMessagePortMessageFilterClosing(
|
| + AppWebMessagePortMessageFilter* filter) {
|
| DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
| for (MessagePorts::iterator iter = ports_.begin();
|
| iter != ports_.end(); iter++) {
|
| @@ -119,7 +129,7 @@ void AwMessagePortServiceImpl::OnMessagePortMessageFilterClosing(
|
| }
|
| }
|
|
|
| -void AwMessagePortServiceImpl::PostAppToWebMessage(
|
| +void AppWebMessagePortServiceImpl::PostAppToWebMessage(
|
| JNIEnv* env,
|
| const JavaParamRef<jobject>& obj,
|
| int sender_id,
|
| @@ -135,7 +145,7 @@ void AwMessagePortServiceImpl::PostAppToWebMessage(
|
| BrowserThread::PostTask(
|
| BrowserThread::IO,
|
| FROM_HERE,
|
| - base::Bind(&AwMessagePortServiceImpl::PostAppToWebMessageOnIOThread,
|
| + base::Bind(&AppWebMessagePortServiceImpl::PostAppToWebMessageOnIOThread,
|
| base::Unretained(this),
|
| sender_id,
|
| base::Owned(j_message),
|
| @@ -146,29 +156,31 @@ void AwMessagePortServiceImpl::PostAppToWebMessage(
|
| // it is possible that messages are still queued in the renderer process
|
| // waiting for a conversion. Instead, it sends a special message with
|
| // a flag which indicates that this message port should be closed.
|
| -void AwMessagePortServiceImpl::ClosePort(JNIEnv* env,
|
| +void AppWebMessagePortServiceImpl::ClosePort(JNIEnv* env,
|
| const JavaParamRef<jobject>& obj,
|
| int message_port_id) {
|
| DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
| BrowserThread::PostTask(
|
| BrowserThread::IO,
|
| FROM_HERE,
|
| - base::Bind(&AwMessagePortServiceImpl::PostClosePortMessage,
|
| + base::Bind(&AppWebMessagePortServiceImpl::PostClosePortMessage,
|
| base::Unretained(this),
|
| message_port_id));
|
| }
|
|
|
| -void AwMessagePortServiceImpl::ReleaseMessages(JNIEnv* env,
|
| +void AppWebMessagePortServiceImpl::ReleaseMessages(JNIEnv* env,
|
| const JavaParamRef<jobject>& obj,
|
| int message_port_id) {
|
| DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
| BrowserThread::PostTask(
|
| BrowserThread::IO,
|
| FROM_HERE,
|
| - base::Bind(&MessagePortProvider::ReleaseMessages, message_port_id));
|
| + base::Bind(&MessagePortService::ReleaseMessages,
|
| + base::Unretained(
|
| + MessagePortService::GetInstance()), message_port_id));
|
| }
|
|
|
| -void AwMessagePortServiceImpl::RemoveSentPorts(
|
| +void AppWebMessagePortServiceImpl::RemoveSentPorts(
|
| const std::vector<int>& sent_ports) {
|
| DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
| // Remove the filters that are associated with the transferred ports
|
| @@ -176,7 +188,7 @@ void AwMessagePortServiceImpl::RemoveSentPorts(
|
| ports_.erase(iter);
|
| }
|
|
|
| -void AwMessagePortServiceImpl::PostAppToWebMessageOnIOThread(
|
| +void AppWebMessagePortServiceImpl::PostAppToWebMessageOnIOThread(
|
| int sender_id,
|
| base::string16* message,
|
| std::vector<int>* sent_ports) {
|
| @@ -184,28 +196,39 @@ void AwMessagePortServiceImpl::PostAppToWebMessageOnIOThread(
|
| ports_[sender_id]->SendAppToWebMessage(sender_id, *message, *sent_ports);
|
| }
|
|
|
| -void AwMessagePortServiceImpl::PostClosePortMessage(int message_port_id) {
|
| +void AppWebMessagePortServiceImpl::PostClosePortMessage(int message_port_id) {
|
| DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
| ports_[message_port_id]->SendClosePortMessage(message_port_id);
|
| }
|
|
|
| -void AwMessagePortServiceImpl::CleanupPort(int message_port_id) {
|
| +void AppWebMessagePortServiceImpl::CleanupPort(int message_port_id) {
|
| DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
| ports_.erase(message_port_id);
|
| }
|
|
|
| -void AwMessagePortServiceImpl::CreateMessageChannelOnIOThread(
|
| - scoped_refptr<AwMessagePortMessageFilter> filter,
|
| +void AppWebMessagePortServiceImpl::CreateMessageChannelOnIOThread(
|
| + scoped_refptr<AppWebMessagePortMessageFilter> filter,
|
| int* portId1,
|
| int* portId2) {
|
| - MessagePortProvider::CreateMessageChannel(filter.get(), portId1, portId2);
|
| - MessagePortProvider::HoldMessages(*portId1);
|
| - MessagePortProvider::HoldMessages(*portId2);
|
| + *portId1 = 0;
|
| + *portId2 = 0;
|
| + MessagePortService* msp = MessagePortService::GetInstance();
|
| + msp->Create(MSG_ROUTING_NONE, filter.get(), portId1);
|
| + msp->Create(MSG_ROUTING_NONE, filter.get(), portId2);
|
| + // Update the routing number of the message ports to be equal to the message
|
| + // port numbers.
|
| + msp->UpdateMessagePort(*portId1, filter.get(), *portId1);
|
| + msp->UpdateMessagePort(*portId2, filter.get(), *portId2);
|
| + msp->Entangle(*portId1, *portId2);
|
| + msp->Entangle(*portId2, *portId1);
|
| +
|
| + MessagePortService::GetInstance()->HoldMessages(*portId1);
|
| + MessagePortService::GetInstance()->HoldMessages(*portId2);
|
| AddPort(*portId1, filter.get());
|
| AddPort(*portId2, filter.get());
|
| }
|
|
|
| -void AwMessagePortServiceImpl::OnMessageChannelCreated(
|
| +void AppWebMessagePortServiceImpl::OnMessageChannelCreated(
|
| ScopedJavaGlobalRef<jobjectArray>* ports,
|
| int* port1,
|
| int* port2) {
|
| @@ -214,13 +237,14 @@ void AwMessagePortServiceImpl::OnMessageChannelCreated(
|
| ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
|
| if (obj.is_null())
|
| return;
|
| - Java_AwMessagePortService_onMessageChannelCreated(env, obj, *port1, *port2,
|
| - *ports);
|
| + Java_AppWebMessagePortService_onMessageChannelCreated(env, obj,
|
| + *port1, *port2,
|
| + *ports);
|
| }
|
|
|
| // Adds a new port to the message port service.
|
| -void AwMessagePortServiceImpl::AddPort(int message_port_id,
|
| - AwMessagePortMessageFilter* filter) {
|
| +void AppWebMessagePortServiceImpl::AddPort(
|
| + int message_port_id, AppWebMessagePortMessageFilter* filter) {
|
| DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
| if (ports_.count(message_port_id)) {
|
| NOTREACHED();
|
| @@ -229,15 +253,17 @@ void AwMessagePortServiceImpl::AddPort(int message_port_id,
|
| ports_[message_port_id] = filter;
|
| }
|
|
|
| -bool RegisterAwMessagePortService(JNIEnv* env) {
|
| +bool RegisterAppWebMessagePortService(JNIEnv* env) {
|
| return RegisterNativesImpl(env);
|
| }
|
|
|
| // static
|
| -jlong InitAwMessagePortService(JNIEnv* env, const JavaParamRef<jobject>& obj) {
|
| - AwMessagePortServiceImpl* service = AwMessagePortServiceImpl::GetInstance();
|
| +jlong InitAppWebMessagePortService(
|
| + JNIEnv* env, const JavaParamRef<jobject>& obj) {
|
| + AppWebMessagePortServiceImpl* service =
|
| + AppWebMessagePortServiceImpl::GetInstance();
|
| service->Init(env, obj);
|
| return reinterpret_cast<intptr_t>(service);
|
| }
|
|
|
| -} // namespace android_webview
|
| +} // namespace content
|
|
|