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 "base/command_line.h"
8 #include "base/version.h"
9 #include "chrome/browser/app_mode/app_mode_utils.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/extensions/activity_log/activity_log.h"
12 #include "chrome/browser/extensions/api/chrome_extensions_api_client.h"
13 #include "chrome/browser/extensions/api/content_settings/content_settings_service.h"
14 #include "chrome/browser/extensions/api/generated_api_registration.h"
15 #include "chrome/browser/extensions/api/preference/chrome_direct_setting.h"
16 #include "chrome/browser/extensions/api/preference/preference_api.h"
17 #include "chrome/browser/extensions/api/runtime/chrome_runtime_api_delegate.h"
18 #include "chrome/browser/extensions/chrome_app_sorting.h"
19 #include "chrome/browser/extensions/chrome_component_extension_resource_manager.h"
20 #include "chrome/browser/extensions/chrome_extension_host_delegate.h"
21 #include "chrome/browser/extensions/chrome_process_manager_delegate.h"
22 #include "chrome/browser/extensions/chrome_url_request_util.h"
23 #include "chrome/browser/extensions/event_router_forwarder.h"
24 #include "chrome/browser/extensions/extension_system_factory.h"
25 #include "chrome/browser/extensions/extension_util.h"
26 #include "chrome/browser/external_protocol/external_protocol_handler.h"
27 #include "chrome/browser/net/chrome_net_log.h"
28 #include "chrome/browser/profiles/profile.h"
29 #include "chrome/browser/profiles/profile_manager.h"
30 #include "chrome/common/chrome_paths.h"
31 #include "chrome/common/chrome_switches.h"
32 #include "chrome/common/chrome_version_info.h"
33 #include "chrome/common/extensions/features/feature_channel.h"
34 #include "chrome/common/pref_names.h"
35 #include "extensions/browser/api/generated_api_registration.h"
36 #include "extensions/browser/extension_function_registry.h"
37 #include "extensions/browser/extension_prefs.h"
38 #include "extensions/browser/pref_names.h"
39 #include "extensions/browser/url_request_util.h"
41 #if defined(OS_CHROMEOS)
42 #include "chrome/browser/chromeos/profiles/profile_helper.h"
43 #include "chrome/browser/extensions/updater/chromeos_extension_cache_delegate.h"
44 #include "chrome/browser/extensions/updater/extension_cache_impl.h"
45 #include "chromeos/chromeos_switches.h"
47 #include "extensions/browser/updater/null_extension_cache.h"
50 namespace extensions
{
52 ChromeExtensionsBrowserClient::ChromeExtensionsBrowserClient() {
53 process_manager_delegate_
.reset(new ChromeProcessManagerDelegate
);
54 api_client_
.reset(new ChromeExtensionsAPIClient
);
55 // Only set if it hasn't already been set (e.g. by a test).
56 if (GetCurrentChannel() == GetDefaultChannel())
57 SetCurrentChannel(chrome::VersionInfo::GetChannel());
58 resource_manager_
.reset(new ChromeComponentExtensionResourceManager());
61 ChromeExtensionsBrowserClient::~ChromeExtensionsBrowserClient() {}
63 bool ChromeExtensionsBrowserClient::IsShuttingDown() {
64 return g_browser_process
->IsShuttingDown();
67 bool ChromeExtensionsBrowserClient::AreExtensionsDisabled(
68 const base::CommandLine
& command_line
,
69 content::BrowserContext
* context
) {
70 Profile
* profile
= static_cast<Profile
*>(context
);
71 return command_line
.HasSwitch(switches::kDisableExtensions
) ||
72 profile
->GetPrefs()->GetBoolean(prefs::kDisableExtensions
);
75 bool ChromeExtensionsBrowserClient::IsValidContext(
76 content::BrowserContext
* context
) {
77 Profile
* profile
= static_cast<Profile
*>(context
);
78 return g_browser_process
->profile_manager() &&
79 g_browser_process
->profile_manager()->IsValidProfile(profile
);
82 bool ChromeExtensionsBrowserClient::IsSameContext(
83 content::BrowserContext
* first
,
84 content::BrowserContext
* second
) {
85 return static_cast<Profile
*>(first
)->IsSameProfile(
86 static_cast<Profile
*>(second
));
89 bool ChromeExtensionsBrowserClient::HasOffTheRecordContext(
90 content::BrowserContext
* context
) {
91 return static_cast<Profile
*>(context
)->HasOffTheRecordProfile();
94 content::BrowserContext
* ChromeExtensionsBrowserClient::GetOffTheRecordContext(
95 content::BrowserContext
* context
) {
96 return static_cast<Profile
*>(context
)->GetOffTheRecordProfile();
99 content::BrowserContext
* ChromeExtensionsBrowserClient::GetOriginalContext(
100 content::BrowserContext
* context
) {
101 return static_cast<Profile
*>(context
)->GetOriginalProfile();
104 #if defined(OS_CHROMEOS)
105 std::string
ChromeExtensionsBrowserClient::GetUserIdHashFromContext(
106 content::BrowserContext
* context
) {
107 return chromeos::ProfileHelper::GetUserIdHashFromProfile(
108 static_cast<Profile
*>(context
));
112 bool ChromeExtensionsBrowserClient::IsGuestSession(
113 content::BrowserContext
* context
) const {
114 return static_cast<Profile
*>(context
)->IsGuestSession();
117 bool ChromeExtensionsBrowserClient::IsExtensionIncognitoEnabled(
118 const std::string
& extension_id
,
119 content::BrowserContext
* context
) const {
120 return IsGuestSession(context
)
121 || util::IsIncognitoEnabled(extension_id
, context
);
124 bool ChromeExtensionsBrowserClient::CanExtensionCrossIncognito(
125 const extensions::Extension
* extension
,
126 content::BrowserContext
* context
) const {
127 return IsGuestSession(context
)
128 || util::CanCrossIncognito(extension
, context
);
132 ChromeExtensionsBrowserClient::MaybeCreateResourceBundleRequestJob(
133 net::URLRequest
* request
,
134 net::NetworkDelegate
* network_delegate
,
135 const base::FilePath
& directory_path
,
136 const std::string
& content_security_policy
,
137 bool send_cors_header
) {
138 return chrome_url_request_util::MaybeCreateURLRequestResourceBundleJob(
142 content_security_policy
,
146 bool ChromeExtensionsBrowserClient::AllowCrossRendererResourceLoad(
147 net::URLRequest
* request
,
149 const Extension
* extension
,
150 InfoMap
* extension_info_map
) {
151 bool allowed
= false;
152 if (chrome_url_request_util::AllowCrossRendererResourceLoad(
153 request
, is_incognito
, extension
, extension_info_map
, &allowed
))
156 // Couldn't determine if resource is allowed. Block the load.
160 PrefService
* ChromeExtensionsBrowserClient::GetPrefServiceForContext(
161 content::BrowserContext
* context
) {
162 return static_cast<Profile
*>(context
)->GetPrefs();
165 void ChromeExtensionsBrowserClient::GetEarlyExtensionPrefsObservers(
166 content::BrowserContext
* context
,
167 std::vector
<ExtensionPrefsObserver
*>* observers
) const {
168 observers
->push_back(ContentSettingsService::Get(context
));
171 ProcessManagerDelegate
*
172 ChromeExtensionsBrowserClient::GetProcessManagerDelegate() const {
173 return process_manager_delegate_
.get();
176 scoped_ptr
<ExtensionHostDelegate
>
177 ChromeExtensionsBrowserClient::CreateExtensionHostDelegate() {
178 return scoped_ptr
<ExtensionHostDelegate
>(new ChromeExtensionHostDelegate
);
181 bool ChromeExtensionsBrowserClient::DidVersionUpdate(
182 content::BrowserContext
* context
) {
183 Profile
* profile
= static_cast<Profile
*>(context
);
185 // Unit tests may not provide prefs; assume everything is up-to-date.
186 ExtensionPrefs
* extension_prefs
= ExtensionPrefs::Get(profile
);
187 if (!extension_prefs
)
190 // If we're inside a browser test, then assume prefs are all up-to-date.
191 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType
))
194 PrefService
* pref_service
= extension_prefs
->pref_service();
195 base::Version last_version
;
196 if (pref_service
->HasPrefPath(pref_names::kLastChromeVersion
)) {
197 std::string last_version_str
=
198 pref_service
->GetString(pref_names::kLastChromeVersion
);
199 last_version
= base::Version(last_version_str
);
202 chrome::VersionInfo current_version_info
;
203 std::string current_version
= current_version_info
.Version();
204 pref_service
->SetString(pref_names::kLastChromeVersion
,
207 // If there was no version string in prefs, assume we're out of date.
208 if (!last_version
.IsValid())
211 return last_version
.IsOlderThan(current_version
);
214 void ChromeExtensionsBrowserClient::PermitExternalProtocolHandler() {
215 ExternalProtocolHandler::PermitLaunchUrl();
218 scoped_ptr
<AppSorting
> ChromeExtensionsBrowserClient::CreateAppSorting() {
219 return scoped_ptr
<AppSorting
>(new ChromeAppSorting());
222 bool ChromeExtensionsBrowserClient::IsRunningInForcedAppMode() {
223 return chrome::IsRunningInForcedAppMode();
226 ApiActivityMonitor
* ChromeExtensionsBrowserClient::GetApiActivityMonitor(
227 content::BrowserContext
* context
) {
228 // The ActivityLog monitors and records function calls and events.
229 return ActivityLog::GetInstance(context
);
232 ExtensionSystemProvider
*
233 ChromeExtensionsBrowserClient::GetExtensionSystemFactory() {
234 return ExtensionSystemFactory::GetInstance();
237 void ChromeExtensionsBrowserClient::RegisterExtensionFunctions(
238 ExtensionFunctionRegistry
* registry
) const {
240 registry
->RegisterFunction
<extensions::GetPreferenceFunction
>();
241 registry
->RegisterFunction
<extensions::SetPreferenceFunction
>();
242 registry
->RegisterFunction
<extensions::ClearPreferenceFunction
>();
244 // Direct Preference Access for Component Extensions.
245 registry
->RegisterFunction
<
246 extensions::chromedirectsetting::GetDirectSettingFunction
>();
247 registry
->RegisterFunction
<
248 extensions::chromedirectsetting::SetDirectSettingFunction
>();
249 registry
->RegisterFunction
<
250 extensions::chromedirectsetting::ClearDirectSettingFunction
>();
252 // Generated APIs from lower-level modules.
253 extensions::core_api::GeneratedFunctionRegistry::RegisterAll(registry
);
255 // Generated APIs from Chrome.
256 extensions::api::GeneratedFunctionRegistry::RegisterAll(registry
);
259 scoped_ptr
<extensions::RuntimeAPIDelegate
>
260 ChromeExtensionsBrowserClient::CreateRuntimeAPIDelegate(
261 content::BrowserContext
* context
) const {
262 return scoped_ptr
<extensions::RuntimeAPIDelegate
>(
263 new ChromeRuntimeAPIDelegate(context
));
266 const ComponentExtensionResourceManager
*
267 ChromeExtensionsBrowserClient::GetComponentExtensionResourceManager() {
268 return resource_manager_
.get();
271 void ChromeExtensionsBrowserClient::BroadcastEventToRenderers(
272 const std::string
& event_name
,
273 scoped_ptr
<base::ListValue
> args
) {
274 g_browser_process
->extension_event_router_forwarder()
275 ->BroadcastEventToRenderers(event_name
, args
.Pass(), GURL());
278 net::NetLog
* ChromeExtensionsBrowserClient::GetNetLog() {
279 return g_browser_process
->net_log();
282 ExtensionCache
* ChromeExtensionsBrowserClient::GetExtensionCache() {
283 if (!extension_cache_
.get()) {
284 #if defined(OS_CHROMEOS)
285 extension_cache_
.reset(new ExtensionCacheImpl(
286 make_scoped_ptr(new ChromeOSExtensionCacheDelegate())));
288 extension_cache_
.reset(new NullExtensionCache());
291 return extension_cache_
.get();
294 bool ChromeExtensionsBrowserClient::IsBackgroundUpdateAllowed() {
295 return !base::CommandLine::ForCurrentProcess()->HasSwitch(
296 switches::kDisableBackgroundNetworking
);
299 bool ChromeExtensionsBrowserClient::IsMinBrowserVersionSupported(
300 const std::string
& min_version
) {
301 chrome::VersionInfo version_info
;
302 base::Version browser_version
= base::Version(version_info
.Version());
303 Version
browser_min_version(min_version
);
304 if (browser_version
.IsValid() && browser_min_version
.IsValid() &&
305 browser_min_version
.CompareTo(browser_version
) > 0) {
311 } // namespace extensions