Chromium Code Reviews| Index: android_webview/native/aw_message_port_service_impl.cc |
| diff --git a/android_webview/native/aw_message_port_service_impl.cc b/android_webview/native/aw_message_port_service_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..74b72fd8b086e96d622d94be7b09c5f13a62530b |
| --- /dev/null |
| +++ b/android_webview/native/aw_message_port_service_impl.cc |
| @@ -0,0 +1,148 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// 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 "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/public/browser/browser_thread.h" |
| +#include "content/public/browser/message_port_provider.h" |
| +#include "jni/AwMessagePortService_jni.h" |
| + |
| +namespace android_webview { |
| + |
| +using base::android::AttachCurrentThread; |
| +using base::android::ConvertUTF16ToJavaString; |
| +using base::android::ScopedJavaGlobalRef; |
| +using base::android::ScopedJavaLocalRef; |
| +using base::android::ToJavaIntArray; |
| +using content::BrowserThread; |
| +using content::MessagePortProvider; |
| + |
| +namespace { |
| + |
| +void PostMessageOnUIThread(ScopedJavaGlobalRef<jobject>* jobj, |
| + int message_port_id, |
| + ScopedJavaGlobalRef<jstring>* jmsg, |
| + ScopedJavaGlobalRef<jintArray>* jports) { |
| + JNIEnv* env = AttachCurrentThread(); |
| + Java_AwMessagePortService_onPostMessage(env, |
| + jobj->obj(), |
| + message_port_id, |
| + jmsg->obj(), |
| + jports->obj()); |
| +} |
| + |
| +} |
| + |
| +//static |
| +AwMessagePortServiceImpl* AwMessagePortServiceImpl::GetInstance() { |
| + return static_cast<AwMessagePortServiceImpl*>( |
| + AwBrowserContext::GetDefault()->GetMessagePortService()); |
| +} |
| + |
| +AwMessagePortServiceImpl::AwMessagePortServiceImpl() { |
| +} |
| + |
| +AwMessagePortServiceImpl::~AwMessagePortServiceImpl() { |
| + JNIEnv* env = AttachCurrentThread(); |
| + ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| + if (obj.is_null()) |
| + return; |
| + Java_AwMessagePortService_unregisterNativeAwMessagePortService(env, |
| + obj.obj()); |
| +} |
| + |
| +void AwMessagePortServiceImpl::Init(JNIEnv* env, jobject obj) { |
| + java_ref_ = JavaObjectWeakGlobalRef(env, obj); |
| +} |
| + |
| +void AwMessagePortServiceImpl::OnMessageChannelCreated( |
| + AwMessagePortMessageFilter* filter, |
| + ScopedJavaGlobalRef<jobject>* callback, |
| + int* port1, |
| + int* port2) { |
| + // TODO(sgurun) if filter is closed return. |
| + |
| + AddPort(*port1, filter); |
| + AddPort(*port2, filter); |
| + |
| + JNIEnv* env = AttachCurrentThread(); |
| + ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| + if (obj.is_null()) |
| + return; |
| + Java_AwMessagePortService_onMessageChannelCreated(env, obj.obj(), *port1, |
| + *port2, callback->obj()); |
| +} |
| + |
| +void AwMessagePortServiceImpl::OnConvertedMessage( |
| + int message_port_id, |
| + const base::ListValue& message, |
| + const std::vector<int>& sent_message_port_ids) { |
| + JNIEnv* env = AttachCurrentThread(); |
| + ScopedJavaGlobalRef<jobject>* jobj = new ScopedJavaGlobalRef<jobject>(); |
| + jobj->Reset(java_ref_.get(env)); |
| + |
| + if (message.GetSize() != 1) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + |
| + base::string16 value; |
| + if (!message.GetString(0, &value)) { |
| + LOG(WARNING) << "Converting post message to a string failed for port " |
| + << message_port_id; |
| + return; |
| + } |
| + ScopedJavaGlobalRef<jstring>* jmsg = new ScopedJavaGlobalRef<jstring>(); |
| + jmsg->Reset(ConvertUTF16ToJavaString(env, value)); |
| + |
| + ScopedJavaGlobalRef<jintArray>* jports = new ScopedJavaGlobalRef<jintArray>(); |
| + jports->Reset(ToJavaIntArray(env, sent_message_port_ids)); |
| + |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&PostMessageOnUIThread, base::Owned(jobj), |
| + message_port_id, |
| + base::Owned(jmsg), |
| + base::Owned(jports))); |
| +} |
| + |
| +void AwMessagePortServiceImpl::OnMessagePortMessageFilterClosing( |
| + AwMessagePortMessageFilter* filter) { |
| + for (MessagePorts::iterator iter = ports_.begin(); |
| + iter != ports_.end(); iter++) { |
| + if (iter->second == filter) { |
| + ports_.erase(iter); |
| + } |
| + } |
| +} |
| + |
| +void AwMessagePortServiceImpl::AddPort(int message_port_id, |
| + AwMessagePortMessageFilter* filter) { |
| + // TODO(sgurun) seems like we are accessing ports_ from both UI and IO thread. |
|
mnaganov (inactive)
2015/01/07 11:43:54
What do we use |ports_| for?
sgurun-gerrit only
2015/01/10 02:36:29
This will be used when posting the messages from J
|
| + if (ports_.count(message_port_id)) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + ports_[message_port_id] = filter; |
| +} |
| + |
| +bool RegisterAwMessagePortService(JNIEnv* env) { |
| + return RegisterNativesImpl(env); |
| +} |
| + |
| +// static |
| +jlong InitAwMessagePortService(JNIEnv* env, jobject obj) { |
| + AwMessagePortServiceImpl* service = AwMessagePortServiceImpl::GetInstance(); |
| + service->Init(env, obj); |
| + return reinterpret_cast<intptr_t>(service); |
| +} |
| + |
| +} // namespace android_webview |