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/path_service.h"
16 #include "base/strings/string_util.h"
17 #include "base/values.h"
18 #include "base/version.h"
19 #include "chrome/browser/app_mode/app_mode_utils.h"
20 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/extensions/extension_management.h"
22 #include "chrome/browser/extensions/extension_service.h"
23 #include "chrome/browser/extensions/external_component_loader.h"
24 #include "chrome/browser/extensions/external_policy_loader.h"
25 #include "chrome/browser/extensions/external_pref_loader.h"
26 #include "chrome/browser/profiles/profile.h"
27 #include "chrome/common/chrome_paths.h"
28 #include "chrome/common/chrome_switches.h"
29 #include "chrome/common/pref_names.h"
30 #include "components/crx_file/id_util.h"
31 #include "content/public/browser/browser_thread.h"
32 #include "extensions/browser/extension_system.h"
33 #include "extensions/browser/external_provider_interface.h"
34 #include "extensions/common/extension.h"
35 #include "extensions/common/manifest.h"
36 #include "ui/base/l10n/l10n_util.h"
38 #if defined(OS_CHROMEOS)
39 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
40 #include "chrome/browser/chromeos/customization_document.h"
41 #include "chrome/browser/chromeos/extensions/device_local_account_external_policy_loader.h"
42 #include "chrome/browser/chromeos/policy/app_pack_updater.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"
48 #include "components/user_manager/user_manager.h"
50 #include "chrome/browser/extensions/default_apps.h"
54 #include "chrome/browser/extensions/external_registry_loader_win.h"
57 using content::BrowserThread
;
59 namespace extensions
{
61 // Constants for keeping track of extension preferences in a dictionary.
62 const char ExternalProviderImpl::kInstallParam
[] = "install_parameter";
63 const char ExternalProviderImpl::kExternalCrx
[] = "external_crx";
64 const char ExternalProviderImpl::kExternalVersion
[] = "external_version";
65 const char ExternalProviderImpl::kExternalUpdateUrl
[] = "external_update_url";
66 const char ExternalProviderImpl::kIsBookmarkApp
[] = "is_bookmark_app";
67 const char ExternalProviderImpl::kIsFromWebstore
[] = "is_from_webstore";
68 const char ExternalProviderImpl::kKeepIfPresent
[] = "keep_if_present";
69 const char ExternalProviderImpl::kWasInstalledByOem
[] = "was_installed_by_oem";
70 const char ExternalProviderImpl::kSupportedLocales
[] = "supported_locales";
71 const char ExternalProviderImpl::kMayBeUntrusted
[] = "may_be_untrusted";
73 ExternalProviderImpl::ExternalProviderImpl(
74 VisitorInterface
* service
,
75 const scoped_refptr
<ExternalLoader
>& loader
,
77 Manifest::Location crx_location
,
78 Manifest::Location download_location
,
80 : crx_location_(crx_location
),
81 download_location_(download_location
),
86 creation_flags_(creation_flags
),
87 auto_acknowledge_(false) {
91 ExternalProviderImpl::~ExternalProviderImpl() {
92 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
93 loader_
->OwnerShutdown();
96 void ExternalProviderImpl::VisitRegisteredExtension() {
97 // The loader will call back to SetPrefs.
98 loader_
->StartLoading();
101 void ExternalProviderImpl::SetPrefs(base::DictionaryValue
* prefs
) {
102 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
104 // Check if the service is still alive. It is possible that it went
105 // away while |loader_| was working on the FILE thread.
106 if (!service_
) return;
109 ready_
= true; // Queries for extensions are allowed from this point.
111 // Set of unsupported extensions that need to be deleted from prefs_.
112 std::set
<std::string
> unsupported_extensions
;
114 // Notify ExtensionService about all the extensions this provider has.
115 for (base::DictionaryValue::Iterator
i(*prefs_
); !i
.IsAtEnd(); i
.Advance()) {
116 const std::string
& extension_id
= i
.key();
117 const base::DictionaryValue
* extension
= NULL
;
119 if (!crx_file::id_util::IdIsValid(extension_id
)) {
120 LOG(WARNING
) << "Malformed extension dictionary: key "
121 << extension_id
.c_str() << " is not a valid id.";
125 if (!i
.value().GetAsDictionary(&extension
)) {
126 LOG(WARNING
) << "Malformed extension dictionary: key "
127 << extension_id
.c_str()
128 << " has a value that is not a dictionary.";
132 base::FilePath::StringType external_crx
;
133 const base::Value
* external_version_value
= NULL
;
134 std::string external_version
;
135 std::string external_update_url
;
137 bool has_external_crx
= extension
->GetString(kExternalCrx
, &external_crx
);
139 bool has_external_version
= false;
140 if (extension
->Get(kExternalVersion
, &external_version_value
)) {
141 if (external_version_value
->IsType(base::Value::TYPE_STRING
)) {
142 external_version_value
->GetAsString(&external_version
);
143 has_external_version
= true;
145 LOG(WARNING
) << "Malformed extension dictionary for extension: "
146 << extension_id
.c_str() << ". " << kExternalVersion
147 << " value must be a string.";
152 bool has_external_update_url
= extension
->GetString(kExternalUpdateUrl
,
153 &external_update_url
);
154 if (has_external_crx
!= has_external_version
) {
155 LOG(WARNING
) << "Malformed extension dictionary for extension: "
156 << extension_id
.c_str() << ". " << kExternalCrx
157 << " and " << kExternalVersion
<< " must be used together.";
161 if (has_external_crx
== has_external_update_url
) {
162 LOG(WARNING
) << "Malformed extension dictionary for extension: "
163 << extension_id
.c_str() << ". Exactly one of the "
164 << "followng keys should be used: " << kExternalCrx
165 << ", " << kExternalUpdateUrl
<< ".";
169 // Check that extension supports current browser locale.
170 const base::ListValue
* supported_locales
= NULL
;
171 if (extension
->GetList(kSupportedLocales
, &supported_locales
)) {
172 std::vector
<std::string
> browser_locales
;
173 l10n_util::GetParentLocales(g_browser_process
->GetApplicationLocale(),
176 size_t num_locales
= supported_locales
->GetSize();
177 bool locale_supported
= false;
178 for (size_t j
= 0; j
< num_locales
; j
++) {
179 std::string current_locale
;
180 if (supported_locales
->GetString(j
, ¤t_locale
) &&
181 l10n_util::IsValidLocaleSyntax(current_locale
)) {
182 current_locale
= l10n_util::NormalizeLocale(current_locale
);
183 if (std::find(browser_locales
.begin(), browser_locales
.end(),
184 current_locale
) != browser_locales
.end()) {
185 locale_supported
= true;
189 LOG(WARNING
) << "Unrecognized locale '" << current_locale
190 << "' found as supported locale for extension: "
195 if (!locale_supported
) {
196 unsupported_extensions
.insert(extension_id
);
197 VLOG(1) << "Skip installing (or uninstall) external extension: "
198 << extension_id
<< " because the extension doesn't support "
199 << "the browser locale.";
204 int creation_flags
= creation_flags_
;
205 bool is_bookmark_app
;
206 if (extension
->GetBoolean(kIsBookmarkApp
, &is_bookmark_app
) &&
208 creation_flags
|= Extension::FROM_BOOKMARK
;
210 bool is_from_webstore
= false;
211 if (extension
->GetBoolean(kIsFromWebstore
, &is_from_webstore
) &&
213 creation_flags
|= Extension::FROM_WEBSTORE
;
215 bool keep_if_present
= false;
216 if (extension
->GetBoolean(kKeepIfPresent
, &keep_if_present
) &&
217 keep_if_present
&& profile_
) {
218 ExtensionServiceInterface
* extension_service
=
219 ExtensionSystem::Get(profile_
)->extension_service();
220 const Extension
* extension
= extension_service
?
221 extension_service
->GetExtensionById(extension_id
, true) : NULL
;
223 VLOG(1) << "Skip installing (or uninstall) external extension: "
224 << extension_id
<< " because the extension should be kept "
225 << "only if it is already installed.";
229 bool was_installed_by_oem
= false;
230 if (extension
->GetBoolean(kWasInstalledByOem
, &was_installed_by_oem
) &&
231 was_installed_by_oem
) {
232 creation_flags
|= Extension::WAS_INSTALLED_BY_OEM
;
234 bool may_be_untrusted
= false;
235 if (extension
->GetBoolean(kMayBeUntrusted
, &may_be_untrusted
) &&
237 creation_flags
|= Extension::MAY_BE_UNTRUSTED
;
240 std::string install_parameter
;
241 extension
->GetString(kInstallParam
, &install_parameter
);
243 if (has_external_crx
) {
244 if (crx_location_
== Manifest::INVALID_LOCATION
) {
245 LOG(WARNING
) << "This provider does not support installing external "
246 << "extensions from crx files.";
249 if (external_crx
.find(base::FilePath::kParentDirectory
) !=
250 base::StringPiece::npos
) {
251 LOG(WARNING
) << "Path traversal not allowed in path: "
252 << external_crx
.c_str();
256 // If the path is relative, and the provider has a base path,
257 // build the absolute path to the crx file.
258 base::FilePath
path(external_crx
);
259 if (!path
.IsAbsolute()) {
260 base::FilePath base_path
= loader_
->GetBaseCrxFilePath();
261 if (base_path
.empty()) {
262 LOG(WARNING
) << "File path " << external_crx
.c_str()
263 << " is relative. An absolute path is required.";
266 path
= base_path
.Append(external_crx
);
269 Version
version(external_version
);
270 if (!version
.IsValid()) {
271 LOG(WARNING
) << "Malformed extension dictionary for extension: "
272 << extension_id
.c_str() << ". Invalid version string \""
273 << external_version
<< "\".";
276 service_
->OnExternalExtensionFileFound(extension_id
, &version
, path
,
277 crx_location_
, creation_flags
,
279 } else { // if (has_external_update_url)
280 CHECK(has_external_update_url
); // Checking of keys above ensures this.
281 if (download_location_
== Manifest::INVALID_LOCATION
) {
282 LOG(WARNING
) << "This provider does not support installing external "
283 << "extensions from update URLs.";
286 GURL
update_url(external_update_url
);
287 if (!update_url
.is_valid()) {
288 LOG(WARNING
) << "Malformed extension dictionary for extension: "
289 << extension_id
.c_str() << ". Key " << kExternalUpdateUrl
290 << " has value \"" << external_update_url
291 << "\", which is not a valid URL.";
294 service_
->OnExternalExtensionUpdateUrlFound(extension_id
,
303 for (std::set
<std::string
>::iterator it
= unsupported_extensions
.begin();
304 it
!= unsupported_extensions
.end(); ++it
) {
305 // Remove extension for the list of know external extensions. The extension
306 // will be uninstalled later because provider doesn't provide it anymore.
307 prefs_
->Remove(*it
, NULL
);
310 service_
->OnExternalProviderReady(this);
313 void ExternalProviderImpl::ServiceShutdown() {
317 bool ExternalProviderImpl::IsReady() const {
321 bool ExternalProviderImpl::HasExtension(
322 const std::string
& id
) const {
323 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
326 return prefs_
->HasKey(id
);
329 bool ExternalProviderImpl::GetExtensionDetails(
330 const std::string
& id
, Manifest::Location
* location
,
331 scoped_ptr
<Version
>* version
) const {
332 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
335 base::DictionaryValue
* extension
= NULL
;
336 if (!prefs_
->GetDictionary(id
, &extension
))
339 Manifest::Location loc
= Manifest::INVALID_LOCATION
;
340 if (extension
->HasKey(kExternalUpdateUrl
)) {
341 loc
= download_location_
;
343 } else if (extension
->HasKey(kExternalCrx
)) {
346 std::string external_version
;
347 if (!extension
->GetString(kExternalVersion
, &external_version
))
351 version
->reset(new Version(external_version
));
354 NOTREACHED(); // Chrome should not allow prefs to get into this state.
365 void ExternalProviderImpl::CreateExternalProviders(
366 VisitorInterface
* service
,
368 ProviderCollection
* provider_list
) {
369 scoped_refptr
<ExternalLoader
> external_loader
;
370 scoped_refptr
<ExternalLoader
> external_recommended_loader
;
371 extensions::Manifest::Location crx_location
= Manifest::INVALID_LOCATION
;
372 #if defined(OS_CHROMEOS)
373 policy::BrowserPolicyConnectorChromeOS
* connector
=
374 g_browser_process
->platform_part()->browser_policy_connector_chromeos();
375 bool is_chrome_os_public_session
= false;
376 const user_manager::User
* user
=
377 chromeos::ProfileHelper::Get()->GetUserByProfile(profile
);
378 policy::DeviceLocalAccount::Type account_type
;
379 if (user
&& policy::IsDeviceLocalAccountUser(user
->email(), &account_type
)) {
380 if (account_type
== policy::DeviceLocalAccount::TYPE_PUBLIC_SESSION
)
381 is_chrome_os_public_session
= true;
382 policy::DeviceLocalAccountPolicyBroker
* broker
=
383 connector
->GetDeviceLocalAccountPolicyService()->GetBrokerForUser(
386 external_loader
= broker
->extension_loader();
387 crx_location
= Manifest::EXTERNAL_POLICY
;
392 external_loader
= new ExternalPolicyLoader(
393 ExtensionManagementFactory::GetForBrowserContext(profile
),
394 ExternalPolicyLoader::FORCED
);
395 external_recommended_loader
= new ExternalPolicyLoader(
396 ExtensionManagementFactory::GetForBrowserContext(profile
),
397 ExternalPolicyLoader::RECOMMENDED
);
400 external_loader
= new ExternalPolicyLoader(
401 ExtensionManagementFactory::GetForBrowserContext(profile
),
402 ExternalPolicyLoader::FORCED
);
403 external_recommended_loader
= new ExternalPolicyLoader(
404 ExtensionManagementFactory::GetForBrowserContext(profile
),
405 ExternalPolicyLoader::RECOMMENDED
);
408 // Policies are mandatory so they can't be skipped with command line flag.
409 if (external_loader
.get()) {
410 provider_list
->push_back(
411 linked_ptr
<ExternalProviderInterface
>(
412 new ExternalProviderImpl(
417 Manifest::EXTERNAL_POLICY_DOWNLOAD
,
418 Extension::NO_FLAGS
)));
421 // Load the KioskAppExternalProvider when running in kiosk mode.
422 if (chrome::IsRunningInForcedAppMode()) {
423 #if defined(OS_CHROMEOS)
424 chromeos::KioskAppManager
* kiosk_app_manager
=
425 chromeos::KioskAppManager::Get();
426 DCHECK(kiosk_app_manager
);
427 if (kiosk_app_manager
&& !kiosk_app_manager
->external_loader_created()) {
428 provider_list
->push_back(linked_ptr
<ExternalProviderInterface
>(
429 new ExternalProviderImpl(service
,
430 kiosk_app_manager
->CreateExternalLoader(),
432 Manifest::EXTERNAL_PREF
,
433 Manifest::INVALID_LOCATION
,
434 Extension::NO_FLAGS
)));
440 // Extensions provided by recommended policies.
441 if (external_recommended_loader
.get()) {
442 provider_list
->push_back(linked_ptr
<ExternalProviderInterface
>(
443 new ExternalProviderImpl(service
,
444 external_recommended_loader
,
447 Manifest::EXTERNAL_PREF_DOWNLOAD
,
448 Extension::NO_FLAGS
)));
451 // In tests don't install extensions from default external sources.
452 // It would only slowdown tests and make them flaky.
453 if (CommandLine::ForCurrentProcess()->HasSwitch(
454 switches::kDisableDefaultApps
))
457 // On Mac OS, items in /Library/... should be written by the superuser.
458 // Check that all components of the path are writable by root only.
459 ExternalPrefLoader::Options check_admin_permissions_on_mac
;
460 #if defined(OS_MACOSX)
461 check_admin_permissions_on_mac
=
462 ExternalPrefLoader::ENSURE_PATH_CONTROLLED_BY_ADMIN
;
464 check_admin_permissions_on_mac
= ExternalPrefLoader::NONE
;
467 bool is_chromeos_demo_session
= false;
468 int bundled_extension_creation_flags
= Extension::NO_FLAGS
;
469 #if defined(OS_CHROMEOS)
470 user_manager::UserManager
* user_manager
= user_manager::UserManager::Get();
471 is_chromeos_demo_session
=
472 user_manager
&& user_manager
->IsLoggedInAsDemoUser() &&
473 connector
->GetDeviceMode() == policy::DEVICE_MODE_RETAIL_KIOSK
;
474 bundled_extension_creation_flags
= Extension::FROM_WEBSTORE
|
475 Extension::WAS_INSTALLED_BY_DEFAULT
;
478 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
479 if (!profile
->IsSupervised()) {
480 provider_list
->push_back(
481 linked_ptr
<ExternalProviderInterface
>(
482 new ExternalProviderImpl(
484 new ExternalPrefLoader(
485 chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS
,
486 ExternalPrefLoader::NONE
),
488 Manifest::EXTERNAL_PREF
,
489 Manifest::EXTERNAL_PREF_DOWNLOAD
,
490 bundled_extension_creation_flags
)));
494 #if defined(OS_CHROMEOS)
495 if (!is_chromeos_demo_session
&& !is_chrome_os_public_session
) {
496 int external_apps_path_id
= profile
->IsSupervised() ?
497 chrome::DIR_SUPERVISED_USERS_DEFAULT_APPS
:
498 chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS
;
499 provider_list
->push_back(
500 linked_ptr
<ExternalProviderInterface
>(new ExternalProviderImpl(
502 new ExternalPrefLoader(external_apps_path_id
,
503 ExternalPrefLoader::NONE
),
505 Manifest::EXTERNAL_PREF
,
506 Manifest::EXTERNAL_PREF_DOWNLOAD
,
507 bundled_extension_creation_flags
)));
510 int oem_extension_creation_flags
=
511 bundled_extension_creation_flags
| Extension::WAS_INSTALLED_BY_OEM
;
512 chromeos::ServicesCustomizationDocument
* customization
=
513 chromeos::ServicesCustomizationDocument::GetInstance();
514 provider_list
->push_back(linked_ptr
<ExternalProviderInterface
>(
515 new ExternalProviderImpl(service
,
516 customization
->CreateExternalLoader(profile
),
518 Manifest::EXTERNAL_PREF
,
519 Manifest::EXTERNAL_PREF_DOWNLOAD
,
520 oem_extension_creation_flags
)));
523 policy::AppPackUpdater
* app_pack_updater
= connector
->GetAppPackUpdater();
524 if (is_chromeos_demo_session
&& app_pack_updater
&&
525 !app_pack_updater
->created_external_loader()) {
526 provider_list
->push_back(
527 linked_ptr
<ExternalProviderInterface
>(
528 new ExternalProviderImpl(
530 app_pack_updater
->CreateExternalLoader(),
532 Manifest::EXTERNAL_PREF
,
533 Manifest::INVALID_LOCATION
,
534 Extension::NO_FLAGS
)));
538 if (!profile
->IsSupervised() && !is_chromeos_demo_session
) {
540 provider_list
->push_back(
541 linked_ptr
<ExternalProviderInterface
>(
542 new ExternalProviderImpl(
544 new ExternalPrefLoader(chrome::DIR_EXTERNAL_EXTENSIONS
,
545 check_admin_permissions_on_mac
),
547 Manifest::EXTERNAL_PREF
,
548 Manifest::EXTERNAL_PREF_DOWNLOAD
,
549 bundled_extension_creation_flags
)));
552 // Define a per-user source of external extensions.
553 #if defined(OS_MACOSX)
554 provider_list
->push_back(
555 linked_ptr
<ExternalProviderInterface
>(
556 new ExternalProviderImpl(
558 new ExternalPrefLoader(chrome::DIR_USER_EXTERNAL_EXTENSIONS
,
559 ExternalPrefLoader::NONE
),
561 Manifest::EXTERNAL_PREF
,
562 Manifest::EXTERNAL_PREF_DOWNLOAD
,
563 Extension::NO_FLAGS
)));
567 provider_list
->push_back(
568 linked_ptr
<ExternalProviderInterface
>(
569 new ExternalProviderImpl(
571 new ExternalRegistryLoader
,
573 Manifest::EXTERNAL_REGISTRY
,
574 Manifest::EXTERNAL_PREF_DOWNLOAD
,
575 Extension::NO_FLAGS
)));
578 #if !defined(OS_CHROMEOS)
579 // The default apps are installed as INTERNAL but use the external
580 // extension installer codeflow.
581 provider_list
->push_back(
582 linked_ptr
<ExternalProviderInterface
>(
583 new default_apps::Provider(
586 new ExternalPrefLoader(chrome::DIR_DEFAULT_APPS
,
587 ExternalPrefLoader::NONE
),
590 Extension::FROM_WEBSTORE
|
591 Extension::WAS_INSTALLED_BY_DEFAULT
)));
594 provider_list
->push_back(
595 linked_ptr
<ExternalProviderInterface
>(
596 new ExternalProviderImpl(
598 new ExternalComponentLoader(profile
),
600 Manifest::INVALID_LOCATION
,
601 Manifest::EXTERNAL_COMPONENT
,
602 Extension::FROM_WEBSTORE
| Extension::WAS_INSTALLED_BY_DEFAULT
)));
606 } // namespace extensions