Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: chrome/browser/extensions/api/messaging/message_service.cc

Issue 709933002: Add frameId to MessageSender (extension messaging API) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Generate frameId in browser Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 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.Swap(source_tab.get());
151 } 155 }
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 int source_frame_id = source_routing_id;
not at google - send to devlin 2014/11/11 00:03:04 It looks like this declaration will shadow the dec
robwu 2014/11/11 00:34:26 Done.
346 RenderFrameHost* rfh = RenderFrameHost::FromID(source_frame_id,
347 source_routing_id);
348 // Main frame's frameId is 0.
349 source_frame_id = rfh && !rfh.GetParent() ? 0 : source_routing_id;
not at google - send to devlin 2014/11/11 00:03:04 I would find this a bit easier to read with parens
robwu 2014/11/11 00:34:26 Done.
340 } 350 }
341 351
342 OpenChannelParams* params = new OpenChannelParams( 352 OpenChannelParams* params = new OpenChannelParams(
343 source, source_tab.Pass(), receiver, receiver_port_id, 353 source, source_tab.Pass(), source_frame_id, receiver, receiver_port_id,
344 source_extension_id, target_extension_id, source_url, channel_name, 354 source_extension_id, target_extension_id, source_url, channel_name,
345 include_tls_channel_id); 355 include_tls_channel_id);
346 356
347 // If the target requests the TLS channel id, begin the lookup for it. 357 // 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 358 // The target might also be a lazy background page, checked next, but the
349 // loading of lazy background pages continues asynchronously, so enqueue 359 // loading of lazy background pages continues asynchronously, so enqueue
350 // messages awaiting TLS channel ID first. 360 // messages awaiting TLS channel ID first.
351 if (include_tls_channel_id) { 361 if (include_tls_channel_id) {
352 pending_tls_channel_id_channels_[GET_CHANNEL_ID(params->receiver_port_id)] 362 pending_tls_channel_id_channels_[GET_CHANNEL_ID(params->receiver_port_id)]
353 = PendingMessagesQueue(); 363 = PendingMessagesQueue();
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 // The tab isn't loaded yet. Don't attempt to connect. 483 // The tab isn't loaded yet. Don't attempt to connect.
474 DispatchOnDisconnect( 484 DispatchOnDisconnect(
475 source, receiver_port_id, kReceivingEndDoesntExistError); 485 source, receiver_port_id, kReceivingEndDoesntExistError);
476 return; 486 return;
477 } 487 }
478 488
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
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
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698