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.
5 #include "chrome/browser/extensions/extension_view_host_factory.h"
7 #include "chrome/browser/extensions/extension_service.h"
8 #include "chrome/browser/extensions/extension_util.h"
9 #include "chrome/browser/extensions/extension_view_host.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/common/url_constants.h"
13 #include "extensions/browser/extension_system.h"
14 #include "extensions/browser/process_manager.h"
15 #include "extensions/common/manifest_handlers/incognito_info.h"
16 #include "extensions/common/view_type.h"
18 #if defined(OS_MACOSX)
19 #include "chrome/browser/extensions/extension_view_host_mac.h"
22 namespace extensions
{
26 // Creates a new ExtensionHost with its associated view, grouping it in the
27 // appropriate SiteInstance (and therefore process) based on the URL and
29 ExtensionViewHost
* CreateViewHostForExtension(const Extension
* extension
,
35 // A NULL browser may only be given for dialogs.
36 DCHECK(browser
|| view_type
== VIEW_TYPE_EXTENSION_DIALOG
);
38 ExtensionSystem::Get(profile
)->process_manager();
39 content::SiteInstance
* site_instance
= pm
->GetSiteInstanceForURL(url
);
40 ExtensionViewHost
* host
=
41 #if defined(OS_MACOSX)
42 new ExtensionViewHostMac(extension
, site_instance
, url
, view_type
);
44 new ExtensionViewHost(extension
, site_instance
, url
, view_type
);
46 host
->CreateView(browser
);
50 // Creates a view host for an extension in an incognito window. Returns NULL
51 // if the extension is not allowed to run in incognito.
52 ExtensionViewHost
* CreateViewHostForIncognito(const Extension
* extension
,
58 DCHECK(profile
->IsOffTheRecord());
60 if (!IncognitoInfo::IsSplitMode(extension
)) {
61 // If it's not split-mode the host is associated with the original profile.
62 Profile
* original_profile
= profile
->GetOriginalProfile();
63 return CreateViewHostForExtension(
64 extension
, url
, original_profile
, browser
, view_type
);
67 // Create the host if the extension can run in incognito.
68 if (util::IsIncognitoEnabled(extension
->id(), profile
)) {
69 return CreateViewHostForExtension(
70 extension
, url
, profile
, browser
, view_type
);
73 "We shouldn't be trying to create an incognito extension view unless "
74 "it has been enabled for incognito.";
78 // Returns the extension associated with |url| in |profile|. Returns NULL if
79 // the extension does not exist.
80 const Extension
* GetExtensionForUrl(Profile
* profile
, const GURL
& url
) {
81 ExtensionService
* service
=
82 ExtensionSystem::Get(profile
)->extension_service();
85 std::string extension_id
= url
.host();
86 if (url
.SchemeIs(content::kChromeUIScheme
) &&
87 url
.host() == chrome::kChromeUIExtensionInfoHost
)
88 extension_id
= url
.path().substr(1);
89 return service
->extensions()->GetByID(extension_id
);
92 // Creates and initializes an ExtensionViewHost for the extension with |url|.
93 ExtensionViewHost
* CreateViewHost(const GURL
& url
,
96 extensions::ViewType view_type
) {
98 // A NULL browser may only be given for dialogs.
99 DCHECK(browser
|| view_type
== VIEW_TYPE_EXTENSION_DIALOG
);
101 const Extension
* extension
= GetExtensionForUrl(profile
, url
);
104 if (profile
->IsOffTheRecord()) {
105 return CreateViewHostForIncognito(
106 extension
, url
, profile
, browser
, view_type
);
108 return CreateViewHostForExtension(
109 extension
, url
, profile
, browser
, view_type
);
115 ExtensionViewHost
* ExtensionViewHostFactory::CreatePopupHost(const GURL
& url
,
118 return CreateViewHost(
119 url
, browser
->profile(), browser
, VIEW_TYPE_EXTENSION_POPUP
);
123 ExtensionViewHost
* ExtensionViewHostFactory::CreateInfobarHost(
127 return CreateViewHost(
128 url
, browser
->profile(), browser
, VIEW_TYPE_EXTENSION_INFOBAR
);
132 ExtensionViewHost
* ExtensionViewHostFactory::CreateDialogHost(
136 return CreateViewHost(url
, profile
, NULL
, VIEW_TYPE_EXTENSION_DIALOG
);
139 } // namespace extensions