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 "android_webview/native/aw_message_port_service_impl.h" | 5 #include "android_webview/native/aw_message_port_service_impl.h" |
6 | 6 |
7 #include "android_webview/browser/aw_browser_context.h" | 7 #include "android_webview/browser/aw_browser_context.h" |
8 #include "android_webview/browser/aw_message_port_message_filter.h" | 8 #include "android_webview/browser/aw_message_port_message_filter.h" |
9 #include "android_webview/native/aw_contents.h" | 9 #include "android_webview/native/aw_contents.h" |
10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 } | 33 } |
34 | 34 |
35 AwMessagePortServiceImpl::AwMessagePortServiceImpl() { | 35 AwMessagePortServiceImpl::AwMessagePortServiceImpl() { |
36 } | 36 } |
37 | 37 |
38 AwMessagePortServiceImpl::~AwMessagePortServiceImpl() { | 38 AwMessagePortServiceImpl::~AwMessagePortServiceImpl() { |
39 JNIEnv* env = AttachCurrentThread(); | 39 JNIEnv* env = AttachCurrentThread(); |
40 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | 40 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
41 if (obj.is_null()) | 41 if (obj.is_null()) |
42 return; | 42 return; |
43 Java_AwMessagePortService_unregisterNativeAwMessagePortService(env, | 43 Java_AwMessagePortService_unregisterNativeAwMessagePortService(env, obj); |
44 obj.obj()); | |
45 } | 44 } |
46 | 45 |
47 void AwMessagePortServiceImpl::Init(JNIEnv* env, jobject obj) { | 46 void AwMessagePortServiceImpl::Init(JNIEnv* env, jobject obj) { |
48 java_ref_ = JavaObjectWeakGlobalRef(env, obj); | 47 java_ref_ = JavaObjectWeakGlobalRef(env, obj); |
49 } | 48 } |
50 | 49 |
51 void AwMessagePortServiceImpl::CreateMessageChannel( | 50 void AwMessagePortServiceImpl::CreateMessageChannel( |
52 JNIEnv* env, | 51 JNIEnv* env, |
53 jobjectArray ports, | 52 jobjectArray ports, |
54 scoped_refptr<AwMessagePortMessageFilter> filter) { | 53 scoped_refptr<AwMessagePortMessageFilter> filter) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 } | 97 } |
99 | 98 |
100 // Add the ports to AwMessagePortService. | 99 // Add the ports to AwMessagePortService. |
101 for (const auto& iter : sent_message_port_ids) { | 100 for (const auto& iter : sent_message_port_ids) { |
102 AddPort(iter, ports_[message_port_id]); | 101 AddPort(iter, ports_[message_port_id]); |
103 } | 102 } |
104 | 103 |
105 ScopedJavaLocalRef<jstring> jmsg = ConvertUTF16ToJavaString(env, value); | 104 ScopedJavaLocalRef<jstring> jmsg = ConvertUTF16ToJavaString(env, value); |
106 ScopedJavaLocalRef<jintArray> jports = | 105 ScopedJavaLocalRef<jintArray> jports = |
107 ToJavaIntArray(env, sent_message_port_ids); | 106 ToJavaIntArray(env, sent_message_port_ids); |
108 Java_AwMessagePortService_onReceivedMessage(env, | 107 Java_AwMessagePortService_onReceivedMessage(env, jobj, message_port_id, jmsg, |
109 jobj.obj(), | 108 jports); |
110 message_port_id, | |
111 jmsg.obj(), | |
112 jports.obj()); | |
113 } | 109 } |
114 | 110 |
115 void AwMessagePortServiceImpl::OnMessagePortMessageFilterClosing( | 111 void AwMessagePortServiceImpl::OnMessagePortMessageFilterClosing( |
116 AwMessagePortMessageFilter* filter) { | 112 AwMessagePortMessageFilter* filter) { |
117 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 113 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
118 for (MessagePorts::iterator iter = ports_.begin(); | 114 for (MessagePorts::iterator iter = ports_.begin(); |
119 iter != ports_.end(); iter++) { | 115 iter != ports_.end(); iter++) { |
120 if (iter->second == filter) { | 116 if (iter->second == filter) { |
121 ports_.erase(iter); | 117 ports_.erase(iter); |
122 } | 118 } |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 | 207 |
212 void AwMessagePortServiceImpl::OnMessageChannelCreated( | 208 void AwMessagePortServiceImpl::OnMessageChannelCreated( |
213 ScopedJavaGlobalRef<jobjectArray>* ports, | 209 ScopedJavaGlobalRef<jobjectArray>* ports, |
214 int* port1, | 210 int* port1, |
215 int* port2) { | 211 int* port2) { |
216 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 212 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
217 JNIEnv* env = AttachCurrentThread(); | 213 JNIEnv* env = AttachCurrentThread(); |
218 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | 214 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
219 if (obj.is_null()) | 215 if (obj.is_null()) |
220 return; | 216 return; |
221 Java_AwMessagePortService_onMessageChannelCreated(env, obj.obj(), *port1, | 217 Java_AwMessagePortService_onMessageChannelCreated(env, obj, *port1, *port2, |
222 *port2, ports->obj()); | 218 ports->obj()); |
223 } | 219 } |
224 | 220 |
225 // Adds a new port to the message port service. | 221 // Adds a new port to the message port service. |
226 void AwMessagePortServiceImpl::AddPort(int message_port_id, | 222 void AwMessagePortServiceImpl::AddPort(int message_port_id, |
227 AwMessagePortMessageFilter* filter) { | 223 AwMessagePortMessageFilter* filter) { |
228 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 224 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
229 if (ports_.count(message_port_id)) { | 225 if (ports_.count(message_port_id)) { |
230 NOTREACHED(); | 226 NOTREACHED(); |
231 return; | 227 return; |
232 } | 228 } |
233 ports_[message_port_id] = filter; | 229 ports_[message_port_id] = filter; |
234 } | 230 } |
235 | 231 |
236 bool RegisterAwMessagePortService(JNIEnv* env) { | 232 bool RegisterAwMessagePortService(JNIEnv* env) { |
237 return RegisterNativesImpl(env); | 233 return RegisterNativesImpl(env); |
238 } | 234 } |
239 | 235 |
240 // static | 236 // static |
241 jlong InitAwMessagePortService(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 237 jlong InitAwMessagePortService(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
242 AwMessagePortServiceImpl* service = AwMessagePortServiceImpl::GetInstance(); | 238 AwMessagePortServiceImpl* service = AwMessagePortServiceImpl::GetInstance(); |
243 service->Init(env, obj); | 239 service->Init(env, obj); |
244 return reinterpret_cast<intptr_t>(service); | 240 return reinterpret_cast<intptr_t>(service); |
245 } | 241 } |
246 | 242 |
247 } // namespace android_webview | 243 } // namespace android_webview |
OLD | NEW |