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