| 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/bind_helpers.h" | |
| 10 #include "base/callback.h" | 9 #include "base/callback.h" |
| 11 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 12 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 13 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 14 #include "base/values.h" | 13 #include "base/values.h" |
| 15 #include "chrome/browser/extensions/api/messaging/extension_message_port.h" | 14 #include "chrome/browser/extensions/api/messaging/extension_message_port.h" |
| 16 #include "chrome/browser/extensions/api/messaging/native_message_port.h" | 15 #include "chrome/browser/extensions/api/messaging/native_message_port.h" |
| 17 #include "chrome/browser/extensions/extension_host.h" | 16 #include "chrome/browser/extensions/extension_host.h" |
| 18 #include "chrome/browser/extensions/extension_process_manager.h" | 17 #include "chrome/browser/extensions/extension_process_manager.h" |
| 19 #include "chrome/browser/extensions/extension_service.h" | 18 #include "chrome/browser/extensions/extension_service.h" |
| (...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 | 422 |
| 424 // Balance the IncrementLazyKeepaliveCount() in OpenChannelImpl. | 423 // Balance the IncrementLazyKeepaliveCount() in OpenChannelImpl. |
| 425 channel->opener->DecrementLazyKeepaliveCount(); | 424 channel->opener->DecrementLazyKeepaliveCount(); |
| 426 channel->receiver->DecrementLazyKeepaliveCount(); | 425 channel->receiver->DecrementLazyKeepaliveCount(); |
| 427 | 426 |
| 428 delete channel_iter->second; | 427 delete channel_iter->second; |
| 429 channels_.erase(channel_iter); | 428 channels_.erase(channel_iter); |
| 430 } | 429 } |
| 431 | 430 |
| 432 void MessageService::PostMessage( | 431 void MessageService::PostMessage( |
| 433 int source_port_id, scoped_ptr<base::ListValue> message) { | 432 int source_port_id, const std::string& message) { |
| 434 int channel_id = GET_CHANNEL_ID(source_port_id); | 433 int channel_id = GET_CHANNEL_ID(source_port_id); |
| 435 MessageChannelMap::iterator iter = channels_.find(channel_id); | 434 MessageChannelMap::iterator iter = channels_.find(channel_id); |
| 436 if (iter == channels_.end()) { | 435 if (iter == channels_.end()) { |
| 437 // If this channel is pending, queue up the PostMessage to run once | 436 // If this channel is pending, queue up the PostMessage to run once |
| 438 // the channel opens. | 437 // the channel opens. |
| 439 PendingChannelMap::iterator pending = pending_channels_.find(channel_id); | 438 PendingChannelMap::iterator pending = pending_channels_.find(channel_id); |
| 440 if (pending != pending_channels_.end()) { | 439 if (pending != pending_channels_.end()) { |
| 441 lazy_background_task_queue_->AddPendingTask( | 440 lazy_background_task_queue_->AddPendingTask( |
| 442 pending->second.first, pending->second.second, | 441 pending->second.first, pending->second.second, |
| 443 base::Bind(&MessageService::PendingPostMessage, | 442 base::Bind(&MessageService::PendingPostMessage, |
| 444 weak_factory_.GetWeakPtr(), | 443 weak_factory_.GetWeakPtr(), source_port_id, message)); |
| 445 source_port_id, | |
| 446 base::Passed(&message))); | |
| 447 } | 444 } |
| 448 return; | 445 return; |
| 449 } | 446 } |
| 450 | 447 |
| 451 // Figure out which port the ID corresponds to. | 448 // Figure out which port the ID corresponds to. |
| 452 int dest_port_id = GET_OPPOSITE_PORT_ID(source_port_id); | 449 int dest_port_id = GET_OPPOSITE_PORT_ID(source_port_id); |
| 453 MessagePort* port = IS_OPENER_PORT_ID(dest_port_id) ? | 450 MessagePort* port = IS_OPENER_PORT_ID(dest_port_id) ? |
| 454 iter->second->opener.get() : iter->second->receiver.get(); | 451 iter->second->opener.get() : iter->second->receiver.get(); |
| 455 | 452 |
| 456 port->DispatchOnMessage(message.Pass(), dest_port_id); | 453 port->DispatchOnMessage(message, dest_port_id); |
| 457 } | 454 } |
| 458 | 455 |
| 459 void MessageService::PostMessageFromNativeProcess( | 456 void MessageService::PostMessageFromNativeProcess(int port_id, |
| 460 int port_id, | 457 const std::string& message) { |
| 461 scoped_ptr<base::ListValue> message) { | 458 PostMessage(port_id, message); |
| 462 PostMessage(port_id, message.Pass()); | |
| 463 } | 459 } |
| 464 | 460 |
| 465 void MessageService::Observe(int type, | 461 void MessageService::Observe(int type, |
| 466 const content::NotificationSource& source, | 462 const content::NotificationSource& source, |
| 467 const content::NotificationDetails& details) { | 463 const content::NotificationDetails& details) { |
| 468 switch (type) { | 464 switch (type) { |
| 469 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: | 465 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: |
| 470 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { | 466 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { |
| 471 content::RenderProcessHost* renderer = | 467 content::RenderProcessHost* renderer = |
| 472 content::Source<content::RenderProcessHost>(source).ptr(); | 468 content::Source<content::RenderProcessHost>(source).ptr(); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 544 return; | 540 return; |
| 545 | 541 |
| 546 params->source = source; | 542 params->source = source; |
| 547 params->receiver.reset(new ExtensionMessagePort(host->render_process_host(), | 543 params->receiver.reset(new ExtensionMessagePort(host->render_process_host(), |
| 548 MSG_ROUTING_CONTROL, | 544 MSG_ROUTING_CONTROL, |
| 549 params->target_extension_id)); | 545 params->target_extension_id)); |
| 550 OpenChannelImpl(params.Pass()); | 546 OpenChannelImpl(params.Pass()); |
| 551 } | 547 } |
| 552 | 548 |
| 553 } // namespace extensions | 549 } // namespace extensions |
| OLD | NEW |