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 <stdint.h> | 7 #include <stdint.h> |
8 #include <limits> | 8 #include <limits> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/atomic_sequence_num.h" | 11 #include "base/atomic_sequence_num.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/ptr_util.h" | |
17 #include "base/metrics/histogram_macros.h" | 18 #include "base/metrics/histogram_macros.h" |
18 #include "base/stl_util.h" | |
19 #include "build/build_config.h" | 19 #include "build/build_config.h" |
20 #include "chrome/browser/extensions/api/messaging/extension_message_port.h" | 20 #include "chrome/browser/extensions/api/messaging/extension_message_port.h" |
21 #include "chrome/browser/extensions/api/messaging/incognito_connectability.h" | 21 #include "chrome/browser/extensions/api/messaging/incognito_connectability.h" |
22 #include "chrome/browser/extensions/api/messaging/native_message_port.h" | 22 #include "chrome/browser/extensions/api/messaging/native_message_port.h" |
23 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 23 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
24 #include "chrome/browser/extensions/extension_service.h" | 24 #include "chrome/browser/extensions/extension_service.h" |
25 #include "chrome/browser/extensions/extension_tab_util.h" | 25 #include "chrome/browser/extensions/extension_tab_util.h" |
26 #include "chrome/browser/extensions/extension_util.h" | 26 #include "chrome/browser/extensions/extension_util.h" |
27 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
28 #include "chrome/browser/tab_contents/tab_util.h" | 28 #include "chrome/browser/tab_contents/tab_util.h" |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
226 MessageService::MessageService(BrowserContext* context) | 226 MessageService::MessageService(BrowserContext* context) |
227 : lazy_background_task_queue_( | 227 : lazy_background_task_queue_( |
228 LazyBackgroundTaskQueue::Get(context)), | 228 LazyBackgroundTaskQueue::Get(context)), |
229 weak_factory_(this) { | 229 weak_factory_(this) { |
230 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 230 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
231 } | 231 } |
232 | 232 |
233 MessageService::~MessageService() { | 233 MessageService::~MessageService() { |
234 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 234 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
235 | 235 |
236 base::STLDeleteContainerPairSecondPointers(channels_.begin(), | |
237 channels_.end()); | |
238 channels_.clear(); | 236 channels_.clear(); |
239 } | 237 } |
240 | 238 |
241 static base::LazyInstance<BrowserContextKeyedAPIFactory<MessageService> > | 239 static base::LazyInstance<BrowserContextKeyedAPIFactory<MessageService> > |
242 g_factory = LAZY_INSTANCE_INITIALIZER; | 240 g_factory = LAZY_INSTANCE_INITIALIZER; |
243 | 241 |
244 // static | 242 // static |
245 BrowserContextKeyedAPIFactory<MessageService>* | 243 BrowserContextKeyedAPIFactory<MessageService>* |
246 MessageService::GetFactoryInstance() { | 244 MessageService::GetFactoryInstance() { |
247 return g_factory.Pointer(); | 245 return g_factory.Pointer(); |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
471 LOG(ERROR) << "Failed to create native process."; | 469 LOG(ERROR) << "Failed to create native process."; |
472 DispatchOnDisconnect(source, receiver_port_id, error); | 470 DispatchOnDisconnect(source, receiver_port_id, error); |
473 return; | 471 return; |
474 } | 472 } |
475 channel->receiver.reset(new NativeMessagePort( | 473 channel->receiver.reset(new NativeMessagePort( |
476 weak_factory_.GetWeakPtr(), receiver_port_id, std::move(native_host))); | 474 weak_factory_.GetWeakPtr(), receiver_port_id, std::move(native_host))); |
477 | 475 |
478 // Keep the opener alive until the channel is closed. | 476 // Keep the opener alive until the channel is closed. |
479 channel->opener->IncrementLazyKeepaliveCount(); | 477 channel->opener->IncrementLazyKeepaliveCount(); |
480 | 478 |
481 AddChannel(channel.release(), receiver_port_id); | 479 AddChannel(std::move(channel), receiver_port_id); |
482 #else // !(defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)) | 480 #else // !(defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)) |
483 const char kNativeMessagingNotSupportedError[] = | 481 const char kNativeMessagingNotSupportedError[] = |
484 "Native Messaging is not supported on this platform."; | 482 "Native Messaging is not supported on this platform."; |
485 DispatchOnDisconnect( | 483 DispatchOnDisconnect( |
486 source, receiver_port_id, kNativeMessagingNotSupportedError); | 484 source, receiver_port_id, kNativeMessagingNotSupportedError); |
487 #endif // !(defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)) | 485 #endif // !(defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)) |
488 } | 486 } |
489 | 487 |
490 void MessageService::OpenChannelToTab(int source_process_id, | 488 void MessageService::OpenChannelToTab(int source_process_id, |
491 int source_routing_id, | 489 int source_routing_id, |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
582 return; | 580 return; |
583 opener->OpenPort(params->source_process_id, params->source_routing_id); | 581 opener->OpenPort(params->source_process_id, params->source_routing_id); |
584 opener->RevalidatePort(); | 582 opener->RevalidatePort(); |
585 | 583 |
586 params->receiver->RemoveCommonFrames(*opener); | 584 params->receiver->RemoveCommonFrames(*opener); |
587 if (!params->receiver->IsValidPort()) { | 585 if (!params->receiver->IsValidPort()) { |
588 opener->DispatchOnDisconnect(kReceivingEndDoesntExistError); | 586 opener->DispatchOnDisconnect(kReceivingEndDoesntExistError); |
589 return; | 587 return; |
590 } | 588 } |
591 | 589 |
592 MessageChannel* channel(new MessageChannel()); | 590 std::unique_ptr<MessageChannel> channel_ptr = |
591 base::MakeUnique<MessageChannel>(); | |
592 MessageChannel* channel = channel_ptr.get(); | |
Devlin
2016/09/16 20:23:19
optional nit: maybe rename to "raw_channel"?
Avi (use Gerrit)
2016/09/16 20:41:45
I'll pass.
That's used for the rest of the functi
| |
593 channel->opener.reset(opener.release()); | 593 channel->opener.reset(opener.release()); |
594 channel->receiver.reset(params->receiver.release()); | 594 channel->receiver.reset(params->receiver.release()); |
595 AddChannel(channel, params->receiver_port_id); | 595 AddChannel(std::move(channel_ptr), params->receiver_port_id); |
596 | 596 |
597 // TODO(robwu): Could |guest_process_id| and |guest_render_frame_routing_id| | 597 // TODO(robwu): Could |guest_process_id| and |guest_render_frame_routing_id| |
598 // be removed? In the past extension message routing was process-based, but | 598 // be removed? In the past extension message routing was process-based, but |
599 // now that extensions are routed from a specific RFH, the special casing for | 599 // now that extensions are routed from a specific RFH, the special casing for |
600 // guest views seems no longer necessary, because the ExtensionMessagePort can | 600 // guest views seems no longer necessary, because the ExtensionMessagePort can |
601 // simply obtain the source process & frame ID directly from the RFH. | 601 // simply obtain the source process & frame ID directly from the RFH. |
602 int guest_process_id = content::ChildProcessHost::kInvalidUniqueID; | 602 int guest_process_id = content::ChildProcessHost::kInvalidUniqueID; |
603 int guest_render_frame_routing_id = MSG_ROUTING_NONE; | 603 int guest_render_frame_routing_id = MSG_ROUTING_NONE; |
604 if (params->include_guest_process_info) { | 604 if (params->include_guest_process_info) { |
605 guest_process_id = params->source_process_id; | 605 guest_process_id = params->source_process_id; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
646 } | 646 } |
647 EventRouter::Get(browser_context) | 647 EventRouter::Get(browser_context) |
648 ->ReportEvent(histogram_value, target_extension, did_enqueue); | 648 ->ReportEvent(histogram_value, target_extension, did_enqueue); |
649 } | 649 } |
650 | 650 |
651 // Keep both ends of the channel alive until the channel is closed. | 651 // Keep both ends of the channel alive until the channel is closed. |
652 channel->opener->IncrementLazyKeepaliveCount(); | 652 channel->opener->IncrementLazyKeepaliveCount(); |
653 channel->receiver->IncrementLazyKeepaliveCount(); | 653 channel->receiver->IncrementLazyKeepaliveCount(); |
654 } | 654 } |
655 | 655 |
656 void MessageService::AddChannel(MessageChannel* channel, int receiver_port_id) { | 656 void MessageService::AddChannel(std::unique_ptr<MessageChannel> channel, |
657 int receiver_port_id) { | |
657 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 658 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
658 | 659 |
659 int channel_id = GET_CHANNEL_ID(receiver_port_id); | 660 int channel_id = GET_CHANNEL_ID(receiver_port_id); |
660 CHECK(channels_.find(channel_id) == channels_.end()); | 661 CHECK(channels_.find(channel_id) == channels_.end()); |
661 channels_[channel_id] = channel; | 662 channels_[channel_id] = std::move(channel); |
662 pending_lazy_background_page_channels_.erase(channel_id); | 663 pending_lazy_background_page_channels_.erase(channel_id); |
663 } | 664 } |
664 | 665 |
665 void MessageService::OpenPort(int port_id, int process_id, int routing_id) { | 666 void MessageService::OpenPort(int port_id, int process_id, int routing_id) { |
666 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 667 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
667 DCHECK(!IS_OPENER_PORT_ID(port_id)); | 668 DCHECK(!IS_OPENER_PORT_ID(port_id)); |
668 | 669 |
669 int channel_id = GET_CHANNEL_ID(port_id); | 670 int channel_id = GET_CHANNEL_ID(port_id); |
670 MessageChannelMap::iterator it = channels_.find(channel_id); | 671 MessageChannelMap::iterator it = channels_.find(channel_id); |
671 if (it == channels_.end()) | 672 if (it == channels_.end()) |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
720 } | 721 } |
721 } | 722 } |
722 | 723 |
723 void MessageService::CloseChannelImpl( | 724 void MessageService::CloseChannelImpl( |
724 MessageChannelMap::iterator channel_iter, | 725 MessageChannelMap::iterator channel_iter, |
725 int closing_port_id, | 726 int closing_port_id, |
726 const std::string& error_message, | 727 const std::string& error_message, |
727 bool notify_other_port) { | 728 bool notify_other_port) { |
728 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 729 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
729 | 730 |
730 MessageChannel* channel = channel_iter->second; | 731 std::unique_ptr<MessageChannel> channel = std::move(channel_iter->second); |
731 // Remove from map to make sure that it is impossible for CloseChannelImpl to | 732 // Remove from map to make sure that it is impossible for CloseChannelImpl to |
732 // run twice for the same channel. | 733 // run twice for the same channel. |
733 channels_.erase(channel_iter); | 734 channels_.erase(channel_iter); |
734 | 735 |
735 // Notify the other side. | 736 // Notify the other side. |
736 if (notify_other_port) { | 737 if (notify_other_port) { |
737 MessagePort* port = IS_OPENER_PORT_ID(closing_port_id) ? | 738 MessagePort* port = IS_OPENER_PORT_ID(closing_port_id) ? |
738 channel->receiver.get() : channel->opener.get(); | 739 channel->receiver.get() : channel->opener.get(); |
739 port->DispatchOnDisconnect(error_message); | 740 port->DispatchOnDisconnect(error_message); |
740 } | 741 } |
741 | 742 |
742 // Balance the IncrementLazyKeepaliveCount() in OpenChannelImpl. | 743 // Balance the IncrementLazyKeepaliveCount() in OpenChannelImpl. |
743 channel->opener->DecrementLazyKeepaliveCount(); | 744 channel->opener->DecrementLazyKeepaliveCount(); |
744 channel->receiver->DecrementLazyKeepaliveCount(); | 745 channel->receiver->DecrementLazyKeepaliveCount(); |
745 | |
746 delete channel; | |
747 } | 746 } |
748 | 747 |
749 void MessageService::PostMessage(int source_port_id, const Message& message) { | 748 void MessageService::PostMessage(int source_port_id, const Message& message) { |
750 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 749 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
751 | 750 |
752 int channel_id = GET_CHANNEL_ID(source_port_id); | 751 int channel_id = GET_CHANNEL_ID(source_port_id); |
753 MessageChannelMap::iterator iter = channels_.find(channel_id); | 752 MessageChannelMap::iterator iter = channels_.find(channel_id); |
754 if (iter == channels_.end()) { | 753 if (iter == channels_.end()) { |
755 // If this channel is pending, queue up the PostMessage to run once | 754 // If this channel is pending, queue up the PostMessage to run once |
756 // the channel opens. | 755 // the channel opens. |
757 EnqueuePendingMessage(source_port_id, channel_id, message); | 756 EnqueuePendingMessage(source_port_id, channel_id, message); |
758 return; | 757 return; |
759 } | 758 } |
760 | 759 |
761 DispatchMessage(source_port_id, iter->second, message); | 760 DispatchMessage(source_port_id, iter->second.get(), message); |
762 } | 761 } |
763 | 762 |
764 void MessageService::EnqueuePendingMessage(int source_port_id, | 763 void MessageService::EnqueuePendingMessage(int source_port_id, |
765 int channel_id, | 764 int channel_id, |
766 const Message& message) { | 765 const Message& message) { |
767 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 766 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
768 | 767 |
769 PendingChannelMap::iterator pending_for_incognito = | 768 PendingChannelMap::iterator pending_for_incognito = |
770 pending_incognito_channels_.find(channel_id); | 769 pending_incognito_channels_.find(channel_id); |
771 if (pending_for_incognito != pending_incognito_channels_.end()) { | 770 if (pending_for_incognito != pending_incognito_channels_.end()) { |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1012 port.DispatchOnDisconnect(error_message); | 1011 port.DispatchOnDisconnect(error_message); |
1013 } | 1012 } |
1014 | 1013 |
1015 void MessageService::DispatchPendingMessages(const PendingMessagesQueue& queue, | 1014 void MessageService::DispatchPendingMessages(const PendingMessagesQueue& queue, |
1016 int channel_id) { | 1015 int channel_id) { |
1017 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1016 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
1018 | 1017 |
1019 MessageChannelMap::iterator channel_iter = channels_.find(channel_id); | 1018 MessageChannelMap::iterator channel_iter = channels_.find(channel_id); |
1020 if (channel_iter != channels_.end()) { | 1019 if (channel_iter != channels_.end()) { |
1021 for (const PendingMessage& message : queue) { | 1020 for (const PendingMessage& message : queue) { |
1022 DispatchMessage(message.first, channel_iter->second, message.second); | 1021 DispatchMessage(message.first, channel_iter->second.get(), |
1022 message.second); | |
1023 } | 1023 } |
1024 } | 1024 } |
1025 } | 1025 } |
1026 | 1026 |
1027 } // namespace extensions | 1027 } // namespace extensions |
OLD | NEW |