Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: chrome/browser/extensions/extension_host_factory.cc

Issue 57923009: Move ExtensionProcessManager to src/extensions, part 3 - remove Browser (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add test Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/extensions/incognito_handler.h"
15 #include "chrome/common/url_constants.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 pm->OnExtensionHostCreated(host, false);
48 return host;
49 }
50
51 // Return true if this extension can run in an incognito window.
52 bool IsIncognitoEnabled(Profile* profile, const Extension* extension) {
53 ExtensionService* service =
54 ExtensionSystem::Get(profile)->extension_service();
55 return extension_util::IsIncognitoEnabled(extension->id(), service);
56 }
57
58 // Creates a view host for an extension in an incognito window. Returns NULL
59 // if the extension is not allowed to run in incognito.
60 ExtensionHost* CreateViewHostForIncognito(const Extension* extension,
61 const GURL& url,
62 Profile* profile,
63 Browser* browser,
64 ViewType view_type) {
65 DCHECK(extension);
66 DCHECK(profile->IsOffTheRecord());
67
68 if (!IncognitoInfo::IsSplitMode(extension)) {
69 // If it's not split-mode the host is associated with the original profile.
70 Profile* original_profile = profile->GetOriginalProfile();
71 return CreateViewHostForExtension(
James Cook 2013/11/05 22:52:28 This is the behavior of the original code, but I'm
72 extension, url, original_profile, browser, view_type);
73 }
74
75 // Create the host if the extension can run in incognito.
76 if (IsIncognitoEnabled(profile, extension)) {
77 return CreateViewHostForExtension(
78 extension, url, profile, browser, view_type);
79 }
80 NOTREACHED() <<
81 "We shouldn't be trying to create an incognito extension view unless "
82 "it has been enabled for incognito.";
83 return NULL;
84 }
85
86 // Returns the extension associated with |url| in |profile|. Returns NULL if
87 // the extension does not exist.
88 const Extension* GetExtensionForUrl(Profile* profile, const GURL& url) {
89 ExtensionService* service =
90 ExtensionSystem::Get(profile)->extension_service();
91 if (!service)
92 return NULL;
93 std::string extension_id = url.host();
94 if (url.SchemeIs(chrome::kChromeUIScheme) &&
95 url.host() == chrome::kChromeUIExtensionInfoHost)
96 extension_id = url.path().substr(1);
97 return service->extensions()->GetByID(extension_id);
98 }
99
100 // Creates and initializes an ExtensionHost for the extension with |url|.
101 ExtensionHost* CreateViewHost(const GURL& url,
102 Profile* profile,
103 Browser* browser,
104 extensions::ViewType view_type) {
105 DCHECK(profile);
106 // A NULL browser may only be given for dialogs.
107 DCHECK(browser || view_type == VIEW_TYPE_EXTENSION_DIALOG);
108
109 const Extension* extension = GetExtensionForUrl(profile, url);
110 if (!extension)
111 return NULL;
112 if (profile->IsOffTheRecord()) {
113 return CreateViewHostForIncognito(
114 extension, url, profile, browser, view_type);
115 }
116 return CreateViewHostForExtension(
117 extension, url, profile, browser, view_type);
118 }
119
120 } // namespace
121
122 // static
123 ExtensionHost* ExtensionHostFactory::CreatePopupHost(const GURL& url,
124 Browser* browser) {
125 DCHECK(browser);
126 return CreateViewHost(
127 url, browser->profile(), browser, VIEW_TYPE_EXTENSION_POPUP);
128 }
129
130 // static
131 ExtensionHost* ExtensionHostFactory::CreateInfobarHost(const GURL& url,
132 Browser* browser) {
133 DCHECK(browser);
134 return CreateViewHost(
135 url, browser->profile(), browser, VIEW_TYPE_EXTENSION_INFOBAR);
136 }
137
138 // static
139 ExtensionHost* ExtensionHostFactory::CreateDialogHost(const GURL& url,
140 Profile* profile) {
141 DCHECK(profile);
142 return CreateViewHost(url, profile, NULL, VIEW_TYPE_EXTENSION_DIALOG);
143 }
144
145 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_host_factory.h ('k') | chrome/browser/extensions/extension_host_factory_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698