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/policy/profile_policy_connector.h"
28 #include "chrome/browser/policy/profile_policy_connector_factory.h"
29 #include "chrome/browser/profiles/profile.h"
30 #include "chrome/common/chrome_paths.h"
31 #include "chrome/common/chrome_switches.h"
32 #include "chrome/common/pref_names.h"
33 #include "components/crx_file/id_util.h"
34 #include "content/public/browser/browser_thread.h"
35 #include "extensions/browser/extension_system.h"
36 #include "extensions/browser/external_provider_interface.h"
37 #include "extensions/common/extension.h"
38 #include "extensions/common/manifest.h"
39 #include "ui/base/l10n/l10n_util.h"
41 #if defined(OS_CHROMEOS)
42 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
43 #include "chrome/browser/chromeos/customization/customization_document.h"
44 #include "chrome/browser/chromeos/extensions/device_local_account_external_policy_loader.h"
45 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
46 #include "chrome/browser/chromeos/policy/device_local_account.h"
47 #include "chrome/browser/chromeos/policy/device_local_account_policy_service.h"
48 #include "chrome/browser/chromeos/profiles/profile_helper.h"
49 #include "components/user_manager/user.h"
51 #include "chrome/browser/extensions/default_apps.h"
55 #include "chrome/browser/extensions/external_registry_loader_win.h"
58 using content::BrowserThread
;
60 namespace extensions
{
62 // Constants for keeping track of extension preferences in a dictionary.
63 const char ExternalProviderImpl::kInstallParam
[] = "install_parameter";
64 const char ExternalProviderImpl::kExternalCrx
[] = "external_crx";
65 const char ExternalProviderImpl::kExternalVersion
[] = "external_version";
66 const char ExternalProviderImpl::kExternalUpdateUrl
[] = "external_update_url";
67 const char ExternalProviderImpl::kIsBookmarkApp
[] = "is_bookmark_app";
68 const char ExternalProviderImpl::kIsFromWebstore
[] = "is_from_webstore";
69 const char ExternalProviderImpl::kKeepIfPresent
[] = "keep_if_present";
70 const char ExternalProviderImpl::kWasInstalledByOem
[] = "was_installed_by_oem";
71 const char ExternalProviderImpl::kSupportedLocales
[] = "supported_locales";
72 const char ExternalProviderImpl::kMayBeUntrusted
[] = "may_be_untrusted";
73 const char ExternalProviderImpl::kMinProfileCreatedByVersion
[] =
74 "min_profile_created_by_version";
75 const char ExternalProviderImpl::kDoNotInstallForEnterprise
[] =
76 "do_not_install_for_enterprise";
78 ExternalProviderImpl::ExternalProviderImpl(
79 VisitorInterface
* service
,
80 const scoped_refptr
<ExternalLoader
>& loader
,
82 Manifest::Location crx_location
,
83 Manifest::Location download_location
,
85 : crx_location_(crx_location
),
86 download_location_(download_location
),
91 creation_flags_(creation_flags
),
92 auto_acknowledge_(false),
93 install_immediately_(false) {
97 ExternalProviderImpl::~ExternalProviderImpl() {
98 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
99 loader_
->OwnerShutdown();
102 void ExternalProviderImpl::VisitRegisteredExtension() {
103 // The loader will call back to SetPrefs.
104 loader_
->StartLoading();
107 void ExternalProviderImpl::SetPrefs(base::DictionaryValue
* prefs
) {
108 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
110 // Check if the service is still alive. It is possible that it went
111 // away while |loader_| was working on the FILE thread.
112 if (!service_
) return;
115 ready_
= true; // Queries for extensions are allowed from this point.
117 // Set of unsupported extensions that need to be deleted from prefs_.
118 std::set
<std::string
> unsupported_extensions
;
120 // Notify ExtensionService about all the extensions this provider has.
121 for (base::DictionaryValue::Iterator
i(*prefs_
); !i
.IsAtEnd(); i
.Advance()) {
122 const std::string
& extension_id
= i
.key();
123 const base::DictionaryValue
* extension
= NULL
;
125 if (!crx_file::id_util::IdIsValid(extension_id
)) {
126 LOG(WARNING
) << "Malformed extension dictionary: key "
127 << extension_id
.c_str() << " is not a valid id.";
131 if (!i
.value().GetAsDictionary(&extension
)) {
132 LOG(WARNING
) << "Malformed extension dictionary: key "
133 << extension_id
.c_str()
134 << " has a value that is not a dictionary.";
138 base::FilePath::StringType external_crx
;
139 const base::Value
* external_version_value
= NULL
;
140 std::string external_version
;
141 std::string external_update_url
;
143 bool has_external_crx
= extension
->GetString(kExternalCrx
, &external_crx
);
145 bool has_external_version
= false;
146 if (extension
->Get(kExternalVersion
, &external_version_value
)) {
147 if (external_version_value
->IsType(base::Value::TYPE_STRING
)) {
148 external_version_value
->GetAsString(&external_version
);
149 has_external_version
= true;
151 LOG(WARNING
) << "Malformed extension dictionary for extension: "
152 << extension_id
.c_str() << ". " << kExternalVersion
153 << " value must be a string.";
158 bool has_external_update_url
= extension
->GetString(kExternalUpdateUrl
,
159 &external_update_url
);
160 if (has_external_crx
!= has_external_version
) {
161 LOG(WARNING
) << "Malformed extension dictionary for extension: "
162 << extension_id
.c_str() << ". " << kExternalCrx
163 << " and " << kExternalVersion
<< " must be used together.";
167 if (has_external_crx
== has_external_update_url
) {
168 LOG(WARNING
) << "Malformed extension dictionary for extension: "
169 << extension_id
.c_str() << ". Exactly one of the "
170 << "followng keys should be used: " << kExternalCrx
171 << ", " << kExternalUpdateUrl
<< ".";
175 // Check that extension supports current browser locale.
176 const base::ListValue
* supported_locales
= NULL
;
177 if (extension
->GetList(kSupportedLocales
, &supported_locales
)) {
178 std::vector
<std::string
> browser_locales
;
179 l10n_util::GetParentLocales(g_browser_process
->GetApplicationLocale(),
182 size_t num_locales
= supported_locales
->GetSize();
183 bool locale_supported
= false;
184 for (size_t j
= 0; j
< num_locales
; j
++) {
185 std::string current_locale
;
186 if (supported_locales
->GetString(j
, ¤t_locale
) &&
187 l10n_util::IsValidLocaleSyntax(current_locale
)) {
188 current_locale
= l10n_util::NormalizeLocale(current_locale
);
189 if (std::find(browser_locales
.begin(), browser_locales
.end(),
190 current_locale
) != browser_locales
.end()) {
191 locale_supported
= true;
195 LOG(WARNING
) << "Unrecognized locale '" << current_locale
196 << "' found as supported locale for extension: "
201 if (!locale_supported
) {
202 unsupported_extensions
.insert(extension_id
);
203 VLOG(1) << "Skip installing (or uninstall) external extension: "
204 << extension_id
<< " because the extension doesn't support "
205 << "the browser locale.";
210 int creation_flags
= creation_flags_
;
211 bool is_bookmark_app
;
212 if (extension
->GetBoolean(kIsBookmarkApp
, &is_bookmark_app
) &&
214 creation_flags
|= Extension::FROM_BOOKMARK
;
216 bool is_from_webstore
= false;
217 if (extension
->GetBoolean(kIsFromWebstore
, &is_from_webstore
) &&
219 creation_flags
|= Extension::FROM_WEBSTORE
;
221 bool keep_if_present
= false;
222 if (extension
->GetBoolean(kKeepIfPresent
, &keep_if_present
) &&
223 keep_if_present
&& profile_
) {
224 ExtensionServiceInterface
* extension_service
=
225 ExtensionSystem::Get(profile_
)->extension_service();
226 const Extension
* extension
= extension_service
?
227 extension_service
->GetExtensionById(extension_id
, true) : NULL
;
229 unsupported_extensions
.insert(extension_id
);
230 VLOG(1) << "Skip installing (or uninstall) external extension: "
231 << extension_id
<< " because the extension should be kept "
232 << "only if it is already installed.";
236 bool was_installed_by_oem
= false;
237 if (extension
->GetBoolean(kWasInstalledByOem
, &was_installed_by_oem
) &&
238 was_installed_by_oem
) {
239 creation_flags
|= Extension::WAS_INSTALLED_BY_OEM
;
241 bool may_be_untrusted
= false;
242 if (extension
->GetBoolean(kMayBeUntrusted
, &may_be_untrusted
) &&
244 creation_flags
|= Extension::MAY_BE_UNTRUSTED
;
247 if (!HandleMinProfileVersion(extension
, extension_id
,
248 &unsupported_extensions
)) {
252 if (!HandleDoNotInstallForEnterprise(extension
, extension_id
,
253 &unsupported_extensions
)) {
257 std::string install_parameter
;
258 extension
->GetString(kInstallParam
, &install_parameter
);
260 if (has_external_crx
) {
261 if (crx_location_
== Manifest::INVALID_LOCATION
) {
262 LOG(WARNING
) << "This provider does not support installing external "
263 << "extensions from crx files.";
266 if (external_crx
.find(base::FilePath::kParentDirectory
) !=
267 base::StringPiece::npos
) {
268 LOG(WARNING
) << "Path traversal not allowed in path: "
269 << external_crx
.c_str();
273 // If the path is relative, and the provider has a base path,
274 // build the absolute path to the crx file.
275 base::FilePath
path(external_crx
);
276 if (!path
.IsAbsolute()) {
277 base::FilePath base_path
= loader_
->GetBaseCrxFilePath();
278 if (base_path
.empty()) {
279 LOG(WARNING
) << "File path " << external_crx
.c_str()
280 << " is relative. An absolute path is required.";
283 path
= base_path
.Append(external_crx
);
286 Version
version(external_version
);
287 if (!version
.IsValid()) {
288 LOG(WARNING
) << "Malformed extension dictionary for extension: "
289 << extension_id
.c_str() << ". Invalid version string \""
290 << external_version
<< "\".";
293 service_
->OnExternalExtensionFileFound(extension_id
, &version
, path
,
294 crx_location_
, creation_flags
,
296 install_immediately_
);
297 } else { // if (has_external_update_url)
298 CHECK(has_external_update_url
); // Checking of keys above ensures this.
299 if (download_location_
== Manifest::INVALID_LOCATION
) {
300 LOG(WARNING
) << "This provider does not support installing external "
301 << "extensions from update URLs.";
304 GURL
update_url(external_update_url
);
305 if (!update_url
.is_valid()) {
306 LOG(WARNING
) << "Malformed extension dictionary for extension: "
307 << extension_id
.c_str() << ". Key " << kExternalUpdateUrl
308 << " has value \"" << external_update_url
309 << "\", which is not a valid URL.";
312 service_
->OnExternalExtensionUpdateUrlFound(extension_id
,
321 for (std::set
<std::string
>::iterator it
= unsupported_extensions
.begin();
322 it
!= unsupported_extensions
.end(); ++it
) {
323 // Remove extension for the list of know external extensions. The extension
324 // will be uninstalled later because provider doesn't provide it anymore.
325 prefs_
->Remove(*it
, NULL
);
328 service_
->OnExternalProviderReady(this);
331 void ExternalProviderImpl::ServiceShutdown() {
335 bool ExternalProviderImpl::IsReady() const {
339 bool ExternalProviderImpl::HasExtension(
340 const std::string
& id
) const {
341 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
344 return prefs_
->HasKey(id
);
347 bool ExternalProviderImpl::GetExtensionDetails(
348 const std::string
& id
, Manifest::Location
* location
,
349 scoped_ptr
<Version
>* version
) const {
350 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
353 base::DictionaryValue
* extension
= NULL
;
354 if (!prefs_
->GetDictionary(id
, &extension
))
357 Manifest::Location loc
= Manifest::INVALID_LOCATION
;
358 if (extension
->HasKey(kExternalUpdateUrl
)) {
359 loc
= download_location_
;
361 } else if (extension
->HasKey(kExternalCrx
)) {
364 std::string external_version
;
365 if (!extension
->GetString(kExternalVersion
, &external_version
))
369 version
->reset(new Version(external_version
));
372 NOTREACHED(); // Chrome should not allow prefs to get into this state.
382 bool ExternalProviderImpl::HandleMinProfileVersion(
383 const base::DictionaryValue
* extension
,
384 const std::string
& extension_id
,
385 std::set
<std::string
>* unsupported_extensions
) {
386 std::string min_profile_created_by_version
;
388 extension
->GetString(kMinProfileCreatedByVersion
,
389 &min_profile_created_by_version
)) {
390 Version
profile_version(
391 profile_
->GetPrefs()->GetString(prefs::kProfileCreatedByVersion
));
392 Version
min_version(min_profile_created_by_version
);
393 if (min_version
.IsValid() && profile_version
.CompareTo(min_version
) < 0) {
394 unsupported_extensions
->insert(extension_id
);
395 VLOG(1) << "Skip installing (or uninstall) external extension: "
397 << " profile.created_by_version: " << profile_version
.GetString()
398 << " min_profile_created_by_version: "
399 << min_profile_created_by_version
;
406 bool ExternalProviderImpl::HandleDoNotInstallForEnterprise(
407 const base::DictionaryValue
* extension
,
408 const std::string
& extension_id
,
409 std::set
<std::string
>* unsupported_extensions
) {
410 bool do_not_install_for_enterprise
= false;
411 if (extension
->GetBoolean(kDoNotInstallForEnterprise
,
412 &do_not_install_for_enterprise
) &&
413 do_not_install_for_enterprise
) {
414 const policy::ProfilePolicyConnector
* const connector
=
415 policy::ProfilePolicyConnectorFactory::GetForBrowserContext(profile_
);
416 if (connector
->IsManaged()) {
417 unsupported_extensions
->insert(extension_id
);
418 VLOG(1) << "Skip installing (or uninstall) external extension "
419 << extension_id
<< " restricted for managed user";
427 void ExternalProviderImpl::CreateExternalProviders(
428 VisitorInterface
* service
,
430 ProviderCollection
* provider_list
) {
431 TRACE_EVENT0("browser,startup",
432 "ExternalProviderImpl::CreateExternalProviders");
433 scoped_refptr
<ExternalLoader
> external_loader
;
434 scoped_refptr
<ExternalLoader
> external_recommended_loader
;
435 extensions::Manifest::Location crx_location
= Manifest::INVALID_LOCATION
;
436 #if defined(OS_CHROMEOS)
437 policy::BrowserPolicyConnectorChromeOS
* connector
=
438 g_browser_process
->platform_part()->browser_policy_connector_chromeos();
439 bool is_chrome_os_public_session
= false;
440 const user_manager::User
* user
=
441 chromeos::ProfileHelper::Get()->GetUserByProfile(profile
);
442 policy::DeviceLocalAccount::Type account_type
;
444 connector
->IsEnterpriseManaged() &&
445 policy::IsDeviceLocalAccountUser(user
->email(), &account_type
)) {
446 if (account_type
== policy::DeviceLocalAccount::TYPE_PUBLIC_SESSION
)
447 is_chrome_os_public_session
= true;
448 policy::DeviceLocalAccountPolicyBroker
* broker
=
449 connector
->GetDeviceLocalAccountPolicyService()->GetBrokerForUser(
452 external_loader
= broker
->extension_loader();
453 crx_location
= Manifest::EXTERNAL_POLICY
;
458 external_loader
= new ExternalPolicyLoader(
459 ExtensionManagementFactory::GetForBrowserContext(profile
),
460 ExternalPolicyLoader::FORCED
);
461 external_recommended_loader
= new ExternalPolicyLoader(
462 ExtensionManagementFactory::GetForBrowserContext(profile
),
463 ExternalPolicyLoader::RECOMMENDED
);
466 external_loader
= new ExternalPolicyLoader(
467 ExtensionManagementFactory::GetForBrowserContext(profile
),
468 ExternalPolicyLoader::FORCED
);
469 external_recommended_loader
= new ExternalPolicyLoader(
470 ExtensionManagementFactory::GetForBrowserContext(profile
),
471 ExternalPolicyLoader::RECOMMENDED
);
474 // Policies are mandatory so they can't be skipped with command line flag.
475 if (external_loader
.get()) {
476 provider_list
->push_back(
477 linked_ptr
<ExternalProviderInterface
>(
478 new ExternalProviderImpl(
483 Manifest::EXTERNAL_POLICY_DOWNLOAD
,
484 Extension::NO_FLAGS
)));
487 // Load the KioskAppExternalProvider when running in kiosk mode.
488 if (chrome::IsRunningInForcedAppMode()) {
489 #if defined(OS_CHROMEOS)
490 chromeos::KioskAppManager
* kiosk_app_manager
=
491 chromeos::KioskAppManager::Get();
492 DCHECK(kiosk_app_manager
);
493 if (kiosk_app_manager
&& !kiosk_app_manager
->external_loader_created()) {
494 scoped_ptr
<ExternalProviderImpl
> kiosk_app_provider(
495 new ExternalProviderImpl(
496 service
, kiosk_app_manager
->CreateExternalLoader(), profile
,
497 Manifest::EXTERNAL_PREF
, Manifest::INVALID_LOCATION
,
498 Extension::NO_FLAGS
));
499 kiosk_app_provider
->set_auto_acknowledge(true);
500 kiosk_app_provider
->set_install_immediately(true);
501 provider_list
->push_back(
502 linked_ptr
<ExternalProviderInterface
>(kiosk_app_provider
.release()));
508 // Extensions provided by recommended policies.
509 if (external_recommended_loader
.get()) {
510 provider_list
->push_back(linked_ptr
<ExternalProviderInterface
>(
511 new ExternalProviderImpl(service
,
512 external_recommended_loader
,
515 Manifest::EXTERNAL_PREF_DOWNLOAD
,
516 Extension::NO_FLAGS
)));
519 // In tests don't install extensions from default external sources.
520 // It would only slowdown tests and make them flaky.
521 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
522 switches::kDisableDefaultApps
))
525 // On Mac OS, items in /Library/... should be written by the superuser.
526 // Check that all components of the path are writable by root only.
527 ExternalPrefLoader::Options check_admin_permissions_on_mac
;
528 #if defined(OS_MACOSX)
529 check_admin_permissions_on_mac
=
530 ExternalPrefLoader::ENSURE_PATH_CONTROLLED_BY_ADMIN
;
532 check_admin_permissions_on_mac
= ExternalPrefLoader::NONE
;
536 int bundled_extension_creation_flags
= Extension::NO_FLAGS
;
538 #if defined(OS_CHROMEOS)
539 bundled_extension_creation_flags
= Extension::FROM_WEBSTORE
|
540 Extension::WAS_INSTALLED_BY_DEFAULT
;
542 if (!is_chrome_os_public_session
) {
543 int external_apps_path_id
= profile
->IsSupervised() ?
544 chrome::DIR_SUPERVISED_USERS_DEFAULT_APPS
:
545 chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS
;
546 ExternalPrefLoader::Options pref_load_flags
=
547 profile
->IsNewProfile()
548 ? ExternalPrefLoader::DELAY_LOAD_UNTIL_PRIORITY_SYNC
549 : ExternalPrefLoader::NONE
;
550 provider_list
->push_back(
551 linked_ptr
<ExternalProviderInterface
>(new ExternalProviderImpl(
552 service
, new ExternalPrefLoader(external_apps_path_id
,
553 pref_load_flags
, profile
),
554 profile
, Manifest::EXTERNAL_PREF
, Manifest::EXTERNAL_PREF_DOWNLOAD
,
555 bundled_extension_creation_flags
)));
558 int oem_extension_creation_flags
=
559 bundled_extension_creation_flags
| Extension::WAS_INSTALLED_BY_OEM
;
560 chromeos::ServicesCustomizationDocument
* customization
=
561 chromeos::ServicesCustomizationDocument::GetInstance();
562 provider_list
->push_back(linked_ptr
<ExternalProviderInterface
>(
563 new ExternalProviderImpl(service
,
564 customization
->CreateExternalLoader(profile
),
566 Manifest::EXTERNAL_PREF
,
567 Manifest::EXTERNAL_PREF_DOWNLOAD
,
568 oem_extension_creation_flags
)));
570 #elif defined(OS_LINUX)
571 if (!profile
->IsLegacySupervised()) {
572 provider_list
->push_back(
573 linked_ptr
<ExternalProviderInterface
>(
574 new ExternalProviderImpl(
576 new ExternalPrefLoader(
577 chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS
,
578 ExternalPrefLoader::NONE
,
581 Manifest::EXTERNAL_PREF
,
582 Manifest::EXTERNAL_PREF_DOWNLOAD
,
583 bundled_extension_creation_flags
)));
587 if (!profile
->IsLegacySupervised()) {
589 provider_list
->push_back(
590 linked_ptr
<ExternalProviderInterface
>(
591 new ExternalProviderImpl(
593 new ExternalRegistryLoader
,
595 Manifest::EXTERNAL_REGISTRY
,
596 Manifest::EXTERNAL_PREF_DOWNLOAD
,
597 Extension::NO_FLAGS
)));
599 provider_list
->push_back(
600 linked_ptr
<ExternalProviderInterface
>(
601 new ExternalProviderImpl(
603 new ExternalPrefLoader(chrome::DIR_EXTERNAL_EXTENSIONS
,
604 check_admin_permissions_on_mac
,
607 Manifest::EXTERNAL_PREF
,
608 Manifest::EXTERNAL_PREF_DOWNLOAD
,
609 bundled_extension_creation_flags
)));
611 // Define a per-user source of external extensions.
612 #if defined(OS_MACOSX)
613 provider_list
->push_back(
614 linked_ptr
<ExternalProviderInterface
>(
615 new ExternalProviderImpl(
617 new ExternalPrefLoader(chrome::DIR_USER_EXTERNAL_EXTENSIONS
,
618 ExternalPrefLoader::NONE
,
621 Manifest::EXTERNAL_PREF
,
622 Manifest::EXTERNAL_PREF_DOWNLOAD
,
623 Extension::NO_FLAGS
)));
627 #if !defined(OS_CHROMEOS)
628 // The default apps are installed as INTERNAL but use the external
629 // extension installer codeflow.
630 provider_list
->push_back(
631 linked_ptr
<ExternalProviderInterface
>(
632 new default_apps::Provider(
635 new ExternalPrefLoader(chrome::DIR_DEFAULT_APPS
,
636 ExternalPrefLoader::NONE
,
640 Extension::FROM_WEBSTORE
|
641 Extension::WAS_INSTALLED_BY_DEFAULT
)));
645 provider_list
->push_back(
646 linked_ptr
<ExternalProviderInterface
>(
647 new ExternalProviderImpl(
649 new ExternalComponentLoader(profile
),
651 Manifest::INVALID_LOCATION
,
652 Manifest::EXTERNAL_COMPONENT
,
653 Extension::FROM_WEBSTORE
| Extension::WAS_INSTALLED_BY_DEFAULT
)));
656 } // namespace extensions