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/extension_cache_impl.h"
44 #include "chromeos/chromeos_switches.h"
46 #include "extensions/browser/updater/null_extension_cache.h"
49 namespace extensions
{
51 ChromeExtensionsBrowserClient::ChromeExtensionsBrowserClient() {
52 process_manager_delegate_
.reset(new ChromeProcessManagerDelegate
);
53 api_client_
.reset(new ChromeExtensionsAPIClient
);
54 // Only set if it hasn't already been set (e.g. by a test).
55 if (GetCurrentChannel() == GetDefaultChannel())
56 SetCurrentChannel(chrome::VersionInfo::GetChannel());
57 resource_manager_
.reset(new ChromeComponentExtensionResourceManager());
60 ChromeExtensionsBrowserClient::~ChromeExtensionsBrowserClient() {}
62 bool ChromeExtensionsBrowserClient::IsShuttingDown() {
63 return g_browser_process
->IsShuttingDown();
66 bool ChromeExtensionsBrowserClient::AreExtensionsDisabled(
67 const base::CommandLine
& command_line
,
68 content::BrowserContext
* context
) {
69 Profile
* profile
= static_cast<Profile
*>(context
);
70 return command_line
.HasSwitch(switches::kDisableExtensions
) ||
71 profile
->GetPrefs()->GetBoolean(prefs::kDisableExtensions
);
74 bool ChromeExtensionsBrowserClient::IsValidContext(
75 content::BrowserContext
* context
) {
76 Profile
* profile
= static_cast<Profile
*>(context
);
77 return g_browser_process
->profile_manager() &&
78 g_browser_process
->profile_manager()->IsValidProfile(profile
);
81 bool ChromeExtensionsBrowserClient::IsSameContext(
82 content::BrowserContext
* first
,
83 content::BrowserContext
* second
) {
84 return static_cast<Profile
*>(first
)->IsSameProfile(
85 static_cast<Profile
*>(second
));
88 bool ChromeExtensionsBrowserClient::HasOffTheRecordContext(
89 content::BrowserContext
* context
) {
90 return static_cast<Profile
*>(context
)->HasOffTheRecordProfile();
93 content::BrowserContext
* ChromeExtensionsBrowserClient::GetOffTheRecordContext(
94 content::BrowserContext
* context
) {
95 return static_cast<Profile
*>(context
)->GetOffTheRecordProfile();
98 content::BrowserContext
* ChromeExtensionsBrowserClient::GetOriginalContext(
99 content::BrowserContext
* context
) {
100 return static_cast<Profile
*>(context
)->GetOriginalProfile();
103 #if defined(OS_CHROMEOS)
104 std::string
ChromeExtensionsBrowserClient::GetUserIdHashFromContext(
105 content::BrowserContext
* context
) {
106 return chromeos::ProfileHelper::GetUserIdHashFromProfile(
107 static_cast<Profile
*>(context
));
111 bool ChromeExtensionsBrowserClient::IsGuestSession(
112 content::BrowserContext
* context
) const {
113 return static_cast<Profile
*>(context
)->IsGuestSession();
116 bool ChromeExtensionsBrowserClient::IsExtensionIncognitoEnabled(
117 const std::string
& extension_id
,
118 content::BrowserContext
* context
) const {
119 return IsGuestSession(context
)
120 || util::IsIncognitoEnabled(extension_id
, context
);
123 bool ChromeExtensionsBrowserClient::CanExtensionCrossIncognito(
124 const extensions::Extension
* extension
,
125 content::BrowserContext
* context
) const {
126 return IsGuestSession(context
)
127 || util::CanCrossIncognito(extension
, context
);
131 ChromeExtensionsBrowserClient::MaybeCreateResourceBundleRequestJob(
132 net::URLRequest
* request
,
133 net::NetworkDelegate
* network_delegate
,
134 const base::FilePath
& directory_path
,
135 const std::string
& content_security_policy
,
136 bool send_cors_header
) {
137 return chrome_url_request_util::MaybeCreateURLRequestResourceBundleJob(
141 content_security_policy
,
145 bool ChromeExtensionsBrowserClient::AllowCrossRendererResourceLoad(
146 net::URLRequest
* request
,
148 const Extension
* extension
,
149 InfoMap
* extension_info_map
) {
150 bool allowed
= false;
151 if (chrome_url_request_util::AllowCrossRendererResourceLoad(
152 request
, is_incognito
, extension
, extension_info_map
, &allowed
))
155 // Couldn't determine if resource is allowed. Block the load.
159 PrefService
* ChromeExtensionsBrowserClient::GetPrefServiceForContext(
160 content::BrowserContext
* context
) {
161 return static_cast<Profile
*>(context
)->GetPrefs();
164 void ChromeExtensionsBrowserClient::GetEarlyExtensionPrefsObservers(
165 content::BrowserContext
* context
,
166 std::vector
<ExtensionPrefsObserver
*>* observers
) const {
167 observers
->push_back(ContentSettingsService::Get(context
));
170 ProcessManagerDelegate
*
171 ChromeExtensionsBrowserClient::GetProcessManagerDelegate() const {
172 return process_manager_delegate_
.get();
175 scoped_ptr
<ExtensionHostDelegate
>
176 ChromeExtensionsBrowserClient::CreateExtensionHostDelegate() {
177 return scoped_ptr
<ExtensionHostDelegate
>(new ChromeExtensionHostDelegate
);
180 bool ChromeExtensionsBrowserClient::DidVersionUpdate(
181 content::BrowserContext
* context
) {
182 Profile
* profile
= static_cast<Profile
*>(context
);
184 // Unit tests may not provide prefs; assume everything is up-to-date.
185 ExtensionPrefs
* extension_prefs
= ExtensionPrefs::Get(profile
);
186 if (!extension_prefs
)
189 // If we're inside a browser test, then assume prefs are all up-to-date.
190 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType
))
193 PrefService
* pref_service
= extension_prefs
->pref_service();
194 base::Version last_version
;
195 if (pref_service
->HasPrefPath(pref_names::kLastChromeVersion
)) {
196 std::string last_version_str
=
197 pref_service
->GetString(pref_names::kLastChromeVersion
);
198 last_version
= base::Version(last_version_str
);
201 chrome::VersionInfo current_version_info
;
202 std::string current_version
= current_version_info
.Version();
203 pref_service
->SetString(pref_names::kLastChromeVersion
,
206 // If there was no version string in prefs, assume we're out of date.
207 if (!last_version
.IsValid())
210 return last_version
.IsOlderThan(current_version
);
213 void ChromeExtensionsBrowserClient::PermitExternalProtocolHandler() {
214 ExternalProtocolHandler::PermitLaunchUrl();
217 scoped_ptr
<AppSorting
> ChromeExtensionsBrowserClient::CreateAppSorting() {
218 return scoped_ptr
<AppSorting
>(new ChromeAppSorting());
221 bool ChromeExtensionsBrowserClient::IsRunningInForcedAppMode() {
222 return chrome::IsRunningInForcedAppMode();
225 ApiActivityMonitor
* ChromeExtensionsBrowserClient::GetApiActivityMonitor(
226 content::BrowserContext
* context
) {
227 // The ActivityLog monitors and records function calls and events.
228 return ActivityLog::GetInstance(context
);
231 ExtensionSystemProvider
*
232 ChromeExtensionsBrowserClient::GetExtensionSystemFactory() {
233 return ExtensionSystemFactory::GetInstance();
236 void ChromeExtensionsBrowserClient::RegisterExtensionFunctions(
237 ExtensionFunctionRegistry
* registry
) const {
239 registry
->RegisterFunction
<extensions::GetPreferenceFunction
>();
240 registry
->RegisterFunction
<extensions::SetPreferenceFunction
>();
241 registry
->RegisterFunction
<extensions::ClearPreferenceFunction
>();
243 // Direct Preference Access for Component Extensions.
244 registry
->RegisterFunction
<
245 extensions::chromedirectsetting::GetDirectSettingFunction
>();
246 registry
->RegisterFunction
<
247 extensions::chromedirectsetting::SetDirectSettingFunction
>();
248 registry
->RegisterFunction
<
249 extensions::chromedirectsetting::ClearDirectSettingFunction
>();
251 // Generated APIs from lower-level modules.
252 extensions::core_api::GeneratedFunctionRegistry::RegisterAll(registry
);
254 // Generated APIs from Chrome.
255 extensions::api::GeneratedFunctionRegistry::RegisterAll(registry
);
258 scoped_ptr
<extensions::RuntimeAPIDelegate
>
259 ChromeExtensionsBrowserClient::CreateRuntimeAPIDelegate(
260 content::BrowserContext
* context
) const {
261 return scoped_ptr
<extensions::RuntimeAPIDelegate
>(
262 new ChromeRuntimeAPIDelegate(context
));
265 const ComponentExtensionResourceManager
*
266 ChromeExtensionsBrowserClient::GetComponentExtensionResourceManager() {
267 return resource_manager_
.get();
270 void ChromeExtensionsBrowserClient::BroadcastEventToRenderers(
271 const std::string
& event_name
,
272 scoped_ptr
<base::ListValue
> args
) {
273 g_browser_process
->extension_event_router_forwarder()
274 ->BroadcastEventToRenderers(event_name
, args
.Pass(), GURL());
277 net::NetLog
* ChromeExtensionsBrowserClient::GetNetLog() {
278 return g_browser_process
->net_log();
281 ExtensionCache
* ChromeExtensionsBrowserClient::GetExtensionCache() {
282 if (!extension_cache_
.get()) {
283 #if defined(OS_CHROMEOS)
284 extension_cache_
.reset(new ExtensionCacheImpl());
286 extension_cache_
.reset(new NullExtensionCache());
289 return extension_cache_
.get();
292 bool ChromeExtensionsBrowserClient::IsBackgroundUpdateAllowed() {
293 return !base::CommandLine::ForCurrentProcess()->HasSwitch(
294 switches::kDisableBackgroundNetworking
);
297 bool ChromeExtensionsBrowserClient::IsMinBrowserVersionSupported(
298 const std::string
& min_version
) {
299 chrome::VersionInfo version_info
;
300 base::Version browser_version
= base::Version(version_info
.Version());
301 Version
browser_min_version(min_version
);
302 if (browser_version
.IsValid() && browser_min_version
.IsValid() &&
303 browser_min_version
.CompareTo(browser_version
) > 0) {
309 } // namespace extensions