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 #endif | 112 #endif |
113 | 113 |
114 struct MessageService::MessageChannel { | 114 struct MessageService::MessageChannel { |
115 scoped_ptr<MessagePort> opener; | 115 scoped_ptr<MessagePort> opener; |
116 scoped_ptr<MessagePort> receiver; | 116 scoped_ptr<MessagePort> receiver; |
117 }; | 117 }; |
118 | 118 |
119 struct MessageService::OpenChannelParams { | 119 struct MessageService::OpenChannelParams { |
120 content::RenderProcessHost* source; | 120 content::RenderProcessHost* source; |
121 base::DictionaryValue source_tab; | 121 base::DictionaryValue source_tab; |
122 int source_frame_id; | |
122 scoped_ptr<MessagePort> receiver; | 123 scoped_ptr<MessagePort> receiver; |
123 int receiver_port_id; | 124 int receiver_port_id; |
124 std::string source_extension_id; | 125 std::string source_extension_id; |
125 std::string target_extension_id; | 126 std::string target_extension_id; |
126 GURL source_url; | 127 GURL source_url; |
127 std::string channel_name; | 128 std::string channel_name; |
128 bool include_tls_channel_id; | 129 bool include_tls_channel_id; |
129 std::string tls_channel_id; | 130 std::string tls_channel_id; |
130 | 131 |
131 // Takes ownership of receiver. | 132 // Takes ownership of receiver. |
132 OpenChannelParams(content::RenderProcessHost* source, | 133 OpenChannelParams(content::RenderProcessHost* source, |
133 scoped_ptr<base::DictionaryValue> source_tab, | 134 scoped_ptr<base::DictionaryValue> source_tab, |
135 int source_frame_id, | |
134 MessagePort* receiver, | 136 MessagePort* receiver, |
135 int receiver_port_id, | 137 int receiver_port_id, |
136 const std::string& source_extension_id, | 138 const std::string& source_extension_id, |
137 const std::string& target_extension_id, | 139 const std::string& target_extension_id, |
138 const GURL& source_url, | 140 const GURL& source_url, |
139 const std::string& channel_name, | 141 const std::string& channel_name, |
140 bool include_tls_channel_id) | 142 bool include_tls_channel_id) |
141 : source(source), | 143 : source(source), |
144 source_frame_id(source_frame_id), | |
142 receiver(receiver), | 145 receiver(receiver), |
143 receiver_port_id(receiver_port_id), | 146 receiver_port_id(receiver_port_id), |
144 source_extension_id(source_extension_id), | 147 source_extension_id(source_extension_id), |
145 target_extension_id(target_extension_id), | 148 target_extension_id(target_extension_id), |
146 source_url(source_url), | 149 source_url(source_url), |
147 channel_name(channel_name), | 150 channel_name(channel_name), |
148 include_tls_channel_id(include_tls_channel_id) { | 151 include_tls_channel_id(include_tls_channel_id) { |
149 if (source_tab) | 152 if (source_tab) |
150 this->source_tab.Swap(source_tab.get()); | 153 this->source_tab.Swap(source_tab.get()); |
151 } | 154 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
226 return g_factory.Pointer(); | 229 return g_factory.Pointer(); |
227 } | 230 } |
228 | 231 |
229 // static | 232 // static |
230 MessageService* MessageService::Get(BrowserContext* context) { | 233 MessageService* MessageService::Get(BrowserContext* context) { |
231 return BrowserContextKeyedAPIFactory<MessageService>::Get(context); | 234 return BrowserContextKeyedAPIFactory<MessageService>::Get(context); |
232 } | 235 } |
233 | 236 |
234 void MessageService::OpenChannelToExtension( | 237 void MessageService::OpenChannelToExtension( |
235 int source_process_id, int source_routing_id, int receiver_port_id, | 238 int source_process_id, int source_routing_id, int receiver_port_id, |
239 int source_frame_id, | |
236 const std::string& source_extension_id, | 240 const std::string& source_extension_id, |
237 const std::string& target_extension_id, | 241 const std::string& target_extension_id, |
238 const GURL& source_url, | 242 const GURL& source_url, |
239 const std::string& channel_name, | 243 const std::string& channel_name, |
240 bool include_tls_channel_id) { | 244 bool include_tls_channel_id) { |
241 content::RenderProcessHost* source = | 245 content::RenderProcessHost* source = |
242 content::RenderProcessHost::FromID(source_process_id); | 246 content::RenderProcessHost::FromID(source_process_id); |
243 if (!source) | 247 if (!source) |
244 return; | 248 return; |
245 BrowserContext* context = source->GetBrowserContext(); | 249 BrowserContext* context = source->GetBrowserContext(); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
330 MSG_ROUTING_CONTROL, | 334 MSG_ROUTING_CONTROL, |
331 target_extension_id); | 335 target_extension_id); |
332 | 336 |
333 // Include info about the opener's tab (if it was a tab). | 337 // Include info about the opener's tab (if it was a tab). |
334 scoped_ptr<base::DictionaryValue> source_tab; | 338 scoped_ptr<base::DictionaryValue> source_tab; |
335 if (source_contents && ExtensionTabUtil::GetTabId(source_contents) >= 0) { | 339 if (source_contents && ExtensionTabUtil::GetTabId(source_contents) >= 0) { |
336 // Only the tab id is useful to platform apps for internal use. The | 340 // Only the tab id is useful to platform apps for internal use. The |
337 // unnecessary bits will be stripped out in | 341 // unnecessary bits will be stripped out in |
338 // MessagingBindings::DispatchOnConnect(). | 342 // MessagingBindings::DispatchOnConnect(). |
339 source_tab.reset(ExtensionTabUtil::CreateTabValue(source_contents)); | 343 source_tab.reset(ExtensionTabUtil::CreateTabValue(source_contents)); |
344 DCHECK_GE(source_frame_id, 0); | |
345 } else { | |
346 source_frame_id = -1; | |
not at google - send to devlin
2014/11/10 18:36:42
I don't like rewriting the value of parameters, it
robwu
2014/11/10 21:43:07
I wanted to make it clear that the the ID is non-n
| |
340 } | 347 } |
341 | 348 |
342 OpenChannelParams* params = new OpenChannelParams( | 349 OpenChannelParams* params = new OpenChannelParams( |
343 source, source_tab.Pass(), receiver, receiver_port_id, | 350 source, source_tab.Pass(), source_frame_id, receiver, receiver_port_id, |
344 source_extension_id, target_extension_id, source_url, channel_name, | 351 source_extension_id, target_extension_id, source_url, channel_name, |
345 include_tls_channel_id); | 352 include_tls_channel_id); |
346 | 353 |
347 // If the target requests the TLS channel id, begin the lookup for it. | 354 // If the target requests the TLS channel id, begin the lookup for it. |
348 // The target might also be a lazy background page, checked next, but the | 355 // The target might also be a lazy background page, checked next, but the |
349 // loading of lazy background pages continues asynchronously, so enqueue | 356 // loading of lazy background pages continues asynchronously, so enqueue |
350 // messages awaiting TLS channel ID first. | 357 // messages awaiting TLS channel ID first. |
351 if (include_tls_channel_id) { | 358 if (include_tls_channel_id) { |
352 pending_tls_channel_id_channels_[GET_CHANNEL_ID(params->receiver_port_id)] | 359 pending_tls_channel_id_channels_[GET_CHANNEL_ID(params->receiver_port_id)] |
353 = PendingMessagesQueue(); | 360 = PendingMessagesQueue(); |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
469 extension_id)); | 476 extension_id)); |
470 } | 477 } |
471 | 478 |
472 if (contents && contents->GetController().NeedsReload()) { | 479 if (contents && contents->GetController().NeedsReload()) { |
473 // The tab isn't loaded yet. Don't attempt to connect. | 480 // The tab isn't loaded yet. Don't attempt to connect. |
474 DispatchOnDisconnect( | 481 DispatchOnDisconnect( |
475 source, receiver_port_id, kReceivingEndDoesntExistError); | 482 source, receiver_port_id, kReceivingEndDoesntExistError); |
476 return; | 483 return; |
477 } | 484 } |
478 | 485 |
486 // TODO(robwu): What if the channel to a tab was created from an extension | |
487 // page in a tab? Then source_tab / source_frame_id / source_url could be | |
488 // meaningful... | |
not at google - send to devlin
2014/11/10 18:36:42
I suppose there wouldn't be much harm in setting a
robwu
2014/11/10 21:43:07
It is not just those two properties, but also the
| |
479 scoped_ptr<OpenChannelParams> params(new OpenChannelParams( | 489 scoped_ptr<OpenChannelParams> params(new OpenChannelParams( |
480 source, | 490 source, |
481 scoped_ptr<base::DictionaryValue>(), // Source tab doesn't make sense | 491 scoped_ptr<base::DictionaryValue>(), // Source tab doesn't make sense |
482 // for opening to tabs. | 492 // for opening to tabs. |
493 -1, // If there is no tab, then there is no frame either. | |
483 receiver.release(), | 494 receiver.release(), |
484 receiver_port_id, | 495 receiver_port_id, |
485 extension_id, | 496 extension_id, |
486 extension_id, | 497 extension_id, |
487 GURL(), // Source URL doesn't make sense for opening to tabs. | 498 GURL(), // Source URL doesn't make sense for opening to tabs. |
488 channel_name, | 499 channel_name, |
489 false)); // Connections to tabs don't get TLS channel IDs. | 500 false)); // Connections to tabs don't get TLS channel IDs. |
490 OpenChannelImpl(params.Pass()); | 501 OpenChannelImpl(params.Pass()); |
491 } | 502 } |
492 | 503 |
(...skipping 22 matching lines...) Expand all Loading... | |
515 | 526 |
516 AddChannel(channel, params->receiver_port_id); | 527 AddChannel(channel, params->receiver_port_id); |
517 | 528 |
518 CHECK(channel->receiver->GetRenderProcessHost()); | 529 CHECK(channel->receiver->GetRenderProcessHost()); |
519 | 530 |
520 // Send the connect event to the receiver. Give it the opener's port ID (the | 531 // Send the connect event to the receiver. Give it the opener's port ID (the |
521 // opener has the opposite port ID). | 532 // opener has the opposite port ID). |
522 channel->receiver->DispatchOnConnect(params->receiver_port_id, | 533 channel->receiver->DispatchOnConnect(params->receiver_port_id, |
523 params->channel_name, | 534 params->channel_name, |
524 params->source_tab, | 535 params->source_tab, |
536 params->source_frame_id, | |
525 params->source_extension_id, | 537 params->source_extension_id, |
526 params->target_extension_id, | 538 params->target_extension_id, |
527 params->source_url, | 539 params->source_url, |
528 params->tls_channel_id); | 540 params->tls_channel_id); |
529 | 541 |
530 // Keep both ends of the channel alive until the channel is closed. | 542 // Keep both ends of the channel alive until the channel is closed. |
531 channel->opener->IncrementLazyKeepaliveCount(); | 543 channel->opener->IncrementLazyKeepaliveCount(); |
532 channel->receiver->IncrementLazyKeepaliveCount(); | 544 channel->receiver->IncrementLazyKeepaliveCount(); |
533 return true; | 545 return true; |
534 } | 546 } |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
783 } | 795 } |
784 | 796 |
785 void MessageService::DispatchOnDisconnect(content::RenderProcessHost* source, | 797 void MessageService::DispatchOnDisconnect(content::RenderProcessHost* source, |
786 int port_id, | 798 int port_id, |
787 const std::string& error_message) { | 799 const std::string& error_message) { |
788 ExtensionMessagePort port(source, MSG_ROUTING_CONTROL, ""); | 800 ExtensionMessagePort port(source, MSG_ROUTING_CONTROL, ""); |
789 port.DispatchOnDisconnect(GET_OPPOSITE_PORT_ID(port_id), error_message); | 801 port.DispatchOnDisconnect(GET_OPPOSITE_PORT_ID(port_id), error_message); |
790 } | 802 } |
791 | 803 |
792 } // namespace extensions | 804 } // namespace extensions |
OLD | NEW |