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/runtime/chrome_runtime_api_delegate.h"
16 #include "chrome/browser/extensions/api/web_request/web_request_api.h"
17 #include "chrome/browser/extensions/chrome_app_sorting.h"
18 #include "chrome/browser/extensions/chrome_extension_host_delegate.h"
19 #include "chrome/browser/extensions/extension_system_factory.h"
20 #include "chrome/browser/extensions/extension_util.h"
21 #include "chrome/browser/extensions/url_request_util.h"
22 #include "chrome/browser/external_protocol/external_protocol_handler.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/profiles/profile_manager.h"
25 #include "chrome/browser/ui/browser_finder.h"
26 #include "chrome/common/chrome_switches.h"
27 #include "chrome/common/chrome_version_info.h"
28 #include "chrome/common/extensions/api/generated_api.h"
29 #include "chrome/common/extensions/features/feature_channel.h"
30 #include "chrome/common/pref_names.h"
31 #include "extensions/browser/extension_function_registry.h"
32 #include "extensions/browser/extension_prefs.h"
33 #include "extensions/browser/pref_names.h"
34 #include "extensions/common/api/generated_api.h"
36 #if defined(OS_CHROMEOS)
37 #include "chromeos/chromeos_switches.h"
40 #if defined(ENABLE_EXTENSIONS)
41 #include "chrome/browser/extensions/api/chrome_extensions_api_client.h"
42 #include "chrome/browser/extensions/api/content_settings/content_settings_service.h"
45 namespace extensions
{
47 ChromeExtensionsBrowserClient::ChromeExtensionsBrowserClient() {
48 #if defined(ENABLE_EXTENSIONS)
49 api_client_
.reset(new ChromeExtensionsAPIClient
);
51 // Only set if it hasn't already been set (e.g. by a test).
52 if (GetCurrentChannel() == GetDefaultChannel())
53 SetCurrentChannel(chrome::VersionInfo::GetChannel());
56 ChromeExtensionsBrowserClient::~ChromeExtensionsBrowserClient() {}
58 bool ChromeExtensionsBrowserClient::IsShuttingDown() {
59 return g_browser_process
->IsShuttingDown();
62 bool ChromeExtensionsBrowserClient::AreExtensionsDisabled(
63 const CommandLine
& command_line
,
64 content::BrowserContext
* context
) {
65 Profile
* profile
= static_cast<Profile
*>(context
);
66 return command_line
.HasSwitch(switches::kDisableExtensions
) ||
67 profile
->GetPrefs()->GetBoolean(prefs::kDisableExtensions
);
70 bool ChromeExtensionsBrowserClient::IsValidContext(
71 content::BrowserContext
* context
) {
72 Profile
* profile
= static_cast<Profile
*>(context
);
73 return g_browser_process
->profile_manager()->IsValidProfile(profile
);
76 bool ChromeExtensionsBrowserClient::IsSameContext(
77 content::BrowserContext
* first
,
78 content::BrowserContext
* second
) {
79 return static_cast<Profile
*>(first
)->IsSameProfile(
80 static_cast<Profile
*>(second
));
83 bool ChromeExtensionsBrowserClient::HasOffTheRecordContext(
84 content::BrowserContext
* context
) {
85 return static_cast<Profile
*>(context
)->HasOffTheRecordProfile();
88 content::BrowserContext
* ChromeExtensionsBrowserClient::GetOffTheRecordContext(
89 content::BrowserContext
* context
) {
90 return static_cast<Profile
*>(context
)->GetOffTheRecordProfile();
93 content::BrowserContext
* ChromeExtensionsBrowserClient::GetOriginalContext(
94 content::BrowserContext
* context
) {
95 return static_cast<Profile
*>(context
)->GetOriginalProfile();
98 bool ChromeExtensionsBrowserClient::IsGuestSession(
99 content::BrowserContext
* context
) const {
100 return static_cast<Profile
*>(context
)->IsGuestSession();
103 bool ChromeExtensionsBrowserClient::IsExtensionIncognitoEnabled(
104 const std::string
& extension_id
,
105 content::BrowserContext
* context
) const {
106 return IsGuestSession(context
)
107 || util::IsIncognitoEnabled(extension_id
, context
);
110 bool ChromeExtensionsBrowserClient::CanExtensionCrossIncognito(
111 const extensions::Extension
* extension
,
112 content::BrowserContext
* context
) const {
113 return IsGuestSession(context
)
114 || util::CanCrossIncognito(extension
, context
);
117 bool ChromeExtensionsBrowserClient::IsWebViewRequest(
118 net::URLRequest
* request
) const {
119 return url_request_util::IsWebViewRequest(request
);
123 ChromeExtensionsBrowserClient::MaybeCreateResourceBundleRequestJob(
124 net::URLRequest
* request
,
125 net::NetworkDelegate
* network_delegate
,
126 const base::FilePath
& directory_path
,
127 const std::string
& content_security_policy
,
128 bool send_cors_header
) {
129 return url_request_util::MaybeCreateURLRequestResourceBundleJob(
133 content_security_policy
,
137 bool ChromeExtensionsBrowserClient::AllowCrossRendererResourceLoad(
138 net::URLRequest
* request
,
140 const Extension
* extension
,
141 InfoMap
* extension_info_map
) {
142 return url_request_util::AllowCrossRendererResourceLoad(
143 request
, is_incognito
, extension
, extension_info_map
);
146 PrefService
* ChromeExtensionsBrowserClient::GetPrefServiceForContext(
147 content::BrowserContext
* context
) {
148 return static_cast<Profile
*>(context
)->GetPrefs();
151 void ChromeExtensionsBrowserClient::GetEarlyExtensionPrefsObservers(
152 content::BrowserContext
* context
,
153 std::vector
<ExtensionPrefsObserver
*>* observers
) const {
154 #if defined(ENABLE_EXTENSIONS)
155 observers
->push_back(ContentSettingsService::Get(context
));
159 bool ChromeExtensionsBrowserClient::DeferLoadingBackgroundHosts(
160 content::BrowserContext
* context
) const {
161 Profile
* profile
= static_cast<Profile
*>(context
);
163 // The profile may not be valid yet if it is still being initialized.
164 // In that case, defer loading, since it depends on an initialized profile.
165 // http://crbug.com/222473
166 if (!g_browser_process
->profile_manager()->IsValidProfile(profile
))
169 #if defined(OS_ANDROID)
172 // There are no browser windows open and the browser process was
173 // started to show the app launcher.
174 return chrome::GetTotalBrowserCountForProfile(profile
) == 0 &&
175 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowAppList
);
179 bool ChromeExtensionsBrowserClient::IsBackgroundPageAllowed(
180 content::BrowserContext
* context
) const {
181 // Returns true if current session is Guest mode session and current
182 // browser context is *not* off-the-record. Such context is artificial and
183 // background page shouldn't be created in it.
184 return !static_cast<Profile
*>(context
)->IsGuestSession() ||
185 context
->IsOffTheRecord();
188 scoped_ptr
<ExtensionHostDelegate
>
189 ChromeExtensionsBrowserClient::CreateExtensionHostDelegate() {
190 return scoped_ptr
<ExtensionHostDelegate
>(new ChromeExtensionHostDelegate
);
193 bool ChromeExtensionsBrowserClient::DidVersionUpdate(
194 content::BrowserContext
* context
) {
195 Profile
* profile
= static_cast<Profile
*>(context
);
197 // Unit tests may not provide prefs; assume everything is up-to-date.
198 ExtensionPrefs
* extension_prefs
= ExtensionPrefs::Get(profile
);
199 if (!extension_prefs
)
202 // If we're inside a browser test, then assume prefs are all up-to-date.
203 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType
))
206 PrefService
* pref_service
= extension_prefs
->pref_service();
207 base::Version last_version
;
208 if (pref_service
->HasPrefPath(pref_names::kLastChromeVersion
)) {
209 std::string last_version_str
=
210 pref_service
->GetString(pref_names::kLastChromeVersion
);
211 last_version
= base::Version(last_version_str
);
214 chrome::VersionInfo current_version_info
;
215 std::string current_version
= current_version_info
.Version();
216 pref_service
->SetString(pref_names::kLastChromeVersion
,
219 // If there was no version string in prefs, assume we're out of date.
220 if (!last_version
.IsValid())
223 return last_version
.IsOlderThan(current_version
);
226 scoped_ptr
<AppSorting
> ChromeExtensionsBrowserClient::CreateAppSorting() {
227 return scoped_ptr
<AppSorting
>(new ChromeAppSorting());
230 bool ChromeExtensionsBrowserClient::IsRunningInForcedAppMode() {
231 return chrome::IsRunningInForcedAppMode();
234 ApiActivityMonitor
* ChromeExtensionsBrowserClient::GetApiActivityMonitor(
235 content::BrowserContext
* context
) {
236 // The ActivityLog monitors and records function calls and events.
237 return ActivityLog::GetInstance(context
);
240 ExtensionSystemProvider
*
241 ChromeExtensionsBrowserClient::GetExtensionSystemFactory() {
242 return ExtensionSystemFactory::GetInstance();
245 void ChromeExtensionsBrowserClient::RegisterExtensionFunctions(
246 ExtensionFunctionRegistry
* registry
) const {
247 // TODO(rockot): Figure out if and why Android really needs to build
248 // ChromeExtensionsBrowserClient and refactor so this ifdef isn't necessary.
249 // See http://crbug.com/349436
250 #if defined(ENABLE_EXTENSIONS)
252 registry
->RegisterFunction
<extensions::GetPreferenceFunction
>();
253 registry
->RegisterFunction
<extensions::SetPreferenceFunction
>();
254 registry
->RegisterFunction
<extensions::ClearPreferenceFunction
>();
256 // Direct Preference Access for Component Extensions.
257 registry
->RegisterFunction
<
258 extensions::chromedirectsetting::GetDirectSettingFunction
>();
259 registry
->RegisterFunction
<
260 extensions::chromedirectsetting::SetDirectSettingFunction
>();
261 registry
->RegisterFunction
<
262 extensions::chromedirectsetting::ClearDirectSettingFunction
>();
264 // Generated APIs from lower-level modules.
265 extensions::core_api::GeneratedFunctionRegistry::RegisterAll(registry
);
266 apps::api::GeneratedFunctionRegistry::RegisterAll(registry
);
268 // Generated APIs from Chrome.
269 extensions::api::GeneratedFunctionRegistry::RegisterAll(registry
);
273 scoped_ptr
<extensions::RuntimeAPIDelegate
>
274 ChromeExtensionsBrowserClient::CreateRuntimeAPIDelegate(
275 content::BrowserContext
* context
) const {
276 return scoped_ptr
<extensions::RuntimeAPIDelegate
>(
277 new ChromeRuntimeAPIDelegate(context
));
280 } // namespace extensions