Chromium Code Reviews| 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 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 112 }; | 112 }; |
| 113 | 113 |
| 114 namespace { | 114 namespace { |
| 115 | 115 |
| 116 static base::StaticAtomicSequenceNumber g_next_channel_id; | 116 static base::StaticAtomicSequenceNumber g_next_channel_id; |
| 117 static base::StaticAtomicSequenceNumber g_channel_id_overflow_count; | 117 static base::StaticAtomicSequenceNumber g_channel_id_overflow_count; |
| 118 | 118 |
| 119 static content::RenderProcessHost* GetExtensionProcess( | 119 static content::RenderProcessHost* GetExtensionProcess( |
| 120 Profile* profile, const std::string& extension_id) { | 120 Profile* profile, const std::string& extension_id) { |
| 121 SiteInstance* site_instance = | 121 SiteInstance* site_instance = |
| 122 ExtensionSystem::Get(profile)->process_manager()-> | 122 ExtensionSystem::Get(profile)->process_manager()->GetSiteInstanceForURL( |
| 123 GetSiteInstanceForURL( | 123 Extension::GetBaseURLFromExtensionId(extension_id)); |
| 124 Extension::GetBaseURLFromExtensionId(extension_id)); | 124 return site_instance->HasProcess() ? site_instance->GetProcess() : NULL; |
| 125 | |
| 126 if (!site_instance->HasProcess()) | |
| 127 return NULL; | |
| 128 | |
| 129 return site_instance->GetProcess(); | |
| 130 } | 125 } |
| 131 | 126 |
| 132 } // namespace | 127 } // namespace |
| 133 | 128 |
| 134 content::RenderProcessHost* | 129 content::RenderProcessHost* |
| 135 MessageService::MessagePort::GetRenderProcessHost() { | 130 MessageService::MessagePort::GetRenderProcessHost() { |
| 136 return NULL; | 131 return NULL; |
| 137 } | 132 } |
| 138 | 133 |
| 139 // static | 134 // static |
| 140 void MessageService::AllocatePortIdPair(int* port1, int* port2) { | 135 void MessageService::AllocatePortIdPair(int* port1, int* port2) { |
| 141 unsigned channel_id = | 136 unsigned channel_id = |
| 142 static_cast<unsigned>(g_next_channel_id.GetNext()) % (kint32max/2); | 137 static_cast<unsigned>(g_next_channel_id.GetNext()) % (kint32max/2); |
| 143 | 138 |
| 144 if (channel_id == 0) { | 139 if (channel_id == 0) { |
| 145 int overflow_count = g_channel_id_overflow_count.GetNext(); | 140 int overflow_count = g_channel_id_overflow_count.GetNext(); |
| 146 if (overflow_count > 0) | 141 if (overflow_count > 0) |
| 147 UMA_HISTOGRAM_BOOLEAN("Extensions.AllocatePortIdPairOverflow", true); | 142 UMA_HISTOGRAM_BOOLEAN("Extensions.AllocatePortIdPairOverflow", true); |
| 148 } | 143 } |
| 149 | 144 |
| 150 unsigned port1_id = channel_id * 2; | 145 unsigned port1_id = channel_id * 2; |
| 151 unsigned port2_id = channel_id * 2 + 1; | 146 unsigned port2_id = channel_id * 2 + 1; |
| 152 | 147 |
| 153 // Sanity checks to make sure our channel<->port converters are correct. | 148 // Sanity checks to make sure our channel<->port converters are correct. |
| 154 DCHECK(IS_OPENER_PORT_ID(port1_id)); | 149 DCHECK(IS_OPENER_PORT_ID(port1_id)); |
| 155 DCHECK(GET_OPPOSITE_PORT_ID(port1_id) == port2_id); | 150 DCHECK_EQ(GET_OPPOSITE_PORT_ID(port1_id), port2_id); |
| 156 DCHECK(GET_OPPOSITE_PORT_ID(port2_id) == port1_id); | 151 DCHECK_EQ(GET_OPPOSITE_PORT_ID(port2_id), port1_id); |
| 157 DCHECK(GET_CHANNEL_ID(port1_id) == GET_CHANNEL_ID(port2_id)); | 152 DCHECK_EQ(GET_CHANNEL_ID(port1_id), GET_CHANNEL_ID(port2_id)); |
| 158 DCHECK(GET_CHANNEL_ID(port1_id) == channel_id); | 153 DCHECK_EQ(GET_CHANNEL_ID(port1_id), channel_id); |
| 159 DCHECK(GET_CHANNEL_OPENER_ID(channel_id) == port1_id); | 154 DCHECK_EQ(GET_CHANNEL_OPENER_ID(channel_id), port1_id); |
| 160 DCHECK(GET_CHANNEL_RECEIVERS_ID(channel_id) == port2_id); | 155 DCHECK_EQ(GET_CHANNEL_RECEIVERS_ID(channel_id), port2_id); |
| 161 | 156 |
| 162 *port1 = port1_id; | 157 *port1 = port1_id; |
| 163 *port2 = port2_id; | 158 *port2 = port2_id; |
| 164 } | 159 } |
| 165 | 160 |
| 166 MessageService::MessageService(Profile* profile) | 161 MessageService::MessageService(Profile* profile) |
| 167 : lazy_background_task_queue_( | 162 : lazy_background_task_queue_( |
| 168 ExtensionSystem::Get(profile)->lazy_background_task_queue()), | 163 ExtensionSystem::Get(profile)->lazy_background_task_queue()), |
| 169 weak_factory_(this) { | 164 weak_factory_(this) { |
| 170 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 165 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 296 MessagePort* receiver = new ExtensionMessagePort( | 291 MessagePort* receiver = new ExtensionMessagePort( |
| 297 GetExtensionProcess(profile, target_extension_id), MSG_ROUTING_CONTROL, | 292 GetExtensionProcess(profile, target_extension_id), MSG_ROUTING_CONTROL, |
| 298 target_extension_id); | 293 target_extension_id); |
| 299 | 294 |
| 300 // Include info about the opener's tab (if it was a tab). | 295 // Include info about the opener's tab (if it was a tab). |
| 301 scoped_ptr<base::DictionaryValue> source_tab; | 296 scoped_ptr<base::DictionaryValue> source_tab; |
| 302 GURL source_url_for_tab; | 297 GURL source_url_for_tab; |
| 303 | 298 |
| 304 if (source_contents && ExtensionTabUtil::GetTabId(source_contents) >= 0) { | 299 if (source_contents && ExtensionTabUtil::GetTabId(source_contents) >= 0) { |
| 305 // Platform apps can be sent messages, but don't have a Tab concept. | 300 // Platform apps can be sent messages, but don't have a Tab concept. |
| 306 if (!target_extension->is_platform_app()) | 301 // Only the tab id is useful to platform apps for internal use. The |
| 307 source_tab.reset(ExtensionTabUtil::CreateTabValue(source_contents)); | 302 // unnecessary bits will be stripped out in |
| 308 source_url_for_tab = source_url; | 303 // MessagingBindings::DispatchOnConnect(). |
| 304 source_tab.reset(ExtensionTabUtil::CreateTabValue(source_contents)); | |
| 309 } | 305 } |
| 306 source_url_for_tab = source_url; | |
|
not at google - send to devlin
2014/01/24 21:37:27
I don't think this source_url_for_tab is necessary
Lei Zhang
2014/01/25 01:47:09
Under |kUrlKey| right? I removed |source_url_for_t
not at google - send to devlin
2014/01/27 17:51:46
Actually not quite what I meant but what you did i
Lei Zhang
2014/01/27 23:19:13
Yes, I noticed the rabbit hole is quite deep and I
| |
| 310 | 307 |
| 311 OpenChannelParams* params = new OpenChannelParams(source, | 308 OpenChannelParams* params = new OpenChannelParams(source, |
| 312 source_tab.Pass(), | 309 source_tab.Pass(), |
| 313 receiver, | 310 receiver, |
| 314 receiver_port_id, | 311 receiver_port_id, |
| 315 source_extension_id, | 312 source_extension_id, |
| 316 target_extension_id, | 313 target_extension_id, |
| 317 source_url_for_tab, | 314 source_url_for_tab, |
| 318 channel_name, | 315 channel_name, |
| 319 include_tls_channel_id); | 316 include_tls_channel_id); |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 542 } | 539 } |
| 543 | 540 |
| 544 // Balance the IncrementLazyKeepaliveCount() in OpenChannelImpl. | 541 // Balance the IncrementLazyKeepaliveCount() in OpenChannelImpl. |
| 545 channel->opener->DecrementLazyKeepaliveCount(); | 542 channel->opener->DecrementLazyKeepaliveCount(); |
| 546 channel->receiver->DecrementLazyKeepaliveCount(); | 543 channel->receiver->DecrementLazyKeepaliveCount(); |
| 547 | 544 |
| 548 delete channel_iter->second; | 545 delete channel_iter->second; |
| 549 channels_.erase(channel_iter); | 546 channels_.erase(channel_iter); |
| 550 } | 547 } |
| 551 | 548 |
| 552 void MessageService::PostMessage( | 549 void MessageService::PostMessage(int source_port_id, const Message& message) { |
| 553 int source_port_id, const Message& message) { | |
| 554 int channel_id = GET_CHANNEL_ID(source_port_id); | 550 int channel_id = GET_CHANNEL_ID(source_port_id); |
| 555 MessageChannelMap::iterator iter = channels_.find(channel_id); | 551 MessageChannelMap::iterator iter = channels_.find(channel_id); |
| 556 if (iter == channels_.end()) { | 552 if (iter == channels_.end()) { |
| 557 // If this channel is pending, queue up the PostMessage to run once | 553 // If this channel is pending, queue up the PostMessage to run once |
| 558 // the channel opens. | 554 // the channel opens. |
| 559 EnqueuePendingMessage(source_port_id, channel_id, message); | 555 EnqueuePendingMessage(source_port_id, channel_id, message); |
| 560 return; | 556 return; |
| 561 } | 557 } |
| 562 | 558 |
| 563 DispatchMessage(source_port_id, iter->second, message); | 559 DispatchMessage(source_port_id, iter->second, message); |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 754 } | 750 } |
| 755 | 751 |
| 756 void MessageService::DispatchOnDisconnect(content::RenderProcessHost* source, | 752 void MessageService::DispatchOnDisconnect(content::RenderProcessHost* source, |
| 757 int port_id, | 753 int port_id, |
| 758 const std::string& error_message) { | 754 const std::string& error_message) { |
| 759 ExtensionMessagePort port(source, MSG_ROUTING_CONTROL, ""); | 755 ExtensionMessagePort port(source, MSG_ROUTING_CONTROL, ""); |
| 760 port.DispatchOnDisconnect(GET_OPPOSITE_PORT_ID(port_id), error_message); | 756 port.DispatchOnDisconnect(GET_OPPOSITE_PORT_ID(port_id), error_message); |
| 761 } | 757 } |
| 762 | 758 |
| 763 } // namespace extensions | 759 } // namespace extensions |
| OLD | NEW |