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" |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "chrome/browser/chrome_notification_types.h" | 16 #include "chrome/browser/chrome_notification_types.h" |
17 #include "chrome/browser/extensions/api/messaging/extension_message_port.h" | 17 #include "chrome/browser/extensions/api/messaging/extension_message_port.h" |
18 #include "chrome/browser/extensions/api/messaging/incognito_connectability.h" | 18 #include "chrome/browser/extensions/api/messaging/incognito_connectability.h" |
19 #include "chrome/browser/extensions/api/messaging/native_message_port.h" | 19 #include "chrome/browser/extensions/api/messaging/native_message_port.h" |
20 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 20 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
21 #include "chrome/browser/extensions/extension_service.h" | 21 #include "chrome/browser/extensions/extension_service.h" |
22 #include "chrome/browser/extensions/extension_tab_util.h" | 22 #include "chrome/browser/extensions/extension_tab_util.h" |
23 #include "chrome/browser/extensions/extension_util.h" | 23 #include "chrome/browser/extensions/extension_util.h" |
24 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
25 #include "chrome/browser/tab_contents/tab_util.h" | 25 #include "chrome/browser/tab_contents/tab_util.h" |
26 #include "content/public/browser/notification_service.h" | 26 #include "content/public/browser/notification_service.h" |
27 #include "content/public/browser/render_frame_host.h" | |
27 #include "content/public/browser/render_process_host.h" | 28 #include "content/public/browser/render_process_host.h" |
28 #include "content/public/browser/render_view_host.h" | 29 #include "content/public/browser/render_view_host.h" |
29 #include "content/public/browser/render_widget_host.h" | 30 #include "content/public/browser/render_widget_host.h" |
30 #include "content/public/browser/render_widget_host_view.h" | 31 #include "content/public/browser/render_widget_host_view.h" |
31 #include "content/public/browser/site_instance.h" | 32 #include "content/public/browser/site_instance.h" |
32 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
33 #include "extensions/browser/extension_host.h" | 34 #include "extensions/browser/extension_host.h" |
34 #include "extensions/browser/extension_system.h" | 35 #include "extensions/browser/extension_system.h" |
35 #include "extensions/browser/extensions_browser_client.h" | 36 #include "extensions/browser/extensions_browser_client.h" |
36 #include "extensions/browser/lazy_background_task_queue.h" | 37 #include "extensions/browser/lazy_background_task_queue.h" |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
111 "administrator."; | 112 "administrator."; |
112 #endif | 113 #endif |
113 | 114 |
114 struct MessageService::MessageChannel { | 115 struct MessageService::MessageChannel { |
115 scoped_ptr<MessagePort> opener; | 116 scoped_ptr<MessagePort> opener; |
116 scoped_ptr<MessagePort> receiver; | 117 scoped_ptr<MessagePort> receiver; |
117 }; | 118 }; |
118 | 119 |
119 struct MessageService::OpenChannelParams { | 120 struct MessageService::OpenChannelParams { |
120 content::RenderProcessHost* source; | 121 content::RenderProcessHost* source; |
121 base::DictionaryValue source_tab; | 122 scoped_ptr<base::DictionaryValue> source_tab; |
123 int source_frame_id; | |
122 scoped_ptr<MessagePort> receiver; | 124 scoped_ptr<MessagePort> receiver; |
123 int receiver_port_id; | 125 int receiver_port_id; |
124 std::string source_extension_id; | 126 std::string source_extension_id; |
125 std::string target_extension_id; | 127 std::string target_extension_id; |
126 GURL source_url; | 128 GURL source_url; |
127 std::string channel_name; | 129 std::string channel_name; |
128 bool include_tls_channel_id; | 130 bool include_tls_channel_id; |
129 std::string tls_channel_id; | 131 std::string tls_channel_id; |
130 | 132 |
131 // Takes ownership of receiver. | 133 // Takes ownership of receiver. |
132 OpenChannelParams(content::RenderProcessHost* source, | 134 OpenChannelParams(content::RenderProcessHost* source, |
133 scoped_ptr<base::DictionaryValue> source_tab, | 135 scoped_ptr<base::DictionaryValue> source_tab, |
136 int source_frame_id, | |
134 MessagePort* receiver, | 137 MessagePort* receiver, |
135 int receiver_port_id, | 138 int receiver_port_id, |
136 const std::string& source_extension_id, | 139 const std::string& source_extension_id, |
137 const std::string& target_extension_id, | 140 const std::string& target_extension_id, |
138 const GURL& source_url, | 141 const GURL& source_url, |
139 const std::string& channel_name, | 142 const std::string& channel_name, |
140 bool include_tls_channel_id) | 143 bool include_tls_channel_id) |
141 : source(source), | 144 : source(source), |
145 source_frame_id(source_frame_id), | |
142 receiver(receiver), | 146 receiver(receiver), |
143 receiver_port_id(receiver_port_id), | 147 receiver_port_id(receiver_port_id), |
144 source_extension_id(source_extension_id), | 148 source_extension_id(source_extension_id), |
145 target_extension_id(target_extension_id), | 149 target_extension_id(target_extension_id), |
146 source_url(source_url), | 150 source_url(source_url), |
147 channel_name(channel_name), | 151 channel_name(channel_name), |
148 include_tls_channel_id(include_tls_channel_id) { | 152 include_tls_channel_id(include_tls_channel_id) { |
149 if (source_tab) | 153 if (source_tab) |
150 this->source_tab.Swap(source_tab.get()); | 154 this->source_tab.reset(source_tab.Pass()); |
not at google - send to devlin
2014/11/12 01:02:46
you can just use = (not .reset())
robwu
2014/11/12 10:00:21
Done.
| |
151 } | 155 } |
152 | 156 |
153 private: | 157 private: |
154 DISALLOW_COPY_AND_ASSIGN(OpenChannelParams); | 158 DISALLOW_COPY_AND_ASSIGN(OpenChannelParams); |
155 }; | 159 }; |
156 | 160 |
157 namespace { | 161 namespace { |
158 | 162 |
159 static base::StaticAtomicSequenceNumber g_next_channel_id; | 163 static base::StaticAtomicSequenceNumber g_next_channel_id; |
160 static base::StaticAtomicSequenceNumber g_channel_id_overflow_count; | 164 static base::StaticAtomicSequenceNumber g_channel_id_overflow_count; |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
325 // Note: we use the source's profile here. If the source is an incognito | 329 // Note: we use the source's profile here. If the source is an incognito |
326 // process, we will use the incognito EPM to find the right extension process, | 330 // process, we will use the incognito EPM to find the right extension process, |
327 // which depends on whether the extension uses spanning or split mode. | 331 // which depends on whether the extension uses spanning or split mode. |
328 MessagePort* receiver = new ExtensionMessagePort( | 332 MessagePort* receiver = new ExtensionMessagePort( |
329 GetExtensionProcess(context, target_extension_id), | 333 GetExtensionProcess(context, target_extension_id), |
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; |
339 int source_frame_id = -1; | |
335 if (source_contents && ExtensionTabUtil::GetTabId(source_contents) >= 0) { | 340 if (source_contents && ExtensionTabUtil::GetTabId(source_contents) >= 0) { |
336 // Only the tab id is useful to platform apps for internal use. The | 341 // Only the tab id is useful to platform apps for internal use. The |
337 // unnecessary bits will be stripped out in | 342 // unnecessary bits will be stripped out in |
338 // MessagingBindings::DispatchOnConnect(). | 343 // MessagingBindings::DispatchOnConnect(). |
339 source_tab.reset(ExtensionTabUtil::CreateTabValue(source_contents)); | 344 source_tab.reset(ExtensionTabUtil::CreateTabValue(source_contents)); |
345 content::RenderFrameHost* rfh = | |
346 content::RenderFrameHost::FromID(source_process_id, source_routing_id); | |
347 // Main frame's frameId is 0. | |
348 source_frame_id = (rfh && !rfh->GetParent()) ? 0 : source_routing_id; | |
340 } | 349 } |
341 | 350 |
342 OpenChannelParams* params = new OpenChannelParams( | 351 OpenChannelParams* params = new OpenChannelParams( |
343 source, source_tab.Pass(), receiver, receiver_port_id, | 352 source, source_tab.Pass(), source_frame_id, receiver, receiver_port_id, |
344 source_extension_id, target_extension_id, source_url, channel_name, | 353 source_extension_id, target_extension_id, source_url, channel_name, |
345 include_tls_channel_id); | 354 include_tls_channel_id); |
346 | 355 |
347 // If the target requests the TLS channel id, begin the lookup for it. | 356 // 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 | 357 // The target might also be a lazy background page, checked next, but the |
349 // loading of lazy background pages continues asynchronously, so enqueue | 358 // loading of lazy background pages continues asynchronously, so enqueue |
350 // messages awaiting TLS channel ID first. | 359 // messages awaiting TLS channel ID first. |
351 if (include_tls_channel_id) { | 360 if (include_tls_channel_id) { |
352 pending_tls_channel_id_channels_[GET_CHANNEL_ID(params->receiver_port_id)] | 361 pending_tls_channel_id_channels_[GET_CHANNEL_ID(params->receiver_port_id)] |
353 = PendingMessagesQueue(); | 362 = PendingMessagesQueue(); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
473 // The tab isn't loaded yet. Don't attempt to connect. | 482 // The tab isn't loaded yet. Don't attempt to connect. |
474 DispatchOnDisconnect( | 483 DispatchOnDisconnect( |
475 source, receiver_port_id, kReceivingEndDoesntExistError); | 484 source, receiver_port_id, kReceivingEndDoesntExistError); |
476 return; | 485 return; |
477 } | 486 } |
478 | 487 |
479 scoped_ptr<OpenChannelParams> params(new OpenChannelParams( | 488 scoped_ptr<OpenChannelParams> params(new OpenChannelParams( |
480 source, | 489 source, |
481 scoped_ptr<base::DictionaryValue>(), // Source tab doesn't make sense | 490 scoped_ptr<base::DictionaryValue>(), // Source tab doesn't make sense |
482 // for opening to tabs. | 491 // for opening to tabs. |
492 -1, // If there is no tab, then there is no frame either. | |
483 receiver.release(), | 493 receiver.release(), |
484 receiver_port_id, | 494 receiver_port_id, |
485 extension_id, | 495 extension_id, |
486 extension_id, | 496 extension_id, |
487 GURL(), // Source URL doesn't make sense for opening to tabs. | 497 GURL(), // Source URL doesn't make sense for opening to tabs. |
488 channel_name, | 498 channel_name, |
489 false)); // Connections to tabs don't get TLS channel IDs. | 499 false)); // Connections to tabs don't get TLS channel IDs. |
490 OpenChannelImpl(params.Pass()); | 500 OpenChannelImpl(params.Pass()); |
491 } | 501 } |
492 | 502 |
(...skipping 21 matching lines...) Expand all Loading... | |
514 CHECK(channel->receiver->GetRenderProcessHost()); | 524 CHECK(channel->receiver->GetRenderProcessHost()); |
515 | 525 |
516 AddChannel(channel, params->receiver_port_id); | 526 AddChannel(channel, params->receiver_port_id); |
517 | 527 |
518 CHECK(channel->receiver->GetRenderProcessHost()); | 528 CHECK(channel->receiver->GetRenderProcessHost()); |
519 | 529 |
520 // Send the connect event to the receiver. Give it the opener's port ID (the | 530 // Send the connect event to the receiver. Give it the opener's port ID (the |
521 // opener has the opposite port ID). | 531 // opener has the opposite port ID). |
522 channel->receiver->DispatchOnConnect(params->receiver_port_id, | 532 channel->receiver->DispatchOnConnect(params->receiver_port_id, |
523 params->channel_name, | 533 params->channel_name, |
524 params->source_tab, | 534 params->source_tab.Pass(), |
535 params->source_frame_id, | |
525 params->source_extension_id, | 536 params->source_extension_id, |
526 params->target_extension_id, | 537 params->target_extension_id, |
527 params->source_url, | 538 params->source_url, |
528 params->tls_channel_id); | 539 params->tls_channel_id); |
529 | 540 |
530 // Keep both ends of the channel alive until the channel is closed. | 541 // Keep both ends of the channel alive until the channel is closed. |
531 channel->opener->IncrementLazyKeepaliveCount(); | 542 channel->opener->IncrementLazyKeepaliveCount(); |
532 channel->receiver->IncrementLazyKeepaliveCount(); | 543 channel->receiver->IncrementLazyKeepaliveCount(); |
533 return true; | 544 return true; |
534 } | 545 } |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
783 } | 794 } |
784 | 795 |
785 void MessageService::DispatchOnDisconnect(content::RenderProcessHost* source, | 796 void MessageService::DispatchOnDisconnect(content::RenderProcessHost* source, |
786 int port_id, | 797 int port_id, |
787 const std::string& error_message) { | 798 const std::string& error_message) { |
788 ExtensionMessagePort port(source, MSG_ROUTING_CONTROL, ""); | 799 ExtensionMessagePort port(source, MSG_ROUTING_CONTROL, ""); |
789 port.DispatchOnDisconnect(GET_OPPOSITE_PORT_ID(port_id), error_message); | 800 port.DispatchOnDisconnect(GET_OPPOSITE_PORT_ID(port_id), error_message); |
790 } | 801 } |
791 | 802 |
792 } // namespace extensions | 803 } // namespace extensions |
OLD | NEW |