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" |
11 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/browser/message_port_provider.h" | 14 #include "content/public/browser/message_port_provider.h" |
15 #include "jni/AwMessagePortService_jni.h" | 15 #include "jni/AwMessagePortService_jni.h" |
16 | 16 |
17 namespace android_webview { | 17 namespace android_webview { |
18 | 18 |
19 using base::android::AttachCurrentThread; | 19 using base::android::AttachCurrentThread; |
| 20 using base::android::ConvertJavaStringToUTF16; |
20 using base::android::ConvertUTF16ToJavaString; | 21 using base::android::ConvertUTF16ToJavaString; |
21 using base::android::ScopedJavaGlobalRef; | 22 using base::android::ScopedJavaGlobalRef; |
22 using base::android::ScopedJavaLocalRef; | 23 using base::android::ScopedJavaLocalRef; |
23 using base::android::ToJavaIntArray; | 24 using base::android::ToJavaIntArray; |
24 using content::BrowserThread; | 25 using content::BrowserThread; |
25 using content::MessagePortProvider; | 26 using content::MessagePortProvider; |
26 | 27 |
27 //static | 28 //static |
28 AwMessagePortServiceImpl* AwMessagePortServiceImpl::GetInstance() { | 29 AwMessagePortServiceImpl* AwMessagePortServiceImpl::GetInstance() { |
29 return static_cast<AwMessagePortServiceImpl*>( | 30 return static_cast<AwMessagePortServiceImpl*>( |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 filter, | 66 filter, |
66 portId1, | 67 portId1, |
67 portId2), | 68 portId2), |
68 base::Bind(&AwMessagePortServiceImpl::OnMessageChannelCreated, | 69 base::Bind(&AwMessagePortServiceImpl::OnMessageChannelCreated, |
69 base::Unretained(this), | 70 base::Unretained(this), |
70 base::Owned(j_callback), | 71 base::Owned(j_callback), |
71 base::Owned(portId1), | 72 base::Owned(portId1), |
72 base::Owned(portId2))); | 73 base::Owned(portId2))); |
73 } | 74 } |
74 | 75 |
75 void AwMessagePortServiceImpl::OnConvertedMessage( | 76 void AwMessagePortServiceImpl::OnConvertedWebToAppMessage( |
76 int message_port_id, | 77 int message_port_id, |
77 const base::ListValue& message, | 78 const base::ListValue& message, |
78 const std::vector<int>& sent_message_port_ids) { | 79 const std::vector<int>& sent_message_port_ids) { |
79 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 80 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
80 JNIEnv* env = AttachCurrentThread(); | 81 JNIEnv* env = AttachCurrentThread(); |
81 ScopedJavaLocalRef<jobject> jobj = java_ref_.get(env); | 82 ScopedJavaLocalRef<jobject> jobj = java_ref_.get(env); |
82 if (jobj.is_null()) | 83 if (jobj.is_null()) |
83 return; | 84 return; |
84 | 85 |
85 if (message.GetSize() != 1) { | 86 if (message.GetSize() != 1) { |
(...skipping 21 matching lines...) Expand all Loading... |
107 AwMessagePortMessageFilter* filter) { | 108 AwMessagePortMessageFilter* filter) { |
108 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 109 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
109 for (MessagePorts::iterator iter = ports_.begin(); | 110 for (MessagePorts::iterator iter = ports_.begin(); |
110 iter != ports_.end(); iter++) { | 111 iter != ports_.end(); iter++) { |
111 if (iter->second == filter) { | 112 if (iter->second == filter) { |
112 ports_.erase(iter); | 113 ports_.erase(iter); |
113 } | 114 } |
114 } | 115 } |
115 } | 116 } |
116 | 117 |
| 118 void AwMessagePortServiceImpl::PostAppToWebMessage(JNIEnv* env, jobject obj, |
| 119 int sender_id, jstring message, jintArray sent_ports) { |
| 120 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 121 base::string16* j_message = new base::string16; |
| 122 ConvertJavaStringToUTF16(env, message, j_message); |
| 123 std::vector<int>* j_sent_ports = new std::vector<int>; |
| 124 if (sent_ports != nullptr) |
| 125 base::android::JavaIntArrayToIntVector(env, sent_ports, j_sent_ports); |
| 126 |
| 127 BrowserThread::PostTask( |
| 128 BrowserThread::IO, |
| 129 FROM_HERE, |
| 130 base::Bind(&AwMessagePortServiceImpl::PostAppToWebMessageOnIOThread, |
| 131 base::Unretained(this), |
| 132 sender_id, |
| 133 base::Owned(j_message), |
| 134 base::Owned(j_sent_ports))); |
| 135 } |
| 136 |
| 137 void AwMessagePortServiceImpl::RemoveSentPorts( |
| 138 const std::vector<int>& sent_ports) { |
| 139 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 140 // Remove the filters that are associated with the transferred ports |
| 141 for (const auto& iter : sent_ports) |
| 142 ports_.erase(iter); |
| 143 } |
| 144 |
| 145 void AwMessagePortServiceImpl::PostAppToWebMessageOnIOThread( |
| 146 int sender_id, |
| 147 base::string16* message, |
| 148 std::vector<int>* sent_ports) { |
| 149 RemoveSentPorts(*sent_ports); |
| 150 ports_[sender_id]->SendAppToWebMessage(sender_id, *message, *sent_ports); |
| 151 } |
| 152 |
| 153 |
117 void AwMessagePortServiceImpl::CreateMessageChannelOnIOThread( | 154 void AwMessagePortServiceImpl::CreateMessageChannelOnIOThread( |
118 scoped_refptr<AwMessagePortMessageFilter> filter, | 155 scoped_refptr<AwMessagePortMessageFilter> filter, |
119 int* portId1, | 156 int* portId1, |
120 int* portId2) { | 157 int* portId2) { |
121 content::MessagePortProvider::CreateMessageChannel(filter.get(), portId1, | 158 MessagePortProvider::CreateMessageChannel(filter.get(), portId1, portId2); |
122 portId2); | |
123 AddPort(*portId1, filter.get()); | 159 AddPort(*portId1, filter.get()); |
124 AddPort(*portId2, filter.get()); | 160 AddPort(*portId2, filter.get()); |
125 } | 161 } |
126 | 162 |
127 void AwMessagePortServiceImpl::OnMessageChannelCreated( | 163 void AwMessagePortServiceImpl::OnMessageChannelCreated( |
128 ScopedJavaGlobalRef<jobject>* callback, | 164 ScopedJavaGlobalRef<jobject>* callback, |
129 int* port1, | 165 int* port1, |
130 int* port2) { | 166 int* port2) { |
131 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 167 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
132 JNIEnv* env = AttachCurrentThread(); | 168 JNIEnv* env = AttachCurrentThread(); |
(...skipping 19 matching lines...) Expand all Loading... |
152 } | 188 } |
153 | 189 |
154 // static | 190 // static |
155 jlong InitAwMessagePortService(JNIEnv* env, jobject obj) { | 191 jlong InitAwMessagePortService(JNIEnv* env, jobject obj) { |
156 AwMessagePortServiceImpl* service = AwMessagePortServiceImpl::GetInstance(); | 192 AwMessagePortServiceImpl* service = AwMessagePortServiceImpl::GetInstance(); |
157 service->Init(env, obj); | 193 service->Init(env, obj); |
158 return reinterpret_cast<intptr_t>(service); | 194 return reinterpret_cast<intptr_t>(service); |
159 } | 195 } |
160 | 196 |
161 } // namespace android_webview | 197 } // namespace android_webview |
OLD | NEW |