Chromium Code Reviews| 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 "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/time.h" | 13 #include "base/time.h" |
| 14 #include "chrome/browser/extensions/api/runtime/runtime_api.h" | 14 #include "chrome/browser/extensions/api/runtime/runtime_api.h" |
| 15 #include "chrome/browser/extensions/extension_host.h" | 15 #include "chrome/browser/extensions/extension_host.h" |
| 16 #include "chrome/browser/extensions/extension_info_map.h" | 16 #include "chrome/browser/extensions/extension_info_map.h" |
| 17 #include "chrome/browser/extensions/extension_process_manager.h" | 17 #include "chrome/browser/extensions/extension_process_manager.h" |
| 18 #include "chrome/browser/extensions/extension_service.h" | 18 #include "chrome/browser/extensions/extension_service.h" |
| 19 #include "chrome/browser/extensions/extension_system.h" | 19 #include "chrome/browser/extensions/extension_system.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
| 22 #include "chrome/browser/ui/browser_finder.h" | 22 #include "chrome/browser/ui/browser_finder.h" |
| 23 #include "chrome/browser/ui/browser_window.h" | 23 #include "chrome/browser/ui/browser_window.h" |
| 24 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 24 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 25 #include "chrome/browser/view_type_utils.h" | 25 #include "chrome/browser/view_type_utils.h" |
| 26 #include "chrome/common/chrome_notification_types.h" | 26 #include "chrome/common/chrome_notification_types.h" |
| 27 #include "chrome/common/chrome_switches.h" | 27 #include "chrome/common/chrome_switches.h" |
| 28 #include "chrome/common/extensions/background_info.h" | |
| 28 #include "chrome/common/extensions/extension.h" | 29 #include "chrome/common/extensions/extension.h" |
| 29 #include "chrome/common/extensions/extension_messages.h" | 30 #include "chrome/common/extensions/extension_messages.h" |
| 31 #include "chrome/common/extensions/manifest_handler.h" | |
| 30 #include "chrome/common/extensions/manifest_url_handler.h" | 32 #include "chrome/common/extensions/manifest_url_handler.h" |
| 31 #include "chrome/common/url_constants.h" | 33 #include "chrome/common/url_constants.h" |
| 32 #include "content/public/browser/browser_thread.h" | 34 #include "content/public/browser/browser_thread.h" |
| 33 #include "content/public/browser/notification_service.h" | 35 #include "content/public/browser/notification_service.h" |
| 34 #include "content/public/browser/render_process_host.h" | 36 #include "content/public/browser/render_process_host.h" |
| 35 #include "content/public/browser/render_view_host.h" | 37 #include "content/public/browser/render_view_host.h" |
| 36 #include "content/public/browser/site_instance.h" | 38 #include "content/public/browser/site_instance.h" |
| 37 #include "content/public/browser/web_contents.h" | 39 #include "content/public/browser/web_contents.h" |
| 38 #include "content/public/browser/web_contents_delegate.h" | 40 #include "content/public/browser/web_contents_delegate.h" |
| 39 #include "content/public/common/renderer_preferences.h" | 41 #include "content/public/common/renderer_preferences.h" |
| 40 | 42 |
| 41 #if defined(OS_MACOSX) | 43 #if defined(OS_MACOSX) |
| 42 #include "chrome/browser/extensions/extension_host_mac.h" | 44 #include "chrome/browser/extensions/extension_host_mac.h" |
| 43 #endif | 45 #endif |
| 44 | 46 |
| 45 using content::BrowserThread; | 47 using content::BrowserThread; |
| 46 using content::OpenURLParams; | 48 using content::OpenURLParams; |
| 47 using content::Referrer; | 49 using content::Referrer; |
| 48 using content::RenderViewHost; | 50 using content::RenderViewHost; |
| 49 using content::SiteInstance; | 51 using content::SiteInstance; |
| 50 using content::WebContents; | 52 using content::WebContents; |
| 53 using extensions::BackgroundInfo; | |
| 54 using extensions::BackgroundManifestHandler; | |
| 51 using extensions::Extension; | 55 using extensions::Extension; |
| 52 using extensions::ExtensionHost; | 56 using extensions::ExtensionHost; |
| 53 | 57 |
| 54 namespace { | 58 namespace { |
| 55 | 59 |
| 56 std::string GetExtensionID(RenderViewHost* render_view_host) { | 60 std::string GetExtensionID(RenderViewHost* render_view_host) { |
| 57 // This works for both apps and extensions because the site has been | 61 // This works for both apps and extensions because the site has been |
| 58 // normalized to the extension URL for apps. | 62 // normalized to the extension URL for apps. |
| 59 if (!render_view_host->GetSiteInstance()) | 63 if (!render_view_host->GetSiteInstance()) |
| 60 return ""; | 64 return ""; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 85 const content::NotificationDetails& details) OVERRIDE; | 89 const content::NotificationDetails& details) OVERRIDE; |
| 86 | 90 |
| 87 // Returns true if the extension is allowed to run in incognito mode. | 91 // Returns true if the extension is allowed to run in incognito mode. |
| 88 bool IsIncognitoEnabled(const Extension* extension); | 92 bool IsIncognitoEnabled(const Extension* extension); |
| 89 | 93 |
| 90 ExtensionProcessManager* original_manager_; | 94 ExtensionProcessManager* original_manager_; |
| 91 }; | 95 }; |
| 92 | 96 |
| 93 static void CreateBackgroundHostForExtensionLoad( | 97 static void CreateBackgroundHostForExtensionLoad( |
| 94 ExtensionProcessManager* manager, const Extension* extension) { | 98 ExtensionProcessManager* manager, const Extension* extension) { |
| 95 if (extension->has_persistent_background_page()) | 99 if (BackgroundInfo::HasPersistentBackgroundPage(extension)) |
| 96 manager->CreateBackgroundHost(extension, extension->GetBackgroundURL()); | 100 manager->CreateBackgroundHost(extension, |
| 101 BackgroundInfo::GetBackgroundURL(extension)); | |
| 97 } | 102 } |
| 98 | 103 |
| 99 static void CreateBackgroundHostsForProfileStartup( | 104 static void CreateBackgroundHostsForProfileStartup( |
| 100 Profile* profile, | 105 Profile* profile, |
| 101 ExtensionProcessManager* manager, | 106 ExtensionProcessManager* manager, |
| 102 const ExtensionSet* extensions) { | 107 const ExtensionSet* extensions) { |
| 103 for (ExtensionSet::const_iterator extension = extensions->begin(); | 108 for (ExtensionSet::const_iterator extension = extensions->begin(); |
| 104 extension != extensions->end(); ++extension) { | 109 extension != extensions->end(); ++extension) { |
| 105 CreateBackgroundHostForExtensionLoad(manager, *extension); | 110 CreateBackgroundHostForExtensionLoad(manager, *extension); |
| 106 | 111 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 178 switches::kEventPageIdleTime), &idle_time_sec)) { | 183 switches::kEventPageIdleTime), &idle_time_sec)) { |
| 179 event_page_idle_time_ = base::TimeDelta::FromSeconds(idle_time_sec); | 184 event_page_idle_time_ = base::TimeDelta::FromSeconds(idle_time_sec); |
| 180 } | 185 } |
| 181 event_page_unloading_time_ = base::TimeDelta::FromSeconds(5); | 186 event_page_unloading_time_ = base::TimeDelta::FromSeconds(5); |
| 182 unsigned unloading_time_sec = 0; | 187 unsigned unloading_time_sec = 0; |
| 183 if (base::StringToUint(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 188 if (base::StringToUint(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 184 switches::kEventPageUnloadingTime), &unloading_time_sec)) { | 189 switches::kEventPageUnloadingTime), &unloading_time_sec)) { |
| 185 event_page_unloading_time_ = base::TimeDelta::FromSeconds( | 190 event_page_unloading_time_ = base::TimeDelta::FromSeconds( |
| 186 unloading_time_sec); | 191 unloading_time_sec); |
| 187 } | 192 } |
| 193 | |
| 194 std::vector<std::string> background_keys(BackgroundManifestHandler::keys()); | |
|
Matt Perry
2013/02/15 20:15:02
This is cumbersome. How about making it a method o
Matt Perry
2013/02/15 20:15:02
Also, it feels a little out of place here, but I c
Yoyo Zhou
2013/02/16 00:54:04
Maybe we could call it ExtensionService? =)
Serio
Yoyo Zhou
2013/02/16 00:54:04
I definitely considered this but I thought the own
| |
| 195 linked_ptr<BackgroundManifestHandler> background_handler( | |
| 196 new BackgroundManifestHandler); | |
| 197 for (size_t i = 0; i < background_keys.size(); ++i) { | |
| 198 extensions::ManifestHandler::Register(background_keys[i], | |
| 199 background_handler); | |
| 200 } | |
| 188 } | 201 } |
| 189 | 202 |
| 190 ExtensionProcessManager::~ExtensionProcessManager() { | 203 ExtensionProcessManager::~ExtensionProcessManager() { |
| 191 CloseBackgroundHosts(); | 204 CloseBackgroundHosts(); |
| 192 DCHECK(background_hosts_.empty()); | 205 DCHECK(background_hosts_.empty()); |
| 193 } | 206 } |
| 194 | 207 |
| 195 const ExtensionProcessManager::ViewSet | 208 const ExtensionProcessManager::ViewSet |
| 196 ExtensionProcessManager::GetAllViews() const { | 209 ExtensionProcessManager::GetAllViews() const { |
| 197 ViewSet result; | 210 ViewSet result; |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 420 return site_instance_->GetRelatedSiteInstance(url); | 433 return site_instance_->GetRelatedSiteInstance(url); |
| 421 } | 434 } |
| 422 | 435 |
| 423 bool ExtensionProcessManager::IsBackgroundHostClosing( | 436 bool ExtensionProcessManager::IsBackgroundHostClosing( |
| 424 const std::string& extension_id) { | 437 const std::string& extension_id) { |
| 425 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); | 438 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); |
| 426 return (host && background_page_data_[extension_id].is_closing); | 439 return (host && background_page_data_[extension_id].is_closing); |
| 427 } | 440 } |
| 428 | 441 |
| 429 int ExtensionProcessManager::GetLazyKeepaliveCount(const Extension* extension) { | 442 int ExtensionProcessManager::GetLazyKeepaliveCount(const Extension* extension) { |
| 430 if (!extension->has_lazy_background_page()) | 443 if (!BackgroundInfo::HasLazyBackgroundPage(extension)) |
| 431 return 0; | 444 return 0; |
| 432 | 445 |
| 433 return background_page_data_[extension->id()].lazy_keepalive_count; | 446 return background_page_data_[extension->id()].lazy_keepalive_count; |
| 434 } | 447 } |
| 435 | 448 |
| 436 int ExtensionProcessManager::IncrementLazyKeepaliveCount( | 449 int ExtensionProcessManager::IncrementLazyKeepaliveCount( |
| 437 const Extension* extension) { | 450 const Extension* extension) { |
| 438 if (!extension->has_lazy_background_page()) | 451 if (!BackgroundInfo::HasLazyBackgroundPage(extension)) |
| 439 return 0; | 452 return 0; |
| 440 | 453 |
| 441 int& count = background_page_data_[extension->id()].lazy_keepalive_count; | 454 int& count = background_page_data_[extension->id()].lazy_keepalive_count; |
| 442 if (++count == 1) | 455 if (++count == 1) |
| 443 OnLazyBackgroundPageActive(extension->id()); | 456 OnLazyBackgroundPageActive(extension->id()); |
| 444 | 457 |
| 445 return count; | 458 return count; |
| 446 } | 459 } |
| 447 | 460 |
| 448 int ExtensionProcessManager::DecrementLazyKeepaliveCount( | 461 int ExtensionProcessManager::DecrementLazyKeepaliveCount( |
| 449 const Extension* extension) { | 462 const Extension* extension) { |
| 450 if (!extension->has_lazy_background_page()) | 463 if (!BackgroundInfo::HasLazyBackgroundPage(extension)) |
| 451 return 0; | 464 return 0; |
| 452 | 465 |
| 453 int& count = background_page_data_[extension->id()].lazy_keepalive_count; | 466 int& count = background_page_data_[extension->id()].lazy_keepalive_count; |
| 454 DCHECK_GT(count, 0); | 467 DCHECK_GT(count, 0); |
| 455 if (--count == 0) { | 468 if (--count == 0) { |
| 456 MessageLoop::current()->PostDelayedTask( | 469 MessageLoop::current()->PostDelayedTask( |
| 457 FROM_HERE, | 470 FROM_HERE, |
| 458 base::Bind(&ExtensionProcessManager::OnLazyBackgroundPageIdle, | 471 base::Bind(&ExtensionProcessManager::OnLazyBackgroundPageIdle, |
| 459 weak_ptr_factory_.GetWeakPtr(), extension->id(), | 472 weak_ptr_factory_.GetWeakPtr(), extension->id(), |
| 460 ++background_page_data_[extension->id()].close_sequence_id), | 473 ++background_page_data_[extension->id()].close_sequence_id), |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 713 Profile* ExtensionProcessManager::GetProfile() const { | 726 Profile* ExtensionProcessManager::GetProfile() const { |
| 714 return Profile::FromBrowserContext(site_instance_->GetBrowserContext()); | 727 return Profile::FromBrowserContext(site_instance_->GetBrowserContext()); |
| 715 } | 728 } |
| 716 | 729 |
| 717 void ExtensionProcessManager::OnExtensionHostCreated(ExtensionHost* host, | 730 void ExtensionProcessManager::OnExtensionHostCreated(ExtensionHost* host, |
| 718 bool is_background) { | 731 bool is_background) { |
| 719 DCHECK_EQ(site_instance_->GetBrowserContext(), host->profile()); | 732 DCHECK_EQ(site_instance_->GetBrowserContext(), host->profile()); |
| 720 if (is_background) { | 733 if (is_background) { |
| 721 background_hosts_.insert(host); | 734 background_hosts_.insert(host); |
| 722 | 735 |
| 723 if (host->extension()->has_lazy_background_page()) { | 736 if (BackgroundInfo::HasLazyBackgroundPage(host->extension())) { |
| 724 linked_ptr<PerfTimer> since_unloaded( | 737 linked_ptr<PerfTimer> since_unloaded( |
| 725 background_page_data_[host->extension()->id()]. | 738 background_page_data_[host->extension()->id()]. |
| 726 since_unloaded.release()); | 739 since_unloaded.release()); |
| 727 if (since_unloaded.get()) { | 740 if (since_unloaded.get()) { |
| 728 UMA_HISTOGRAM_LONG_TIMES("Extensions.EventPageIdleTime", | 741 UMA_HISTOGRAM_LONG_TIMES("Extensions.EventPageIdleTime", |
| 729 since_unloaded->Elapsed()); | 742 since_unloaded->Elapsed()); |
| 730 } | 743 } |
| 731 } | 744 } |
| 732 } | 745 } |
| 733 } | 746 } |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 856 CreateBackgroundHostsForProfileStartup(GetProfile(), | 869 CreateBackgroundHostsForProfileStartup(GetProfile(), |
| 857 this, service->extensions()); | 870 this, service->extensions()); |
| 858 } | 871 } |
| 859 break; | 872 break; |
| 860 } | 873 } |
| 861 default: | 874 default: |
| 862 ExtensionProcessManager::Observe(type, source, details); | 875 ExtensionProcessManager::Observe(type, source, details); |
| 863 break; | 876 break; |
| 864 } | 877 } |
| 865 } | 878 } |
| OLD | NEW |