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_util.h"
8 #include "chrome/browser/extensions/extension_view_host.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/common/url_constants.h"
12 #include "extensions/browser/extension_registry.h"
13 #include "extensions/browser/process_manager.h"
14 #include "extensions/common/manifest_handlers/incognito_info.h"
15 #include "extensions/common/view_type.h"
17 namespace extensions
{
21 // Creates a new ExtensionHost with its associated view, grouping it in the
22 // appropriate SiteInstance (and therefore process) based on the URL and
24 ExtensionViewHost
* CreateViewHostForExtension(const Extension
* extension
,
30 // A NULL browser may only be given for dialogs.
31 DCHECK(browser
|| view_type
== VIEW_TYPE_EXTENSION_DIALOG
);
32 scoped_refptr
<content::SiteInstance
> site_instance
=
33 ProcessManager::Get(profile
)->GetSiteInstanceForURL(url
);
34 ExtensionViewHost
* host
=
35 new ExtensionViewHost(extension
, site_instance
.get(), url
, view_type
);
36 host
->CreateView(browser
);
40 // Creates a view host for an extension in an incognito window. Returns NULL
41 // if the extension is not allowed to run in incognito.
42 ExtensionViewHost
* CreateViewHostForIncognito(const Extension
* extension
,
48 DCHECK(profile
->IsOffTheRecord());
50 if (!IncognitoInfo::IsSplitMode(extension
)) {
51 // If it's not split-mode the host is associated with the original profile.
52 Profile
* original_profile
= profile
->GetOriginalProfile();
53 return CreateViewHostForExtension(
54 extension
, url
, original_profile
, browser
, view_type
);
57 // Create the host if the extension can run in incognito.
58 if (util::IsIncognitoEnabled(extension
->id(), profile
)) {
59 return CreateViewHostForExtension(
60 extension
, url
, profile
, browser
, view_type
);
63 "We shouldn't be trying to create an incognito extension view unless "
64 "it has been enabled for incognito.";
68 // Returns the extension associated with |url| in |profile|. Returns NULL if
69 // the extension does not exist.
70 const Extension
* GetExtensionForUrl(Profile
* profile
, const GURL
& url
) {
71 ExtensionRegistry
* registry
= ExtensionRegistry::Get(profile
);
74 std::string extension_id
= url
.host();
75 return registry
->enabled_extensions().GetByID(extension_id
);
78 // Creates and initializes an ExtensionViewHost for the extension with |url|.
79 ExtensionViewHost
* CreateViewHost(const GURL
& url
,
82 extensions::ViewType view_type
) {
84 // A NULL browser may only be given for dialogs.
85 DCHECK(browser
|| view_type
== VIEW_TYPE_EXTENSION_DIALOG
);
87 const Extension
* extension
= GetExtensionForUrl(profile
, url
);
90 if (profile
->IsOffTheRecord()) {
91 return CreateViewHostForIncognito(
92 extension
, url
, profile
, browser
, view_type
);
94 return CreateViewHostForExtension(
95 extension
, url
, profile
, browser
, view_type
);
101 ExtensionViewHost
* ExtensionViewHostFactory::CreatePopupHost(const GURL
& url
,
104 return CreateViewHost(
105 url
, browser
->profile(), browser
, VIEW_TYPE_EXTENSION_POPUP
);
109 ExtensionViewHost
* ExtensionViewHostFactory::CreateDialogHost(
113 return CreateViewHost(url
, profile
, NULL
, VIEW_TYPE_EXTENSION_DIALOG
);
116 } // namespace extensions