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 "android_webview/native/aw_message_port_service_impl.h" | |
6 | |
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" | |
16 | |
17 namespace android_webview { | |
18 | |
19 using base::android::AttachCurrentThread; | |
20 using base::android::ConvertJavaStringToUTF16; | |
21 using base::android::ConvertUTF16ToJavaString; | |
22 using base::android::JavaParamRef; | |
23 using base::android::ScopedJavaGlobalRef; | |
24 using base::android::ScopedJavaLocalRef; | |
25 using base::android::ToJavaIntArray; | |
26 using content::BrowserThread; | |
27 using content::MessagePortProvider; | |
28 | |
29 // static | |
30 AwMessagePortServiceImpl* AwMessagePortServiceImpl::GetInstance() { | |
31 return static_cast<AwMessagePortServiceImpl*>( | |
32 AwBrowserContext::GetDefault()->GetMessagePortService()); | |
33 } | |
34 | |
35 AwMessagePortServiceImpl::AwMessagePortServiceImpl() { | |
36 } | |
37 | |
38 AwMessagePortServiceImpl::~AwMessagePortServiceImpl() { | |
39 JNIEnv* env = AttachCurrentThread(); | |
40 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | |
41 if (obj.is_null()) | |
42 return; | |
43 Java_AwMessagePortService_unregisterNativeAwMessagePortService(env, obj); | |
44 } | |
45 | |
46 void AwMessagePortServiceImpl::Init(JNIEnv* env, jobject obj) { | |
47 java_ref_ = JavaObjectWeakGlobalRef(env, obj); | |
48 } | |
49 | |
50 void AwMessagePortServiceImpl::CreateMessageChannel( | |
51 JNIEnv* env, | |
52 jobjectArray ports, | |
53 scoped_refptr<AwMessagePortMessageFilter> filter) { | |
54 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
55 | |
56 ScopedJavaGlobalRef<jobjectArray>* j_ports = | |
57 new ScopedJavaGlobalRef<jobjectArray>(); | |
58 j_ports->Reset(env, ports); | |
59 | |
60 int* portId1 = new int; | |
61 int* portId2 = new int; | |
62 BrowserThread::PostTaskAndReply( | |
63 BrowserThread::IO, | |
64 FROM_HERE, | |
65 base::Bind(&AwMessagePortServiceImpl::CreateMessageChannelOnIOThread, | |
66 base::Unretained(this), | |
67 filter, | |
68 portId1, | |
69 portId2), | |
70 base::Bind(&AwMessagePortServiceImpl::OnMessageChannelCreated, | |
71 base::Unretained(this), | |
72 base::Owned(j_ports), | |
73 base::Owned(portId1), | |
74 base::Owned(portId2))); | |
75 } | |
76 | |
77 void AwMessagePortServiceImpl::OnConvertedWebToAppMessage( | |
78 int message_port_id, | |
79 const base::ListValue& message, | |
80 const std::vector<int>& sent_message_port_ids) { | |
81 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
82 JNIEnv* env = AttachCurrentThread(); | |
83 ScopedJavaLocalRef<jobject> jobj = java_ref_.get(env); | |
84 if (jobj.is_null()) | |
85 return; | |
86 | |
87 base::string16 value; | |
88 if (!message.GetString(0, &value)) { | |
89 LOG(WARNING) << "Converting post message to a string failed for port " | |
90 << message_port_id; | |
91 return; | |
92 } | |
93 | |
94 if (message.GetSize() != 1) { | |
95 NOTREACHED(); | |
96 return; | |
97 } | |
98 | |
99 // Add the ports to AwMessagePortService. | |
100 for (const auto& iter : sent_message_port_ids) { | |
101 AddPort(iter, ports_[message_port_id]); | |
102 } | |
103 | |
104 ScopedJavaLocalRef<jstring> jmsg = ConvertUTF16ToJavaString(env, value); | |
105 ScopedJavaLocalRef<jintArray> jports = | |
106 ToJavaIntArray(env, sent_message_port_ids); | |
107 Java_AwMessagePortService_onReceivedMessage(env, jobj, message_port_id, jmsg, | |
108 jports); | |
109 } | |
110 | |
111 void AwMessagePortServiceImpl::OnMessagePortMessageFilterClosing( | |
112 AwMessagePortMessageFilter* filter) { | |
113 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
114 for (MessagePorts::iterator iter = ports_.begin(); | |
115 iter != ports_.end(); iter++) { | |
116 if (iter->second == filter) { | |
117 ports_.erase(iter); | |
118 } | |
119 } | |
120 } | |
121 | |
122 void AwMessagePortServiceImpl::PostAppToWebMessage( | |
123 JNIEnv* env, | |
124 const JavaParamRef<jobject>& obj, | |
125 int sender_id, | |
126 const JavaParamRef<jstring>& message, | |
127 const JavaParamRef<jintArray>& sent_ports) { | |
128 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
129 base::string16* j_message = new base::string16; | |
130 ConvertJavaStringToUTF16(env, message, j_message); | |
131 std::vector<int>* j_sent_ports = new std::vector<int>; | |
132 if (sent_ports != nullptr) | |
133 base::android::JavaIntArrayToIntVector(env, sent_ports, j_sent_ports); | |
134 | |
135 BrowserThread::PostTask( | |
136 BrowserThread::IO, | |
137 FROM_HERE, | |
138 base::Bind(&AwMessagePortServiceImpl::PostAppToWebMessageOnIOThread, | |
139 base::Unretained(this), | |
140 sender_id, | |
141 base::Owned(j_message), | |
142 base::Owned(j_sent_ports))); | |
143 } | |
144 | |
145 // The message port service cannot immediately close the port, because | |
146 // it is possible that messages are still queued in the renderer process | |
147 // waiting for a conversion. Instead, it sends a special message with | |
148 // a flag which indicates that this message port should be closed. | |
149 void AwMessagePortServiceImpl::ClosePort(JNIEnv* env, | |
150 const JavaParamRef<jobject>& obj, | |
151 int message_port_id) { | |
152 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
153 BrowserThread::PostTask( | |
154 BrowserThread::IO, | |
155 FROM_HERE, | |
156 base::Bind(&AwMessagePortServiceImpl::PostClosePortMessage, | |
157 base::Unretained(this), | |
158 message_port_id)); | |
159 } | |
160 | |
161 void AwMessagePortServiceImpl::ReleaseMessages(JNIEnv* env, | |
162 const JavaParamRef<jobject>& obj, | |
163 int message_port_id) { | |
164 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
165 BrowserThread::PostTask( | |
166 BrowserThread::IO, | |
167 FROM_HERE, | |
168 base::Bind(&MessagePortProvider::ReleaseMessages, message_port_id)); | |
169 } | |
170 | |
171 void AwMessagePortServiceImpl::RemoveSentPorts( | |
172 const std::vector<int>& sent_ports) { | |
173 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
174 // Remove the filters that are associated with the transferred ports | |
175 for (const auto& iter : sent_ports) | |
176 ports_.erase(iter); | |
177 } | |
178 | |
179 void AwMessagePortServiceImpl::PostAppToWebMessageOnIOThread( | |
180 int sender_id, | |
181 base::string16* message, | |
182 std::vector<int>* sent_ports) { | |
183 RemoveSentPorts(*sent_ports); | |
184 ports_[sender_id]->SendAppToWebMessage(sender_id, *message, *sent_ports); | |
185 } | |
186 | |
187 void AwMessagePortServiceImpl::PostClosePortMessage(int message_port_id) { | |
188 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
189 ports_[message_port_id]->SendClosePortMessage(message_port_id); | |
190 } | |
191 | |
192 void AwMessagePortServiceImpl::CleanupPort(int message_port_id) { | |
193 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
194 ports_.erase(message_port_id); | |
195 } | |
196 | |
197 void AwMessagePortServiceImpl::CreateMessageChannelOnIOThread( | |
198 scoped_refptr<AwMessagePortMessageFilter> filter, | |
199 int* portId1, | |
200 int* portId2) { | |
201 MessagePortProvider::CreateMessageChannel(filter.get(), portId1, portId2); | |
202 MessagePortProvider::HoldMessages(*portId1); | |
203 MessagePortProvider::HoldMessages(*portId2); | |
204 AddPort(*portId1, filter.get()); | |
205 AddPort(*portId2, filter.get()); | |
206 } | |
207 | |
208 void AwMessagePortServiceImpl::OnMessageChannelCreated( | |
209 ScopedJavaGlobalRef<jobjectArray>* ports, | |
210 int* port1, | |
211 int* port2) { | |
212 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
213 JNIEnv* env = AttachCurrentThread(); | |
214 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | |
215 if (obj.is_null()) | |
216 return; | |
217 Java_AwMessagePortService_onMessageChannelCreated(env, obj, *port1, *port2, | |
218 *ports); | |
219 } | |
220 | |
221 // Adds a new port to the message port service. | |
222 void AwMessagePortServiceImpl::AddPort(int message_port_id, | |
223 AwMessagePortMessageFilter* filter) { | |
224 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
225 if (ports_.count(message_port_id)) { | |
226 NOTREACHED(); | |
227 return; | |
228 } | |
229 ports_[message_port_id] = filter; | |
230 } | |
231 | |
232 bool RegisterAwMessagePortService(JNIEnv* env) { | |
233 return RegisterNativesImpl(env); | |
234 } | |
235 | |
236 // static | |
237 jlong InitAwMessagePortService(JNIEnv* env, const JavaParamRef<jobject>& obj) { | |
238 AwMessagePortServiceImpl* service = AwMessagePortServiceImpl::GetInstance(); | |
239 service->Init(env, obj); | |
240 return reinterpret_cast<intptr_t>(service); | |
241 } | |
242 | |
243 } // namespace android_webview | |
OLD | NEW |