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