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 |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 BrowserContext* context, | 186 BrowserContext* context, |
187 const std::string& extension_id) { | 187 const std::string& extension_id) { |
188 scoped_refptr<SiteInstance> site_instance = | 188 scoped_refptr<SiteInstance> site_instance = |
189 ProcessManager::Get(context)->GetSiteInstanceForURL( | 189 ProcessManager::Get(context)->GetSiteInstanceForURL( |
190 Extension::GetBaseURLFromExtensionId(extension_id)); | 190 Extension::GetBaseURLFromExtensionId(extension_id)); |
191 return site_instance->HasProcess() ? site_instance->GetProcess() : NULL; | 191 return site_instance->HasProcess() ? site_instance->GetProcess() : NULL; |
192 } | 192 } |
193 | 193 |
194 } // namespace | 194 } // namespace |
195 | 195 |
| 196 void MessageService::MessagePort::RemoveCommonFrames(const MessagePort& port) {} |
| 197 |
| 198 bool MessageService::MessagePort::HasFrame( |
| 199 content::RenderFrameHost* rfh) const { |
| 200 return false; |
| 201 } |
| 202 |
196 // static | 203 // static |
197 void MessageService::AllocatePortIdPair(int* port1, int* port2) { | 204 void MessageService::AllocatePortIdPair(int* port1, int* port2) { |
198 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 205 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
199 | 206 |
200 unsigned channel_id = static_cast<unsigned>(g_next_channel_id.GetNext()) % | 207 unsigned channel_id = static_cast<unsigned>(g_next_channel_id.GetNext()) % |
201 (std::numeric_limits<int32_t>::max() / 2); | 208 (std::numeric_limits<int32_t>::max() / 2); |
202 unsigned port1_id = channel_id * 2; | 209 unsigned port1_id = channel_id * 2; |
203 unsigned port2_id = channel_id * 2 + 1; | 210 unsigned port2_id = channel_id * 2 + 1; |
204 | 211 |
205 // Sanity checks to make sure our channel<->port converters are correct. | 212 // Sanity checks to make sure our channel<->port converters are correct. |
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
562 return; | 569 return; |
563 } | 570 } |
564 | 571 |
565 scoped_ptr<MessagePort> opener( | 572 scoped_ptr<MessagePort> opener( |
566 new ExtensionMessagePort(weak_factory_.GetWeakPtr(), | 573 new ExtensionMessagePort(weak_factory_.GetWeakPtr(), |
567 GET_OPPOSITE_PORT_ID(params->receiver_port_id), | 574 GET_OPPOSITE_PORT_ID(params->receiver_port_id), |
568 params->source_extension_id, source, false)); | 575 params->source_extension_id, source, false)); |
569 if (!opener->IsValidPort()) | 576 if (!opener->IsValidPort()) |
570 return; | 577 return; |
571 | 578 |
| 579 params->receiver->RemoveCommonFrames(*opener); |
| 580 if (!params->receiver->IsValidPort()) { |
| 581 opener->DispatchOnDisconnect(kReceivingEndDoesntExistError); |
| 582 return; |
| 583 } |
| 584 |
572 MessageChannel* channel(new MessageChannel()); | 585 MessageChannel* channel(new MessageChannel()); |
573 channel->opener.reset(opener.release()); | 586 channel->opener.reset(opener.release()); |
574 channel->receiver.reset(params->receiver.release()); | 587 channel->receiver.reset(params->receiver.release()); |
575 AddChannel(channel, params->receiver_port_id); | 588 AddChannel(channel, params->receiver_port_id); |
576 | 589 |
577 // TODO(robwu): Could |guest_process_id| and |guest_render_frame_routing_id| | 590 // TODO(robwu): Could |guest_process_id| and |guest_render_frame_routing_id| |
578 // be removed? In the past extension message routing was process-based, but | 591 // be removed? In the past extension message routing was process-based, but |
579 // now that extensions are routed from a specific RFH, the special casing for | 592 // now that extensions are routed from a specific RFH, the special casing for |
580 // guest views seems no longer necessary, because the ExtensionMessagePort can | 593 // guest views seems no longer necessary, because the ExtensionMessagePort can |
581 // simply obtain the source process & frame ID directly from the RFH. | 594 // simply obtain the source process & frame ID directly from the RFH. |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
701 } | 714 } |
702 | 715 |
703 void MessageService::CloseChannelImpl( | 716 void MessageService::CloseChannelImpl( |
704 MessageChannelMap::iterator channel_iter, | 717 MessageChannelMap::iterator channel_iter, |
705 int closing_port_id, | 718 int closing_port_id, |
706 const std::string& error_message, | 719 const std::string& error_message, |
707 bool notify_other_port) { | 720 bool notify_other_port) { |
708 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 721 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
709 | 722 |
710 MessageChannel* channel = channel_iter->second; | 723 MessageChannel* channel = channel_iter->second; |
| 724 // Remove from map to make sure that it is impossible for CloseChannelImpl to |
| 725 // run twice for the same channel. |
| 726 channels_.erase(channel_iter); |
711 | 727 |
712 // Notify the other side. | 728 // Notify the other side. |
713 if (notify_other_port) { | 729 if (notify_other_port) { |
714 MessagePort* port = IS_OPENER_PORT_ID(closing_port_id) ? | 730 MessagePort* port = IS_OPENER_PORT_ID(closing_port_id) ? |
715 channel->receiver.get() : channel->opener.get(); | 731 channel->receiver.get() : channel->opener.get(); |
716 port->DispatchOnDisconnect(error_message); | 732 port->DispatchOnDisconnect(error_message); |
717 } | 733 } |
718 | 734 |
719 // Balance the IncrementLazyKeepaliveCount() in OpenChannelImpl. | 735 // Balance the IncrementLazyKeepaliveCount() in OpenChannelImpl. |
720 channel->opener->DecrementLazyKeepaliveCount(); | 736 channel->opener->DecrementLazyKeepaliveCount(); |
721 channel->receiver->DecrementLazyKeepaliveCount(); | 737 channel->receiver->DecrementLazyKeepaliveCount(); |
722 | 738 |
723 delete channel_iter->second; | 739 delete channel; |
724 channels_.erase(channel_iter); | |
725 } | 740 } |
726 | 741 |
727 void MessageService::PostMessage(int source_port_id, const Message& message) { | 742 void MessageService::PostMessage(int source_port_id, const Message& message) { |
728 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 743 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
729 | 744 |
730 int channel_id = GET_CHANNEL_ID(source_port_id); | 745 int channel_id = GET_CHANNEL_ID(source_port_id); |
731 MessageChannelMap::iterator iter = channels_.find(channel_id); | 746 MessageChannelMap::iterator iter = channels_.find(channel_id); |
732 if (iter == channels_.end()) { | 747 if (iter == channels_.end()) { |
733 // If this channel is pending, queue up the PostMessage to run once | 748 // If this channel is pending, queue up the PostMessage to run once |
734 // the channel opens. | 749 // the channel opens. |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
993 | 1008 |
994 MessageChannelMap::iterator channel_iter = channels_.find(channel_id); | 1009 MessageChannelMap::iterator channel_iter = channels_.find(channel_id); |
995 if (channel_iter != channels_.end()) { | 1010 if (channel_iter != channels_.end()) { |
996 for (const PendingMessage& message : queue) { | 1011 for (const PendingMessage& message : queue) { |
997 DispatchMessage(message.first, channel_iter->second, message.second); | 1012 DispatchMessage(message.first, channel_iter->second, message.second); |
998 } | 1013 } |
999 } | 1014 } |
1000 } | 1015 } |
1001 | 1016 |
1002 } // namespace extensions | 1017 } // namespace extensions |
OLD | NEW |