1 // Copyright (c) 2012 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/external_provider_impl.h"
10 #include "base/command_line.h"
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "base/memory/linked_ptr.h"
14 #include "base/metrics/field_trial.h"
15 #include "base/prefs/pref_service.h"
16 #include "base/strings/string_util.h"
17 #include "base/trace_event/trace_event.h"
18 #include "base/values.h"
19 #include "base/version.h"
20 #include "chrome/browser/app_mode/app_mode_utils.h"
21 #include "chrome/browser/browser_process.h"
22 #include "chrome/browser/extensions/extension_management.h"
23 #include "chrome/browser/extensions/extension_service.h"
24 #include "chrome/browser/extensions/external_component_loader.h"
25 #include "chrome/browser/extensions/external_policy_loader.h"
26 #include "chrome/browser/extensions/external_pref_loader.h"
27 #include "chrome/browser/profiles/profile.h"
28 #include "chrome/common/chrome_paths.h"
29 #include "chrome/common/chrome_switches.h"
30 #include "chrome/common/pref_names.h"
31 #include "components/crx_file/id_util.h"
32 #include "content/public/browser/browser_thread.h"
33 #include "extensions/browser/extension_system.h"
34 #include "extensions/browser/external_provider_interface.h"
35 #include "extensions/common/extension.h"
36 #include "extensions/common/manifest.h"
37 #include "ui/base/l10n/l10n_util.h"
39 #if defined(OS_CHROMEOS)
40 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
41 #include "chrome/browser/chromeos/customization/customization_document.h"
42 #include "chrome/browser/chromeos/extensions/device_local_account_external_policy_loader.h"
43 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
44 #include "chrome/browser/chromeos/policy/device_local_account.h"
45 #include "chrome/browser/chromeos/policy/device_local_account_policy_service.h"
46 #include "chrome/browser/chromeos/profiles/profile_helper.h"
47 #include "components/user_manager/user.h"
49 #include "chrome/browser/extensions/default_apps.h"
53 #include "chrome/browser/extensions/external_registry_loader_win.h"
56 using content::BrowserThread
;
58 namespace extensions
{
60 // Constants for keeping track of extension preferences in a dictionary.
61 const char ExternalProviderImpl::kInstallParam
[] = "install_parameter";
62 const char ExternalProviderImpl::kExternalCrx
[] = "external_crx";
63 const char ExternalProviderImpl::kExternalVersion
[] = "external_version";
64 const char ExternalProviderImpl::kExternalUpdateUrl
[] = "external_update_url";
65 const char ExternalProviderImpl::kIsBookmarkApp
[] = "is_bookmark_app";
66 const char ExternalProviderImpl::kIsFromWebstore
[] = "is_from_webstore";
67 const char ExternalProviderImpl::kKeepIfPresent
[] = "keep_if_present";
68 const char ExternalProviderImpl::kWasInstalledByOem
[] = "was_installed_by_oem";
69 const char ExternalProviderImpl::kSupportedLocales
[] = "supported_locales";
70 const char ExternalProviderImpl::kMayBeUntrusted
[] = "may_be_untrusted";
71 const char ExternalProviderImpl::kMinProfileCreatedByVersion
[] =
72 "min_profile_created_by_version";
74 ExternalProviderImpl::ExternalProviderImpl(
75 VisitorInterface
* service
,
76 const scoped_refptr
<ExternalLoader
>& loader
,
78 Manifest::Location crx_location
,
79 Manifest::Location download_location
,
81 : crx_location_(crx_location
),
82 download_location_(download_location
),
87 creation_flags_(creation_flags
),
88 auto_acknowledge_(false),
89 install_immediately_(false) {
93 ExternalProviderImpl::~ExternalProviderImpl() {
94 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
95 loader_
->OwnerShutdown();
98 void ExternalProviderImpl::VisitRegisteredExtension() {
99 // The loader will call back to SetPrefs.
100 loader_
->StartLoading();
103 void ExternalProviderImpl::SetPrefs(base::DictionaryValue
* prefs
) {
104 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
106 // Check if the service is still alive. It is possible that it went
107 // away while |loader_| was working on the FILE thread.
108 if (!service_
) return;
111 ready_
= true; // Queries for extensions are allowed from this point.
113 // Set of unsupported extensions that need to be deleted from prefs_.
114 std::set
<std::string
> unsupported_extensions
;
116 // Notify ExtensionService about all the extensions this provider has.
117 for (base::DictionaryValue::Iterator
i(*prefs_
); !i
.IsAtEnd(); i
.Advance()) {
118 const std::string
& extension_id
= i
.key();
119 const base::DictionaryValue
* extension
= NULL
;
121 if (!crx_file::id_util::IdIsValid(extension_id
)) {
122 LOG(WARNING
) << "Malformed extension dictionary: key "
123 << extension_id
.c_str() << " is not a valid id.";
127 if (!i
.value().GetAsDictionary(&extension
)) {
128 LOG(WARNING
) << "Malformed extension dictionary: key "
129 << extension_id
.c_str()
130 << " has a value that is not a dictionary.";
134 base::FilePath::StringType external_crx
;
135 const base::Value
* external_version_value
= NULL
;
136 std::string external_version
;
137 std::string external_update_url
;
139 bool has_external_crx
= extension
->GetString(kExternalCrx
, &external_crx
);
141 bool has_external_version
= false;
142 if (extension
->Get(kExternalVersion
, &external_version_value
)) {
143 if (external_version_value
->IsType(base::Value::TYPE_STRING
)) {
144 external_version_value
->GetAsString(&external_version
);
145 has_external_version
= true;
147 LOG(WARNING
) << "Malformed extension dictionary for extension: "
148 << extension_id
.c_str() << ". " << kExternalVersion
149 << " value must be a string.";
154 bool has_external_update_url
= extension
->GetString(kExternalUpdateUrl
,
155 &external_update_url
);
156 if (has_external_crx
!= has_external_version
) {
157 LOG(WARNING
) << "Malformed extension dictionary for extension: "
158 << extension_id
.c_str() << ". " << kExternalCrx
159 << " and " << kExternalVersion
<< " must be used together.";
163 if (has_external_crx
== has_external_update_url
) {
164 LOG(WARNING
) << "Malformed extension dictionary for extension: "
165 << extension_id
.c_str() << ". Exactly one of the "
166 << "followng keys should be used: " << kExternalCrx
167 << ", " << kExternalUpdateUrl
<< ".";
171 // Check that extension supports current browser locale.
172 const base::ListValue
* supported_locales
= NULL
;
173 if (extension
->GetList(kSupportedLocales
, &supported_locales
)) {
174 std::vector
<std::string
> browser_locales
;
175 l10n_util::GetParentLocales(g_browser_process
->GetApplicationLocale(),
178 size_t num_locales
= supported_locales
->GetSize();
179 bool locale_supported
= false;
180 for (size_t j
= 0; j
< num_locales
; j
++) {
181 std::string current_locale
;
182 if (supported_locales
->GetString(j
, ¤t_locale
) &&
183 l10n_util::IsValidLocaleSyntax(current_locale
)) {
184 current_locale
= l10n_util::NormalizeLocale(current_locale
);
185 if (std::find(browser_locales
.begin(), browser_locales
.end(),
186 current_locale
) != browser_locales
.end()) {
187 locale_supported
= true;
191 LOG(WARNING
) << "Unrecognized locale '" << current_locale
192 << "' found as supported locale for extension: "
197 if (!locale_supported
) {
198 unsupported_extensions
.insert(extension_id
);
199 VLOG(1) << "Skip installing (or uninstall) external extension: "
200 << extension_id
<< " because the extension doesn't support "
201 << "the browser locale.";
206 int creation_flags
= creation_flags_
;
207 bool is_bookmark_app
;
208 if (extension
->GetBoolean(kIsBookmarkApp
, &is_bookmark_app
) &&
210 creation_flags
|= Extension::FROM_BOOKMARK
;
212 bool is_from_webstore
= false;
213 if (extension
->GetBoolean(kIsFromWebstore
, &is_from_webstore
) &&
215 creation_flags
|= Extension::FROM_WEBSTORE
;
217 bool keep_if_present
= false;
218 if (extension
->GetBoolean(kKeepIfPresent
, &keep_if_present
) &&
219 keep_if_present
&& profile_
) {
220 ExtensionServiceInterface
* extension_service
=
221 ExtensionSystem::Get(profile_
)->extension_service();
222 const Extension
* extension
= extension_service
?
223 extension_service
->GetExtensionById(extension_id
, true) : NULL
;
225 unsupported_extensions
.insert(extension_id
);
226 VLOG(1) << "Skip installing (or uninstall) external extension: "
227 << extension_id
<< " because the extension should be kept "
228 << "only if it is already installed.";
232 bool was_installed_by_oem
= false;
233 if (extension
->GetBoolean(kWasInstalledByOem
, &was_installed_by_oem
) &&
234 was_installed_by_oem
) {
235 creation_flags
|= Extension::WAS_INSTALLED_BY_OEM
;
237 bool may_be_untrusted
= false;
238 if (extension
->GetBoolean(kMayBeUntrusted
, &may_be_untrusted
) &&
240 creation_flags
|= Extension::MAY_BE_UNTRUSTED
;
243 if (!ExternalProviderImpl::HandleMinProfileVersion(extension
, extension_id
,
244 &unsupported_extensions
))
247 std::string install_parameter
;
248 extension
->GetString(kInstallParam
, &install_parameter
);
250 if (has_external_crx
) {
251 if (crx_location_
== Manifest::INVALID_LOCATION
) {
252 LOG(WARNING
) << "This provider does not support installing external "
253 << "extensions from crx files.";
256 if (external_crx
.find(base::FilePath::kParentDirectory
) !=
257 base::StringPiece::npos
) {
258 LOG(WARNING
) << "Path traversal not allowed in path: "
259 << external_crx
.c_str();
263 // If the path is relative, and the provider has a base path,
264 // build the absolute path to the crx file.
265 base::FilePath
path(external_crx
);
266 if (!path
.IsAbsolute()) {
267 base::FilePath base_path
= loader_
->GetBaseCrxFilePath();
268 if (base_path
.empty()) {
269 LOG(WARNING
) << "File path " << external_crx
.c_str()
270 << " is relative. An absolute path is required.";
273 path
= base_path
.Append(external_crx
);
276 Version
version(external_version
);
277 if (!version
.IsValid()) {
278 LOG(WARNING
) << "Malformed extension dictionary for extension: "
279 << extension_id
.c_str() << ". Invalid version string \""
280 << external_version
<< "\".";
283 service_
->OnExternalExtensionFileFound(extension_id
, &version
, path
,
284 crx_location_
, creation_flags
,
286 install_immediately_
);
287 } else { // if (has_external_update_url)
288 CHECK(has_external_update_url
); // Checking of keys above ensures this.
289 if (download_location_
== Manifest::INVALID_LOCATION
) {
290 LOG(WARNING
) << "This provider does not support installing external "
291 << "extensions from update URLs.";
294 GURL
update_url(external_update_url
);
295 if (!update_url
.is_valid()) {
296 LOG(WARNING
) << "Malformed extension dictionary for extension: "
297 << extension_id
.c_str() << ". Key " << kExternalUpdateUrl
298 << " has value \"" << external_update_url
299 << "\", which is not a valid URL.";
302 service_
->OnExternalExtensionUpdateUrlFound(extension_id
,
311 for (std::set
<std::string
>::iterator it
= unsupported_extensions
.begin();
312 it
!= unsupported_extensions
.end(); ++it
) {
313 // Remove extension for the list of know external extensions. The extension
314 // will be uninstalled later because provider doesn't provide it anymore.
315 prefs_
->Remove(*it
, NULL
);
318 service_
->OnExternalProviderReady(this);
321 void ExternalProviderImpl::ServiceShutdown() {
325 bool ExternalProviderImpl::IsReady() const {
329 bool ExternalProviderImpl::HasExtension(
330 const std::string
& id
) const {
331 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
334 return prefs_
->HasKey(id
);
337 bool ExternalProviderImpl::GetExtensionDetails(
338 const std::string
& id
, Manifest::Location
* location
,
339 scoped_ptr
<Version
>* version
) const {
340 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
343 base::DictionaryValue
* extension
= NULL
;
344 if (!prefs_
->GetDictionary(id
, &extension
))
347 Manifest::Location loc
= Manifest::INVALID_LOCATION
;
348 if (extension
->HasKey(kExternalUpdateUrl
)) {
349 loc
= download_location_
;
351 } else if (extension
->HasKey(kExternalCrx
)) {
354 std::string external_version
;
355 if (!extension
->GetString(kExternalVersion
, &external_version
))
359 version
->reset(new Version(external_version
));
362 NOTREACHED(); // Chrome should not allow prefs to get into this state.
372 bool ExternalProviderImpl::HandleMinProfileVersion(
373 const base::DictionaryValue
* extension
,
374 const std::string
& extension_id
,
375 std::set
<std::string
>* unsupported_extensions
) {
376 std::string min_profile_created_by_version
;
378 extension
->GetString(kMinProfileCreatedByVersion
,
379 &min_profile_created_by_version
)) {
380 Version
profile_version(
381 profile_
->GetPrefs()->GetString(prefs::kProfileCreatedByVersion
));
382 Version
min_version(min_profile_created_by_version
);
383 if (min_version
.IsValid() && profile_version
.CompareTo(min_version
) < 0) {
384 unsupported_extensions
->insert(extension_id
);
385 VLOG(1) << "Skip installing (or uninstall) external extension: "
387 << " profile.created_by_version: " << profile_version
.GetString()
388 << " min_profile_created_by_version: "
389 << min_profile_created_by_version
;
397 void ExternalProviderImpl::CreateExternalProviders(
398 VisitorInterface
* service
,
400 ProviderCollection
* provider_list
) {
401 TRACE_EVENT0("browser,startup",
402 "ExternalProviderImpl::CreateExternalProviders");
403 scoped_refptr
<ExternalLoader
> external_loader
;
404 scoped_refptr
<ExternalLoader
> external_recommended_loader
;
405 extensions::Manifest::Location crx_location
= Manifest::INVALID_LOCATION
;
406 #if defined(OS_CHROMEOS)
407 policy::BrowserPolicyConnectorChromeOS
* connector
=
408 g_browser_process
->platform_part()->browser_policy_connector_chromeos();
409 bool is_chrome_os_public_session
= false;
410 const user_manager::User
* user
=
411 chromeos::ProfileHelper::Get()->GetUserByProfile(profile
);
412 policy::DeviceLocalAccount::Type account_type
;
414 connector
->IsEnterpriseManaged() &&
415 policy::IsDeviceLocalAccountUser(user
->email(), &account_type
)) {
416 if (account_type
== policy::DeviceLocalAccount::TYPE_PUBLIC_SESSION
)
417 is_chrome_os_public_session
= true;
418 policy::DeviceLocalAccountPolicyBroker
* broker
=
419 connector
->GetDeviceLocalAccountPolicyService()->GetBrokerForUser(
422 external_loader
= broker
->extension_loader();
423 crx_location
= Manifest::EXTERNAL_POLICY
;
428 external_loader
= new ExternalPolicyLoader(
429 ExtensionManagementFactory::GetForBrowserContext(profile
),
430 ExternalPolicyLoader::FORCED
);
431 external_recommended_loader
= new ExternalPolicyLoader(
432 ExtensionManagementFactory::GetForBrowserContext(profile
),
433 ExternalPolicyLoader::RECOMMENDED
);
436 external_loader
= new ExternalPolicyLoader(
437 ExtensionManagementFactory::GetForBrowserContext(profile
),
438 ExternalPolicyLoader::FORCED
);
439 external_recommended_loader
= new ExternalPolicyLoader(
440 ExtensionManagementFactory::GetForBrowserContext(profile
),
441 ExternalPolicyLoader::RECOMMENDED
);
444 // Policies are mandatory so they can't be skipped with command line flag.
445 if (external_loader
.get()) {
446 provider_list
->push_back(
447 linked_ptr
<ExternalProviderInterface
>(
448 new ExternalProviderImpl(
453 Manifest::EXTERNAL_POLICY_DOWNLOAD
,
454 Extension::NO_FLAGS
)));
457 // Load the KioskAppExternalProvider when running in kiosk mode.
458 if (chrome::IsRunningInForcedAppMode()) {
459 #if defined(OS_CHROMEOS)
460 chromeos::KioskAppManager
* kiosk_app_manager
=
461 chromeos::KioskAppManager::Get();
462 DCHECK(kiosk_app_manager
);
463 if (kiosk_app_manager
&& !kiosk_app_manager
->external_loader_created()) {
464 scoped_ptr
<ExternalProviderImpl
> kiosk_app_provider(
465 new ExternalProviderImpl(
466 service
, kiosk_app_manager
->CreateExternalLoader(), profile
,
467 Manifest::EXTERNAL_PREF
, Manifest::INVALID_LOCATION
,
468 Extension::NO_FLAGS
));
469 kiosk_app_provider
->set_auto_acknowledge(true);
470 kiosk_app_provider
->set_install_immediately(true);
471 provider_list
->push_back(
472 linked_ptr
<ExternalProviderInterface
>(kiosk_app_provider
.release()));
478 // Extensions provided by recommended policies.
479 if (external_recommended_loader
.get()) {
480 provider_list
->push_back(linked_ptr
<ExternalProviderInterface
>(
481 new ExternalProviderImpl(service
,
482 external_recommended_loader
,
485 Manifest::EXTERNAL_PREF_DOWNLOAD
,
486 Extension::NO_FLAGS
)));
489 // In tests don't install extensions from default external sources.
490 // It would only slowdown tests and make them flaky.
491 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
492 switches::kDisableDefaultApps
))
495 // On Mac OS, items in /Library/... should be written by the superuser.
496 // Check that all components of the path are writable by root only.
497 ExternalPrefLoader::Options check_admin_permissions_on_mac
;
498 #if defined(OS_MACOSX)
499 check_admin_permissions_on_mac
=
500 ExternalPrefLoader::ENSURE_PATH_CONTROLLED_BY_ADMIN
;
502 check_admin_permissions_on_mac
= ExternalPrefLoader::NONE
;
506 int bundled_extension_creation_flags
= Extension::NO_FLAGS
;
508 #if defined(OS_CHROMEOS)
509 bundled_extension_creation_flags
= Extension::FROM_WEBSTORE
|
510 Extension::WAS_INSTALLED_BY_DEFAULT
;
512 if (!is_chrome_os_public_session
) {
513 int external_apps_path_id
= profile
->IsSupervised() ?
514 chrome::DIR_SUPERVISED_USERS_DEFAULT_APPS
:
515 chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS
;
516 ExternalPrefLoader::Options pref_load_flags
=
517 profile
->IsNewProfile()
518 ? ExternalPrefLoader::DELAY_LOAD_UNTIL_PRIORITY_SYNC
519 : ExternalPrefLoader::NONE
;
520 provider_list
->push_back(
521 linked_ptr
<ExternalProviderInterface
>(new ExternalProviderImpl(
522 service
, new ExternalPrefLoader(external_apps_path_id
,
523 pref_load_flags
, profile
),
524 profile
, Manifest::EXTERNAL_PREF
, Manifest::EXTERNAL_PREF_DOWNLOAD
,
525 bundled_extension_creation_flags
)));
528 int oem_extension_creation_flags
=
529 bundled_extension_creation_flags
| Extension::WAS_INSTALLED_BY_OEM
;
530 chromeos::ServicesCustomizationDocument
* customization
=
531 chromeos::ServicesCustomizationDocument::GetInstance();
532 provider_list
->push_back(linked_ptr
<ExternalProviderInterface
>(
533 new ExternalProviderImpl(service
,
534 customization
->CreateExternalLoader(profile
),
536 Manifest::EXTERNAL_PREF
,
537 Manifest::EXTERNAL_PREF_DOWNLOAD
,
538 oem_extension_creation_flags
)));
540 #elif defined(OS_LINUX)
541 if (!profile
->IsLegacySupervised()) {
542 provider_list
->push_back(
543 linked_ptr
<ExternalProviderInterface
>(
544 new ExternalProviderImpl(
546 new ExternalPrefLoader(
547 chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS
,
548 ExternalPrefLoader::NONE
,
551 Manifest::EXTERNAL_PREF
,
552 Manifest::EXTERNAL_PREF_DOWNLOAD
,
553 bundled_extension_creation_flags
)));
557 if (!profile
->IsLegacySupervised()) {
559 provider_list
->push_back(
560 linked_ptr
<ExternalProviderInterface
>(
561 new ExternalProviderImpl(
563 new ExternalRegistryLoader
,
565 Manifest::EXTERNAL_REGISTRY
,
566 Manifest::EXTERNAL_PREF_DOWNLOAD
,
567 Extension::NO_FLAGS
)));
569 provider_list
->push_back(
570 linked_ptr
<ExternalProviderInterface
>(
571 new ExternalProviderImpl(
573 new ExternalPrefLoader(chrome::DIR_EXTERNAL_EXTENSIONS
,
574 check_admin_permissions_on_mac
,
577 Manifest::EXTERNAL_PREF
,
578 Manifest::EXTERNAL_PREF_DOWNLOAD
,
579 bundled_extension_creation_flags
)));
581 // Define a per-user source of external extensions.
582 #if defined(OS_MACOSX)
583 provider_list
->push_back(
584 linked_ptr
<ExternalProviderInterface
>(
585 new ExternalProviderImpl(
587 new ExternalPrefLoader(chrome::DIR_USER_EXTERNAL_EXTENSIONS
,
588 ExternalPrefLoader::NONE
,
591 Manifest::EXTERNAL_PREF
,
592 Manifest::EXTERNAL_PREF_DOWNLOAD
,
593 Extension::NO_FLAGS
)));
597 #if !defined(OS_CHROMEOS)
598 // The default apps are installed as INTERNAL but use the external
599 // extension installer codeflow.
600 provider_list
->push_back(
601 linked_ptr
<ExternalProviderInterface
>(
602 new default_apps::Provider(
605 new ExternalPrefLoader(chrome::DIR_DEFAULT_APPS
,
606 ExternalPrefLoader::NONE
,
610 Extension::FROM_WEBSTORE
|
611 Extension::WAS_INSTALLED_BY_DEFAULT
)));
615 provider_list
->push_back(
616 linked_ptr
<ExternalProviderInterface
>(
617 new ExternalProviderImpl(
619 new ExternalComponentLoader(profile
),
621 Manifest::INVALID_LOCATION
,
622 Manifest::EXTERNAL_COMPONENT
,
623 Extension::FROM_WEBSTORE
| Extension::WAS_INSTALLED_BY_DEFAULT
)));
626 } // namespace extensions