1 // Copyright 2014 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/extension_system_impl.h"
7 #include "base/base_switches.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/metrics/field_trial.h"
13 #include "base/metrics/histogram.h"
14 #include "base/strings/string_tokenizer.h"
15 #include "base/strings/string_util.h"
16 #include "base/trace_event/trace_event.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/content_settings/cookie_settings.h"
19 #include "chrome/browser/extensions/component_loader.h"
20 #include "chrome/browser/extensions/error_console/error_console.h"
21 #include "chrome/browser/extensions/extension_error_reporter.h"
22 #include "chrome/browser/extensions/extension_management.h"
23 #include "chrome/browser/extensions/extension_service.h"
24 #include "chrome/browser/extensions/extension_system_factory.h"
25 #include "chrome/browser/extensions/extension_util.h"
26 #include "chrome/browser/extensions/install_verifier.h"
27 #include "chrome/browser/extensions/navigation_observer.h"
28 #include "chrome/browser/extensions/shared_module_service.h"
29 #include "chrome/browser/extensions/shared_user_script_master.h"
30 #include "chrome/browser/extensions/state_store_notification_observer.h"
31 #include "chrome/browser/extensions/unpacked_installer.h"
32 #include "chrome/browser/profiles/profile.h"
33 #include "chrome/browser/profiles/profile_manager.h"
34 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
35 #include "chrome/common/chrome_switches.h"
36 #include "chrome/common/chrome_version_info.h"
37 #include "chrome/common/extensions/extension_constants.h"
38 #include "chrome/common/extensions/features/feature_channel.h"
39 #include "content/public/browser/browser_thread.h"
40 #include "content/public/browser/url_data_source.h"
41 #include "extensions/browser/content_verifier.h"
42 #include "extensions/browser/content_verifier_delegate.h"
43 #include "extensions/browser/declarative_user_script_manager.h"
44 #include "extensions/browser/event_router.h"
45 #include "extensions/browser/extension_pref_store.h"
46 #include "extensions/browser/extension_pref_value_map.h"
47 #include "extensions/browser/extension_pref_value_map_factory.h"
48 #include "extensions/browser/extension_prefs.h"
49 #include "extensions/browser/extension_registry.h"
50 #include "extensions/browser/info_map.h"
51 #include "extensions/browser/lazy_background_task_queue.h"
52 #include "extensions/browser/management_policy.h"
53 #include "extensions/browser/quota_service.h"
54 #include "extensions/browser/runtime_data.h"
55 #include "extensions/browser/state_store.h"
56 #include "extensions/common/constants.h"
57 #include "extensions/common/extension.h"
58 #include "extensions/common/extension_urls.h"
59 #include "extensions/common/extensions_client.h"
60 #include "extensions/common/manifest.h"
61 #include "extensions/common/manifest_url_handlers.h"
62 #include "net/base/escape.h"
64 #if defined(ENABLE_NOTIFICATIONS)
65 #include "chrome/browser/notifications/desktop_notification_service.h"
66 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
67 #include "ui/message_center/notifier_settings.h"
70 #if defined(OS_CHROMEOS)
71 #include "chrome/browser/app_mode/app_mode_utils.h"
72 #include "chrome/browser/chromeos/extensions/device_local_account_management_policy_provider.h"
73 #include "chrome/browser/chromeos/policy/device_local_account.h"
74 #include "chrome/browser/extensions/extension_assets_manager_chromeos.h"
75 #include "chromeos/chromeos_switches.h"
76 #include "chromeos/login/login_state.h"
77 #include "components/user_manager/user.h"
78 #include "components/user_manager/user_manager.h"
81 using content::BrowserThread
;
85 const char kContentVerificationExperimentName
[] =
86 "ExtensionContentVerification";
90 namespace extensions
{
93 // ExtensionSystemImpl::Shared
96 ExtensionSystemImpl::Shared::Shared(Profile
* profile
)
100 ExtensionSystemImpl::Shared::~Shared() {
103 void ExtensionSystemImpl::Shared::InitPrefs() {
104 lazy_background_task_queue_
.reset(new LazyBackgroundTaskQueue(profile_
));
105 event_router_
.reset(new EventRouter(profile_
, ExtensionPrefs::Get(profile_
)));
106 // Two state stores. The latter, which contains declarative rules, must be
107 // loaded immediately so that the rules are ready before we issue network
109 state_store_
.reset(new StateStore(
111 profile_
->GetPath().AppendASCII(extensions::kStateStoreName
),
113 state_store_notification_observer_
.reset(
114 new StateStoreNotificationObserver(state_store_
.get()));
116 rules_store_
.reset(new StateStore(
118 profile_
->GetPath().AppendASCII(extensions::kRulesStoreName
),
121 #if defined(OS_CHROMEOS)
122 const user_manager::User
* user
=
123 user_manager::UserManager::Get()->GetActiveUser();
124 policy::DeviceLocalAccount::Type device_local_account_type
;
125 if (user
&& policy::IsDeviceLocalAccountUser(user
->email(),
126 &device_local_account_type
)) {
127 device_local_account_management_policy_provider_
.reset(
128 new chromeos::DeviceLocalAccountManagementPolicyProvider(
129 device_local_account_type
));
131 #endif // defined(OS_CHROMEOS)
134 void ExtensionSystemImpl::Shared::RegisterManagementPolicyProviders() {
135 management_policy_
->RegisterProviders(
136 ExtensionManagementFactory::GetForBrowserContext(profile_
)
139 #if defined(OS_CHROMEOS)
140 if (device_local_account_management_policy_provider_
) {
141 management_policy_
->RegisterProvider(
142 device_local_account_management_policy_provider_
.get());
144 #endif // defined(OS_CHROMEOS)
146 management_policy_
->RegisterProvider(install_verifier_
.get());
151 class ContentVerifierDelegateImpl
: public ContentVerifierDelegate
{
153 explicit ContentVerifierDelegateImpl(ExtensionService
* service
)
154 : service_(service
->AsWeakPtr()), default_mode_(GetDefaultMode()) {}
156 ~ContentVerifierDelegateImpl() override
{}
158 Mode
ShouldBeVerified(const Extension
& extension
) override
{
159 #if defined(OS_CHROMEOS)
160 if (ExtensionAssetsManagerChromeOS::IsSharedInstall(&extension
))
161 return ContentVerifierDelegate::ENFORCE_STRICT
;
164 if (!extension
.is_extension() && !extension
.is_legacy_packaged_app())
165 return ContentVerifierDelegate::NONE
;
166 if (!Manifest::IsAutoUpdateableLocation(extension
.location()))
167 return ContentVerifierDelegate::NONE
;
169 if (!ManifestURL::UpdatesFromGallery(&extension
)) {
170 // It's possible that the webstore update url was overridden for testing
171 // so also consider extensions with the default (production) update url
172 // to be from the store as well.
173 GURL default_webstore_url
= extension_urls::GetDefaultWebstoreUpdateUrl();
174 if (ManifestURL::GetUpdateURL(&extension
) != default_webstore_url
)
175 return ContentVerifierDelegate::NONE
;
178 return default_mode_
;
181 const ContentVerifierKey
& PublicKey() override
{
182 static ContentVerifierKey
key(
183 extension_misc::kWebstoreSignaturesPublicKey
,
184 extension_misc::kWebstoreSignaturesPublicKeySize
);
188 GURL
GetSignatureFetchUrl(const std::string
& extension_id
,
189 const base::Version
& version
) override
{
190 // TODO(asargent) Factor out common code from the extension updater's
191 // ManifestFetchData class that can be shared for use here.
192 std::vector
<std::string
> parts
;
193 parts
.push_back("uc");
194 parts
.push_back("installsource=signature");
195 parts
.push_back("id=" + extension_id
);
196 parts
.push_back("v=" + version
.GetString());
197 std::string x_value
=
198 net::EscapeQueryParamValue(JoinString(parts
, "&"), true);
199 std::string query
= "response=redirect&x=" + x_value
;
201 GURL base_url
= extension_urls::GetWebstoreUpdateUrl();
202 GURL::Replacements replacements
;
203 replacements
.SetQuery(query
.c_str(), url::Component(0, query
.length()));
204 return base_url
.ReplaceComponents(replacements
);
207 std::set
<base::FilePath
> GetBrowserImagePaths(
208 const extensions::Extension
* extension
) override
{
209 return ExtensionsClient::Get()->GetBrowserImagePaths(extension
);
212 void VerifyFailed(const std::string
& extension_id
,
213 ContentVerifyJob::FailureReason reason
) override
{
216 ExtensionRegistry
* registry
= ExtensionRegistry::Get(service_
->profile());
217 const Extension
* extension
=
218 registry
->GetExtensionById(extension_id
, ExtensionRegistry::ENABLED
);
221 Mode mode
= ShouldBeVerified(*extension
);
222 if (mode
>= ContentVerifierDelegate::ENFORCE
) {
223 service_
->DisableExtension(extension_id
, Extension::DISABLE_CORRUPTED
);
224 ExtensionPrefs::Get(service_
->profile())
225 ->IncrementCorruptedDisableCount();
226 UMA_HISTOGRAM_BOOLEAN("Extensions.CorruptExtensionBecameDisabled", true);
227 UMA_HISTOGRAM_ENUMERATION("Extensions.CorruptExtensionDisabledReason",
228 reason
, ContentVerifyJob::FAILURE_REASON_MAX
);
229 } else if (!ContainsKey(would_be_disabled_ids_
, extension_id
)) {
230 UMA_HISTOGRAM_BOOLEAN("Extensions.CorruptExtensionWouldBeDisabled", true);
231 would_be_disabled_ids_
.insert(extension_id
);
235 static Mode
GetDefaultMode() {
236 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
238 Mode experiment_value
= NONE
;
239 const std::string group
= base::FieldTrialList::FindFullName(
240 kContentVerificationExperimentName
);
241 if (group
== "EnforceStrict")
242 experiment_value
= ContentVerifierDelegate::ENFORCE_STRICT
;
243 else if (group
== "Enforce")
244 experiment_value
= ContentVerifierDelegate::ENFORCE
;
245 else if (group
== "Bootstrap")
246 experiment_value
= ContentVerifierDelegate::BOOTSTRAP
;
248 // The field trial value that normally comes from the server can be
249 // overridden on the command line, which we don't want to allow since
250 // malware can set chrome command line flags. There isn't currently a way
251 // to find out what the server-provided value is in this case, so we
252 // conservatively default to the strictest mode if we detect our experiment
253 // name being overridden.
254 if (command_line
->HasSwitch(switches::kForceFieldTrials
)) {
255 std::string forced_trials
=
256 command_line
->GetSwitchValueASCII(switches::kForceFieldTrials
);
257 if (forced_trials
.find(kContentVerificationExperimentName
) !=
259 experiment_value
= ContentVerifierDelegate::ENFORCE_STRICT
;
262 Mode cmdline_value
= NONE
;
263 if (command_line
->HasSwitch(switches::kExtensionContentVerification
)) {
264 std::string switch_value
= command_line
->GetSwitchValueASCII(
265 switches::kExtensionContentVerification
);
266 if (switch_value
== switches::kExtensionContentVerificationBootstrap
)
267 cmdline_value
= ContentVerifierDelegate::BOOTSTRAP
;
268 else if (switch_value
== switches::kExtensionContentVerificationEnforce
)
269 cmdline_value
= ContentVerifierDelegate::ENFORCE
;
270 else if (switch_value
==
271 switches::kExtensionContentVerificationEnforceStrict
)
272 cmdline_value
= ContentVerifierDelegate::ENFORCE_STRICT
;
274 // If no value was provided (or the wrong one), just default to enforce.
275 cmdline_value
= ContentVerifierDelegate::ENFORCE
;
278 // We don't want to allow the command-line flags to eg disable enforcement
279 // if the experiment group says it should be on, or malware may just modify
280 // the command line flags. So return the more restrictive of the 2 values.
281 return std::max(experiment_value
, cmdline_value
);
285 base::WeakPtr
<ExtensionService
> service_
;
286 ContentVerifierDelegate::Mode default_mode_
;
288 // For reporting metrics in BOOTSTRAP mode, when an extension would be
289 // disabled if content verification was in ENFORCE mode.
290 std::set
<std::string
> would_be_disabled_ids_
;
292 DISALLOW_COPY_AND_ASSIGN(ContentVerifierDelegateImpl
);
297 void ExtensionSystemImpl::Shared::Init(bool extensions_enabled
) {
298 TRACE_EVENT0("browser,startup", "ExtensionSystemImpl::Shared::Init");
299 const base::CommandLine
* command_line
=
300 base::CommandLine::ForCurrentProcess();
302 navigation_observer_
.reset(new NavigationObserver(profile_
));
304 bool allow_noisy_errors
= !command_line
->HasSwitch(switches::kNoErrorDialogs
);
305 ExtensionErrorReporter::Init(allow_noisy_errors
);
307 shared_user_script_master_
.reset(new SharedUserScriptMaster(profile_
));
308 declarative_user_script_manager_
.reset(
309 new DeclarativeUserScriptManager(profile_
));
311 // ExtensionService depends on RuntimeData.
312 runtime_data_
.reset(new RuntimeData(ExtensionRegistry::Get(profile_
)));
314 bool autoupdate_enabled
= !profile_
->IsGuestSession();
315 #if defined(OS_CHROMEOS)
316 if (!extensions_enabled
)
317 autoupdate_enabled
= false;
318 #endif // defined(OS_CHROMEOS)
319 extension_service_
.reset(new ExtensionService(
320 profile_
, base::CommandLine::ForCurrentProcess(),
321 profile_
->GetPath().AppendASCII(extensions::kInstallDirectoryName
),
322 ExtensionPrefs::Get(profile_
), Blacklist::Get(profile_
),
323 autoupdate_enabled
, extensions_enabled
, &ready_
));
325 // These services must be registered before the ExtensionService tries to
326 // load any extensions.
328 install_verifier_
.reset(
329 new InstallVerifier(ExtensionPrefs::Get(profile_
), profile_
));
330 install_verifier_
->Init();
331 content_verifier_
= new ContentVerifier(
332 profile_
, new ContentVerifierDelegateImpl(extension_service_
.get()));
333 ContentVerifierDelegate::Mode mode
=
334 ContentVerifierDelegateImpl::GetDefaultMode();
335 #if defined(OS_CHROMEOS)
336 mode
= std::max(mode
, ContentVerifierDelegate::BOOTSTRAP
);
337 #endif // defined(OS_CHROMEOS)
338 if (mode
>= ContentVerifierDelegate::BOOTSTRAP
)
339 content_verifier_
->Start();
340 info_map()->SetContentVerifier(content_verifier_
.get());
342 management_policy_
.reset(new ManagementPolicy
);
343 RegisterManagementPolicyProviders();
346 bool skip_session_extensions
= false;
347 #if defined(OS_CHROMEOS)
348 // Skip loading session extensions if we are not in a user session.
349 skip_session_extensions
= !chromeos::LoginState::Get()->IsUserLoggedIn();
350 if (chrome::IsRunningInForcedAppMode()) {
351 extension_service_
->component_loader()->
352 AddDefaultComponentExtensionsForKioskMode(skip_session_extensions
);
354 extension_service_
->component_loader()->AddDefaultComponentExtensions(
355 skip_session_extensions
);
358 extension_service_
->component_loader()->AddDefaultComponentExtensions(
359 skip_session_extensions
);
361 if (command_line
->HasSwitch(switches::kLoadComponentExtension
)) {
362 base::CommandLine::StringType path_list
=
363 command_line
->GetSwitchValueNative(switches::kLoadComponentExtension
);
364 base::StringTokenizerT
<base::CommandLine::StringType
,
365 base::CommandLine::StringType::const_iterator
>
366 t(path_list
, FILE_PATH_LITERAL(","));
367 while (t
.GetNext()) {
368 // Load the component extension manifest synchronously.
369 // Blocking the UI thread is acceptable here since
370 // this flag designated for developers.
371 base::ThreadRestrictions::ScopedAllowIO allow_io
;
372 extension_service_
->component_loader()->AddOrReplace(
373 base::FilePath(t
.token()));
376 extension_service_
->Init();
378 // Make the chrome://extension-icon/ resource available.
379 content::URLDataSource::Add(profile_
, new ExtensionIconSource(profile_
));
381 error_console_
.reset(new ErrorConsole(profile_
));
382 quota_service_
.reset(new QuotaService
);
384 if (extensions_enabled
) {
385 // Load any extensions specified with --load-extension.
386 // TODO(yoz): Seems like this should move into ExtensionService::Init.
387 // But maybe it's no longer important.
388 if (command_line
->HasSwitch(switches::kLoadExtension
)) {
389 base::CommandLine::StringType path_list
=
390 command_line
->GetSwitchValueNative(switches::kLoadExtension
);
391 base::StringTokenizerT
<base::CommandLine::StringType
,
392 base::CommandLine::StringType::const_iterator
>
393 t(path_list
, FILE_PATH_LITERAL(","));
394 while (t
.GetNext()) {
395 std::string extension_id
;
396 UnpackedInstaller::Create(extension_service_
.get())->
397 LoadFromCommandLine(base::FilePath(t
.token()), &extension_id
);
403 void ExtensionSystemImpl::Shared::Shutdown() {
404 if (content_verifier_
.get())
405 content_verifier_
->Shutdown();
406 if (extension_service_
)
407 extension_service_
->Shutdown();
410 StateStore
* ExtensionSystemImpl::Shared::state_store() {
411 return state_store_
.get();
414 StateStore
* ExtensionSystemImpl::Shared::rules_store() {
415 return rules_store_
.get();
418 ExtensionService
* ExtensionSystemImpl::Shared::extension_service() {
419 return extension_service_
.get();
422 RuntimeData
* ExtensionSystemImpl::Shared::runtime_data() {
423 return runtime_data_
.get();
426 ManagementPolicy
* ExtensionSystemImpl::Shared::management_policy() {
427 return management_policy_
.get();
430 SharedUserScriptMaster
*
431 ExtensionSystemImpl::Shared::shared_user_script_master() {
432 return shared_user_script_master_
.get();
435 DeclarativeUserScriptManager
*
436 ExtensionSystemImpl::Shared::declarative_user_script_manager() {
437 return declarative_user_script_manager_
.get();
440 InfoMap
* ExtensionSystemImpl::Shared::info_map() {
441 if (!extension_info_map_
.get())
442 extension_info_map_
= new InfoMap();
443 return extension_info_map_
.get();
446 LazyBackgroundTaskQueue
*
447 ExtensionSystemImpl::Shared::lazy_background_task_queue() {
448 return lazy_background_task_queue_
.get();
451 EventRouter
* ExtensionSystemImpl::Shared::event_router() {
452 return event_router_
.get();
455 ErrorConsole
* ExtensionSystemImpl::Shared::error_console() {
456 return error_console_
.get();
459 InstallVerifier
* ExtensionSystemImpl::Shared::install_verifier() {
460 return install_verifier_
.get();
463 QuotaService
* ExtensionSystemImpl::Shared::quota_service() {
464 return quota_service_
.get();
467 ContentVerifier
* ExtensionSystemImpl::Shared::content_verifier() {
468 return content_verifier_
.get();
472 // ExtensionSystemImpl
475 ExtensionSystemImpl::ExtensionSystemImpl(Profile
* profile
)
476 : profile_(profile
) {
477 shared_
= ExtensionSystemSharedFactory::GetForBrowserContext(profile
);
479 if (!profile
->IsOffTheRecord()) {
480 shared_
->InitPrefs();
484 ExtensionSystemImpl::~ExtensionSystemImpl() {
487 void ExtensionSystemImpl::Shutdown() {
490 void ExtensionSystemImpl::InitForRegularProfile(bool extensions_enabled
) {
491 TRACE_EVENT0("browser,startup", "ExtensionSystemImpl::InitForRegularProfile");
492 DCHECK(!profile_
->IsOffTheRecord());
493 if (shared_user_script_master() || extension_service())
494 return; // Already initialized.
496 // The InfoMap needs to be created before the ProcessManager.
498 shared_
->Init(extensions_enabled
);
501 ExtensionService
* ExtensionSystemImpl::extension_service() {
502 return shared_
->extension_service();
505 RuntimeData
* ExtensionSystemImpl::runtime_data() {
506 return shared_
->runtime_data();
509 ManagementPolicy
* ExtensionSystemImpl::management_policy() {
510 return shared_
->management_policy();
513 SharedUserScriptMaster
* ExtensionSystemImpl::shared_user_script_master() {
514 return shared_
->shared_user_script_master();
517 DeclarativeUserScriptManager
*
518 ExtensionSystemImpl::declarative_user_script_manager() {
519 return shared_
->declarative_user_script_manager();
522 StateStore
* ExtensionSystemImpl::state_store() {
523 return shared_
->state_store();
526 StateStore
* ExtensionSystemImpl::rules_store() {
527 return shared_
->rules_store();
530 InfoMap
* ExtensionSystemImpl::info_map() { return shared_
->info_map(); }
532 LazyBackgroundTaskQueue
* ExtensionSystemImpl::lazy_background_task_queue() {
533 return shared_
->lazy_background_task_queue();
536 EventRouter
* ExtensionSystemImpl::event_router() {
537 return shared_
->event_router();
540 const OneShotEvent
& ExtensionSystemImpl::ready() const {
541 return shared_
->ready();
544 ErrorConsole
* ExtensionSystemImpl::error_console() {
545 return shared_
->error_console();
548 InstallVerifier
* ExtensionSystemImpl::install_verifier() {
549 return shared_
->install_verifier();
552 QuotaService
* ExtensionSystemImpl::quota_service() {
553 return shared_
->quota_service();
556 ContentVerifier
* ExtensionSystemImpl::content_verifier() {
557 return shared_
->content_verifier();
560 scoped_ptr
<ExtensionSet
> ExtensionSystemImpl::GetDependentExtensions(
561 const Extension
* extension
) {
562 return extension_service()->shared_module_service()->GetDependentExtensions(
566 void ExtensionSystemImpl::RegisterExtensionWithRequestContexts(
567 const Extension
* extension
) {
568 base::Time install_time
;
569 if (extension
->location() != Manifest::COMPONENT
) {
570 install_time
= ExtensionPrefs::Get(profile_
)->
571 GetInstallTime(extension
->id());
573 bool incognito_enabled
= util::IsIncognitoEnabled(extension
->id(), profile_
);
575 bool notifications_disabled
= false;
576 #if defined(ENABLE_NOTIFICATIONS)
577 message_center::NotifierId
notifier_id(
578 message_center::NotifierId::APPLICATION
,
581 DesktopNotificationService
* notification_service
=
582 DesktopNotificationServiceFactory::GetForProfile(profile_
);
583 notifications_disabled
=
584 !notification_service
->IsNotifierEnabled(notifier_id
);
587 BrowserThread::PostTask(
588 BrowserThread::IO
, FROM_HERE
,
589 base::Bind(&InfoMap::AddExtension
, info_map(),
590 make_scoped_refptr(extension
), install_time
,
591 incognito_enabled
, notifications_disabled
));
594 void ExtensionSystemImpl::UnregisterExtensionWithRequestContexts(
595 const std::string
& extension_id
,
596 const UnloadedExtensionInfo::Reason reason
) {
597 BrowserThread::PostTask(
600 base::Bind(&InfoMap::RemoveExtension
, info_map(), extension_id
, reason
));
603 } // namespace extensions