OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/extension_host_factory.h" |
| 6 |
| 7 #include "chrome/browser/extensions/extension_host.h" |
| 8 #include "chrome/browser/extensions/extension_process_manager.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/extensions/extension_system.h" |
| 11 #include "chrome/browser/extensions/extension_util.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/common/url_constants.h" |
| 15 #include "extensions/common/manifest_handlers/incognito_info.h" |
| 16 #include "extensions/common/view_type.h" |
| 17 |
| 18 #if defined(OS_MACOSX) |
| 19 #include "chrome/browser/extensions/extension_host_mac.h" |
| 20 #endif |
| 21 |
| 22 namespace extensions { |
| 23 |
| 24 namespace { |
| 25 |
| 26 // Creates a new ExtensionHost with its associated view, grouping it in the |
| 27 // appropriate SiteInstance (and therefore process) based on the URL and |
| 28 // profile. |
| 29 ExtensionHost* CreateViewHostForExtension(const Extension* extension, |
| 30 const GURL& url, |
| 31 Profile* profile, |
| 32 Browser* browser, |
| 33 ViewType view_type) { |
| 34 DCHECK(profile); |
| 35 // A NULL browser may only be given for dialogs. |
| 36 DCHECK(browser || view_type == VIEW_TYPE_EXTENSION_DIALOG); |
| 37 ExtensionProcessManager* pm = |
| 38 ExtensionSystem::Get(profile)->process_manager(); |
| 39 content::SiteInstance* site_instance = pm->GetSiteInstanceForURL(url); |
| 40 ExtensionHost* host = |
| 41 #if defined(OS_MACOSX) |
| 42 new ExtensionHostMac(extension, site_instance, url, view_type); |
| 43 #else |
| 44 new ExtensionHost(extension, site_instance, url, view_type); |
| 45 #endif |
| 46 host->CreateView(browser); |
| 47 return host; |
| 48 } |
| 49 |
| 50 // Return true if this extension can run in an incognito window. |
| 51 bool IsIncognitoEnabled(Profile* profile, const Extension* extension) { |
| 52 ExtensionService* service = |
| 53 ExtensionSystem::Get(profile)->extension_service(); |
| 54 return extension_util::IsIncognitoEnabled(extension->id(), service); |
| 55 } |
| 56 |
| 57 // Creates a view host for an extension in an incognito window. Returns NULL |
| 58 // if the extension is not allowed to run in incognito. |
| 59 ExtensionHost* CreateViewHostForIncognito(const Extension* extension, |
| 60 const GURL& url, |
| 61 Profile* profile, |
| 62 Browser* browser, |
| 63 ViewType view_type) { |
| 64 DCHECK(extension); |
| 65 DCHECK(profile->IsOffTheRecord()); |
| 66 |
| 67 if (!IncognitoInfo::IsSplitMode(extension)) { |
| 68 // If it's not split-mode the host is associated with the original profile. |
| 69 Profile* original_profile = profile->GetOriginalProfile(); |
| 70 return CreateViewHostForExtension( |
| 71 extension, url, original_profile, browser, view_type); |
| 72 } |
| 73 |
| 74 // Create the host if the extension can run in incognito. |
| 75 if (IsIncognitoEnabled(profile, extension)) { |
| 76 return CreateViewHostForExtension( |
| 77 extension, url, profile, browser, view_type); |
| 78 } |
| 79 NOTREACHED() << |
| 80 "We shouldn't be trying to create an incognito extension view unless " |
| 81 "it has been enabled for incognito."; |
| 82 return NULL; |
| 83 } |
| 84 |
| 85 // Returns the extension associated with |url| in |profile|. Returns NULL if |
| 86 // the extension does not exist. |
| 87 const Extension* GetExtensionForUrl(Profile* profile, const GURL& url) { |
| 88 ExtensionService* service = |
| 89 ExtensionSystem::Get(profile)->extension_service(); |
| 90 if (!service) |
| 91 return NULL; |
| 92 std::string extension_id = url.host(); |
| 93 if (url.SchemeIs(chrome::kChromeUIScheme) && |
| 94 url.host() == chrome::kChromeUIExtensionInfoHost) |
| 95 extension_id = url.path().substr(1); |
| 96 return service->extensions()->GetByID(extension_id); |
| 97 } |
| 98 |
| 99 // Creates and initializes an ExtensionHost for the extension with |url|. |
| 100 ExtensionHost* CreateViewHost(const GURL& url, |
| 101 Profile* profile, |
| 102 Browser* browser, |
| 103 extensions::ViewType view_type) { |
| 104 DCHECK(profile); |
| 105 // A NULL browser may only be given for dialogs. |
| 106 DCHECK(browser || view_type == VIEW_TYPE_EXTENSION_DIALOG); |
| 107 |
| 108 const Extension* extension = GetExtensionForUrl(profile, url); |
| 109 if (!extension) |
| 110 return NULL; |
| 111 if (profile->IsOffTheRecord()) { |
| 112 return CreateViewHostForIncognito( |
| 113 extension, url, profile, browser, view_type); |
| 114 } |
| 115 return CreateViewHostForExtension( |
| 116 extension, url, profile, browser, view_type); |
| 117 } |
| 118 |
| 119 } // namespace |
| 120 |
| 121 // static |
| 122 ExtensionHost* ExtensionHostFactory::CreatePopupHost(const GURL& url, |
| 123 Browser* browser) { |
| 124 DCHECK(browser); |
| 125 return CreateViewHost( |
| 126 url, browser->profile(), browser, VIEW_TYPE_EXTENSION_POPUP); |
| 127 } |
| 128 |
| 129 // static |
| 130 ExtensionHost* ExtensionHostFactory::CreateInfobarHost(const GURL& url, |
| 131 Browser* browser) { |
| 132 DCHECK(browser); |
| 133 return CreateViewHost( |
| 134 url, browser->profile(), browser, VIEW_TYPE_EXTENSION_INFOBAR); |
| 135 } |
| 136 |
| 137 // static |
| 138 ExtensionHost* ExtensionHostFactory::CreateDialogHost(const GURL& url, |
| 139 Profile* profile) { |
| 140 DCHECK(profile); |
| 141 return CreateViewHost(url, profile, NULL, VIEW_TYPE_EXTENSION_DIALOG); |
| 142 } |
| 143 |
| 144 } // namespace extensions |
OLD | NEW |