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::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::CreateMessageChannel( |
| 66 JNIEnv* env, |
| 67 jobject callback, |
| 68 scoped_refptr<AwMessagePortMessageFilter> filter) { |
| 69 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 70 |
| 71 ScopedJavaGlobalRef<jobject>* j_callback = new ScopedJavaGlobalRef<jobject>(); |
| 72 j_callback->Reset(env, callback); |
| 73 |
| 74 int* portId1 = new int; |
| 75 int* portId2 = new int; |
| 76 BrowserThread::PostTaskAndReply( |
| 77 BrowserThread::IO, |
| 78 FROM_HERE, |
| 79 base::Bind(&AwMessagePortServiceImpl::CreateMessageChannelOnIOThread, |
| 80 base::Unretained(this), |
| 81 filter, |
| 82 portId1, |
| 83 portId2), |
| 84 base::Bind(&AwMessagePortServiceImpl::OnMessageChannelCreated, |
| 85 base::Unretained(this), |
| 86 base::Owned(j_callback), |
| 87 base::Owned(portId1), |
| 88 base::Owned(portId2))); |
| 89 } |
| 90 |
| 91 void AwMessagePortServiceImpl::OnConvertedMessage( |
| 92 int message_port_id, |
| 93 const base::ListValue& message, |
| 94 const std::vector<int>& sent_message_port_ids) { |
| 95 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 96 JNIEnv* env = AttachCurrentThread(); |
| 97 ScopedJavaGlobalRef<jobject>* jobj = new ScopedJavaGlobalRef<jobject>(); |
| 98 jobj->Reset(java_ref_.get(env)); |
| 99 |
| 100 if (message.GetSize() != 1) { |
| 101 NOTREACHED(); |
| 102 return; |
| 103 } |
| 104 |
| 105 base::string16 value; |
| 106 if (!message.GetString(0, &value)) { |
| 107 LOG(WARNING) << "Converting post message to a string failed for port " |
| 108 << message_port_id; |
| 109 return; |
| 110 } |
| 111 ScopedJavaGlobalRef<jstring>* jmsg = new ScopedJavaGlobalRef<jstring>(); |
| 112 jmsg->Reset(ConvertUTF16ToJavaString(env, value)); |
| 113 |
| 114 ScopedJavaGlobalRef<jintArray>* jports = new ScopedJavaGlobalRef<jintArray>(); |
| 115 jports->Reset(ToJavaIntArray(env, sent_message_port_ids)); |
| 116 |
| 117 BrowserThread::PostTask( |
| 118 BrowserThread::UI, |
| 119 FROM_HERE, |
| 120 base::Bind(&PostMessageOnUIThread, base::Owned(jobj), |
| 121 message_port_id, |
| 122 base::Owned(jmsg), |
| 123 base::Owned(jports))); |
| 124 } |
| 125 |
| 126 void AwMessagePortServiceImpl::OnMessagePortMessageFilterClosing( |
| 127 AwMessagePortMessageFilter* filter) { |
| 128 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 129 for (MessagePorts::iterator iter = ports_.begin(); |
| 130 iter != ports_.end(); iter++) { |
| 131 if (iter->second == filter) { |
| 132 ports_.erase(iter); |
| 133 } |
| 134 } |
| 135 } |
| 136 |
| 137 void AwMessagePortServiceImpl::CreateMessageChannelOnIOThread( |
| 138 scoped_refptr<AwMessagePortMessageFilter> filter, |
| 139 int* portId1, |
| 140 int* portId2) { |
| 141 content::MessagePortProvider::CreateMessageChannel(filter.get(), portId1, |
| 142 portId2); |
| 143 AddPort(*portId1, filter.get()); |
| 144 AddPort(*portId2, filter.get()); |
| 145 } |
| 146 |
| 147 void AwMessagePortServiceImpl::OnMessageChannelCreated( |
| 148 ScopedJavaGlobalRef<jobject>* callback, |
| 149 int* port1, |
| 150 int* port2) { |
| 151 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 152 JNIEnv* env = AttachCurrentThread(); |
| 153 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| 154 if (obj.is_null()) |
| 155 return; |
| 156 Java_AwMessagePortService_onMessageChannelCreated(env, obj.obj(), *port1, |
| 157 *port2, callback->obj()); |
| 158 } |
| 159 |
| 160 void AwMessagePortServiceImpl::AddPort(int message_port_id, |
| 161 AwMessagePortMessageFilter* filter) { |
| 162 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 163 if (ports_.count(message_port_id)) { |
| 164 NOTREACHED(); |
| 165 return; |
| 166 } |
| 167 ports_[message_port_id] = filter; |
| 168 } |
| 169 |
| 170 bool RegisterAwMessagePortService(JNIEnv* env) { |
| 171 return RegisterNativesImpl(env); |
| 172 } |
| 173 |
| 174 // static |
| 175 jlong InitAwMessagePortService(JNIEnv* env, jobject obj) { |
| 176 AwMessagePortServiceImpl* service = AwMessagePortServiceImpl::GetInstance(); |
| 177 service->Init(env, obj); |
| 178 return reinterpret_cast<intptr_t>(service); |
| 179 } |
| 180 |
| 181 } // namespace android_webview |
OLD | NEW |