OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/messaging/message_service.h" | 5 #include "chrome/browser/extensions/api/messaging/message_service.h" |
6 | 6 |
7 #include "base/atomic_sequence_num.h" | 7 #include "base/atomic_sequence_num.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 } | 180 } |
181 | 181 |
182 OpenChannelImpl(scoped_ptr<OpenChannelParams>(params)); | 182 OpenChannelImpl(scoped_ptr<OpenChannelParams>(params)); |
183 } | 183 } |
184 | 184 |
185 void MessageService::OpenChannelToNativeApp( | 185 void MessageService::OpenChannelToNativeApp( |
186 int source_process_id, | 186 int source_process_id, |
187 int source_routing_id, | 187 int source_routing_id, |
188 int receiver_port_id, | 188 int receiver_port_id, |
189 const std::string& source_extension_id, | 189 const std::string& source_extension_id, |
190 const std::string& native_app_name, | 190 const std::string& native_app_name) { |
191 const std::string& channel_name, | |
192 const std::string& connect_message) { | |
193 content::RenderProcessHost* source = | 191 content::RenderProcessHost* source = |
194 content::RenderProcessHost::FromID(source_process_id); | 192 content::RenderProcessHost::FromID(source_process_id); |
195 if (!source) | 193 if (!source) |
196 return; | 194 return; |
197 | 195 |
198 WebContents* source_contents = tab_util::GetWebContentsByID( | 196 WebContents* source_contents = tab_util::GetWebContentsByID( |
199 source_process_id, source_routing_id); | 197 source_process_id, source_routing_id); |
200 | 198 |
201 // Include info about the opener's tab (if it was a tab). | 199 // Include info about the opener's tab (if it was a tab). |
202 std::string tab_json = "null"; | 200 std::string tab_json = "null"; |
203 if (source_contents) { | 201 if (source_contents) { |
204 scoped_ptr<DictionaryValue> tab_value(ExtensionTabUtil::CreateTabValue( | 202 scoped_ptr<DictionaryValue> tab_value(ExtensionTabUtil::CreateTabValue( |
205 source_contents)); | 203 source_contents)); |
206 base::JSONWriter::Write(tab_value.get(), &tab_json); | 204 base::JSONWriter::Write(tab_value.get(), &tab_json); |
207 } | 205 } |
208 | 206 |
209 scoped_ptr<MessageChannel> channel(new MessageChannel()); | 207 scoped_ptr<MessageChannel> channel(new MessageChannel()); |
210 channel->opener.reset(new ExtensionMessagePort(source, MSG_ROUTING_CONTROL, | 208 channel->opener.reset(new ExtensionMessagePort(source, MSG_ROUTING_CONTROL, |
211 source_extension_id)); | 209 source_extension_id)); |
212 | 210 |
213 NativeMessageProcessHost::MessageType type = | |
214 channel_name == "chrome.runtime.sendNativeMessage" ? | |
215 NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST : | |
216 NativeMessageProcessHost::TYPE_CONNECT; | |
217 | 211 |
218 content::BrowserThread::PostTask( | 212 scoped_ptr<NativeMessageProcessHost> native_process = |
219 content::BrowserThread::FILE, | 213 NativeMessageProcessHost::Create( |
220 FROM_HERE, | 214 base::WeakPtr<NativeMessageProcessHost::Client>( |
221 base::Bind(&NativeMessageProcessHost::Create, | 215 weak_factory_.GetWeakPtr()), |
222 base::WeakPtr<NativeMessageProcessHost::Client>( | 216 native_app_name, receiver_port_id); |
223 weak_factory_.GetWeakPtr()), | |
224 native_app_name, connect_message, receiver_port_id, | |
225 type, | |
226 base::Bind(&MessageService::FinalizeOpenChannelToNativeApp, | |
227 weak_factory_.GetWeakPtr(), | |
228 receiver_port_id, | |
229 channel_name, | |
230 base::Passed(&channel), | |
231 tab_json))); | |
232 } | |
233 | |
234 void MessageService::FinalizeOpenChannelToNativeApp( | |
235 int receiver_port_id, | |
236 const std::string& channel_name, | |
237 scoped_ptr<MessageChannel> channel, | |
238 const std::string& tab_json, | |
239 NativeMessageProcessHost::ScopedHost native_process) { | |
240 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
241 | 217 |
242 // Abandon the channel | 218 // Abandon the channel |
243 if (!native_process.get()) { | 219 if (!native_process.get()) { |
244 LOG(ERROR) << "Failed to create native process."; | 220 LOG(ERROR) << "Failed to create native process."; |
245 return; | 221 return; |
246 } | 222 } |
247 channel->receiver.reset(new NativeMessagePort(native_process.release())); | 223 channel->receiver.reset(new NativeMessagePort(native_process.release())); |
248 | 224 |
249 // Keep the opener alive until the channel is closed. | 225 // Keep the opener alive until the channel is closed. |
250 channel->opener->IncrementLazyKeepaliveCount(); | 226 channel->opener->IncrementLazyKeepaliveCount(); |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
500 return; | 476 return; |
501 | 477 |
502 params->source = source; | 478 params->source = source; |
503 params->receiver.reset(new ExtensionMessagePort(host->render_process_host(), | 479 params->receiver.reset(new ExtensionMessagePort(host->render_process_host(), |
504 MSG_ROUTING_CONTROL, | 480 MSG_ROUTING_CONTROL, |
505 params->target_extension_id)); | 481 params->target_extension_id)); |
506 OpenChannelImpl(params.Pass()); | 482 OpenChannelImpl(params.Pass()); |
507 } | 483 } |
508 | 484 |
509 } // namespace extensions | 485 } // namespace extensions |
OLD | NEW |