OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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::ConvertUTF16ToJavaString; |
| 21 using base::android::ScopedJavaGlobalRef; |
| 22 using base::android::ScopedJavaLocalRef; |
| 23 using base::android::ToJavaIntArray; |
| 24 using content::BrowserThread; |
| 25 using content::MessagePortProvider; |
| 26 |
| 27 namespace { |
| 28 |
| 29 void PostMessageOnUIThread(ScopedJavaGlobalRef<jobject>* jobj, |
| 30 int message_port_id, |
| 31 ScopedJavaGlobalRef<jstring>* jmsg, |
| 32 ScopedJavaGlobalRef<jintArray>* jports) { |
| 33 JNIEnv* env = AttachCurrentThread(); |
| 34 Java_AwMessagePortService_onPostMessage(env, |
| 35 jobj->obj(), |
| 36 message_port_id, |
| 37 jmsg->obj(), |
| 38 jports->obj()); |
| 39 } |
| 40 |
| 41 } |
| 42 |
| 43 //static |
| 44 AwMessagePortServiceImpl* AwMessagePortServiceImpl::GetInstance() { |
| 45 return static_cast<AwMessagePortServiceImpl*>( |
| 46 AwBrowserContext::GetDefault()->GetMessagePortService()); |
| 47 } |
| 48 |
| 49 AwMessagePortServiceImpl::AwMessagePortServiceImpl() { |
| 50 } |
| 51 |
| 52 AwMessagePortServiceImpl::~AwMessagePortServiceImpl() { |
| 53 JNIEnv* env = AttachCurrentThread(); |
| 54 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| 55 if (obj.is_null()) |
| 56 return; |
| 57 Java_AwMessagePortService_unregisterNativeAwMessagePortService(env, |
| 58 obj.obj()); |
| 59 } |
| 60 |
| 61 void AwMessagePortServiceImpl::Init(JNIEnv* env, jobject obj) { |
| 62 java_ref_ = JavaObjectWeakGlobalRef(env, obj); |
| 63 } |
| 64 |
| 65 void AwMessagePortServiceImpl::OnMessageChannelCreated( |
| 66 AwMessagePortMessageFilter* filter, |
| 67 ScopedJavaGlobalRef<jobject>* callback, |
| 68 int* port1, |
| 69 int* port2) { |
| 70 // TODO(sgurun) if filter is closed return. |
| 71 |
| 72 AddPort(*port1, filter); |
| 73 AddPort(*port2, filter); |
| 74 |
| 75 JNIEnv* env = AttachCurrentThread(); |
| 76 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| 77 if (obj.is_null()) |
| 78 return; |
| 79 Java_AwMessagePortService_onMessageChannelCreated(env, obj.obj(), *port1, |
| 80 *port2, callback->obj()); |
| 81 } |
| 82 |
| 83 void AwMessagePortServiceImpl::OnConvertedMessage( |
| 84 int message_port_id, |
| 85 const base::ListValue& message, |
| 86 const std::vector<int>& sent_message_port_ids) { |
| 87 JNIEnv* env = AttachCurrentThread(); |
| 88 ScopedJavaGlobalRef<jobject>* jobj = new ScopedJavaGlobalRef<jobject>(); |
| 89 jobj->Reset(java_ref_.get(env)); |
| 90 |
| 91 if (message.GetSize() != 1) { |
| 92 NOTREACHED(); |
| 93 return; |
| 94 } |
| 95 |
| 96 base::string16 value; |
| 97 if (!message.GetString(0, &value)) { |
| 98 LOG(WARNING) << "Converting post message to a string failed for port " |
| 99 << message_port_id; |
| 100 return; |
| 101 } |
| 102 ScopedJavaGlobalRef<jstring>* jmsg = new ScopedJavaGlobalRef<jstring>(); |
| 103 jmsg->Reset(ConvertUTF16ToJavaString(env, value)); |
| 104 |
| 105 ScopedJavaGlobalRef<jintArray>* jports = new ScopedJavaGlobalRef<jintArray>(); |
| 106 jports->Reset(ToJavaIntArray(env, sent_message_port_ids)); |
| 107 |
| 108 BrowserThread::PostTask( |
| 109 BrowserThread::UI, |
| 110 FROM_HERE, |
| 111 base::Bind(&PostMessageOnUIThread, base::Owned(jobj), |
| 112 message_port_id, |
| 113 base::Owned(jmsg), |
| 114 base::Owned(jports))); |
| 115 } |
| 116 |
| 117 void AwMessagePortServiceImpl::OnMessagePortMessageFilterClosing( |
| 118 AwMessagePortMessageFilter* filter) { |
| 119 for (MessagePorts::iterator iter = ports_.begin(); |
| 120 iter != ports_.end(); iter++) { |
| 121 if (iter->second == filter) { |
| 122 ports_.erase(iter); |
| 123 } |
| 124 } |
| 125 } |
| 126 |
| 127 void AwMessagePortServiceImpl::AddPort(int message_port_id, |
| 128 AwMessagePortMessageFilter* filter) { |
| 129 if (ports_.count(message_port_id)) { |
| 130 NOTREACHED(); |
| 131 return; |
| 132 } |
| 133 ports_[message_port_id] = filter; |
| 134 } |
| 135 |
| 136 bool RegisterAwMessagePortService(JNIEnv* env) { |
| 137 return RegisterNativesImpl(env); |
| 138 } |
| 139 |
| 140 // static |
| 141 jlong InitAwMessagePortService(JNIEnv* env, jobject obj) { |
| 142 AwMessagePortServiceImpl* service = AwMessagePortServiceImpl::GetInstance(); |
| 143 service->Init(env, obj); |
| 144 return reinterpret_cast<intptr_t>(service); |
| 145 } |
| 146 |
| 147 } // namespace android_webview |
OLD | NEW |