| 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 "chrome/browser/extensions/extension_process_manager.h" | 7 #include "chrome/browser/extensions/extension_process_manager.h" |
| 8 | 8 |
| 9 #include "chrome/browser/ui/browser_window.h" | 9 #include "chrome/browser/ui/browser_window.h" |
| 10 #include "content/browser/browsing_instance.h" | |
| 11 #include "chrome/browser/extensions/extension_event_router.h" | 10 #include "chrome/browser/extensions/extension_event_router.h" |
| 12 #if defined(OS_MACOSX) | 11 #if defined(OS_MACOSX) |
| 13 #include "chrome/browser/extensions/extension_host_mac.h" | 12 #include "chrome/browser/extensions/extension_host_mac.h" |
| 14 #endif | 13 #endif |
| 15 #include "chrome/browser/extensions/extension_host.h" | 14 #include "chrome/browser/extensions/extension_host.h" |
| 16 #include "chrome/browser/extensions/extension_info_map.h" | 15 #include "chrome/browser/extensions/extension_info_map.h" |
| 17 #include "chrome/browser/extensions/extension_service.h" | 16 #include "chrome/browser/extensions/extension_service.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
| 20 #include "chrome/common/chrome_notification_types.h" | 19 #include "chrome/common/chrome_notification_types.h" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 // | 88 // |
| 90 | 89 |
| 91 // static | 90 // static |
| 92 ExtensionProcessManager* ExtensionProcessManager::Create(Profile* profile) { | 91 ExtensionProcessManager* ExtensionProcessManager::Create(Profile* profile) { |
| 93 return (profile->IsOffTheRecord()) ? | 92 return (profile->IsOffTheRecord()) ? |
| 94 new IncognitoExtensionProcessManager(profile) : | 93 new IncognitoExtensionProcessManager(profile) : |
| 95 new ExtensionProcessManager(profile); | 94 new ExtensionProcessManager(profile); |
| 96 } | 95 } |
| 97 | 96 |
| 98 ExtensionProcessManager::ExtensionProcessManager(Profile* profile) | 97 ExtensionProcessManager::ExtensionProcessManager(Profile* profile) |
| 99 : browsing_instance_(new BrowsingInstance(profile)) { | 98 : site_instance_(SiteInstance::CreateSiteInstance(profile)) { |
| 100 Profile* original_profile = profile->GetOriginalProfile(); | 99 Profile* original_profile = profile->GetOriginalProfile(); |
| 101 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY, | 100 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY, |
| 102 content::Source<Profile>(original_profile)); | 101 content::Source<Profile>(original_profile)); |
| 103 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 102 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 104 content::Source<Profile>(original_profile)); | 103 content::Source<Profile>(original_profile)); |
| 105 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 104 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 106 content::Source<Profile>(original_profile)); | 105 content::Source<Profile>(original_profile)); |
| 107 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 106 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 108 content::Source<Profile>(profile)); | 107 content::Source<Profile>(profile)); |
| 109 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, | 108 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 host->CreateView(browser); | 148 host->CreateView(browser); |
| 150 OnExtensionHostCreated(host, false); | 149 OnExtensionHostCreated(host, false); |
| 151 return host; | 150 return host; |
| 152 } | 151 } |
| 153 | 152 |
| 154 ExtensionHost* ExtensionProcessManager::CreateViewHost( | 153 ExtensionHost* ExtensionProcessManager::CreateViewHost( |
| 155 const GURL& url, Browser* browser, content::ViewType view_type) { | 154 const GURL& url, Browser* browser, content::ViewType view_type) { |
| 156 // A NULL browser may only be given for pop-up views. | 155 // A NULL browser may only be given for pop-up views. |
| 157 DCHECK(browser || | 156 DCHECK(browser || |
| 158 (!browser && view_type == chrome::VIEW_TYPE_EXTENSION_POPUP)); | 157 (!browser && view_type == chrome::VIEW_TYPE_EXTENSION_POPUP)); |
| 159 Profile* profile = | 158 ExtensionService* service = GetProfile()->GetExtensionService(); |
| 160 Profile::FromBrowserContext(browsing_instance_->browser_context()); | |
| 161 ExtensionService* service = profile->GetExtensionService(); | |
| 162 if (service) { | 159 if (service) { |
| 163 const Extension* extension = | 160 const Extension* extension = |
| 164 service->extensions()->GetByID(url.host()); | 161 service->extensions()->GetByID(url.host()); |
| 165 if (extension) | 162 if (extension) |
| 166 return CreateViewHost(extension, url, browser, view_type); | 163 return CreateViewHost(extension, url, browser, view_type); |
| 167 } | 164 } |
| 168 return NULL; | 165 return NULL; |
| 169 } | 166 } |
| 170 | 167 |
| 171 ExtensionHost* ExtensionProcessManager::CreatePopupHost( | 168 ExtensionHost* ExtensionProcessManager::CreatePopupHost( |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 OnExtensionHostCreated(host, true); | 216 OnExtensionHostCreated(host, true); |
| 220 } | 217 } |
| 221 | 218 |
| 222 void ExtensionProcessManager::OpenOptionsPage(const Extension* extension, | 219 void ExtensionProcessManager::OpenOptionsPage(const Extension* extension, |
| 223 Browser* browser) { | 220 Browser* browser) { |
| 224 DCHECK(!extension->options_url().is_empty()); | 221 DCHECK(!extension->options_url().is_empty()); |
| 225 | 222 |
| 226 // Force the options page to open in non-OTR window, because it won't be | 223 // Force the options page to open in non-OTR window, because it won't be |
| 227 // able to save settings from OTR. | 224 // able to save settings from OTR. |
| 228 if (!browser || browser->profile()->IsOffTheRecord()) { | 225 if (!browser || browser->profile()->IsOffTheRecord()) { |
| 229 Profile* profile = | 226 Profile* profile = GetProfile(); |
| 230 Profile::FromBrowserContext(browsing_instance_->browser_context()); | |
| 231 browser = Browser::GetOrCreateTabbedBrowser(profile->GetOriginalProfile()); | 227 browser = Browser::GetOrCreateTabbedBrowser(profile->GetOriginalProfile()); |
| 232 } | 228 } |
| 233 | 229 |
| 234 OpenURLParams params(extension->options_url(), Referrer(), SINGLETON_TAB, | 230 OpenURLParams params(extension->options_url(), Referrer(), SINGLETON_TAB, |
| 235 content::PAGE_TRANSITION_LINK, false); | 231 content::PAGE_TRANSITION_LINK, false); |
| 236 browser->OpenURL(params); | 232 browser->OpenURL(params); |
| 237 browser->window()->Show(); | 233 browser->window()->Show(); |
| 238 browser->GetSelectedWebContents()->GetRenderViewHost()->delegate()-> | 234 browser->GetSelectedWebContents()->GetRenderViewHost()->delegate()-> |
| 239 Activate(); | 235 Activate(); |
| 240 } | 236 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 const Extension* extension) { | 272 const Extension* extension) { |
| 277 all_extension_views_.insert(render_view_host); | 273 all_extension_views_.insert(render_view_host); |
| 278 } | 274 } |
| 279 | 275 |
| 280 void ExtensionProcessManager::UnregisterRenderViewHost( | 276 void ExtensionProcessManager::UnregisterRenderViewHost( |
| 281 RenderViewHost* render_view_host) { | 277 RenderViewHost* render_view_host) { |
| 282 all_extension_views_.erase(render_view_host); | 278 all_extension_views_.erase(render_view_host); |
| 283 } | 279 } |
| 284 | 280 |
| 285 SiteInstance* ExtensionProcessManager::GetSiteInstanceForURL(const GURL& url) { | 281 SiteInstance* ExtensionProcessManager::GetSiteInstanceForURL(const GURL& url) { |
| 286 return browsing_instance_->GetSiteInstanceForURL(url); | 282 return site_instance_->GetRelatedSiteInstance(url); |
| 287 } | 283 } |
| 288 | 284 |
| 289 bool ExtensionProcessManager::HasExtensionHost(ExtensionHost* host) const { | 285 bool ExtensionProcessManager::HasExtensionHost(ExtensionHost* host) const { |
| 290 return all_hosts_.find(host) != all_hosts_.end(); | 286 return all_hosts_.find(host) != all_hosts_.end(); |
| 291 } | 287 } |
| 292 | 288 |
| 293 void ExtensionProcessManager::OnExtensionIdle(const std::string& extension_id) { | 289 void ExtensionProcessManager::OnExtensionIdle(const std::string& extension_id) { |
| 294 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); | 290 ExtensionHost* host = GetBackgroundHostForExtension(extension_id); |
| 295 if (host && !HasVisibleViews(extension_id)) { | 291 if (host && !HasVisibleViews(extension_id)) { |
| 296 Profile* profile = | 292 Profile* profile = GetProfile(); |
| 297 Profile::FromBrowserContext(browsing_instance_->browser_context()); | |
| 298 if (!profile->GetExtensionEventRouter()->HasInFlightEvents(extension_id)) | 293 if (!profile->GetExtensionEventRouter()->HasInFlightEvents(extension_id)) |
| 299 CloseBackgroundHost(host); | 294 CloseBackgroundHost(host); |
| 300 } | 295 } |
| 301 } | 296 } |
| 302 | 297 |
| 303 bool ExtensionProcessManager::HasVisibleViews(const std::string& extension_id) { | 298 bool ExtensionProcessManager::HasVisibleViews(const std::string& extension_id) { |
| 304 const std::set<RenderViewHost*>& views = | 299 const std::set<RenderViewHost*>& views = |
| 305 GetRenderViewHostsForExtension(extension_id); | 300 GetRenderViewHostsForExtension(extension_id); |
| 306 for (std::set<RenderViewHost*>::const_iterator it = views.begin(); | 301 for (std::set<RenderViewHost*>::const_iterator it = views.begin(); |
| 307 it != views.end(); ++it) { | 302 it != views.end(); ++it) { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 // destructor is called too late in the shutdown sequence. | 369 // destructor is called too late in the shutdown sequence. |
| 375 CloseBackgroundHosts(); | 370 CloseBackgroundHosts(); |
| 376 break; | 371 break; |
| 377 } | 372 } |
| 378 | 373 |
| 379 default: | 374 default: |
| 380 NOTREACHED(); | 375 NOTREACHED(); |
| 381 } | 376 } |
| 382 } | 377 } |
| 383 | 378 |
| 379 Profile* ExtensionProcessManager::GetProfile() const { |
| 380 return Profile::FromBrowserContext(site_instance_->GetBrowserContext()); |
| 381 } |
| 382 |
| 384 void ExtensionProcessManager::OnExtensionHostCreated(ExtensionHost* host, | 383 void ExtensionProcessManager::OnExtensionHostCreated(ExtensionHost* host, |
| 385 bool is_background) { | 384 bool is_background) { |
| 386 DCHECK_EQ(browsing_instance_->browser_context(), host->profile()); | 385 DCHECK_EQ(site_instance_->GetBrowserContext(), host->profile()); |
| 387 | 386 |
| 388 all_hosts_.insert(host); | 387 all_hosts_.insert(host); |
| 389 if (is_background) | 388 if (is_background) |
| 390 background_hosts_.insert(host); | 389 background_hosts_.insert(host); |
| 391 } | 390 } |
| 392 | 391 |
| 393 void ExtensionProcessManager::CloseBackgroundHost(ExtensionHost* host) { | 392 void ExtensionProcessManager::CloseBackgroundHost(ExtensionHost* host) { |
| 394 CHECK(host->extension_host_type() == | 393 CHECK(host->extension_host_type() == |
| 395 chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); | 394 chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); |
| 396 delete host; | 395 delete host; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 if (IsIncognitoEnabled(extension)) | 447 if (IsIncognitoEnabled(extension)) |
| 449 ExtensionProcessManager::CreateBackgroundHost(extension, url); | 448 ExtensionProcessManager::CreateBackgroundHost(extension, url); |
| 450 } else { | 449 } else { |
| 451 // Do nothing. If an extension is spanning, then its original-profile | 450 // Do nothing. If an extension is spanning, then its original-profile |
| 452 // background page is shared with incognito, so we don't create another. | 451 // background page is shared with incognito, so we don't create another. |
| 453 } | 452 } |
| 454 } | 453 } |
| 455 | 454 |
| 456 SiteInstance* IncognitoExtensionProcessManager::GetSiteInstanceForURL( | 455 SiteInstance* IncognitoExtensionProcessManager::GetSiteInstanceForURL( |
| 457 const GURL& url) { | 456 const GURL& url) { |
| 458 Profile* profile = | 457 ExtensionService* service = GetProfile()->GetExtensionService(); |
| 459 Profile::FromBrowserContext(browsing_instance_->browser_context()); | |
| 460 ExtensionService* service = profile->GetExtensionService(); | |
| 461 if (service) { | 458 if (service) { |
| 462 const Extension* extension = service->extensions()->GetExtensionOrAppByURL( | 459 const Extension* extension = service->extensions()->GetExtensionOrAppByURL( |
| 463 ExtensionURLInfo(url)); | 460 ExtensionURLInfo(url)); |
| 464 if (extension && !extension->incognito_split_mode()) | 461 if (extension && !extension->incognito_split_mode()) |
| 465 return original_manager_->GetSiteInstanceForURL(url); | 462 return original_manager_->GetSiteInstanceForURL(url); |
| 466 } | 463 } |
| 467 return ExtensionProcessManager::GetSiteInstanceForURL(url); | 464 return ExtensionProcessManager::GetSiteInstanceForURL(url); |
| 468 } | 465 } |
| 469 | 466 |
| 470 bool IncognitoExtensionProcessManager::IsIncognitoEnabled( | 467 bool IncognitoExtensionProcessManager::IsIncognitoEnabled( |
| 471 const Extension* extension) { | 468 const Extension* extension) { |
| 472 // Keep in sync with duplicate in extension_info_map.cc. | 469 // Keep in sync with duplicate in extension_info_map.cc. |
| 473 Profile* profile = | 470 ExtensionService* service = GetProfile()->GetExtensionService(); |
| 474 Profile::FromBrowserContext(browsing_instance_->browser_context()); | |
| 475 ExtensionService* service = profile->GetExtensionService(); | |
| 476 return service && service->IsIncognitoEnabled(extension->id()); | 471 return service && service->IsIncognitoEnabled(extension->id()); |
| 477 } | 472 } |
| 478 | 473 |
| 479 void IncognitoExtensionProcessManager::Observe( | 474 void IncognitoExtensionProcessManager::Observe( |
| 480 int type, | 475 int type, |
| 481 const content::NotificationSource& source, | 476 const content::NotificationSource& source, |
| 482 const content::NotificationDetails& details) { | 477 const content::NotificationDetails& details) { |
| 483 switch (type) { | 478 switch (type) { |
| 484 case chrome::NOTIFICATION_BROWSER_WINDOW_READY: { | 479 case chrome::NOTIFICATION_BROWSER_WINDOW_READY: { |
| 485 if (CommandLine::ForCurrentProcess()->HasSwitch( | 480 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 486 switches::kEnableLazyBackgroundPages)) | 481 switches::kEnableLazyBackgroundPages)) |
| 487 break; | 482 break; |
| 488 // We want to spawn our background hosts as soon as the user opens an | 483 // We want to spawn our background hosts as soon as the user opens an |
| 489 // incognito window. Watch for new browsers and create the hosts if | 484 // incognito window. Watch for new browsers and create the hosts if |
| 490 // it matches our profile. | 485 // it matches our profile. |
| 491 Browser* browser = content::Source<Browser>(source).ptr(); | 486 Browser* browser = content::Source<Browser>(source).ptr(); |
| 492 if (browser->profile() == browsing_instance_->browser_context()) { | 487 if (browser->profile() == site_instance_->GetBrowserContext()) { |
| 493 // On Chrome OS, a login screen is implemented as a browser. | 488 // On Chrome OS, a login screen is implemented as a browser. |
| 494 // This browser has no extension service. In this case, | 489 // This browser has no extension service. In this case, |
| 495 // service will be NULL. | 490 // service will be NULL. |
| 496 Profile* profile = | 491 ExtensionService* service = GetProfile()->GetExtensionService(); |
| 497 Profile::FromBrowserContext(browsing_instance_->browser_context()); | |
| 498 ExtensionService* service = profile->GetExtensionService(); | |
| 499 if (service && service->is_ready()) | 492 if (service && service->is_ready()) |
| 500 CreateBackgroundHostsForProfileStartup(this, service->extensions()); | 493 CreateBackgroundHostsForProfileStartup(this, service->extensions()); |
| 501 } | 494 } |
| 502 break; | 495 break; |
| 503 } | 496 } |
| 504 default: | 497 default: |
| 505 ExtensionProcessManager::Observe(type, source, details); | 498 ExtensionProcessManager::Observe(type, source, details); |
| 506 break; | 499 break; |
| 507 } | 500 } |
| 508 } | 501 } |
| OLD | NEW |