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/chrome_extensions_browser_client.h"
7 #include "apps/common/api/generated_api.h"
8 #include "base/command_line.h"
9 #include "base/version.h"
10 #include "chrome/browser/app_mode/app_mode_utils.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/extensions/activity_log/activity_log.h"
13 #include "chrome/browser/extensions/api/preference/chrome_direct_setting.h"
14 #include "chrome/browser/extensions/api/preference/preference_api.h"
15 #include "chrome/browser/extensions/api/web_request/web_request_api.h"
16 #include "chrome/browser/extensions/chrome_app_sorting.h"
17 #include "chrome/browser/extensions/chrome_extension_host_delegate.h"
18 #include "chrome/browser/extensions/extension_system_factory.h"
19 #include "chrome/browser/extensions/extension_util.h"
20 #include "chrome/browser/extensions/url_request_util.h"
21 #include "chrome/browser/external_protocol/external_protocol_handler.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/profiles/profile_manager.h"
24 #include "chrome/browser/ui/browser_finder.h"
25 #include "chrome/common/chrome_switches.h"
26 #include "chrome/common/chrome_version_info.h"
27 #include "chrome/common/extensions/api/generated_api.h"
28 #include "chrome/common/extensions/features/feature_channel.h"
29 #include "chrome/common/pref_names.h"
30 #include "extensions/browser/extension_function_registry.h"
31 #include "extensions/browser/extension_prefs.h"
32 #include "extensions/browser/pref_names.h"
33 #include "extensions/common/api/generated_api.h"
35 #if defined(OS_CHROMEOS)
36 #include "chromeos/chromeos_switches.h"
39 #if defined(ENABLE_EXTENSIONS)
40 #include "chrome/browser/extensions/api/chrome_extensions_api_client.h"
41 #include "chrome/browser/extensions/api/content_settings/content_settings_service.h"
44 namespace extensions
{
46 ChromeExtensionsBrowserClient::ChromeExtensionsBrowserClient() {
47 #if defined(ENABLE_EXTENSIONS)
48 api_client_
.reset(new ChromeExtensionsAPIClient
);
50 // Only set if it hasn't already been set (e.g. by a test).
51 if (GetCurrentChannel() == GetDefaultChannel())
52 SetCurrentChannel(chrome::VersionInfo::GetChannel());
55 ChromeExtensionsBrowserClient::~ChromeExtensionsBrowserClient() {}
57 bool ChromeExtensionsBrowserClient::IsShuttingDown() {
58 return g_browser_process
->IsShuttingDown();
61 bool ChromeExtensionsBrowserClient::AreExtensionsDisabled(
62 const CommandLine
& command_line
,
63 content::BrowserContext
* context
) {
64 Profile
* profile
= static_cast<Profile
*>(context
);
65 return command_line
.HasSwitch(switches::kDisableExtensions
) ||
66 profile
->GetPrefs()->GetBoolean(prefs::kDisableExtensions
);
69 bool ChromeExtensionsBrowserClient::IsValidContext(
70 content::BrowserContext
* context
) {
71 Profile
* profile
= static_cast<Profile
*>(context
);
72 return g_browser_process
->profile_manager()->IsValidProfile(profile
);
75 bool ChromeExtensionsBrowserClient::IsSameContext(
76 content::BrowserContext
* first
,
77 content::BrowserContext
* second
) {
78 return static_cast<Profile
*>(first
)->IsSameProfile(
79 static_cast<Profile
*>(second
));
82 bool ChromeExtensionsBrowserClient::HasOffTheRecordContext(
83 content::BrowserContext
* context
) {
84 return static_cast<Profile
*>(context
)->HasOffTheRecordProfile();
87 content::BrowserContext
* ChromeExtensionsBrowserClient::GetOffTheRecordContext(
88 content::BrowserContext
* context
) {
89 return static_cast<Profile
*>(context
)->GetOffTheRecordProfile();
92 content::BrowserContext
* ChromeExtensionsBrowserClient::GetOriginalContext(
93 content::BrowserContext
* context
) {
94 return static_cast<Profile
*>(context
)->GetOriginalProfile();
97 bool ChromeExtensionsBrowserClient::IsGuestSession(
98 content::BrowserContext
* context
) const {
99 return static_cast<Profile
*>(context
)->IsGuestSession();
102 bool ChromeExtensionsBrowserClient::IsExtensionIncognitoEnabled(
103 const std::string
& extension_id
,
104 content::BrowserContext
* context
) const {
105 return IsGuestSession(context
)
106 || util::IsIncognitoEnabled(extension_id
, context
);
109 bool ChromeExtensionsBrowserClient::CanExtensionCrossIncognito(
110 const extensions::Extension
* extension
,
111 content::BrowserContext
* context
) const {
112 return IsGuestSession(context
)
113 || util::CanCrossIncognito(extension
, context
);
116 bool ChromeExtensionsBrowserClient::IsWebViewRequest(
117 net::URLRequest
* request
) const {
118 return url_request_util::IsWebViewRequest(request
);
122 ChromeExtensionsBrowserClient::MaybeCreateResourceBundleRequestJob(
123 net::URLRequest
* request
,
124 net::NetworkDelegate
* network_delegate
,
125 const base::FilePath
& directory_path
,
126 const std::string
& content_security_policy
,
127 bool send_cors_header
) {
128 return url_request_util::MaybeCreateURLRequestResourceBundleJob(
132 content_security_policy
,
136 bool ChromeExtensionsBrowserClient::AllowCrossRendererResourceLoad(
137 net::URLRequest
* request
,
139 const Extension
* extension
,
140 InfoMap
* extension_info_map
) {
141 return url_request_util::AllowCrossRendererResourceLoad(
142 request
, is_incognito
, extension
, extension_info_map
);
145 PrefService
* ChromeExtensionsBrowserClient::GetPrefServiceForContext(
146 content::BrowserContext
* context
) {
147 return static_cast<Profile
*>(context
)->GetPrefs();
150 void ChromeExtensionsBrowserClient::GetEarlyExtensionPrefsObservers(
151 content::BrowserContext
* context
,
152 std::vector
<ExtensionPrefsObserver
*>* observers
) const {
153 #if defined(ENABLE_EXTENSIONS)
154 observers
->push_back(ContentSettingsService::Get(context
));
158 bool ChromeExtensionsBrowserClient::DeferLoadingBackgroundHosts(
159 content::BrowserContext
* context
) const {
160 Profile
* profile
= static_cast<Profile
*>(context
);
162 // The profile may not be valid yet if it is still being initialized.
163 // In that case, defer loading, since it depends on an initialized profile.
164 // http://crbug.com/222473
165 if (!g_browser_process
->profile_manager()->IsValidProfile(profile
))
168 #if defined(OS_ANDROID)
171 // There are no browser windows open and the browser process was
172 // started to show the app launcher.
173 return chrome::GetTotalBrowserCountForProfile(profile
) == 0 &&
174 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowAppList
);
178 bool ChromeExtensionsBrowserClient::IsBackgroundPageAllowed(
179 content::BrowserContext
* context
) const {
180 // Returns true if current session is Guest mode session and current
181 // browser context is *not* off-the-record. Such context is artificial and
182 // background page shouldn't be created in it.
183 return !static_cast<Profile
*>(context
)->IsGuestSession() ||
184 context
->IsOffTheRecord();
187 scoped_ptr
<ExtensionHostDelegate
>
188 ChromeExtensionsBrowserClient::CreateExtensionHostDelegate() {
189 return scoped_ptr
<ExtensionHostDelegate
>(new ChromeExtensionHostDelegate
);
192 bool ChromeExtensionsBrowserClient::DidVersionUpdate(
193 content::BrowserContext
* context
) {
194 Profile
* profile
= static_cast<Profile
*>(context
);
196 // Unit tests may not provide prefs; assume everything is up-to-date.
197 ExtensionPrefs
* extension_prefs
= ExtensionPrefs::Get(profile
);
198 if (!extension_prefs
)
201 // If we're inside a browser test, then assume prefs are all up-to-date.
202 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType
))
205 PrefService
* pref_service
= extension_prefs
->pref_service();
206 base::Version last_version
;
207 if (pref_service
->HasPrefPath(pref_names::kLastChromeVersion
)) {
208 std::string last_version_str
=
209 pref_service
->GetString(pref_names::kLastChromeVersion
);
210 last_version
= base::Version(last_version_str
);
213 chrome::VersionInfo current_version_info
;
214 std::string current_version
= current_version_info
.Version();
215 pref_service
->SetString(pref_names::kLastChromeVersion
,
218 // If there was no version string in prefs, assume we're out of date.
219 if (!last_version
.IsValid())
222 return last_version
.IsOlderThan(current_version
);
225 scoped_ptr
<AppSorting
> ChromeExtensionsBrowserClient::CreateAppSorting() {
226 return scoped_ptr
<AppSorting
>(new ChromeAppSorting());
229 bool ChromeExtensionsBrowserClient::IsRunningInForcedAppMode() {
230 return chrome::IsRunningInForcedAppMode();
233 ApiActivityMonitor
* ChromeExtensionsBrowserClient::GetApiActivityMonitor(
234 content::BrowserContext
* context
) {
235 // The ActivityLog monitors and records function calls and events.
236 return ActivityLog::GetInstance(context
);
239 ExtensionSystemProvider
*
240 ChromeExtensionsBrowserClient::GetExtensionSystemFactory() {
241 return ExtensionSystemFactory::GetInstance();
244 void ChromeExtensionsBrowserClient::RegisterExtensionFunctions(
245 ExtensionFunctionRegistry
* registry
) const {
246 // TODO(rockot): Figure out if and why Android really needs to build
247 // ChromeExtensionsBrowserClient and refactor so this ifdef isn't necessary.
248 // See http://crbug.com/349436
249 #if defined(ENABLE_EXTENSIONS)
251 registry
->RegisterFunction
<extensions::GetPreferenceFunction
>();
252 registry
->RegisterFunction
<extensions::SetPreferenceFunction
>();
253 registry
->RegisterFunction
<extensions::ClearPreferenceFunction
>();
255 // Direct Preference Access for Component Extensions.
256 registry
->RegisterFunction
<
257 extensions::chromedirectsetting::GetDirectSettingFunction
>();
258 registry
->RegisterFunction
<
259 extensions::chromedirectsetting::SetDirectSettingFunction
>();
260 registry
->RegisterFunction
<
261 extensions::chromedirectsetting::ClearDirectSettingFunction
>();
263 // Generated APIs from lower-level modules.
264 extensions::core_api::GeneratedFunctionRegistry::RegisterAll(registry
);
265 apps::api::GeneratedFunctionRegistry::RegisterAll(registry
);
267 // Generated APIs from Chrome.
268 extensions::api::GeneratedFunctionRegistry::RegisterAll(registry
);
272 } // namespace extensions