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

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

Issue 1413543005: Use FrameTreeNode ID as frameId in extension APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Improve port lifetime management, add tests Created 5 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 "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/extensions/api/messaging/extension_message_port.h" 15 #include "chrome/browser/extensions/api/messaging/extension_message_port.h"
17 #include "chrome/browser/extensions/api/messaging/incognito_connectability.h" 16 #include "chrome/browser/extensions/api/messaging/incognito_connectability.h"
18 #include "chrome/browser/extensions/api/messaging/native_message_port.h" 17 #include "chrome/browser/extensions/api/messaging/native_message_port.h"
19 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" 18 #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
20 #include "chrome/browser/extensions/extension_service.h" 19 #include "chrome/browser/extensions/extension_service.h"
21 #include "chrome/browser/extensions/extension_tab_util.h" 20 #include "chrome/browser/extensions/extension_tab_util.h"
22 #include "chrome/browser/extensions/extension_util.h" 21 #include "chrome/browser/extensions/extension_util.h"
23 #include "chrome/browser/profiles/profile.h" 22 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/tab_contents/tab_util.h" 23 #include "chrome/browser/tab_contents/tab_util.h"
25 #include "components/guest_view/common/guest_view_constants.h" 24 #include "components/guest_view/common/guest_view_constants.h"
26 #include "content/public/browser/browser_thread.h" 25 #include "content/public/browser/browser_thread.h"
27 #include "content/public/browser/notification_service.h"
28 #include "content/public/browser/render_frame_host.h" 26 #include "content/public/browser/render_frame_host.h"
29 #include "content/public/browser/render_process_host.h" 27 #include "content/public/browser/render_process_host.h"
30 #include "content/public/browser/render_view_host.h" 28 #include "content/public/browser/render_view_host.h"
31 #include "content/public/browser/render_widget_host.h" 29 #include "content/public/browser/render_widget_host.h"
32 #include "content/public/browser/render_widget_host_view.h" 30 #include "content/public/browser/render_widget_host_view.h"
33 #include "content/public/browser/site_instance.h" 31 #include "content/public/browser/site_instance.h"
34 #include "content/public/browser/web_contents.h" 32 #include "content/public/browser/web_contents.h"
35 #include "content/public/common/child_process_host.h" 33 #include "content/public/common/child_process_host.h"
36 #include "extensions/browser/event_router.h" 34 #include "extensions/browser/event_router.h"
37 #include "extensions/browser/extension_host.h" 35 #include "extensions/browser/extension_host.h"
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 "administrator."; 119 "administrator.";
122 #endif 120 #endif
123 121
124 struct MessageService::MessageChannel { 122 struct MessageService::MessageChannel {
125 scoped_ptr<MessagePort> opener; 123 scoped_ptr<MessagePort> opener;
126 scoped_ptr<MessagePort> receiver; 124 scoped_ptr<MessagePort> receiver;
127 }; 125 };
128 126
129 struct MessageService::OpenChannelParams { 127 struct MessageService::OpenChannelParams {
130 int source_process_id; 128 int source_process_id;
129 int source_routing_id;
131 scoped_ptr<base::DictionaryValue> source_tab; 130 scoped_ptr<base::DictionaryValue> source_tab;
132 int source_frame_id; 131 int source_frame_id;
133 int target_tab_id;
134 int target_frame_id;
135 scoped_ptr<MessagePort> receiver; 132 scoped_ptr<MessagePort> receiver;
136 int receiver_port_id; 133 int receiver_port_id;
137 std::string source_extension_id; 134 std::string source_extension_id;
138 std::string target_extension_id; 135 std::string target_extension_id;
139 GURL source_url; 136 GURL source_url;
140 std::string channel_name; 137 std::string channel_name;
141 bool include_tls_channel_id; 138 bool include_tls_channel_id;
142 std::string tls_channel_id; 139 std::string tls_channel_id;
143 bool include_guest_process_info; 140 bool include_guest_process_info;
144 141
145 // Takes ownership of receiver. 142 // Takes ownership of receiver.
146 OpenChannelParams(int source_process_id, 143 OpenChannelParams(int source_process_id,
144 int source_routing_id,
147 scoped_ptr<base::DictionaryValue> source_tab, 145 scoped_ptr<base::DictionaryValue> source_tab,
148 int source_frame_id, 146 int source_frame_id,
149 int target_tab_id,
150 int target_frame_id,
151 MessagePort* receiver, 147 MessagePort* receiver,
152 int receiver_port_id, 148 int receiver_port_id,
153 const std::string& source_extension_id, 149 const std::string& source_extension_id,
154 const std::string& target_extension_id, 150 const std::string& target_extension_id,
155 const GURL& source_url, 151 const GURL& source_url,
156 const std::string& channel_name, 152 const std::string& channel_name,
157 bool include_tls_channel_id, 153 bool include_tls_channel_id,
158 bool include_guest_process_info) 154 bool include_guest_process_info)
159 : source_process_id(source_process_id), 155 : source_process_id(source_process_id),
156 source_routing_id(source_routing_id),
160 source_frame_id(source_frame_id), 157 source_frame_id(source_frame_id),
161 target_tab_id(target_tab_id),
162 target_frame_id(target_frame_id),
163 receiver(receiver), 158 receiver(receiver),
164 receiver_port_id(receiver_port_id), 159 receiver_port_id(receiver_port_id),
165 source_extension_id(source_extension_id), 160 source_extension_id(source_extension_id),
166 target_extension_id(target_extension_id), 161 target_extension_id(target_extension_id),
167 source_url(source_url), 162 source_url(source_url),
168 channel_name(channel_name), 163 channel_name(channel_name),
169 include_tls_channel_id(include_tls_channel_id), 164 include_tls_channel_id(include_tls_channel_id),
170 include_guest_process_info(include_guest_process_info) { 165 include_guest_process_info(include_guest_process_info) {
171 if (source_tab) 166 if (source_tab)
172 this->source_tab = source_tab.Pass(); 167 this->source_tab = source_tab.Pass();
(...skipping 11 matching lines...) Expand all
184 BrowserContext* context, 179 BrowserContext* context,
185 const std::string& extension_id) { 180 const std::string& extension_id) {
186 scoped_refptr<SiteInstance> site_instance = 181 scoped_refptr<SiteInstance> site_instance =
187 ProcessManager::Get(context)->GetSiteInstanceForURL( 182 ProcessManager::Get(context)->GetSiteInstanceForURL(
188 Extension::GetBaseURLFromExtensionId(extension_id)); 183 Extension::GetBaseURLFromExtensionId(extension_id));
189 return site_instance->HasProcess() ? site_instance->GetProcess() : NULL; 184 return site_instance->HasProcess() ? site_instance->GetProcess() : NULL;
190 } 185 }
191 186
192 } // namespace 187 } // namespace
193 188
194 content::RenderProcessHost* 189 bool MessageService::MessagePort::IsValidPort() {
195 MessageService::MessagePort::GetRenderProcessHost() { 190 return true;
196 return NULL;
197 } 191 }
198 192
199 // static 193 // static
200 void MessageService::AllocatePortIdPair(int* port1, int* port2) { 194 void MessageService::AllocatePortIdPair(int* port1, int* port2) {
201 DCHECK_CURRENTLY_ON(BrowserThread::IO); 195 DCHECK_CURRENTLY_ON(BrowserThread::IO);
202 196
203 unsigned channel_id = 197 unsigned channel_id =
204 static_cast<unsigned>(g_next_channel_id.GetNext()) % (kint32max/2); 198 static_cast<unsigned>(g_next_channel_id.GetNext()) % (kint32max/2);
205 unsigned port1_id = channel_id * 2; 199 unsigned port1_id = channel_id * 2;
206 unsigned port2_id = channel_id * 2 + 1; 200 unsigned port2_id = channel_id * 2 + 1;
207 201
208 // Sanity checks to make sure our channel<->port converters are correct. 202 // Sanity checks to make sure our channel<->port converters are correct.
209 DCHECK(IS_OPENER_PORT_ID(port1_id)); 203 DCHECK(IS_OPENER_PORT_ID(port1_id));
210 DCHECK_EQ(GET_OPPOSITE_PORT_ID(port1_id), port2_id); 204 DCHECK_EQ(GET_OPPOSITE_PORT_ID(port1_id), port2_id);
211 DCHECK_EQ(GET_OPPOSITE_PORT_ID(port2_id), port1_id); 205 DCHECK_EQ(GET_OPPOSITE_PORT_ID(port2_id), port1_id);
212 DCHECK_EQ(GET_CHANNEL_ID(port1_id), GET_CHANNEL_ID(port2_id)); 206 DCHECK_EQ(GET_CHANNEL_ID(port1_id), GET_CHANNEL_ID(port2_id));
213 DCHECK_EQ(GET_CHANNEL_ID(port1_id), channel_id); 207 DCHECK_EQ(GET_CHANNEL_ID(port1_id), channel_id);
214 DCHECK_EQ(GET_CHANNEL_OPENER_ID(channel_id), port1_id); 208 DCHECK_EQ(GET_CHANNEL_OPENER_ID(channel_id), port1_id);
215 DCHECK_EQ(GET_CHANNEL_RECEIVERS_ID(channel_id), port2_id); 209 DCHECK_EQ(GET_CHANNEL_RECEIVERS_ID(channel_id), port2_id);
216 210
217 *port1 = port1_id; 211 *port1 = port1_id;
218 *port2 = port2_id; 212 *port2 = port2_id;
219 } 213 }
220 214
221 MessageService::MessageService(BrowserContext* context) 215 MessageService::MessageService(BrowserContext* context)
222 : lazy_background_task_queue_( 216 : lazy_background_task_queue_(
223 LazyBackgroundTaskQueue::Get(context)), 217 LazyBackgroundTaskQueue::Get(context)),
224 weak_factory_(this) { 218 weak_factory_(this) {
225 DCHECK_CURRENTLY_ON(BrowserThread::UI); 219 DCHECK_CURRENTLY_ON(BrowserThread::UI);
226
227 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
228 content::NotificationService::AllBrowserContextsAndSources());
229 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
230 content::NotificationService::AllBrowserContextsAndSources());
231 } 220 }
232 221
233 MessageService::~MessageService() { 222 MessageService::~MessageService() {
234 DCHECK_CURRENTLY_ON(BrowserThread::UI); 223 DCHECK_CURRENTLY_ON(BrowserThread::UI);
235 224
236 STLDeleteContainerPairSecondPointers(channels_.begin(), channels_.end()); 225 STLDeleteContainerPairSecondPointers(channels_.begin(), channels_.end());
237 channels_.clear(); 226 channels_.clear();
238 } 227 }
239 228
240 static base::LazyInstance<BrowserContextKeyedAPIFactory<MessageService> > 229 static base::LazyInstance<BrowserContextKeyedAPIFactory<MessageService> >
(...skipping 12 matching lines...) Expand all
253 242
254 void MessageService::OpenChannelToExtension( 243 void MessageService::OpenChannelToExtension(
255 int source_process_id, int source_routing_id, int receiver_port_id, 244 int source_process_id, int source_routing_id, int receiver_port_id,
256 const std::string& source_extension_id, 245 const std::string& source_extension_id,
257 const std::string& target_extension_id, 246 const std::string& target_extension_id,
258 const GURL& source_url, 247 const GURL& source_url,
259 const std::string& channel_name, 248 const std::string& channel_name,
260 bool include_tls_channel_id) { 249 bool include_tls_channel_id) {
261 DCHECK_CURRENTLY_ON(BrowserThread::UI); 250 DCHECK_CURRENTLY_ON(BrowserThread::UI);
262 251
263 content::RenderProcessHost* source = 252 content::RenderFrameHost* source =
264 content::RenderProcessHost::FromID(source_process_id); 253 content::RenderFrameHost::FromID(source_process_id, source_routing_id);
265 if (!source) 254 if (!source)
266 return; 255 return;
267 BrowserContext* context = source->GetBrowserContext(); 256 BrowserContext* context = source->GetProcess()->GetBrowserContext();
268 257
269 ExtensionRegistry* registry = ExtensionRegistry::Get(context); 258 ExtensionRegistry* registry = ExtensionRegistry::Get(context);
270 const Extension* target_extension = 259 const Extension* target_extension =
271 registry->enabled_extensions().GetByID(target_extension_id); 260 registry->enabled_extensions().GetByID(target_extension_id);
272 if (!target_extension) { 261 if (!target_extension) {
273 DispatchOnDisconnect( 262 DispatchOnDisconnect(
274 source, receiver_port_id, kReceivingEndDoesntExistError); 263 source, receiver_port_id, kReceivingEndDoesntExistError);
275 return; 264 return;
276 } 265 }
277 266
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 if (source_contents && ExtensionTabUtil::GetTabId(source_contents) >= 0) { 319 if (source_contents && ExtensionTabUtil::GetTabId(source_contents) >= 0) {
331 // Only the tab id is useful to platform apps for internal use. The 320 // Only the tab id is useful to platform apps for internal use. The
332 // unnecessary bits will be stripped out in 321 // unnecessary bits will be stripped out in
333 // MessagingBindings::DispatchOnConnect(). 322 // MessagingBindings::DispatchOnConnect().
334 source_tab.reset(ExtensionTabUtil::CreateTabValue(source_contents)); 323 source_tab.reset(ExtensionTabUtil::CreateTabValue(source_contents));
335 324
336 content::RenderFrameHost* rfh = 325 content::RenderFrameHost* rfh =
337 content::RenderFrameHost::FromID(source_process_id, source_routing_id); 326 content::RenderFrameHost::FromID(source_process_id, source_routing_id);
338 // Main frame's frameId is 0. 327 // Main frame's frameId is 0.
339 if (rfh) 328 if (rfh)
340 source_frame_id = !rfh->GetParent() ? 0 : source_routing_id; 329 source_frame_id = !rfh->GetParent() ? 0 : rfh->GetFrameTreeNodeID();
341 } else { 330 } else {
342 // Check to see if it was a WebView making the request. 331 // Check to see if it was a WebView making the request.
343 // Sending messages from WebViews to extensions breaks webview isolation, 332 // Sending messages from WebViews to extensions breaks webview isolation,
344 // so only allow component extensions to receive messages from WebViews. 333 // so only allow component extensions to receive messages from WebViews.
345 bool is_web_view = !!WebViewGuest::FromWebContents(source_contents); 334 bool is_web_view = !!WebViewGuest::FromWebContents(source_contents);
346 if (is_web_view && extensions::Manifest::IsComponentLocation( 335 if (is_web_view && extensions::Manifest::IsComponentLocation(
347 target_extension->location())) { 336 target_extension->location())) {
348 include_guest_process_info = true; 337 include_guest_process_info = true;
349 auto* rfh = content::RenderFrameHost::FromID(source_process_id,
350 source_routing_id);
351 // Include |source_frame_id| so that we can retrieve the guest's frame
352 // routing id in OpenChannelImpl.
353 if (rfh)
354 source_frame_id = source_routing_id;
355 } 338 }
356 } 339 }
357 340
358 scoped_ptr<OpenChannelParams> params(new OpenChannelParams( 341 scoped_ptr<OpenChannelParams> params(new OpenChannelParams(
359 source_process_id, source_tab.Pass(), source_frame_id, -1, 342 source_process_id, source_routing_id, source_tab.Pass(), source_frame_id,
360 -1, // no target_tab_id/target_frame_id for connections to extensions
361 nullptr, receiver_port_id, source_extension_id, target_extension_id, 343 nullptr, receiver_port_id, source_extension_id, target_extension_id,
362 source_url, channel_name, include_tls_channel_id, 344 source_url, channel_name, include_tls_channel_id,
363 include_guest_process_info)); 345 include_guest_process_info));
364 346
365 pending_incognito_channels_[GET_CHANNEL_ID(params->receiver_port_id)] = 347 pending_incognito_channels_[GET_CHANNEL_ID(params->receiver_port_id)] =
366 PendingMessagesQueue(); 348 PendingMessagesQueue();
367 if (context->IsOffTheRecord() && 349 if (context->IsOffTheRecord() &&
368 !util::IsIncognitoEnabled(target_extension_id, context)) { 350 !util::IsIncognitoEnabled(target_extension_id, context)) {
369 // Give the user a chance to accept an incognito connection from the web if 351 // Give the user a chance to accept an incognito connection from the web if
370 // they haven't already, with the conditions: 352 // they haven't already, with the conditions:
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 } 392 }
411 393
412 void MessageService::OpenChannelToNativeApp( 394 void MessageService::OpenChannelToNativeApp(
413 int source_process_id, 395 int source_process_id,
414 int source_routing_id, 396 int source_routing_id,
415 int receiver_port_id, 397 int receiver_port_id,
416 const std::string& source_extension_id, 398 const std::string& source_extension_id,
417 const std::string& native_app_name) { 399 const std::string& native_app_name) {
418 DCHECK_CURRENTLY_ON(BrowserThread::UI); 400 DCHECK_CURRENTLY_ON(BrowserThread::UI);
419 401
420 content::RenderProcessHost* source = 402 content::RenderFrameHost* source =
421 content::RenderProcessHost::FromID(source_process_id); 403 content::RenderFrameHost::FromID(source_process_id, source_routing_id);
422 if (!source) 404 if (!source)
423 return; 405 return;
424 406
425 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) 407 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)
426 Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext()); 408 Profile* profile =
409 Profile::FromBrowserContext(source->GetProcess()->GetBrowserContext());
427 ExtensionService* extension_service = 410 ExtensionService* extension_service =
428 ExtensionSystem::Get(profile)->extension_service(); 411 ExtensionSystem::Get(profile)->extension_service();
429 bool has_permission = false; 412 bool has_permission = false;
430 if (extension_service) { 413 if (extension_service) {
431 const Extension* extension = 414 const Extension* extension =
432 extension_service->GetExtensionById(source_extension_id, false); 415 extension_service->GetExtensionById(source_extension_id, false);
433 has_permission = extension && 416 has_permission = extension &&
434 extension->permissions_data()->HasAPIPermission( 417 extension->permissions_data()->HasAPIPermission(
435 APIPermission::kNativeMessaging); 418 APIPermission::kNativeMessaging);
436 } 419 }
437 420
438 if (!has_permission) { 421 if (!has_permission) {
439 DispatchOnDisconnect(source, receiver_port_id, kMissingPermissionError); 422 DispatchOnDisconnect(source, receiver_port_id, kMissingPermissionError);
440 return; 423 return;
441 } 424 }
442 425
443 PrefService* pref_service = profile->GetPrefs(); 426 PrefService* pref_service = profile->GetPrefs();
444 427
445 // Verify that the host is not blocked by policies. 428 // Verify that the host is not blocked by policies.
446 PolicyPermission policy_permission = 429 PolicyPermission policy_permission =
447 IsNativeMessagingHostAllowed(pref_service, native_app_name); 430 IsNativeMessagingHostAllowed(pref_service, native_app_name);
448 if (policy_permission == DISALLOW) { 431 if (policy_permission == DISALLOW) {
449 DispatchOnDisconnect(source, receiver_port_id, kProhibitedByPoliciesError); 432 DispatchOnDisconnect(source, receiver_port_id, kProhibitedByPoliciesError);
450 return; 433 return;
451 } 434 }
452 435
453 scoped_ptr<MessageChannel> channel(new MessageChannel()); 436 scoped_ptr<MessageChannel> channel(new MessageChannel());
454 channel->opener.reset(new ExtensionMessagePort(source, MSG_ROUTING_CONTROL, 437 channel->opener.reset(
455 source_extension_id)); 438 new ExtensionMessagePort(weak_factory_.GetWeakPtr(),
439 GET_OPPOSITE_PORT_ID(receiver_port_id),
440 source_extension_id, source, false));
456 441
457 // Get handle of the native view and pass it to the native messaging host. 442 // Get handle of the native view and pass it to the native messaging host.
458 content::RenderFrameHost* render_frame_host = 443 gfx::NativeView native_view = source ? source->GetNativeView() : nullptr;
459 content::RenderFrameHost::FromID(source_process_id, source_routing_id);
460 gfx::NativeView native_view =
461 render_frame_host ? render_frame_host->GetNativeView() : nullptr;
462 444
463 std::string error = kReceivingEndDoesntExistError; 445 std::string error = kReceivingEndDoesntExistError;
464 scoped_ptr<NativeMessageHost> native_host = NativeMessageHost::Create( 446 scoped_ptr<NativeMessageHost> native_host = NativeMessageHost::Create(
465 native_view, 447 native_view,
466 source_extension_id, 448 source_extension_id,
467 native_app_name, 449 native_app_name,
468 policy_permission == ALLOW_ALL, 450 policy_permission == ALLOW_ALL,
469 &error); 451 &error);
470 452
471 // Abandon the channel. 453 // Abandon the channel.
(...skipping 11 matching lines...) Expand all
483 AddChannel(channel.release(), receiver_port_id); 465 AddChannel(channel.release(), receiver_port_id);
484 #else // !(defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)) 466 #else // !(defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX))
485 const char kNativeMessagingNotSupportedError[] = 467 const char kNativeMessagingNotSupportedError[] =
486 "Native Messaging is not supported on this platform."; 468 "Native Messaging is not supported on this platform.";
487 DispatchOnDisconnect( 469 DispatchOnDisconnect(
488 source, receiver_port_id, kNativeMessagingNotSupportedError); 470 source, receiver_port_id, kNativeMessagingNotSupportedError);
489 #endif // !(defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)) 471 #endif // !(defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX))
490 } 472 }
491 473
492 void MessageService::OpenChannelToTab(int source_process_id, 474 void MessageService::OpenChannelToTab(int source_process_id,
475 int source_routing_id,
493 int receiver_port_id, 476 int receiver_port_id,
494 int tab_id, 477 int tab_id,
495 int frame_id, 478 int frame_id,
496 const std::string& extension_id, 479 const std::string& extension_id,
497 const std::string& channel_name) { 480 const std::string& channel_name) {
498 DCHECK_CURRENTLY_ON(BrowserThread::UI); 481 DCHECK_CURRENTLY_ON(BrowserThread::UI);
482 DCHECK_GE(frame_id, -1);
499 483
500 content::RenderProcessHost* source = 484 content::RenderFrameHost* source =
501 content::RenderProcessHost::FromID(source_process_id); 485 content::RenderFrameHost::FromID(source_process_id, source_routing_id);
502 if (!source) 486 if (!source)
503 return; 487 return;
504 Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext()); 488 Profile* profile =
489 Profile::FromBrowserContext(source->GetProcess()->GetBrowserContext());
505 490
506 WebContents* contents = NULL; 491 WebContents* contents = NULL;
507 scoped_ptr<MessagePort> receiver; 492 scoped_ptr<MessagePort> receiver;
508 if (!ExtensionTabUtil::GetTabById(tab_id, profile, true, NULL, NULL, 493 if (!ExtensionTabUtil::GetTabById(tab_id, profile, true, NULL, NULL,
509 &contents, NULL) || 494 &contents, NULL) ||
510 contents->GetController().NeedsReload()) { 495 contents->GetController().NeedsReload()) {
511 // The tab isn't loaded yet. Don't attempt to connect. 496 // The tab isn't loaded yet. Don't attempt to connect.
512 DispatchOnDisconnect( 497 DispatchOnDisconnect(
513 source, receiver_port_id, kReceivingEndDoesntExistError); 498 source, receiver_port_id, kReceivingEndDoesntExistError);
514 return; 499 return;
515 } 500 }
516 501
517 int receiver_routing_id; 502 content::RenderFrameHost* receiver_rfh = contents->GetMainFrame();
503 if (!receiver_rfh) {
504 DispatchOnDisconnect(
505 source, receiver_port_id, kReceivingEndDoesntExistError);
506 return;
507 }
508 // Frame ID 0 is main frame.
509 // Frame ID -1 is every frame in the tab.
510 bool include_child_frames = frame_id == -1;
518 if (frame_id > 0) { 511 if (frame_id > 0) {
519 // Positive frame ID is child frame. 512 // Positive frame ID is child frame.
520 int receiver_process_id = contents->GetRenderProcessHost()->GetID(); 513 receiver_rfh = receiver_rfh->FindByFrameTreeNodeID(frame_id);
521 if (!content::RenderFrameHost::FromID(receiver_process_id, frame_id)) { 514 if (!receiver_rfh) {
522 // Frame does not exist.
523 DispatchOnDisconnect( 515 DispatchOnDisconnect(
524 source, receiver_port_id, kReceivingEndDoesntExistError); 516 source, receiver_port_id, kReceivingEndDoesntExistError);
525 return; 517 return;
526 } 518 }
527 receiver_routing_id = frame_id;
528 } else if (frame_id == 0) {
529 // Frame ID 0 is main frame.
530 receiver_routing_id = contents->GetMainFrame()->GetRoutingID();
531 } else {
532 DCHECK_EQ(-1, frame_id);
533 // If the frame ID is not set (i.e. -1), then the channel has to be opened
534 // in every frame.
535 // TODO(robwu): Update logic so that frames that are not hosted in the main
536 // frame's process can also receive the port.
537 receiver_routing_id = MSG_ROUTING_CONTROL;
538 } 519 }
539 receiver.reset(new ExtensionMessagePort(contents->GetRenderProcessHost(), 520 receiver.reset(
540 receiver_routing_id, extension_id)); 521 new ExtensionMessagePort(weak_factory_.GetWeakPtr(),
522 receiver_port_id, extension_id, receiver_rfh,
523 include_child_frames));
541 524
542 const Extension* extension = nullptr; 525 const Extension* extension = nullptr;
543 if (!extension_id.empty()) { 526 if (!extension_id.empty()) {
544 // Source extension == target extension so the extension must exist, or 527 // Source extension == target extension so the extension must exist, or
545 // where did the IPC come from? 528 // where did the IPC come from?
546 extension = ExtensionRegistry::Get(profile)->enabled_extensions().GetByID( 529 extension = ExtensionRegistry::Get(profile)->enabled_extensions().GetByID(
547 extension_id); 530 extension_id);
548 DCHECK(extension); 531 DCHECK(extension);
549 } 532 }
550 533
551 scoped_ptr<OpenChannelParams> params(new OpenChannelParams( 534 scoped_ptr<OpenChannelParams> params(new OpenChannelParams(
552 source_process_id, 535 source_process_id,
536 source_routing_id,
553 scoped_ptr<base::DictionaryValue>(), // Source tab doesn't make sense 537 scoped_ptr<base::DictionaryValue>(), // Source tab doesn't make sense
554 // for opening to tabs. 538 // for opening to tabs.
555 -1, // If there is no tab, then there is no frame either. 539 -1, // If there is no tab, then there is no frame either.
556 tab_id, frame_id, receiver.release(), receiver_port_id, extension_id, 540 receiver.release(), receiver_port_id, extension_id, extension_id,
557 extension_id,
558 GURL(), // Source URL doesn't make sense for opening to tabs. 541 GURL(), // Source URL doesn't make sense for opening to tabs.
559 channel_name, 542 channel_name,
560 false, // Connections to tabs don't get TLS channel IDs. 543 false, // Connections to tabs don't get TLS channel IDs.
561 false)); // Connections to tabs aren't webview guests. 544 false)); // Connections to tabs aren't webview guests.
562 OpenChannelImpl(contents->GetBrowserContext(), params.Pass(), extension, 545 OpenChannelImpl(contents->GetBrowserContext(), params.Pass(), extension,
563 false /* did_enqueue */); 546 false /* did_enqueue */);
564 } 547 }
565 548
566 void MessageService::OpenChannelImpl(BrowserContext* browser_context, 549 void MessageService::OpenChannelImpl(BrowserContext* browser_context,
567 scoped_ptr<OpenChannelParams> params, 550 scoped_ptr<OpenChannelParams> params,
568 const Extension* target_extension, 551 const Extension* target_extension,
569 bool did_enqueue) { 552 bool did_enqueue) {
570 DCHECK_CURRENTLY_ON(BrowserThread::UI); 553 DCHECK_CURRENTLY_ON(BrowserThread::UI);
571 DCHECK_EQ(target_extension != nullptr, !params->target_extension_id.empty()); 554 DCHECK_EQ(target_extension != nullptr, !params->target_extension_id.empty());
572 555
573 content::RenderProcessHost* source = 556 content::RenderFrameHost* source =
574 content::RenderProcessHost::FromID(params->source_process_id); 557 content::RenderFrameHost::FromID(params->source_process_id,
558 params->source_routing_id);
575 if (!source) 559 if (!source)
576 return; // Closed while in flight. 560 return; // Closed while in flight.
577 561
578 if (!params->receiver || !params->receiver->GetRenderProcessHost()) { 562 if (!params->receiver || !params->receiver->IsValidPort()) {
579 DispatchOnDisconnect(source, params->receiver_port_id, 563 DispatchOnDisconnect(source, params->receiver_port_id,
580 kReceivingEndDoesntExistError); 564 kReceivingEndDoesntExistError);
581 return; 565 return;
582 } 566 }
583 567
584 MessageChannel* channel(new MessageChannel); 568 MessageChannel* channel(new MessageChannel);
585 channel->opener.reset(new ExtensionMessagePort(source, MSG_ROUTING_CONTROL, 569 channel->opener.reset(
586 params->source_extension_id)); 570 new ExtensionMessagePort(weak_factory_.GetWeakPtr(),
571 GET_OPPOSITE_PORT_ID(params->receiver_port_id),
572 params->source_extension_id, source, false));
587 channel->receiver.reset(params->receiver.release()); 573 channel->receiver.reset(params->receiver.release());
588 AddChannel(channel, params->receiver_port_id); 574 AddChannel(channel, params->receiver_port_id);
589 575
576 // TODO(robwu): Could |guest_process_id| and |guest_render_frame_routing_id|
577 // be removed? In the past extension message routing was process-based, but
578 // now that extensions are routed from a specific RFH, the special casing for
579 // guest views seems no longer necessary, because the ExtensionMessagePort can
580 // simply obtain the source process & frame ID directly from the RFH.
590 int guest_process_id = content::ChildProcessHost::kInvalidUniqueID; 581 int guest_process_id = content::ChildProcessHost::kInvalidUniqueID;
591 int guest_render_frame_routing_id = MSG_ROUTING_NONE; 582 int guest_render_frame_routing_id = MSG_ROUTING_NONE;
592 if (params->include_guest_process_info) { 583 if (params->include_guest_process_info) {
593 guest_process_id = params->source_process_id; 584 guest_process_id = params->source_process_id;
594 guest_render_frame_routing_id = params->source_frame_id; 585 guest_render_frame_routing_id = params->source_routing_id;
595 auto* guest_rfh = content::RenderFrameHost::FromID(
596 guest_process_id, guest_render_frame_routing_id);
597 // Reset the |source_frame_id| parameter.
598 params->source_frame_id = -1;
599 586
600 DCHECK(guest_rfh == nullptr || 587 DCHECK(WebViewGuest::FromWebContents(
601 WebViewGuest::FromWebContents( 588 WebContents::FromRenderFrameHost(source)));
602 WebContents::FromRenderFrameHost(guest_rfh)) != nullptr);
603 } 589 }
604 590
605 // Send the connect event to the receiver. Give it the opener's port ID (the 591 // Send the connect event to the receiver. Give it the opener's port ID (the
606 // opener has the opposite port ID). 592 // opener has the opposite port ID).
607 channel->receiver->DispatchOnConnect( 593 channel->receiver->DispatchOnConnect(
608 params->receiver_port_id, params->channel_name, params->source_tab.Pass(), 594 params->channel_name, params->source_tab.Pass(), params->source_frame_id,
609 params->source_frame_id, params->target_tab_id, params->target_frame_id,
610 guest_process_id, guest_render_frame_routing_id, 595 guest_process_id, guest_render_frame_routing_id,
611 params->source_extension_id, params->target_extension_id, 596 params->source_extension_id, params->target_extension_id,
612 params->source_url, params->tls_channel_id); 597 params->source_url, params->tls_channel_id);
613 598
614 // Report the event to the event router, if the target is an extension. 599 // Report the event to the event router, if the target is an extension.
615 // 600 //
616 // First, determine what event this will be (runtime.onConnect vs 601 // First, determine what event this will be (runtime.onConnect vs
617 // runtime.onMessage etc), and what the event target is (view vs background 602 // runtime.onMessage etc), and what the event target is (view vs background
618 // page etc). 603 // page etc).
619 // 604 //
(...skipping 29 matching lines...) Expand all
649 634
650 void MessageService::AddChannel(MessageChannel* channel, int receiver_port_id) { 635 void MessageService::AddChannel(MessageChannel* channel, int receiver_port_id) {
651 DCHECK_CURRENTLY_ON(BrowserThread::UI); 636 DCHECK_CURRENTLY_ON(BrowserThread::UI);
652 637
653 int channel_id = GET_CHANNEL_ID(receiver_port_id); 638 int channel_id = GET_CHANNEL_ID(receiver_port_id);
654 CHECK(channels_.find(channel_id) == channels_.end()); 639 CHECK(channels_.find(channel_id) == channels_.end());
655 channels_[channel_id] = channel; 640 channels_[channel_id] = channel;
656 pending_lazy_background_page_channels_.erase(channel_id); 641 pending_lazy_background_page_channels_.erase(channel_id);
657 } 642 }
658 643
644 void MessageService::OpenPort(int port_id, int process_id, int routing_id) {
645 DCHECK_CURRENTLY_ON(BrowserThread::UI);
646 DCHECK(!IS_OPENER_PORT_ID(port_id));
647
648 int channel_id = GET_CHANNEL_ID(port_id);
649 MessageChannelMap::iterator it = channels_.find(channel_id);
650 if (it == channels_.end())
651 return;
652
653 it->second->receiver->OpenPort(process_id, routing_id);
654 }
655
656 void MessageService::ClosePort(
657 int port_id, int process_id, int routing_id, bool force_close) {
658 DCHECK_CURRENTLY_ON(BrowserThread::UI);
659 ClosePortImpl(port_id, process_id, routing_id, force_close, std::string());
660 }
661
659 void MessageService::CloseChannel(int port_id, 662 void MessageService::CloseChannel(int port_id,
660 const std::string& error_message) { 663 const std::string& error_message) {
661 DCHECK_CURRENTLY_ON(BrowserThread::UI); 664 DCHECK_CURRENTLY_ON(BrowserThread::UI);
665 ClosePortImpl(port_id, content::ChildProcessHost::kInvalidUniqueID,
666 MSG_ROUTING_NONE, true, error_message);
667 }
662 668
669 void MessageService::ClosePortImpl(int port_id, int process_id, int routing_id,
Devlin 2015/10/30 01:49:39 I think style is to put function parameters in the
robwu 2015/11/02 19:08:34 Done.
670 bool force_close,
671 const std::string& error_message) {
663 // Note: The channel might be gone already, if the other side closed first. 672 // Note: The channel might be gone already, if the other side closed first.
664 int channel_id = GET_CHANNEL_ID(port_id); 673 int channel_id = GET_CHANNEL_ID(port_id);
665 MessageChannelMap::iterator it = channels_.find(channel_id); 674 MessageChannelMap::iterator it = channels_.find(channel_id);
666 if (it == channels_.end()) { 675 if (it == channels_.end()) {
667 PendingLazyBackgroundPageChannelMap::iterator pending = 676 PendingLazyBackgroundPageChannelMap::iterator pending =
668 pending_lazy_background_page_channels_.find(channel_id); 677 pending_lazy_background_page_channels_.find(channel_id);
669 if (pending != pending_lazy_background_page_channels_.end()) { 678 if (pending != pending_lazy_background_page_channels_.end()) {
670 lazy_background_task_queue_->AddPendingTask( 679 lazy_background_task_queue_->AddPendingTask(
671 pending->second.first, pending->second.second, 680 pending->second.first, pending->second.second,
672 base::Bind(&MessageService::PendingLazyBackgroundPageCloseChannel, 681 base::Bind(&MessageService::PendingLazyBackgroundPageClosePort,
673 weak_factory_.GetWeakPtr(), port_id, error_message)); 682 weak_factory_.GetWeakPtr(), port_id, process_id,
683 routing_id, force_close, error_message));
674 } 684 }
675 return; 685 return;
676 } 686 }
677 CloseChannelImpl(it, port_id, error_message, true); 687
688 // The difference between closing a channel and port is that closing a port
689 // does not necessarily have to destroy the channel if there are multiple
690 // receivers, whereas closing a channel always forces all ports to be closed.
691 if (force_close) {
692 CloseChannelImpl(it, port_id, error_message, true);
693 } else if (IS_OPENER_PORT_ID(port_id)) {
694 it->second->opener->ClosePort(process_id, routing_id);
695 } else {
696 it->second->receiver->ClosePort(process_id, routing_id);
697 }
678 } 698 }
679 699
680 void MessageService::CloseChannelImpl( 700 void MessageService::CloseChannelImpl(
681 MessageChannelMap::iterator channel_iter, 701 MessageChannelMap::iterator channel_iter,
682 int closing_port_id, 702 int closing_port_id,
683 const std::string& error_message, 703 const std::string& error_message,
684 bool notify_other_port) { 704 bool notify_other_port) {
685 DCHECK_CURRENTLY_ON(BrowserThread::UI); 705 DCHECK_CURRENTLY_ON(BrowserThread::UI);
686 706
687 MessageChannel* channel = channel_iter->second; 707 MessageChannel* channel = channel_iter->second;
688 708
689 // Notify the other side. 709 // Notify the other side.
690 if (notify_other_port) { 710 if (notify_other_port) {
691 MessagePort* port = IS_OPENER_PORT_ID(closing_port_id) ? 711 MessagePort* port = IS_OPENER_PORT_ID(closing_port_id) ?
692 channel->receiver.get() : channel->opener.get(); 712 channel->receiver.get() : channel->opener.get();
693 port->DispatchOnDisconnect(GET_OPPOSITE_PORT_ID(closing_port_id), 713 port->DispatchOnDisconnect(error_message);
694 error_message);
695 } 714 }
696 715
697 // Balance the IncrementLazyKeepaliveCount() in OpenChannelImpl. 716 // Balance the IncrementLazyKeepaliveCount() in OpenChannelImpl.
698 channel->opener->DecrementLazyKeepaliveCount(); 717 channel->opener->DecrementLazyKeepaliveCount();
699 channel->receiver->DecrementLazyKeepaliveCount(); 718 channel->receiver->DecrementLazyKeepaliveCount();
700 719
701 delete channel_iter->second; 720 delete channel_iter->second;
702 channels_.erase(channel_iter); 721 channels_.erase(channel_iter);
703 } 722 }
704 723
705 void MessageService::PostMessage(int source_port_id, const Message& message) { 724 void MessageService::PostMessage(int source_port_id, const Message& message) {
706 DCHECK_CURRENTLY_ON(BrowserThread::UI); 725 DCHECK_CURRENTLY_ON(BrowserThread::UI);
707 726
708 int channel_id = GET_CHANNEL_ID(source_port_id); 727 int channel_id = GET_CHANNEL_ID(source_port_id);
709 MessageChannelMap::iterator iter = channels_.find(channel_id); 728 MessageChannelMap::iterator iter = channels_.find(channel_id);
710 if (iter == channels_.end()) { 729 if (iter == channels_.end()) {
711 // If this channel is pending, queue up the PostMessage to run once 730 // If this channel is pending, queue up the PostMessage to run once
712 // the channel opens. 731 // the channel opens.
713 EnqueuePendingMessage(source_port_id, channel_id, message); 732 EnqueuePendingMessage(source_port_id, channel_id, message);
714 return; 733 return;
715 } 734 }
716 735
717 DispatchMessage(source_port_id, iter->second, message); 736 DispatchMessage(source_port_id, iter->second, message);
718 } 737 }
719 738
720 void MessageService::Observe(int type,
721 const content::NotificationSource& source,
722 const content::NotificationDetails& details) {
723 DCHECK_CURRENTLY_ON(BrowserThread::UI);
724
725 switch (type) {
726 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED:
727 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: {
728 content::RenderProcessHost* renderer =
729 content::Source<content::RenderProcessHost>(source).ptr();
730 OnProcessClosed(renderer);
731 break;
732 }
733 default:
734 NOTREACHED();
735 return;
736 }
737 }
738
739 void MessageService::OnProcessClosed(content::RenderProcessHost* process) {
740 DCHECK_CURRENTLY_ON(BrowserThread::UI);
741
742 // Close any channels that share this renderer. We notify the opposite
743 // port that its pair has closed.
744 for (MessageChannelMap::iterator it = channels_.begin();
745 it != channels_.end(); ) {
746 MessageChannelMap::iterator current = it++;
747
748 content::RenderProcessHost* opener_process =
749 current->second->opener->GetRenderProcessHost();
750 content::RenderProcessHost* receiver_process =
751 current->second->receiver->GetRenderProcessHost();
752
753 // Only notify the other side if it has a different porocess host.
754 bool notify_other_port = opener_process && receiver_process &&
755 opener_process != receiver_process;
756
757 if (opener_process == process) {
758 CloseChannelImpl(current, GET_CHANNEL_OPENER_ID(current->first),
759 std::string(), notify_other_port);
760 } else if (receiver_process == process) {
761 CloseChannelImpl(current, GET_CHANNEL_RECEIVERS_ID(current->first),
762 std::string(), notify_other_port);
763 }
764 }
765 }
766
767 void MessageService::EnqueuePendingMessage(int source_port_id, 739 void MessageService::EnqueuePendingMessage(int source_port_id,
768 int channel_id, 740 int channel_id,
769 const Message& message) { 741 const Message& message) {
770 DCHECK_CURRENTLY_ON(BrowserThread::UI); 742 DCHECK_CURRENTLY_ON(BrowserThread::UI);
771 743
772 PendingChannelMap::iterator pending_for_incognito = 744 PendingChannelMap::iterator pending_for_incognito =
773 pending_incognito_channels_.find(channel_id); 745 pending_incognito_channels_.find(channel_id);
774 if (pending_for_incognito != pending_incognito_channels_.end()) { 746 if (pending_for_incognito != pending_incognito_channels_.end()) {
775 pending_for_incognito->second.push_back( 747 pending_for_incognito->second.push_back(
776 PendingMessage(source_port_id, message)); 748 PendingMessage(source_port_id, message));
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
814 void MessageService::DispatchMessage(int source_port_id, 786 void MessageService::DispatchMessage(int source_port_id,
815 MessageChannel* channel, 787 MessageChannel* channel,
816 const Message& message) { 788 const Message& message) {
817 DCHECK_CURRENTLY_ON(BrowserThread::UI); 789 DCHECK_CURRENTLY_ON(BrowserThread::UI);
818 790
819 // Figure out which port the ID corresponds to. 791 // Figure out which port the ID corresponds to.
820 int dest_port_id = GET_OPPOSITE_PORT_ID(source_port_id); 792 int dest_port_id = GET_OPPOSITE_PORT_ID(source_port_id);
821 MessagePort* port = IS_OPENER_PORT_ID(dest_port_id) ? 793 MessagePort* port = IS_OPENER_PORT_ID(dest_port_id) ?
822 channel->opener.get() : channel->receiver.get(); 794 channel->opener.get() : channel->receiver.get();
823 795
824 port->DispatchOnMessage(message, dest_port_id); 796 port->DispatchOnMessage(message);
825 } 797 }
826 798
827 bool MessageService::MaybeAddPendingLazyBackgroundPageOpenChannelTask( 799 bool MessageService::MaybeAddPendingLazyBackgroundPageOpenChannelTask(
828 BrowserContext* context, 800 BrowserContext* context,
829 const Extension* extension, 801 const Extension* extension,
830 scoped_ptr<OpenChannelParams>* params, 802 scoped_ptr<OpenChannelParams>* params,
831 const PendingMessagesQueue& pending_messages) { 803 const PendingMessagesQueue& pending_messages) {
832 DCHECK_CURRENTLY_ON(BrowserThread::UI); 804 DCHECK_CURRENTLY_ON(BrowserThread::UI);
833 805
834 if (!BackgroundInfo::HasLazyBackgroundPage(extension)) 806 if (!BackgroundInfo::HasLazyBackgroundPage(extension))
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
869 pending_incognito_channels_.find(channel_id); 841 pending_incognito_channels_.find(channel_id);
870 if (pending_for_incognito == pending_incognito_channels_.end()) { 842 if (pending_for_incognito == pending_incognito_channels_.end()) {
871 NOTREACHED(); 843 NOTREACHED();
872 return; 844 return;
873 } 845 }
874 PendingMessagesQueue pending_messages; 846 PendingMessagesQueue pending_messages;
875 pending_messages.swap(pending_for_incognito->second); 847 pending_messages.swap(pending_for_incognito->second);
876 pending_incognito_channels_.erase(pending_for_incognito); 848 pending_incognito_channels_.erase(pending_for_incognito);
877 849
878 // Re-lookup the source process since it may no longer be valid. 850 // Re-lookup the source process since it may no longer be valid.
879 content::RenderProcessHost* source = 851 content::RenderFrameHost* source =
880 content::RenderProcessHost::FromID(params->source_process_id); 852 content::RenderFrameHost::FromID(params->source_process_id,
853 params->source_routing_id);
881 if (!source) { 854 if (!source) {
882 return; 855 return;
883 } 856 }
884 857
885 if (!allowed) { 858 if (!allowed) {
886 DispatchOnDisconnect(source, params->receiver_port_id, 859 DispatchOnDisconnect(source, params->receiver_port_id,
887 kReceivingEndDoesntExistError); 860 kReceivingEndDoesntExistError);
888 return; 861 return;
889 } 862 }
890 863
891 BrowserContext* context = source->GetBrowserContext(); 864 BrowserContext* context = source->GetProcess()->GetBrowserContext();
892 865
893 // Note: we use the source's profile here. If the source is an incognito 866 // Note: we use the source's profile here. If the source is an incognito
894 // process, we will use the incognito EPM to find the right extension process, 867 // process, we will use the incognito EPM to find the right extension process,
895 // which depends on whether the extension uses spanning or split mode. 868 // which depends on whether the extension uses spanning or split mode.
896 params->receiver.reset(new ExtensionMessagePort( 869 if (content::RenderProcessHost* extension_process =
897 GetExtensionProcess(context, params->target_extension_id), 870 GetExtensionProcess(context, params->target_extension_id)) {
898 MSG_ROUTING_CONTROL, params->target_extension_id)); 871 params->receiver.reset(
872 new ExtensionMessagePort(
873 weak_factory_.GetWeakPtr(), params->receiver_port_id,
874 params->target_extension_id, extension_process));
875 } else {
876 params->receiver.reset();
877 }
899 878
900 // If the target requests the TLS channel id, begin the lookup for it. 879 // If the target requests the TLS channel id, begin the lookup for it.
901 // The target might also be a lazy background page, checked next, but the 880 // The target might also be a lazy background page, checked next, but the
902 // loading of lazy background pages continues asynchronously, so enqueue 881 // loading of lazy background pages continues asynchronously, so enqueue
903 // messages awaiting TLS channel ID first. 882 // messages awaiting TLS channel ID first.
904 if (params->include_tls_channel_id) { 883 if (params->include_tls_channel_id) {
905 // Transfer pending messages to the next pending channel list. 884 // Transfer pending messages to the next pending channel list.
906 pending_tls_channel_id_channels_[channel_id].swap(pending_messages); 885 pending_tls_channel_id_channels_[channel_id].swap(pending_messages);
907 // Capture this reference before params is invalidated by base::Passed(). 886 // Capture this reference before params is invalidated by base::Passed().
908 const GURL& source_url = params->source_url; 887 const GURL& source_url = params->source_url;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
944 pending_tls_channel_id_channels_.find(channel_id); 923 pending_tls_channel_id_channels_.find(channel_id);
945 if (pending_for_tls_channel_id == pending_tls_channel_id_channels_.end()) { 924 if (pending_for_tls_channel_id == pending_tls_channel_id_channels_.end()) {
946 NOTREACHED(); 925 NOTREACHED();
947 return; 926 return;
948 } 927 }
949 PendingMessagesQueue pending_messages; 928 PendingMessagesQueue pending_messages;
950 pending_messages.swap(pending_for_tls_channel_id->second); 929 pending_messages.swap(pending_for_tls_channel_id->second);
951 pending_tls_channel_id_channels_.erase(pending_for_tls_channel_id); 930 pending_tls_channel_id_channels_.erase(pending_for_tls_channel_id);
952 931
953 // Re-lookup the source process since it may no longer be valid. 932 // Re-lookup the source process since it may no longer be valid.
954 content::RenderProcessHost* source = 933 content::RenderFrameHost* source =
955 content::RenderProcessHost::FromID(params->source_process_id); 934 content::RenderFrameHost::FromID(params->source_process_id,
935 params->source_routing_id);
956 if (!source) { 936 if (!source) {
957 return; 937 return;
958 } 938 }
959 939
960 BrowserContext* context = source->GetBrowserContext(); 940 BrowserContext* context = source->GetProcess()->GetBrowserContext();
961 ExtensionRegistry* registry = ExtensionRegistry::Get(context); 941 ExtensionRegistry* registry = ExtensionRegistry::Get(context);
962 const Extension* target_extension = 942 const Extension* target_extension =
963 registry->enabled_extensions().GetByID(params->target_extension_id); 943 registry->enabled_extensions().GetByID(params->target_extension_id);
964 if (!target_extension) { 944 if (!target_extension) {
965 DispatchOnDisconnect(source, params->receiver_port_id, 945 DispatchOnDisconnect(source, params->receiver_port_id,
966 kReceivingEndDoesntExistError); 946 kReceivingEndDoesntExistError);
967 return; 947 return;
968 } 948 }
969 949
970 if (!MaybeAddPendingLazyBackgroundPageOpenChannelTask( 950 if (!MaybeAddPendingLazyBackgroundPageOpenChannelTask(
971 context, target_extension, &params, pending_messages)) { 951 context, target_extension, &params, pending_messages)) {
972 OpenChannelImpl(context, params.Pass(), target_extension, 952 OpenChannelImpl(context, params.Pass(), target_extension,
973 false /* did_enqueue */); 953 false /* did_enqueue */);
974 DispatchPendingMessages(pending_messages, channel_id); 954 DispatchPendingMessages(pending_messages, channel_id);
975 } 955 }
976 } 956 }
977 957
978 void MessageService::PendingLazyBackgroundPageOpenChannel( 958 void MessageService::PendingLazyBackgroundPageOpenChannel(
979 scoped_ptr<OpenChannelParams> params, 959 scoped_ptr<OpenChannelParams> params,
980 int source_process_id, 960 int source_process_id,
981 ExtensionHost* host) { 961 ExtensionHost* host) {
982 DCHECK_CURRENTLY_ON(BrowserThread::UI); 962 DCHECK_CURRENTLY_ON(BrowserThread::UI);
983 963
984 if (!host) 964 if (!host)
985 return; // TODO(mpcomplete): notify source of disconnect? 965 return; // TODO(mpcomplete): notify source of disconnect?
986 966
987 params->receiver.reset(new ExtensionMessagePort(host->render_process_host(), 967 params->receiver.reset(
988 MSG_ROUTING_CONTROL, 968 new ExtensionMessagePort(
989 params->target_extension_id)); 969 weak_factory_.GetWeakPtr(), params->receiver_port_id,
970 params->target_extension_id, host->render_process_host()));
990 OpenChannelImpl(host->browser_context(), params.Pass(), host->extension(), 971 OpenChannelImpl(host->browser_context(), params.Pass(), host->extension(),
991 true /* did_enqueue */); 972 true /* did_enqueue */);
992 } 973 }
993 974
994 void MessageService::DispatchOnDisconnect(content::RenderProcessHost* source, 975 void MessageService::DispatchOnDisconnect(content::RenderFrameHost* source,
995 int port_id, 976 int port_id,
996 const std::string& error_message) { 977 const std::string& error_message) {
997 DCHECK_CURRENTLY_ON(BrowserThread::UI); 978 DCHECK_CURRENTLY_ON(BrowserThread::UI);
998 979
999 ExtensionMessagePort port(source, MSG_ROUTING_CONTROL, ""); 980 ExtensionMessagePort port(weak_factory_.GetWeakPtr(),
1000 port.DispatchOnDisconnect(GET_OPPOSITE_PORT_ID(port_id), error_message); 981 GET_OPPOSITE_PORT_ID(port_id), "", source, false);
982 port.DispatchOnDisconnect(error_message);
1001 } 983 }
1002 984
1003 void MessageService::DispatchPendingMessages(const PendingMessagesQueue& queue, 985 void MessageService::DispatchPendingMessages(const PendingMessagesQueue& queue,
1004 int channel_id) { 986 int channel_id) {
1005 DCHECK_CURRENTLY_ON(BrowserThread::UI); 987 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1006 988
1007 MessageChannelMap::iterator channel_iter = channels_.find(channel_id); 989 MessageChannelMap::iterator channel_iter = channels_.find(channel_id);
1008 if (channel_iter != channels_.end()) { 990 if (channel_iter != channels_.end()) {
1009 for (const PendingMessage& message : queue) { 991 for (const PendingMessage& message : queue) {
1010 DispatchMessage(message.first, channel_iter->second, message.second); 992 DispatchMessage(message.first, channel_iter->second, message.second);
1011 } 993 }
1012 } 994 }
1013 } 995 }
1014 996
1015 } // namespace extensions 997 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698