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 (std::vector<int>::const_iterator iter = sent_ports.begin(); | |
mnaganov (inactive)
2015/01/29 16:56:20
nit: These days you can write it simpler:
for (c
sgurun-gerrit only
2015/01/29 19:15:40
^
no deref?
loved it.
| |
142 iter != sent_ports.end(); iter++) | |
143 ports_.erase(*iter); | |
144 } | |
145 | |
146 void AwMessagePortServiceImpl::PostAppToWebMessageOnIOThread( | |
147 int sender_id, | |
148 base::string16* message, | |
149 std::vector<int>* sent_ports) { | |
150 RemoveSentPorts(*sent_ports); | |
151 ports_[sender_id]->SendAppToWebMessage(sender_id, *message, *sent_ports); | |
152 } | |
153 | |
154 | |
117 void AwMessagePortServiceImpl::CreateMessageChannelOnIOThread( | 155 void AwMessagePortServiceImpl::CreateMessageChannelOnIOThread( |
118 scoped_refptr<AwMessagePortMessageFilter> filter, | 156 scoped_refptr<AwMessagePortMessageFilter> filter, |
119 int* portId1, | 157 int* portId1, |
120 int* portId2) { | 158 int* portId2) { |
121 content::MessagePortProvider::CreateMessageChannel(filter.get(), portId1, | 159 MessagePortProvider::CreateMessageChannel(filter.get(), portId1, portId2); |
122 portId2); | |
123 AddPort(*portId1, filter.get()); | 160 AddPort(*portId1, filter.get()); |
124 AddPort(*portId2, filter.get()); | 161 AddPort(*portId2, filter.get()); |
125 } | 162 } |
126 | 163 |
127 void AwMessagePortServiceImpl::OnMessageChannelCreated( | 164 void AwMessagePortServiceImpl::OnMessageChannelCreated( |
128 ScopedJavaGlobalRef<jobject>* callback, | 165 ScopedJavaGlobalRef<jobject>* callback, |
129 int* port1, | 166 int* port1, |
130 int* port2) { | 167 int* port2) { |
131 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 168 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
132 JNIEnv* env = AttachCurrentThread(); | 169 JNIEnv* env = AttachCurrentThread(); |
(...skipping 19 matching lines...) Expand all Loading... | |
152 } | 189 } |
153 | 190 |
154 // static | 191 // static |
155 jlong InitAwMessagePortService(JNIEnv* env, jobject obj) { | 192 jlong InitAwMessagePortService(JNIEnv* env, jobject obj) { |
156 AwMessagePortServiceImpl* service = AwMessagePortServiceImpl::GetInstance(); | 193 AwMessagePortServiceImpl* service = AwMessagePortServiceImpl::GetInstance(); |
157 service->Init(env, obj); | 194 service->Init(env, obj); |
158 return reinterpret_cast<intptr_t>(service); | 195 return reinterpret_cast<intptr_t>(service); |
159 } | 196 } |
160 | 197 |
161 } // namespace android_webview | 198 } // namespace android_webview |
OLD | NEW |