| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/extension_process_manager.h" | 5 #include "chrome/browser/extensions/extension_process_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "chrome/browser/chrome_notification_types.h" | 16 #include "chrome/browser/chrome_notification_types.h" |
| 17 #include "chrome/browser/extensions/api/runtime/runtime_api.h" | 17 #include "chrome/browser/extensions/api/runtime/runtime_api.h" |
| 18 #include "chrome/browser/extensions/extension_host.h" | 18 #include "chrome/browser/extensions/extension_host.h" |
| 19 #include "chrome/browser/extensions/extension_info_map.h" | |
| 20 #include "chrome/browser/extensions/extension_service.h" | 19 #include "chrome/browser/extensions/extension_service.h" |
| 21 #include "chrome/browser/extensions/extension_system.h" | 20 #include "chrome/browser/extensions/extension_system.h" |
| 22 #include "chrome/browser/extensions/extension_util.h" | 21 #include "chrome/browser/extensions/extension_util.h" |
| 23 #include "chrome/browser/ui/browser.h" | |
| 24 #include "chrome/common/extensions/background_info.h" | 22 #include "chrome/common/extensions/background_info.h" |
| 25 #include "chrome/common/extensions/extension.h" | 23 #include "chrome/common/extensions/extension.h" |
| 26 #include "chrome/common/extensions/extension_messages.h" | 24 #include "chrome/common/extensions/extension_messages.h" |
| 27 #include "chrome/common/extensions/manifest_url_handler.h" | 25 #include "content/public/browser/browser_context.h" |
| 28 #include "chrome/common/url_constants.h" | |
| 29 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 30 #include "content/public/browser/devtools_agent_host.h" | 27 #include "content/public/browser/devtools_agent_host.h" |
| 31 #include "content/public/browser/devtools_manager.h" | 28 #include "content/public/browser/devtools_manager.h" |
| 32 #include "content/public/browser/notification_service.h" | 29 #include "content/public/browser/notification_service.h" |
| 33 #include "content/public/browser/render_process_host.h" | 30 #include "content/public/browser/render_process_host.h" |
| 34 #include "content/public/browser/render_view_host.h" | 31 #include "content/public/browser/render_view_host.h" |
| 35 #include "content/public/browser/site_instance.h" | 32 #include "content/public/browser/site_instance.h" |
| 36 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
| 37 #include "content/public/browser/web_contents_delegate.h" | 34 #include "content/public/browser/web_contents_delegate.h" |
| 38 #include "content/public/browser/web_contents_observer.h" | 35 #include "content/public/browser/web_contents_observer.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 } | 78 } |
| 82 | 79 |
| 83 // Incognito profiles use this process manager. It is mostly a shim that decides | 80 // Incognito profiles use this process manager. It is mostly a shim that decides |
| 84 // whether to fall back on the original profile's ExtensionProcessManager based | 81 // whether to fall back on the original profile's ExtensionProcessManager based |
| 85 // on whether a given extension uses "split" or "spanning" incognito behavior. | 82 // on whether a given extension uses "split" or "spanning" incognito behavior. |
| 86 class IncognitoExtensionProcessManager : public ExtensionProcessManager { | 83 class IncognitoExtensionProcessManager : public ExtensionProcessManager { |
| 87 public: | 84 public: |
| 88 IncognitoExtensionProcessManager(BrowserContext* incognito_context, | 85 IncognitoExtensionProcessManager(BrowserContext* incognito_context, |
| 89 BrowserContext* original_context); | 86 BrowserContext* original_context); |
| 90 virtual ~IncognitoExtensionProcessManager(); | 87 virtual ~IncognitoExtensionProcessManager(); |
| 91 virtual ExtensionHost* CreateViewHost( | |
| 92 const Extension* extension, | |
| 93 const GURL& url, | |
| 94 Browser* browser, | |
| 95 extensions::ViewType view_type) OVERRIDE; | |
| 96 virtual ExtensionHost* CreateBackgroundHost(const Extension* extension, | 88 virtual ExtensionHost* CreateBackgroundHost(const Extension* extension, |
| 97 const GURL& url) OVERRIDE; | 89 const GURL& url) OVERRIDE; |
| 98 virtual SiteInstance* GetSiteInstanceForURL(const GURL& url) OVERRIDE; | 90 virtual SiteInstance* GetSiteInstanceForURL(const GURL& url) OVERRIDE; |
| 99 | 91 |
| 100 private: | 92 private: |
| 101 // Returns true if the extension is allowed to run in incognito mode. | 93 // Returns true if the extension is allowed to run in incognito mode. |
| 102 bool IsIncognitoEnabled(const Extension* extension); | 94 bool IsIncognitoEnabled(const Extension* extension); |
| 103 | 95 |
| 104 ExtensionProcessManager* original_manager_; | 96 ExtensionProcessManager* original_manager_; |
| 105 | 97 |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 ExtensionProcessManager::GetAllViews() const { | 233 ExtensionProcessManager::GetAllViews() const { |
| 242 ViewSet result; | 234 ViewSet result; |
| 243 for (ExtensionRenderViews::const_iterator iter = | 235 for (ExtensionRenderViews::const_iterator iter = |
| 244 all_extension_views_.begin(); | 236 all_extension_views_.begin(); |
| 245 iter != all_extension_views_.end(); ++iter) { | 237 iter != all_extension_views_.end(); ++iter) { |
| 246 result.insert(iter->first); | 238 result.insert(iter->first); |
| 247 } | 239 } |
| 248 return result; | 240 return result; |
| 249 } | 241 } |
| 250 | 242 |
| 251 void ExtensionProcessManager::EnsureBrowserWhenRequired( | |
| 252 Browser* browser, | |
| 253 extensions::ViewType view_type) { | |
| 254 if (!browser) { | |
| 255 // A NULL browser may only be given for pop-up views and dialogs. | |
| 256 DCHECK(view_type == extensions::VIEW_TYPE_EXTENSION_POPUP || | |
| 257 view_type == extensions::VIEW_TYPE_EXTENSION_DIALOG); | |
| 258 } | |
| 259 } | |
| 260 | |
| 261 ExtensionHost* ExtensionProcessManager::CreateViewHost( | |
| 262 const Extension* extension, | |
| 263 const GURL& url, | |
| 264 Browser* browser, | |
| 265 extensions::ViewType view_type) { | |
| 266 DVLOG(1) << "CreateViewHost"; | |
| 267 DCHECK(extension); | |
| 268 EnsureBrowserWhenRequired(browser, view_type); | |
| 269 ExtensionHost* host = | |
| 270 #if defined(OS_MACOSX) | |
| 271 new extensions::ExtensionHostMac( | |
| 272 extension, GetSiteInstanceForURL(url), url, view_type); | |
| 273 #else | |
| 274 new ExtensionHost(extension, GetSiteInstanceForURL(url), url, view_type); | |
| 275 #endif | |
| 276 host->CreateView(browser); | |
| 277 OnExtensionHostCreated(host, false); | |
| 278 return host; | |
| 279 } | |
| 280 | |
| 281 ExtensionHost* ExtensionProcessManager::CreateViewHost( | |
| 282 const GURL& url, Browser* browser, extensions::ViewType view_type) { | |
| 283 EnsureBrowserWhenRequired(browser, view_type); | |
| 284 ExtensionService* service = ExtensionSystem::GetForBrowserContext( | |
| 285 GetBrowserContext())->extension_service(); | |
| 286 if (service) { | |
| 287 std::string extension_id = url.host(); | |
| 288 if (url.SchemeIs(chrome::kChromeUIScheme) && | |
| 289 url.host() == chrome::kChromeUIExtensionInfoHost) | |
| 290 extension_id = url.path().substr(1); | |
| 291 const Extension* extension = | |
| 292 service->extensions()->GetByID(extension_id); | |
| 293 if (extension) | |
| 294 return CreateViewHost(extension, url, browser, view_type); | |
| 295 } | |
| 296 return NULL; | |
| 297 } | |
| 298 | |
| 299 ExtensionHost* ExtensionProcessManager::CreatePopupHost( | |
| 300 const Extension* extension, const GURL& url, Browser* browser) { | |
| 301 return CreateViewHost( | |
| 302 extension, url, browser, extensions::VIEW_TYPE_EXTENSION_POPUP); | |
| 303 } | |
| 304 | |
| 305 ExtensionHost* ExtensionProcessManager::CreatePopupHost( | |
| 306 const GURL& url, Browser* browser) { | |
| 307 return CreateViewHost(url, browser, extensions::VIEW_TYPE_EXTENSION_POPUP); | |
| 308 } | |
| 309 | |
| 310 ExtensionHost* ExtensionProcessManager::CreateDialogHost(const GURL& url) { | |
| 311 return CreateViewHost(url, NULL, extensions::VIEW_TYPE_EXTENSION_DIALOG); | |
| 312 } | |
| 313 | |
| 314 ExtensionHost* ExtensionProcessManager::CreateInfobarHost( | |
| 315 const Extension* extension, const GURL& url, Browser* browser) { | |
| 316 return CreateViewHost( | |
| 317 extension, url, browser, extensions::VIEW_TYPE_EXTENSION_INFOBAR); | |
| 318 } | |
| 319 | |
| 320 ExtensionHost* ExtensionProcessManager::CreateInfobarHost( | |
| 321 const GURL& url, Browser* browser) { | |
| 322 return CreateViewHost(url, browser, extensions::VIEW_TYPE_EXTENSION_INFOBAR); | |
| 323 } | |
| 324 | |
| 325 ExtensionHost* ExtensionProcessManager::CreateBackgroundHost( | 243 ExtensionHost* ExtensionProcessManager::CreateBackgroundHost( |
| 326 const Extension* extension, const GURL& url) { | 244 const Extension* extension, const GURL& url) { |
| 327 DVLOG(1) << "CreateBackgroundHost " << url.spec(); | 245 DVLOG(1) << "CreateBackgroundHost " << url.spec(); |
| 328 // Hosted apps are taken care of from BackgroundContentsService. Ignore them | 246 // Hosted apps are taken care of from BackgroundContentsService. Ignore them |
| 329 // here. | 247 // here. |
| 330 if (extension->is_hosted_app()) | 248 if (extension->is_hosted_app()) |
| 331 return NULL; | 249 return NULL; |
| 332 | 250 |
| 333 // Don't create multiple background hosts for an extension. | 251 // Don't create multiple background hosts for an extension. |
| 334 if (ExtensionHost* host = GetBackgroundHostForExtension(extension->id())) | 252 if (ExtensionHost* host = GetBackgroundHostForExtension(extension->id())) |
| 335 return host; // TODO(kalman): return NULL here? It might break things... | 253 return host; // TODO(kalman): return NULL here? It might break things... |
| 336 | 254 |
| 337 ExtensionHost* host = | 255 ExtensionHost* host = |
| 338 #if defined(OS_MACOSX) | 256 #if defined(OS_MACOSX) |
| 339 new extensions::ExtensionHostMac( | 257 new extensions::ExtensionHostMac( |
| 340 extension, GetSiteInstanceForURL(url), url, | 258 extension, GetSiteInstanceForURL(url), url, |
| 341 extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); | 259 extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); |
| 342 #else | 260 #else |
| 343 new ExtensionHost(extension, GetSiteInstanceForURL(url), url, | 261 new ExtensionHost(extension, GetSiteInstanceForURL(url), url, |
| 344 extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); | 262 extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); |
| 345 #endif | 263 #endif |
| 346 | 264 |
| 347 host->CreateRenderViewSoon(); | 265 host->CreateRenderViewSoon(); |
| 348 OnExtensionHostCreated(host, true); | 266 OnBackgroundHostCreated(host); |
| 349 return host; | 267 return host; |
| 350 } | 268 } |
| 351 | 269 |
| 352 ExtensionHost* ExtensionProcessManager::GetBackgroundHostForExtension( | 270 ExtensionHost* ExtensionProcessManager::GetBackgroundHostForExtension( |
| 353 const std::string& extension_id) { | 271 const std::string& extension_id) { |
| 354 for (ExtensionHostSet::iterator iter = background_hosts_.begin(); | 272 for (ExtensionHostSet::iterator iter = background_hosts_.begin(); |
| 355 iter != background_hosts_.end(); ++iter) { | 273 iter != background_hosts_.end(); ++iter) { |
| 356 ExtensionHost* host = *iter; | 274 ExtensionHost* host = *iter; |
| 357 if (host->extension_id() == extension_id) | 275 if (host->extension_id() == extension_id) |
| 358 return host; | 276 return host; |
| (...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 785 if (registrar_.IsRegistered( | 703 if (registrar_.IsRegistered( |
| 786 this, | 704 this, |
| 787 chrome::NOTIFICATION_EXTENSIONS_READY, | 705 chrome::NOTIFICATION_EXTENSIONS_READY, |
| 788 content::Source<BrowserContext>(original_context))) { | 706 content::Source<BrowserContext>(original_context))) { |
| 789 registrar_.Remove(this, | 707 registrar_.Remove(this, |
| 790 chrome::NOTIFICATION_EXTENSIONS_READY, | 708 chrome::NOTIFICATION_EXTENSIONS_READY, |
| 791 content::Source<BrowserContext>(original_context)); | 709 content::Source<BrowserContext>(original_context)); |
| 792 } | 710 } |
| 793 } | 711 } |
| 794 | 712 |
| 795 void ExtensionProcessManager::OnExtensionHostCreated(ExtensionHost* host, | 713 void ExtensionProcessManager::OnBackgroundHostCreated(ExtensionHost* host) { |
| 796 bool is_background) { | 714 DCHECK_EQ(GetBrowserContext(), host->browser_context()); |
| 797 DCHECK_EQ(site_instance_->GetBrowserContext(), host->browser_context()); | 715 background_hosts_.insert(host); |
| 798 if (is_background) { | |
| 799 background_hosts_.insert(host); | |
| 800 | 716 |
| 801 if (BackgroundInfo::HasLazyBackgroundPage(host->extension())) { | 717 if (BackgroundInfo::HasLazyBackgroundPage(host->extension())) { |
| 802 linked_ptr<base::ElapsedTimer> since_suspended( | 718 linked_ptr<base::ElapsedTimer> since_suspended( |
| 803 background_page_data_[host->extension()->id()]. | 719 background_page_data_[host->extension()->id()]. |
| 804 since_suspended.release()); | 720 since_suspended.release()); |
| 805 if (since_suspended.get()) { | 721 if (since_suspended.get()) { |
| 806 UMA_HISTOGRAM_LONG_TIMES("Extensions.EventPageIdleTime", | 722 UMA_HISTOGRAM_LONG_TIMES("Extensions.EventPageIdleTime", |
| 807 since_suspended->Elapsed()); | 723 since_suspended->Elapsed()); |
| 808 } | |
| 809 } | 724 } |
| 810 } | 725 } |
| 811 } | 726 } |
| 812 | 727 |
| 813 void ExtensionProcessManager::CloseBackgroundHost(ExtensionHost* host) { | 728 void ExtensionProcessManager::CloseBackgroundHost(ExtensionHost* host) { |
| 814 CHECK(host->extension_host_type() == | 729 CHECK(host->extension_host_type() == |
| 815 extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); | 730 extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); |
| 816 delete host; | 731 delete host; |
| 817 // |host| should deregister itself from our structures. | 732 // |host| should deregister itself from our structures. |
| 818 CHECK(background_hosts_.find(host) == background_hosts_.end()); | 733 CHECK(background_hosts_.find(host) == background_hosts_.end()); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 895 | 810 |
| 896 IncognitoExtensionProcessManager::~IncognitoExtensionProcessManager() { | 811 IncognitoExtensionProcessManager::~IncognitoExtensionProcessManager() { |
| 897 // TODO(yoz): This cleanup code belongs in the MenuManager. | 812 // TODO(yoz): This cleanup code belongs in the MenuManager. |
| 898 // Remove "incognito" "split" mode context menu items. | 813 // Remove "incognito" "split" mode context menu items. |
| 899 ExtensionService* service = ExtensionSystem::GetForBrowserContext( | 814 ExtensionService* service = ExtensionSystem::GetForBrowserContext( |
| 900 GetBrowserContext())->extension_service(); | 815 GetBrowserContext())->extension_service(); |
| 901 if (service) | 816 if (service) |
| 902 service->menu_manager()->RemoveAllIncognitoContextItems(); | 817 service->menu_manager()->RemoveAllIncognitoContextItems(); |
| 903 } | 818 } |
| 904 | 819 |
| 905 ExtensionHost* IncognitoExtensionProcessManager::CreateViewHost( | |
| 906 const Extension* extension, | |
| 907 const GURL& url, | |
| 908 Browser* browser, | |
| 909 extensions::ViewType view_type) { | |
| 910 if (extensions::IncognitoInfo::IsSplitMode(extension)) { | |
| 911 if (IsIncognitoEnabled(extension)) { | |
| 912 return ExtensionProcessManager::CreateViewHost(extension, url, | |
| 913 browser, view_type); | |
| 914 } else { | |
| 915 NOTREACHED() << | |
| 916 "We shouldn't be trying to create an incognito extension view unless " | |
| 917 "it has been enabled for incognito."; | |
| 918 return NULL; | |
| 919 } | |
| 920 } else { | |
| 921 return original_manager_->CreateViewHost(extension, url, | |
| 922 browser, view_type); | |
| 923 } | |
| 924 } | |
| 925 | |
| 926 ExtensionHost* IncognitoExtensionProcessManager::CreateBackgroundHost( | 820 ExtensionHost* IncognitoExtensionProcessManager::CreateBackgroundHost( |
| 927 const Extension* extension, const GURL& url) { | 821 const Extension* extension, const GURL& url) { |
| 928 if (extensions::IncognitoInfo::IsSplitMode(extension)) { | 822 if (extensions::IncognitoInfo::IsSplitMode(extension)) { |
| 929 if (IsIncognitoEnabled(extension)) | 823 if (IsIncognitoEnabled(extension)) |
| 930 return ExtensionProcessManager::CreateBackgroundHost(extension, url); | 824 return ExtensionProcessManager::CreateBackgroundHost(extension, url); |
| 931 } else { | 825 } else { |
| 932 // Do nothing. If an extension is spanning, then its original-profile | 826 // Do nothing. If an extension is spanning, then its original-profile |
| 933 // background page is shared with incognito, so we don't create another. | 827 // background page is shared with incognito, so we don't create another. |
| 934 } | 828 } |
| 935 return NULL; | 829 return NULL; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 950 return ExtensionProcessManager::GetSiteInstanceForURL(url); | 844 return ExtensionProcessManager::GetSiteInstanceForURL(url); |
| 951 } | 845 } |
| 952 | 846 |
| 953 bool IncognitoExtensionProcessManager::IsIncognitoEnabled( | 847 bool IncognitoExtensionProcessManager::IsIncognitoEnabled( |
| 954 const Extension* extension) { | 848 const Extension* extension) { |
| 955 // Keep in sync with duplicate in extension_info_map.cc. | 849 // Keep in sync with duplicate in extension_info_map.cc. |
| 956 ExtensionService* service = ExtensionSystem::GetForBrowserContext( | 850 ExtensionService* service = ExtensionSystem::GetForBrowserContext( |
| 957 GetBrowserContext())->extension_service(); | 851 GetBrowserContext())->extension_service(); |
| 958 return extension_util::IsIncognitoEnabled(extension->id(), service); | 852 return extension_util::IsIncognitoEnabled(extension->id(), service); |
| 959 } | 853 } |
| OLD | NEW |