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.
5 #include "android_webview/native/aw_message_port_service_impl.h"
7 #include "android_webview/browser/aw_browser_context.h"
8 #include "android_webview/browser/aw_message_port_message_filter.h"
9 #include "android_webview/native/aw_contents.h"
10 #include "base/android/jni_array.h"
11 #include "base/android/jni_string.h"
12 #include "base/bind.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/message_port_provider.h"
15 #include "jni/AwMessagePortService_jni.h"
17 namespace android_webview
{
19 using base::android::AttachCurrentThread
;
20 using base::android::ConvertUTF16ToJavaString
;
21 using base::android::ScopedJavaGlobalRef
;
22 using base::android::ScopedJavaLocalRef
;
23 using base::android::ToJavaIntArray
;
24 using content::BrowserThread
;
25 using content::MessagePortProvider
;
28 AwMessagePortServiceImpl
* AwMessagePortServiceImpl::GetInstance() {
29 return static_cast<AwMessagePortServiceImpl
*>(
30 AwBrowserContext::GetDefault()->GetMessagePortService());
33 AwMessagePortServiceImpl::AwMessagePortServiceImpl() {
36 AwMessagePortServiceImpl::~AwMessagePortServiceImpl() {
37 JNIEnv
* env
= AttachCurrentThread();
38 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
41 Java_AwMessagePortService_unregisterNativeAwMessagePortService(env
,
45 void AwMessagePortServiceImpl::Init(JNIEnv
* env
, jobject obj
) {
46 java_ref_
= JavaObjectWeakGlobalRef(env
, obj
);
49 void AwMessagePortServiceImpl::CreateMessageChannel(
52 scoped_refptr
<AwMessagePortMessageFilter
> filter
) {
53 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
55 ScopedJavaGlobalRef
<jobject
>* j_callback
= new ScopedJavaGlobalRef
<jobject
>();
56 j_callback
->Reset(env
, callback
);
58 int* portId1
= new int;
59 int* portId2
= new int;
60 BrowserThread::PostTaskAndReply(
63 base::Bind(&AwMessagePortServiceImpl::CreateMessageChannelOnIOThread
,
64 base::Unretained(this),
68 base::Bind(&AwMessagePortServiceImpl::OnMessageChannelCreated
,
69 base::Unretained(this),
70 base::Owned(j_callback
),
72 base::Owned(portId2
)));
75 void AwMessagePortServiceImpl::OnConvertedMessage(
77 const base::ListValue
& message
,
78 const std::vector
<int>& sent_message_port_ids
) {
79 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
80 JNIEnv
* env
= AttachCurrentThread();
81 ScopedJavaLocalRef
<jobject
> jobj
= java_ref_
.get(env
);
85 if (message
.GetSize() != 1) {
91 if (!message
.GetString(0, &value
)) {
92 LOG(WARNING
) << "Converting post message to a string failed for port "
96 ScopedJavaLocalRef
<jstring
> jmsg
= ConvertUTF16ToJavaString(env
, value
);
97 ScopedJavaLocalRef
<jintArray
> jports
=
98 ToJavaIntArray(env
, sent_message_port_ids
);
99 Java_AwMessagePortService_onPostMessage(env
,
106 void AwMessagePortServiceImpl::OnMessagePortMessageFilterClosing(
107 AwMessagePortMessageFilter
* filter
) {
108 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
109 for (MessagePorts::iterator iter
= ports_
.begin();
110 iter
!= ports_
.end(); iter
++) {
111 if (iter
->second
== filter
) {
117 void AwMessagePortServiceImpl::CreateMessageChannelOnIOThread(
118 scoped_refptr
<AwMessagePortMessageFilter
> filter
,
121 content::MessagePortProvider::CreateMessageChannel(filter
.get(), portId1
,
123 AddPort(*portId1
, filter
.get());
124 AddPort(*portId2
, filter
.get());
127 void AwMessagePortServiceImpl::OnMessageChannelCreated(
128 ScopedJavaGlobalRef
<jobject
>* callback
,
131 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
132 JNIEnv
* env
= AttachCurrentThread();
133 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
136 Java_AwMessagePortService_onMessageChannelCreated(env
, obj
.obj(), *port1
,
137 *port2
, callback
->obj());
140 void AwMessagePortServiceImpl::AddPort(int message_port_id
,
141 AwMessagePortMessageFilter
* filter
) {
142 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
143 if (ports_
.count(message_port_id
)) {
147 ports_
[message_port_id
] = filter
;
150 bool RegisterAwMessagePortService(JNIEnv
* env
) {
151 return RegisterNativesImpl(env
);
155 jlong
InitAwMessagePortService(JNIEnv
* env
, jobject obj
) {
156 AwMessagePortServiceImpl
* service
= AwMessagePortServiceImpl::GetInstance();
157 service
->Init(env
, obj
);
158 return reinterpret_cast<intptr_t>(service
);
161 } // namespace android_webview