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_system.h"
9 #include "chrome/browser/extensions/extension_util.h"
10 #include "chrome/browser/extensions/extension_view_host.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/common/url_constants.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 // 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
);
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 ExtensionViewHost
* CreateViewHostForIncognito(const Extension
* extension
,
65 DCHECK(profile
->IsOffTheRecord());
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
);
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
);
80 "We shouldn't be trying to create an incognito extension view unless "
81 "it has been enabled for incognito.";
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();
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
);
99 // Creates and initializes an ExtensionViewHost for the extension with |url|.
100 ExtensionViewHost
* CreateViewHost(const GURL
& url
,
103 extensions::ViewType view_type
) {
105 // A NULL browser may only be given for dialogs.
106 DCHECK(browser
|| view_type
== VIEW_TYPE_EXTENSION_DIALOG
);
108 const Extension
* extension
= GetExtensionForUrl(profile
, url
);
111 if (profile
->IsOffTheRecord()) {
112 return CreateViewHostForIncognito(
113 extension
, url
, profile
, browser
, view_type
);
115 return CreateViewHostForExtension(
116 extension
, url
, profile
, browser
, view_type
);
122 ExtensionViewHost
* ExtensionViewHostFactory::CreatePopupHost(const GURL
& url
,
125 return CreateViewHost(
126 url
, browser
->profile(), browser
, VIEW_TYPE_EXTENSION_POPUP
);
130 ExtensionViewHost
* ExtensionViewHostFactory::CreateInfobarHost(
134 return CreateViewHost(
135 url
, browser
->profile(), browser
, VIEW_TYPE_EXTENSION_INFOBAR
);
139 ExtensionViewHost
* ExtensionViewHostFactory::CreateDialogHost(
143 return CreateViewHost(url
, profile
, NULL
, VIEW_TYPE_EXTENSION_DIALOG
);
146 } // namespace extensions