Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / extensions / extension_view_host_factory.cc
blob481aa14efc46c5633da70950ba0288d433f48efc
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"
20 #endif
22 namespace extensions {
24 namespace {
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 ExtensionViewHost* 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 ProcessManager* pm =
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);
43 #else
44 new ExtensionViewHost(extension, site_instance, url, view_type);
45 #endif
46 host->CreateView(browser);
47 return host;
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,
60 const GURL& url,
61 Profile* profile,
62 Browser* browser,
63 ViewType view_type) {
64 DCHECK(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);
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;
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);
99 // Creates and initializes an ExtensionViewHost for the extension with |url|.
100 ExtensionViewHost* 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);
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);
115 return CreateViewHostForExtension(
116 extension, url, profile, browser, view_type);
119 } // namespace
121 // static
122 ExtensionViewHost* ExtensionViewHostFactory::CreatePopupHost(const GURL& url,
123 Browser* browser) {
124 DCHECK(browser);
125 return CreateViewHost(
126 url, browser->profile(), browser, VIEW_TYPE_EXTENSION_POPUP);
129 // static
130 ExtensionViewHost* ExtensionViewHostFactory::CreateInfobarHost(
131 const GURL& url,
132 Browser* browser) {
133 DCHECK(browser);
134 return CreateViewHost(
135 url, browser->profile(), browser, VIEW_TYPE_EXTENSION_INFOBAR);
138 // static
139 ExtensionViewHost* ExtensionViewHostFactory::CreateDialogHost(
140 const GURL& url,
141 Profile* profile) {
142 DCHECK(profile);
143 return CreateViewHost(url, profile, NULL, VIEW_TYPE_EXTENSION_DIALOG);
146 } // namespace extensions