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_extension_web_contents_observer.h"
22 #include "chrome/browser/extensions/chrome_mojo_service_registration.h"
23 #include "chrome/browser/extensions/chrome_process_manager_delegate.h"
24 #include "chrome/browser/extensions/chrome_url_request_util.h"
25 #include "chrome/browser/extensions/error_console/error_console.h"
26 #include "chrome/browser/extensions/event_router_forwarder.h"
27 #include "chrome/browser/extensions/extension_system_factory.h"
28 #include "chrome/browser/extensions/extension_util.h"
29 #include "chrome/browser/external_protocol/external_protocol_handler.h"
30 #include "chrome/browser/net/chrome_net_log.h"
31 #include "chrome/browser/profiles/profile.h"
32 #include "chrome/browser/profiles/profile_manager.h"
33 #include "chrome/common/chrome_paths.h"
34 #include "chrome/common/chrome_switches.h"
35 #include "chrome/common/chrome_version_info.h"
36 #include "chrome/common/extensions/features/feature_channel.h"
37 #include "chrome/common/pref_names.h"
38 #include "extensions/browser/api/generated_api_registration.h"
39 #include "extensions/browser/extension_function_registry.h"
40 #include "extensions/browser/extension_prefs.h"
41 #include "extensions/browser/mojo/service_registration.h"
42 #include "extensions/browser/pref_names.h"
43 #include "extensions/browser/url_request_util.h"
45 #if defined(OS_CHROMEOS)
46 #include "chrome/browser/chromeos/profiles/profile_helper.h"
47 #include "chrome/browser/extensions/updater/chromeos_extension_cache_delegate.h"
48 #include "chrome/browser/extensions/updater/extension_cache_impl.h"
49 #include "chromeos/chromeos_switches.h"
51 #include "extensions/browser/updater/null_extension_cache.h"
54 namespace extensions
{
56 ChromeExtensionsBrowserClient::ChromeExtensionsBrowserClient() {
57 process_manager_delegate_
.reset(new ChromeProcessManagerDelegate
);
58 api_client_
.reset(new ChromeExtensionsAPIClient
);
59 // Only set if it hasn't already been set (e.g. by a test).
60 if (GetCurrentChannel() == GetDefaultChannel())
61 SetCurrentChannel(chrome::VersionInfo::GetChannel());
62 resource_manager_
.reset(new ChromeComponentExtensionResourceManager());
65 ChromeExtensionsBrowserClient::~ChromeExtensionsBrowserClient() {}
67 bool ChromeExtensionsBrowserClient::IsShuttingDown() {
68 return g_browser_process
->IsShuttingDown();
71 bool ChromeExtensionsBrowserClient::AreExtensionsDisabled(
72 const base::CommandLine
& command_line
,
73 content::BrowserContext
* context
) {
74 Profile
* profile
= static_cast<Profile
*>(context
);
75 return command_line
.HasSwitch(switches::kDisableExtensions
) ||
76 profile
->GetPrefs()->GetBoolean(prefs::kDisableExtensions
);
79 bool ChromeExtensionsBrowserClient::IsValidContext(
80 content::BrowserContext
* context
) {
81 Profile
* profile
= static_cast<Profile
*>(context
);
82 return g_browser_process
->profile_manager() &&
83 g_browser_process
->profile_manager()->IsValidProfile(profile
);
86 bool ChromeExtensionsBrowserClient::IsSameContext(
87 content::BrowserContext
* first
,
88 content::BrowserContext
* second
) {
89 return static_cast<Profile
*>(first
)->IsSameProfile(
90 static_cast<Profile
*>(second
));
93 bool ChromeExtensionsBrowserClient::HasOffTheRecordContext(
94 content::BrowserContext
* context
) {
95 return static_cast<Profile
*>(context
)->HasOffTheRecordProfile();
98 content::BrowserContext
* ChromeExtensionsBrowserClient::GetOffTheRecordContext(
99 content::BrowserContext
* context
) {
100 return static_cast<Profile
*>(context
)->GetOffTheRecordProfile();
103 content::BrowserContext
* ChromeExtensionsBrowserClient::GetOriginalContext(
104 content::BrowserContext
* context
) {
105 return static_cast<Profile
*>(context
)->GetOriginalProfile();
108 #if defined(OS_CHROMEOS)
109 std::string
ChromeExtensionsBrowserClient::GetUserIdHashFromContext(
110 content::BrowserContext
* context
) {
111 return chromeos::ProfileHelper::GetUserIdHashFromProfile(
112 static_cast<Profile
*>(context
));
116 bool ChromeExtensionsBrowserClient::IsGuestSession(
117 content::BrowserContext
* context
) const {
118 return static_cast<Profile
*>(context
)->IsGuestSession();
121 bool ChromeExtensionsBrowserClient::IsExtensionIncognitoEnabled(
122 const std::string
& extension_id
,
123 content::BrowserContext
* context
) const {
124 return IsGuestSession(context
)
125 || util::IsIncognitoEnabled(extension_id
, context
);
128 bool ChromeExtensionsBrowserClient::CanExtensionCrossIncognito(
129 const extensions::Extension
* extension
,
130 content::BrowserContext
* context
) const {
131 return IsGuestSession(context
)
132 || util::CanCrossIncognito(extension
, context
);
136 ChromeExtensionsBrowserClient::MaybeCreateResourceBundleRequestJob(
137 net::URLRequest
* request
,
138 net::NetworkDelegate
* network_delegate
,
139 const base::FilePath
& directory_path
,
140 const std::string
& content_security_policy
,
141 bool send_cors_header
) {
142 return chrome_url_request_util::MaybeCreateURLRequestResourceBundleJob(
146 content_security_policy
,
150 bool ChromeExtensionsBrowserClient::AllowCrossRendererResourceLoad(
151 net::URLRequest
* request
,
153 const Extension
* extension
,
154 InfoMap
* extension_info_map
) {
155 bool allowed
= false;
156 if (chrome_url_request_util::AllowCrossRendererResourceLoad(
157 request
, is_incognito
, extension
, extension_info_map
, &allowed
))
160 // Couldn't determine if resource is allowed. Block the load.
164 PrefService
* ChromeExtensionsBrowserClient::GetPrefServiceForContext(
165 content::BrowserContext
* context
) {
166 return static_cast<Profile
*>(context
)->GetPrefs();
169 void ChromeExtensionsBrowserClient::GetEarlyExtensionPrefsObservers(
170 content::BrowserContext
* context
,
171 std::vector
<ExtensionPrefsObserver
*>* observers
) const {
172 observers
->push_back(ContentSettingsService::Get(context
));
175 ProcessManagerDelegate
*
176 ChromeExtensionsBrowserClient::GetProcessManagerDelegate() const {
177 return process_manager_delegate_
.get();
180 scoped_ptr
<ExtensionHostDelegate
>
181 ChromeExtensionsBrowserClient::CreateExtensionHostDelegate() {
182 return scoped_ptr
<ExtensionHostDelegate
>(new ChromeExtensionHostDelegate
);
185 bool ChromeExtensionsBrowserClient::DidVersionUpdate(
186 content::BrowserContext
* context
) {
187 Profile
* profile
= static_cast<Profile
*>(context
);
189 // Unit tests may not provide prefs; assume everything is up-to-date.
190 ExtensionPrefs
* extension_prefs
= ExtensionPrefs::Get(profile
);
191 if (!extension_prefs
)
194 // If we're inside a browser test, then assume prefs are all up-to-date.
195 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType
))
198 PrefService
* pref_service
= extension_prefs
->pref_service();
199 base::Version last_version
;
200 if (pref_service
->HasPrefPath(pref_names::kLastChromeVersion
)) {
201 std::string last_version_str
=
202 pref_service
->GetString(pref_names::kLastChromeVersion
);
203 last_version
= base::Version(last_version_str
);
206 chrome::VersionInfo current_version_info
;
207 std::string current_version
= current_version_info
.Version();
208 pref_service
->SetString(pref_names::kLastChromeVersion
,
211 // If there was no version string in prefs, assume we're out of date.
212 if (!last_version
.IsValid())
215 return last_version
.IsOlderThan(current_version
);
218 void ChromeExtensionsBrowserClient::PermitExternalProtocolHandler() {
219 ExternalProtocolHandler::PermitLaunchUrl();
222 scoped_ptr
<AppSorting
> ChromeExtensionsBrowserClient::CreateAppSorting() {
223 return scoped_ptr
<AppSorting
>(new ChromeAppSorting());
226 bool ChromeExtensionsBrowserClient::IsRunningInForcedAppMode() {
227 return chrome::IsRunningInForcedAppMode();
230 ApiActivityMonitor
* ChromeExtensionsBrowserClient::GetApiActivityMonitor(
231 content::BrowserContext
* context
) {
232 // The ActivityLog monitors and records function calls and events.
233 return ActivityLog::GetInstance(context
);
236 ExtensionSystemProvider
*
237 ChromeExtensionsBrowserClient::GetExtensionSystemFactory() {
238 return ExtensionSystemFactory::GetInstance();
241 void ChromeExtensionsBrowserClient::RegisterExtensionFunctions(
242 ExtensionFunctionRegistry
* registry
) const {
244 registry
->RegisterFunction
<extensions::GetPreferenceFunction
>();
245 registry
->RegisterFunction
<extensions::SetPreferenceFunction
>();
246 registry
->RegisterFunction
<extensions::ClearPreferenceFunction
>();
248 // Direct Preference Access for Component Extensions.
249 registry
->RegisterFunction
<
250 extensions::chromedirectsetting::GetDirectSettingFunction
>();
251 registry
->RegisterFunction
<
252 extensions::chromedirectsetting::SetDirectSettingFunction
>();
253 registry
->RegisterFunction
<
254 extensions::chromedirectsetting::ClearDirectSettingFunction
>();
256 // Generated APIs from lower-level modules.
257 extensions::core_api::GeneratedFunctionRegistry::RegisterAll(registry
);
259 // Generated APIs from Chrome.
260 extensions::api::GeneratedFunctionRegistry::RegisterAll(registry
);
263 void ChromeExtensionsBrowserClient::RegisterMojoServices(
264 content::RenderFrameHost
* render_frame_host
,
265 const Extension
* extension
) const {
266 RegisterServicesForFrame(render_frame_host
, extension
);
267 RegisterChromeServicesForFrame(render_frame_host
, extension
);
270 scoped_ptr
<extensions::RuntimeAPIDelegate
>
271 ChromeExtensionsBrowserClient::CreateRuntimeAPIDelegate(
272 content::BrowserContext
* context
) const {
273 return scoped_ptr
<extensions::RuntimeAPIDelegate
>(
274 new ChromeRuntimeAPIDelegate(context
));
277 const ComponentExtensionResourceManager
*
278 ChromeExtensionsBrowserClient::GetComponentExtensionResourceManager() {
279 return resource_manager_
.get();
282 void ChromeExtensionsBrowserClient::BroadcastEventToRenderers(
283 const std::string
& event_name
,
284 scoped_ptr
<base::ListValue
> args
) {
285 g_browser_process
->extension_event_router_forwarder()
286 ->BroadcastEventToRenderers(event_name
, args
.Pass(), GURL());
289 net::NetLog
* ChromeExtensionsBrowserClient::GetNetLog() {
290 return g_browser_process
->net_log();
293 ExtensionCache
* ChromeExtensionsBrowserClient::GetExtensionCache() {
294 if (!extension_cache_
.get()) {
295 #if defined(OS_CHROMEOS)
296 extension_cache_
.reset(new ExtensionCacheImpl(
297 make_scoped_ptr(new ChromeOSExtensionCacheDelegate())));
299 extension_cache_
.reset(new NullExtensionCache());
302 return extension_cache_
.get();
305 bool ChromeExtensionsBrowserClient::IsBackgroundUpdateAllowed() {
306 return !base::CommandLine::ForCurrentProcess()->HasSwitch(
307 switches::kDisableBackgroundNetworking
);
310 bool ChromeExtensionsBrowserClient::IsMinBrowserVersionSupported(
311 const std::string
& min_version
) {
312 chrome::VersionInfo version_info
;
313 base::Version browser_version
= base::Version(version_info
.Version());
314 Version
browser_min_version(min_version
);
315 if (browser_version
.IsValid() && browser_min_version
.IsValid() &&
316 browser_min_version
.CompareTo(browser_version
) > 0) {
322 ExtensionWebContentsObserver
*
323 ChromeExtensionsBrowserClient::GetExtensionWebContentsObserver(
324 content::WebContents
* web_contents
) {
325 return ChromeExtensionWebContentsObserver::FromWebContents(web_contents
);
328 void ChromeExtensionsBrowserClient::ReportError(
329 content::BrowserContext
* context
,
330 scoped_ptr
<ExtensionError
> error
) {
331 extensions::ErrorConsole::Get(context
)->ReportError(error
.Pass());
334 } // namespace extensions