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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "chrome/browser/extensions/extension_event_router.h" | 8 #include "chrome/browser/extensions/extension_event_router.h" |
9 #include "chrome/browser/extensions/extension_process_manager.h" | 9 #include "chrome/browser/extensions/extension_process_manager.h" |
10 #include "chrome/browser/extensions/extension_host.h" | 10 #include "chrome/browser/extensions/extension_host.h" |
11 #include "chrome/browser/extensions/extension_info_map.h" | 11 #include "chrome/browser/extensions/extension_info_map.h" |
12 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/extensions/extension_system.h" |
13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/browser/ui/browser_window.h" | 16 #include "chrome/browser/ui/browser_window.h" |
16 #include "chrome/common/chrome_notification_types.h" | 17 #include "chrome/common/chrome_notification_types.h" |
17 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
18 #include "chrome/common/chrome_view_type.h" | 19 #include "chrome/common/chrome_view_type.h" |
19 #include "chrome/common/extensions/extension.h" | 20 #include "chrome/common/extensions/extension.h" |
20 #include "chrome/common/extensions/extension_messages.h" | 21 #include "chrome/common/extensions/extension_messages.h" |
21 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" |
22 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
23 #include "content/public/browser/notification_service.h" | 24 #include "content/public/browser/notification_service.h" |
24 #include "content/public/browser/render_process_host.h" | 25 #include "content/public/browser/render_process_host.h" |
25 #include "content/public/browser/render_view_host.h" | 26 #include "content/public/browser/render_view_host.h" |
26 #include "content/public/browser/render_view_host_delegate.h" | 27 #include "content/public/browser/render_view_host_delegate.h" |
27 #include "content/public/browser/site_instance.h" | 28 #include "content/public/browser/site_instance.h" |
28 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
29 #include "content/public/common/renderer_preferences.h" | 30 #include "content/public/common/renderer_preferences.h" |
30 | 31 |
31 #if defined(OS_MACOSX) | 32 #if defined(OS_MACOSX) |
32 #include "chrome/browser/extensions/extension_host_mac.h" | 33 #include "chrome/browser/extensions/extension_host_mac.h" |
33 #endif | 34 #endif |
34 | 35 |
35 using content::BrowserThread; | 36 using content::BrowserThread; |
36 using content::OpenURLParams; | 37 using content::OpenURLParams; |
37 using content::Referrer; | 38 using content::Referrer; |
38 using content::RenderViewHost; | 39 using content::RenderViewHost; |
39 using content::SiteInstance; | 40 using content::SiteInstance; |
| 41 using content::WebContents; |
40 | 42 |
41 namespace { | 43 namespace { |
42 | 44 |
43 std::string GetExtensionID(RenderViewHost* render_view_host) { | 45 std::string GetExtensionID(RenderViewHost* render_view_host) { |
44 // This works for both apps and extensions because the site has been | 46 // This works for both apps and extensions because the site has been |
45 // normalized to the extension URL for apps. | 47 // normalized to the extension URL for apps. |
46 return render_view_host->GetSiteInstance()->GetSite().host(); | 48 return render_view_host->GetSiteInstance()->GetSite().host(); |
47 } | 49 } |
48 | 50 |
49 // Incognito profiles use this process manager. It is mostly a shim that decides | 51 // Incognito profiles use this process manager. It is mostly a shim that decides |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 ExtensionProcessManager::GetRenderViewHostsForExtension( | 302 ExtensionProcessManager::GetRenderViewHostsForExtension( |
301 const std::string& extension_id) { | 303 const std::string& extension_id) { |
302 std::set<RenderViewHost*> result; | 304 std::set<RenderViewHost*> result; |
303 | 305 |
304 SiteInstance* site_instance = GetSiteInstanceForURL( | 306 SiteInstance* site_instance = GetSiteInstanceForURL( |
305 Extension::GetBaseURLFromExtensionId(extension_id)); | 307 Extension::GetBaseURLFromExtensionId(extension_id)); |
306 if (!site_instance) | 308 if (!site_instance) |
307 return result; | 309 return result; |
308 | 310 |
309 // Gather up all the views for that site. | 311 // Gather up all the views for that site. |
310 for (ExtensionRenderViews::iterator view = all_extension_views_.begin(); | 312 for (ExtensionWebContents::iterator contents = |
311 view != all_extension_views_.end(); ++view) { | 313 all_extension_contents_.begin(); |
312 if (view->first->GetSiteInstance() == site_instance) | 314 contents != all_extension_contents_.end(); ++contents) { |
313 result.insert(view->first); | 315 if (contents->first->GetSiteInstance() == site_instance) |
| 316 result.insert(contents->first->GetRenderViewHost()); |
314 } | 317 } |
315 | 318 |
316 return result; | 319 return result; |
317 } | 320 } |
318 | 321 |
319 void ExtensionProcessManager::RegisterRenderViewHost( | 322 const Extension* ExtensionProcessManager::GetExtensionForRenderViewHost( |
320 RenderViewHost* render_view_host, | 323 content::RenderViewHost* render_view_host) { |
321 const Extension* extension) { | 324 ExtensionService* service = |
322 all_extension_views_[render_view_host] = content::VIEW_TYPE_INVALID; | 325 ExtensionSystem::Get(GetProfile())->extension_service(); |
| 326 return service->extensions()->GetByID( |
| 327 render_view_host->GetSiteInstance()->GetSite().host()); |
323 } | 328 } |
324 | 329 |
325 void ExtensionProcessManager::UnregisterRenderViewHost( | 330 void ExtensionProcessManager::RegisterWebContents( |
326 RenderViewHost* render_view_host) { | 331 WebContents* web_contents, |
327 ExtensionRenderViews::iterator view = | 332 const Extension* extension) { |
328 all_extension_views_.find(render_view_host); | 333 all_extension_contents_[web_contents] = content::VIEW_TYPE_INVALID; |
329 if (view == all_extension_views_.end()) | 334 } |
| 335 |
| 336 void ExtensionProcessManager::UnregisterWebContents( |
| 337 WebContents* web_contents) { |
| 338 ExtensionWebContents::iterator contents = |
| 339 all_extension_contents_.find(web_contents); |
| 340 if (contents == all_extension_contents_.end()) |
330 return; | 341 return; |
331 | 342 |
332 content::ViewType view_type = view->second; | 343 content::ViewType view_type = contents->second; |
333 all_extension_views_.erase(view); | 344 all_extension_contents_.erase(contents); |
334 | 345 |
335 // Keepalive count, balanced in UpdateRegisteredRenderView. | 346 // Keepalive count, balanced in UpdateRegisteredRenderView. |
336 if (view_type != content::VIEW_TYPE_INVALID && | 347 if (view_type != content::VIEW_TYPE_INVALID && |
337 view_type != chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { | 348 view_type != chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { |
338 const Extension* extension = | 349 const Extension* extension = |
339 GetProfile()->GetExtensionService()->extensions()->GetByID( | 350 GetProfile()->GetExtensionService()->extensions()->GetByID( |
340 GetExtensionID(render_view_host)); | 351 GetExtensionID(contents->first->GetRenderViewHost())); |
341 if (extension) | 352 if (extension) |
342 DecrementLazyKeepaliveCount(extension); | 353 DecrementLazyKeepaliveCount(extension); |
343 } | 354 } |
344 } | 355 } |
345 | 356 |
346 void ExtensionProcessManager::UpdateRegisteredRenderView( | 357 void ExtensionProcessManager::UpdateRegisteredWebContents( |
347 RenderViewHost* render_view_host) { | 358 WebContents* web_contents) { |
348 ExtensionRenderViews::iterator view = | 359 ExtensionWebContents::iterator contents = |
349 all_extension_views_.find(render_view_host); | 360 all_extension_contents_.find(web_contents); |
350 if (view == all_extension_views_.end()) | 361 if (contents == all_extension_contents_.end()) |
351 return; | 362 return; |
352 | 363 |
353 view->second = render_view_host->GetDelegate()->GetRenderViewType(); | 364 RenderViewHost* render_view_host = contents->first->GetRenderViewHost(); |
| 365 contents->second = render_view_host->GetDelegate()->GetRenderViewType(); |
354 | 366 |
355 // Keep the lazy background page alive as long as any non-background-page | 367 // Keep the lazy background page alive as long as any non-background-page |
356 // extension views are visible. Keepalive count balanced in | 368 // extension views are visible. Keepalive count balanced in |
357 // UnregisterRenderViewHost. | 369 // UnregisterRenderViewHost. |
358 if (view->second != content::VIEW_TYPE_INVALID && | 370 if (contents->second != content::VIEW_TYPE_INVALID && |
359 view->second != chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { | 371 contents->second != chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { |
360 const Extension* extension = | 372 const Extension* extension = |
361 GetProfile()->GetExtensionService()->extensions()->GetByID( | 373 GetProfile()->GetExtensionService()->extensions()->GetByID( |
362 GetExtensionID(render_view_host)); | 374 GetExtensionID(render_view_host)); |
363 if (extension) | 375 if (extension) |
364 IncrementLazyKeepaliveCount(extension); | 376 IncrementLazyKeepaliveCount(extension); |
365 } | 377 } |
366 } | 378 } |
367 | 379 |
368 SiteInstance* ExtensionProcessManager::GetSiteInstanceForURL(const GURL& url) { | 380 SiteInstance* ExtensionProcessManager::GetSiteInstanceForURL(const GURL& url) { |
369 return site_instance_->GetRelatedSiteInstance(url); | 381 return site_instance_->GetRelatedSiteInstance(url); |
370 } | 382 } |
371 | 383 |
372 bool ExtensionProcessManager::HasExtensionHost(ExtensionHost* host) const { | |
373 return all_hosts_.find(host) != all_hosts_.end(); | |
374 } | |
375 | |
376 int ExtensionProcessManager::GetLazyKeepaliveCount(const Extension* extension) { | 384 int ExtensionProcessManager::GetLazyKeepaliveCount(const Extension* extension) { |
377 if (!extension->has_lazy_background_page()) | 385 if (!extension->has_lazy_background_page()) |
378 return 0; | 386 return 0; |
379 | 387 |
380 return background_page_data_[extension->id()].lazy_keepalive_count; | 388 return background_page_data_[extension->id()].lazy_keepalive_count; |
381 } | 389 } |
382 | 390 |
383 int ExtensionProcessManager::IncrementLazyKeepaliveCount( | 391 int ExtensionProcessManager::IncrementLazyKeepaliveCount( |
384 const Extension* extension) { | 392 const Extension* extension) { |
385 if (!extension->has_lazy_background_page()) | 393 if (!extension->has_lazy_background_page()) |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 CloseBackgroundHost(host); | 511 CloseBackgroundHost(host); |
504 break; | 512 break; |
505 } | 513 } |
506 } | 514 } |
507 background_page_data_.erase(extension->id()); | 515 background_page_data_.erase(extension->id()); |
508 break; | 516 break; |
509 } | 517 } |
510 | 518 |
511 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { | 519 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { |
512 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); | 520 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); |
513 all_hosts_.erase(host); | |
514 if (background_hosts_.erase(host)) | 521 if (background_hosts_.erase(host)) |
515 background_page_data_.erase(host->extension()->id()); | 522 background_page_data_.erase(host->extension()->id()); |
| 523 platform_app_hosts_.erase(host); |
516 break; | 524 break; |
517 } | 525 } |
518 | 526 |
519 case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: { | 527 case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: { |
520 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); | 528 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); |
521 if (host->extension_host_type() == | 529 if (host->extension_host_type() == |
522 chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { | 530 chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { |
523 CloseBackgroundHost(host); | 531 CloseBackgroundHost(host); |
524 } | 532 } |
525 break; | 533 break; |
526 } | 534 } |
527 | 535 |
528 case content::NOTIFICATION_WEB_CONTENTS_CONNECTED: { | 536 case content::NOTIFICATION_WEB_CONTENTS_CONNECTED: { |
529 content::WebContents* contents = | 537 content::WebContents* contents = |
530 content::Source<content::WebContents>(source).ptr(); | 538 content::Source<content::WebContents>(source).ptr(); |
531 UpdateRegisteredRenderView(contents->GetRenderViewHost()); | 539 UpdateRegisteredWebContents(contents); |
532 break; | 540 break; |
533 } | 541 } |
534 | 542 |
535 case content::NOTIFICATION_APP_TERMINATING: { | 543 case content::NOTIFICATION_APP_TERMINATING: { |
536 // Close background hosts when the last browser is closed so that they | 544 // Close background hosts when the last browser is closed so that they |
537 // have time to shutdown various objects on different threads. Our | 545 // have time to shutdown various objects on different threads. Our |
538 // destructor is called too late in the shutdown sequence. | 546 // destructor is called too late in the shutdown sequence. |
539 CloseBackgroundHosts(); | 547 CloseBackgroundHosts(); |
540 break; | 548 break; |
541 } | 549 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
576 } | 584 } |
577 } | 585 } |
578 | 586 |
579 Profile* ExtensionProcessManager::GetProfile() const { | 587 Profile* ExtensionProcessManager::GetProfile() const { |
580 return Profile::FromBrowserContext(site_instance_->GetBrowserContext()); | 588 return Profile::FromBrowserContext(site_instance_->GetBrowserContext()); |
581 } | 589 } |
582 | 590 |
583 void ExtensionProcessManager::OnExtensionHostCreated(ExtensionHost* host, | 591 void ExtensionProcessManager::OnExtensionHostCreated(ExtensionHost* host, |
584 bool is_background) { | 592 bool is_background) { |
585 DCHECK_EQ(site_instance_->GetBrowserContext(), host->profile()); | 593 DCHECK_EQ(site_instance_->GetBrowserContext(), host->profile()); |
586 | |
587 all_hosts_.insert(host); | |
588 if (is_background) | 594 if (is_background) |
589 background_hosts_.insert(host); | 595 background_hosts_.insert(host); |
| 596 if (host->extension()->is_platform_app()) |
| 597 platform_app_hosts_.insert(host); |
590 } | 598 } |
591 | 599 |
592 void ExtensionProcessManager::CloseBackgroundHost(ExtensionHost* host) { | 600 void ExtensionProcessManager::CloseBackgroundHost(ExtensionHost* host) { |
593 CHECK(host->extension_host_type() == | 601 CHECK(host->extension_host_type() == |
594 chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); | 602 chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); |
595 delete host; | 603 delete host; |
596 // |host| should deregister itself from our structures. | 604 // |host| should deregister itself from our structures. |
597 CHECK(background_hosts_.find(host) == background_hosts_.end()); | 605 CHECK(background_hosts_.find(host) == background_hosts_.end()); |
598 } | 606 } |
599 | 607 |
(...skipping 13 matching lines...) Expand all Loading... |
613 Profile* profile) | 621 Profile* profile) |
614 : ExtensionProcessManager(profile), | 622 : ExtensionProcessManager(profile), |
615 original_manager_(profile->GetOriginalProfile()-> | 623 original_manager_(profile->GetOriginalProfile()-> |
616 GetExtensionProcessManager()) { | 624 GetExtensionProcessManager()) { |
617 DCHECK(profile->IsOffTheRecord()); | 625 DCHECK(profile->IsOffTheRecord()); |
618 | 626 |
619 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY, | 627 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY, |
620 content::NotificationService::AllSources()); | 628 content::NotificationService::AllSources()); |
621 } | 629 } |
622 | 630 |
| 631 const ExtensionProcessManager::ContentsSet |
| 632 ExtensionProcessManager::GetAllContents() const { |
| 633 ContentsSet result; |
| 634 for (ExtensionWebContents::const_iterator iter = |
| 635 all_extension_contents_.begin(); |
| 636 iter != all_extension_contents_.end(); ++iter) { |
| 637 result.insert(iter->first); |
| 638 } |
| 639 return result; |
| 640 } |
| 641 |
623 ExtensionHost* IncognitoExtensionProcessManager::CreateViewHost( | 642 ExtensionHost* IncognitoExtensionProcessManager::CreateViewHost( |
624 const Extension* extension, | 643 const Extension* extension, |
625 const GURL& url, | 644 const GURL& url, |
626 Browser* browser, | 645 Browser* browser, |
627 content::ViewType view_type) { | 646 content::ViewType view_type) { |
628 if (extension->incognito_split_mode()) { | 647 if (extension->incognito_split_mode()) { |
629 if (IsIncognitoEnabled(extension)) { | 648 if (IsIncognitoEnabled(extension)) { |
630 return ExtensionProcessManager::CreateViewHost(extension, url, | 649 return ExtensionProcessManager::CreateViewHost(extension, url, |
631 browser, view_type); | 650 browser, view_type); |
632 } else { | 651 } else { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
689 if (service && service->is_ready()) | 708 if (service && service->is_ready()) |
690 CreateBackgroundHostsForProfileStartup(this, service->extensions()); | 709 CreateBackgroundHostsForProfileStartup(this, service->extensions()); |
691 } | 710 } |
692 break; | 711 break; |
693 } | 712 } |
694 default: | 713 default: |
695 ExtensionProcessManager::Observe(type, source, details); | 714 ExtensionProcessManager::Observe(type, source, details); |
696 break; | 715 break; |
697 } | 716 } |
698 } | 717 } |
OLD | NEW |