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/path_service.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/chrome_extensions_api_client.h"
14 #include "chrome/browser/extensions/api/content_settings/content_settings_service.h"
15 #include "chrome/browser/extensions/api/generated_api_registration.h"
16 #include "chrome/browser/extensions/api/preference/chrome_direct_setting.h"
17 #include "chrome/browser/extensions/api/preference/preference_api.h"
18 #include "chrome/browser/extensions/api/runtime/chrome_runtime_api_delegate.h"
19 #include "chrome/browser/extensions/chrome_app_sorting.h"
20 #include "chrome/browser/extensions/chrome_component_extension_resource_manager.h"
21 #include "chrome/browser/extensions/chrome_extension_host_delegate.h"
22 #include "chrome/browser/extensions/chrome_process_manager_delegate.h"
23 #include "chrome/browser/extensions/chrome_url_request_util.h"
24 #include "chrome/browser/extensions/event_router_forwarder.h"
25 #include "chrome/browser/extensions/extension_system_factory.h"
26 #include "chrome/browser/extensions/extension_util.h"
27 #include "chrome/browser/external_protocol/external_protocol_handler.h"
28 #include "chrome/browser/net/chrome_net_log.h"
29 #include "chrome/browser/profiles/profile.h"
30 #include "chrome/browser/profiles/profile_manager.h"
31 #include "chrome/common/chrome_paths.h"
32 #include "chrome/common/chrome_switches.h"
33 #include "chrome/common/chrome_version_info.h"
34 #include "chrome/common/extensions/features/feature_channel.h"
35 #include "chrome/common/pref_names.h"
36 #include "extensions/browser/api/generated_api_registration.h"
37 #include "extensions/browser/extension_function_registry.h"
38 #include "extensions/browser/extension_prefs.h"
39 #include "extensions/browser/pref_names.h"
40 #include "extensions/browser/url_request_util.h"
42 #if defined(OS_CHROMEOS)
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());
59 ChromeExtensionsBrowserClient::~ChromeExtensionsBrowserClient() {}
61 bool ChromeExtensionsBrowserClient::IsShuttingDown() {
62 return g_browser_process
->IsShuttingDown();
65 bool ChromeExtensionsBrowserClient::AreExtensionsDisabled(
66 const CommandLine
& command_line
,
67 content::BrowserContext
* context
) {
68 Profile
* profile
= static_cast<Profile
*>(context
);
69 return command_line
.HasSwitch(switches::kDisableExtensions
) ||
70 profile
->GetPrefs()->GetBoolean(prefs::kDisableExtensions
);
73 bool ChromeExtensionsBrowserClient::IsValidContext(
74 content::BrowserContext
* context
) {
75 Profile
* profile
= static_cast<Profile
*>(context
);
76 return g_browser_process
->profile_manager()->IsValidProfile(profile
);
79 bool ChromeExtensionsBrowserClient::IsSameContext(
80 content::BrowserContext
* first
,
81 content::BrowserContext
* second
) {
82 return static_cast<Profile
*>(first
)->IsSameProfile(
83 static_cast<Profile
*>(second
));
86 bool ChromeExtensionsBrowserClient::HasOffTheRecordContext(
87 content::BrowserContext
* context
) {
88 return static_cast<Profile
*>(context
)->HasOffTheRecordProfile();
91 content::BrowserContext
* ChromeExtensionsBrowserClient::GetOffTheRecordContext(
92 content::BrowserContext
* context
) {
93 return static_cast<Profile
*>(context
)->GetOffTheRecordProfile();
96 content::BrowserContext
* ChromeExtensionsBrowserClient::GetOriginalContext(
97 content::BrowserContext
* context
) {
98 return static_cast<Profile
*>(context
)->GetOriginalProfile();
101 bool ChromeExtensionsBrowserClient::IsGuestSession(
102 content::BrowserContext
* context
) const {
103 return static_cast<Profile
*>(context
)->IsGuestSession();
106 bool ChromeExtensionsBrowserClient::IsExtensionIncognitoEnabled(
107 const std::string
& extension_id
,
108 content::BrowserContext
* context
) const {
109 return IsGuestSession(context
)
110 || util::IsIncognitoEnabled(extension_id
, context
);
113 bool ChromeExtensionsBrowserClient::CanExtensionCrossIncognito(
114 const extensions::Extension
* extension
,
115 content::BrowserContext
* context
) const {
116 return IsGuestSession(context
)
117 || util::CanCrossIncognito(extension
, context
);
121 ChromeExtensionsBrowserClient::MaybeCreateResourceBundleRequestJob(
122 net::URLRequest
* request
,
123 net::NetworkDelegate
* network_delegate
,
124 const base::FilePath
& directory_path
,
125 const std::string
& content_security_policy
,
126 bool send_cors_header
) {
127 return chrome_url_request_util::MaybeCreateURLRequestResourceBundleJob(
131 content_security_policy
,
135 bool ChromeExtensionsBrowserClient::AllowCrossRendererResourceLoad(
136 net::URLRequest
* request
,
138 const Extension
* extension
,
139 InfoMap
* extension_info_map
) {
140 bool allowed
= false;
141 if (chrome_url_request_util::AllowCrossRendererResourceLoad(
142 request
, is_incognito
, extension
, extension_info_map
, &allowed
))
145 // Couldn't determine if resource is allowed. Block the load.
149 PrefService
* ChromeExtensionsBrowserClient::GetPrefServiceForContext(
150 content::BrowserContext
* context
) {
151 return static_cast<Profile
*>(context
)->GetPrefs();
154 void ChromeExtensionsBrowserClient::GetEarlyExtensionPrefsObservers(
155 content::BrowserContext
* context
,
156 std::vector
<ExtensionPrefsObserver
*>* observers
) const {
157 observers
->push_back(ContentSettingsService::Get(context
));
160 ProcessManagerDelegate
*
161 ChromeExtensionsBrowserClient::GetProcessManagerDelegate() const {
162 return process_manager_delegate_
.get();
165 scoped_ptr
<ExtensionHostDelegate
>
166 ChromeExtensionsBrowserClient::CreateExtensionHostDelegate() {
167 return scoped_ptr
<ExtensionHostDelegate
>(new ChromeExtensionHostDelegate
);
170 bool ChromeExtensionsBrowserClient::DidVersionUpdate(
171 content::BrowserContext
* context
) {
172 Profile
* profile
= static_cast<Profile
*>(context
);
174 // Unit tests may not provide prefs; assume everything is up-to-date.
175 ExtensionPrefs
* extension_prefs
= ExtensionPrefs::Get(profile
);
176 if (!extension_prefs
)
179 // If we're inside a browser test, then assume prefs are all up-to-date.
180 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType
))
183 PrefService
* pref_service
= extension_prefs
->pref_service();
184 base::Version last_version
;
185 if (pref_service
->HasPrefPath(pref_names::kLastChromeVersion
)) {
186 std::string last_version_str
=
187 pref_service
->GetString(pref_names::kLastChromeVersion
);
188 last_version
= base::Version(last_version_str
);
191 chrome::VersionInfo current_version_info
;
192 std::string current_version
= current_version_info
.Version();
193 pref_service
->SetString(pref_names::kLastChromeVersion
,
196 // If there was no version string in prefs, assume we're out of date.
197 if (!last_version
.IsValid())
200 return last_version
.IsOlderThan(current_version
);
203 void ChromeExtensionsBrowserClient::PermitExternalProtocolHandler() {
204 ExternalProtocolHandler::PermitLaunchUrl();
207 scoped_ptr
<AppSorting
> ChromeExtensionsBrowserClient::CreateAppSorting() {
208 return scoped_ptr
<AppSorting
>(new ChromeAppSorting());
211 bool ChromeExtensionsBrowserClient::IsRunningInForcedAppMode() {
212 return chrome::IsRunningInForcedAppMode();
215 ApiActivityMonitor
* ChromeExtensionsBrowserClient::GetApiActivityMonitor(
216 content::BrowserContext
* context
) {
217 // The ActivityLog monitors and records function calls and events.
218 return ActivityLog::GetInstance(context
);
221 ExtensionSystemProvider
*
222 ChromeExtensionsBrowserClient::GetExtensionSystemFactory() {
223 return ExtensionSystemFactory::GetInstance();
226 void ChromeExtensionsBrowserClient::RegisterExtensionFunctions(
227 ExtensionFunctionRegistry
* registry
) const {
229 registry
->RegisterFunction
<extensions::GetPreferenceFunction
>();
230 registry
->RegisterFunction
<extensions::SetPreferenceFunction
>();
231 registry
->RegisterFunction
<extensions::ClearPreferenceFunction
>();
233 // Direct Preference Access for Component Extensions.
234 registry
->RegisterFunction
<
235 extensions::chromedirectsetting::GetDirectSettingFunction
>();
236 registry
->RegisterFunction
<
237 extensions::chromedirectsetting::SetDirectSettingFunction
>();
238 registry
->RegisterFunction
<
239 extensions::chromedirectsetting::ClearDirectSettingFunction
>();
241 // Generated APIs from lower-level modules.
242 extensions::core_api::GeneratedFunctionRegistry::RegisterAll(registry
);
244 // Generated APIs from Chrome.
245 extensions::api::GeneratedFunctionRegistry::RegisterAll(registry
);
248 scoped_ptr
<extensions::RuntimeAPIDelegate
>
249 ChromeExtensionsBrowserClient::CreateRuntimeAPIDelegate(
250 content::BrowserContext
* context
) const {
251 return scoped_ptr
<extensions::RuntimeAPIDelegate
>(
252 new ChromeRuntimeAPIDelegate(context
));
255 ComponentExtensionResourceManager
*
256 ChromeExtensionsBrowserClient::GetComponentExtensionResourceManager() {
257 if (!resource_manager_
)
258 resource_manager_
.reset(new ChromeComponentExtensionResourceManager());
259 return resource_manager_
.get();
262 void ChromeExtensionsBrowserClient::BroadcastEventToRenderers(
263 const std::string
& event_name
,
264 scoped_ptr
<base::ListValue
> args
) {
265 g_browser_process
->extension_event_router_forwarder()
266 ->BroadcastEventToRenderers(event_name
, args
.Pass(), GURL());
269 net::NetLog
* ChromeExtensionsBrowserClient::GetNetLog() {
270 return g_browser_process
->net_log();
273 ExtensionCache
* ChromeExtensionsBrowserClient::GetExtensionCache() {
274 if (!extension_cache_
.get()) {
275 #if defined(OS_CHROMEOS)
276 extension_cache_
.reset(new ExtensionCacheImpl());
278 extension_cache_
.reset(new NullExtensionCache());
281 return extension_cache_
.get();
284 bool ChromeExtensionsBrowserClient::IsBackgroundUpdateAllowed() {
285 return !CommandLine::ForCurrentProcess()->HasSwitch(
286 switches::kDisableBackgroundNetworking
);
289 bool ChromeExtensionsBrowserClient::IsMinBrowserVersionSupported(
290 const std::string
& min_version
) {
291 chrome::VersionInfo version_info
;
292 base::Version browser_version
= base::Version(version_info
.Version());
293 Version
browser_min_version(min_version
);
294 if (browser_version
.IsValid() && browser_min_version
.IsValid() &&
295 browser_min_version
.CompareTo(browser_version
) > 0) {
301 } // namespace extensions