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_service.h"
22 #include "chrome/browser/extensions/external_component_loader.h"
23 #include "chrome/browser/extensions/external_policy_loader.h"
24 #include "chrome/browser/extensions/external_pref_loader.h"
25 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/common/chrome_paths.h"
27 #include "chrome/common/chrome_switches.h"
28 #include "chrome/common/pref_names.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "extensions/browser/extension_system.h"
31 #include "extensions/browser/external_provider_interface.h"
32 #include "extensions/common/extension.h"
33 #include "extensions/common/manifest.h"
34 #include "ui/base/l10n/l10n_util.h"
36 #if defined(OS_CHROMEOS)
37 #include "chrome/browser/chromeos/customization_document.h"
38 #include "chrome/browser/chromeos/extensions/device_local_account_external_policy_loader.h"
39 #include "chrome/browser/chromeos/login/user.h"
40 #include "chrome/browser/chromeos/login/user_manager.h"
41 #include "chrome/browser/chromeos/policy/app_pack_updater.h"
42 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
43 #include "chrome/browser/chromeos/policy/device_local_account.h"
44 #include "chrome/browser/chromeos/policy/device_local_account_policy_service.h"
46 #include "chrome/browser/extensions/default_apps.h"
50 #include "chrome/browser/extensions/external_registry_loader_win.h"
53 using content::BrowserThread
;
55 namespace extensions
{
57 // Constants for keeping track of extension preferences in a dictionary.
58 const char ExternalProviderImpl::kInstallParam
[] = "install_parameter";
59 const char ExternalProviderImpl::kExternalCrx
[] = "external_crx";
60 const char ExternalProviderImpl::kExternalVersion
[] = "external_version";
61 const char ExternalProviderImpl::kExternalUpdateUrl
[] = "external_update_url";
62 const char ExternalProviderImpl::kIsBookmarkApp
[] = "is_bookmark_app";
63 const char ExternalProviderImpl::kIsFromWebstore
[] = "is_from_webstore";
64 const char ExternalProviderImpl::kKeepIfPresent
[] = "keep_if_present";
65 const char ExternalProviderImpl::kWasInstalledByOem
[] = "was_installed_by_oem";
66 const char ExternalProviderImpl::kSupportedLocales
[] = "supported_locales";
68 ExternalProviderImpl::ExternalProviderImpl(
69 VisitorInterface
* service
,
70 const scoped_refptr
<ExternalLoader
>& loader
,
72 Manifest::Location crx_location
,
73 Manifest::Location download_location
,
75 : crx_location_(crx_location
),
76 download_location_(download_location
),
81 creation_flags_(creation_flags
),
82 auto_acknowledge_(false) {
86 ExternalProviderImpl::~ExternalProviderImpl() {
87 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
88 loader_
->OwnerShutdown();
91 void ExternalProviderImpl::VisitRegisteredExtension() {
92 // The loader will call back to SetPrefs.
93 loader_
->StartLoading();
96 void ExternalProviderImpl::SetPrefs(base::DictionaryValue
* prefs
) {
97 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
99 // Check if the service is still alive. It is possible that it went
100 // away while |loader_| was working on the FILE thread.
101 if (!service_
) return;
104 ready_
= true; // Queries for extensions are allowed from this point.
106 // Set of unsupported extensions that need to be deleted from prefs_.
107 std::set
<std::string
> unsupported_extensions
;
109 // Notify ExtensionService about all the extensions this provider has.
110 for (base::DictionaryValue::Iterator
i(*prefs_
); !i
.IsAtEnd(); i
.Advance()) {
111 const std::string
& extension_id
= i
.key();
112 const base::DictionaryValue
* extension
= NULL
;
114 if (!Extension::IdIsValid(extension_id
)) {
115 LOG(WARNING
) << "Malformed extension dictionary: key "
116 << extension_id
.c_str() << " is not a valid id.";
120 if (!i
.value().GetAsDictionary(&extension
)) {
121 LOG(WARNING
) << "Malformed extension dictionary: key "
122 << extension_id
.c_str()
123 << " has a value that is not a dictionary.";
127 base::FilePath::StringType external_crx
;
128 const base::Value
* external_version_value
= NULL
;
129 std::string external_version
;
130 std::string external_update_url
;
132 bool has_external_crx
= extension
->GetString(kExternalCrx
, &external_crx
);
134 bool has_external_version
= false;
135 if (extension
->Get(kExternalVersion
, &external_version_value
)) {
136 if (external_version_value
->IsType(base::Value::TYPE_STRING
)) {
137 external_version_value
->GetAsString(&external_version
);
138 has_external_version
= true;
140 LOG(WARNING
) << "Malformed extension dictionary for extension: "
141 << extension_id
.c_str() << ". " << kExternalVersion
142 << " value must be a string.";
147 bool has_external_update_url
= extension
->GetString(kExternalUpdateUrl
,
148 &external_update_url
);
149 if (has_external_crx
!= has_external_version
) {
150 LOG(WARNING
) << "Malformed extension dictionary for extension: "
151 << extension_id
.c_str() << ". " << kExternalCrx
152 << " and " << kExternalVersion
<< " must be used together.";
156 if (has_external_crx
== has_external_update_url
) {
157 LOG(WARNING
) << "Malformed extension dictionary for extension: "
158 << extension_id
.c_str() << ". Exactly one of the "
159 << "followng keys should be used: " << kExternalCrx
160 << ", " << kExternalUpdateUrl
<< ".";
164 // Check that extension supports current browser locale.
165 const base::ListValue
* supported_locales
= NULL
;
166 if (extension
->GetList(kSupportedLocales
, &supported_locales
)) {
167 std::vector
<std::string
> browser_locales
;
168 l10n_util::GetParentLocales(g_browser_process
->GetApplicationLocale(),
171 size_t num_locales
= supported_locales
->GetSize();
172 bool locale_supported
= false;
173 for (size_t j
= 0; j
< num_locales
; j
++) {
174 std::string current_locale
;
175 if (supported_locales
->GetString(j
, ¤t_locale
) &&
176 l10n_util::IsValidLocaleSyntax(current_locale
)) {
177 current_locale
= l10n_util::NormalizeLocale(current_locale
);
178 if (std::find(browser_locales
.begin(), browser_locales
.end(),
179 current_locale
) != browser_locales
.end()) {
180 locale_supported
= true;
184 LOG(WARNING
) << "Unrecognized locale '" << current_locale
185 << "' found as supported locale for extension: "
190 if (!locale_supported
) {
191 unsupported_extensions
.insert(extension_id
);
192 VLOG(1) << "Skip installing (or uninstall) external extension: "
193 << extension_id
<< " because the extension doesn't support "
194 << "the browser locale.";
199 int creation_flags
= creation_flags_
;
200 bool is_bookmark_app
;
201 if (extension
->GetBoolean(kIsBookmarkApp
, &is_bookmark_app
) &&
203 creation_flags
|= Extension::FROM_BOOKMARK
;
205 bool is_from_webstore
;
206 if (extension
->GetBoolean(kIsFromWebstore
, &is_from_webstore
) &&
208 creation_flags
|= Extension::FROM_WEBSTORE
;
210 bool keep_if_present
;
211 if (extension
->GetBoolean(kKeepIfPresent
, &keep_if_present
) &&
212 keep_if_present
&& profile_
) {
213 ExtensionServiceInterface
* extension_service
=
214 ExtensionSystem::Get(profile_
)->extension_service();
215 const Extension
* extension
= extension_service
?
216 extension_service
->GetExtensionById(extension_id
, true) : NULL
;
218 VLOG(1) << "Skip installing (or uninstall) external extension: "
219 << extension_id
<< " because the extension should be kept "
220 << "only if it is already installed.";
224 bool was_installed_by_oem
;
225 if (extension
->GetBoolean(kWasInstalledByOem
, &was_installed_by_oem
) &&
226 was_installed_by_oem
) {
227 creation_flags
|= Extension::WAS_INSTALLED_BY_OEM
;
230 std::string install_parameter
;
231 extension
->GetString(kInstallParam
, &install_parameter
);
233 if (has_external_crx
) {
234 if (crx_location_
== Manifest::INVALID_LOCATION
) {
235 LOG(WARNING
) << "This provider does not support installing external "
236 << "extensions from crx files.";
239 if (external_crx
.find(base::FilePath::kParentDirectory
) !=
240 base::StringPiece::npos
) {
241 LOG(WARNING
) << "Path traversal not allowed in path: "
242 << external_crx
.c_str();
246 // If the path is relative, and the provider has a base path,
247 // build the absolute path to the crx file.
248 base::FilePath
path(external_crx
);
249 if (!path
.IsAbsolute()) {
250 base::FilePath base_path
= loader_
->GetBaseCrxFilePath();
251 if (base_path
.empty()) {
252 LOG(WARNING
) << "File path " << external_crx
.c_str()
253 << " is relative. An absolute path is required.";
256 path
= base_path
.Append(external_crx
);
259 Version
version(external_version
);
260 if (!version
.IsValid()) {
261 LOG(WARNING
) << "Malformed extension dictionary for extension: "
262 << extension_id
.c_str() << ". Invalid version string \""
263 << external_version
<< "\".";
266 service_
->OnExternalExtensionFileFound(extension_id
, &version
, path
,
267 crx_location_
, creation_flags
,
269 } else { // if (has_external_update_url)
270 CHECK(has_external_update_url
); // Checking of keys above ensures this.
271 if (download_location_
== Manifest::INVALID_LOCATION
) {
272 LOG(WARNING
) << "This provider does not support installing external "
273 << "extensions from update URLs.";
276 GURL
update_url(external_update_url
);
277 if (!update_url
.is_valid()) {
278 LOG(WARNING
) << "Malformed extension dictionary for extension: "
279 << extension_id
.c_str() << ". Key " << kExternalUpdateUrl
280 << " has value \"" << external_update_url
281 << "\", which is not a valid URL.";
284 service_
->OnExternalExtensionUpdateUrlFound(extension_id
,
293 for (std::set
<std::string
>::iterator it
= unsupported_extensions
.begin();
294 it
!= unsupported_extensions
.end(); ++it
) {
295 // Remove extension for the list of know external extensions. The extension
296 // will be uninstalled later because provider doesn't provide it anymore.
297 prefs_
->Remove(*it
, NULL
);
300 service_
->OnExternalProviderReady(this);
303 void ExternalProviderImpl::ServiceShutdown() {
307 bool ExternalProviderImpl::IsReady() const {
311 bool ExternalProviderImpl::HasExtension(
312 const std::string
& id
) const {
313 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
316 return prefs_
->HasKey(id
);
319 bool ExternalProviderImpl::GetExtensionDetails(
320 const std::string
& id
, Manifest::Location
* location
,
321 scoped_ptr
<Version
>* version
) const {
322 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
325 base::DictionaryValue
* extension
= NULL
;
326 if (!prefs_
->GetDictionary(id
, &extension
))
329 Manifest::Location loc
= Manifest::INVALID_LOCATION
;
330 if (extension
->HasKey(kExternalUpdateUrl
)) {
331 loc
= download_location_
;
333 } else if (extension
->HasKey(kExternalCrx
)) {
336 std::string external_version
;
337 if (!extension
->GetString(kExternalVersion
, &external_version
))
341 version
->reset(new Version(external_version
));
344 NOTREACHED(); // Chrome should not allow prefs to get into this state.
355 void ExternalProviderImpl::CreateExternalProviders(
356 VisitorInterface
* service
,
358 ProviderCollection
* provider_list
) {
359 scoped_refptr
<ExternalLoader
> external_loader
;
360 extensions::Manifest::Location crx_location
= Manifest::INVALID_LOCATION
;
361 #if defined(OS_CHROMEOS)
362 policy::BrowserPolicyConnectorChromeOS
* connector
=
363 g_browser_process
->platform_part()->browser_policy_connector_chromeos();
364 bool is_chrome_os_public_session
= false;
365 const chromeos::User
* user
=
366 chromeos::UserManager::Get()->GetUserByProfile(profile
);
367 policy::DeviceLocalAccount::Type account_type
;
368 if (user
&& policy::IsDeviceLocalAccountUser(user
->email(), &account_type
)) {
369 if (account_type
== policy::DeviceLocalAccount::TYPE_PUBLIC_SESSION
)
370 is_chrome_os_public_session
= true;
371 policy::DeviceLocalAccountPolicyBroker
* broker
=
372 connector
->GetDeviceLocalAccountPolicyService()->GetBrokerForUser(
375 external_loader
= broker
->extension_loader();
376 crx_location
= Manifest::EXTERNAL_POLICY
;
381 external_loader
= new ExternalPolicyLoader(profile
);
384 external_loader
= new ExternalPolicyLoader(profile
);
387 // Policies are mandatory so they can't be skipped with command line flag.
388 if (external_loader
) {
389 provider_list
->push_back(
390 linked_ptr
<ExternalProviderInterface
>(
391 new ExternalProviderImpl(
396 Manifest::EXTERNAL_POLICY_DOWNLOAD
,
397 Extension::NO_FLAGS
)));
400 // In tests don't install extensions from default external sources.
401 // It would only slowdown tests and make them flaky.
402 if (CommandLine::ForCurrentProcess()->HasSwitch(
403 switches::kDisableDefaultApps
))
406 // No external app install in app mode.
407 if (chrome::IsRunningInForcedAppMode())
410 // On Mac OS, items in /Library/... should be written by the superuser.
411 // Check that all components of the path are writable by root only.
412 ExternalPrefLoader::Options check_admin_permissions_on_mac
;
413 #if defined(OS_MACOSX)
414 check_admin_permissions_on_mac
=
415 ExternalPrefLoader::ENSURE_PATH_CONTROLLED_BY_ADMIN
;
417 check_admin_permissions_on_mac
= ExternalPrefLoader::NONE
;
420 bool is_chromeos_demo_session
= false;
421 int bundled_extension_creation_flags
= Extension::NO_FLAGS
;
422 #if defined(OS_CHROMEOS)
423 chromeos::UserManager
* user_manager
= chromeos::UserManager::Get();
424 is_chromeos_demo_session
=
425 user_manager
&& user_manager
->IsLoggedInAsDemoUser() &&
426 connector
->GetDeviceMode() == policy::DEVICE_MODE_RETAIL_KIOSK
;
427 bundled_extension_creation_flags
= Extension::FROM_WEBSTORE
|
428 Extension::WAS_INSTALLED_BY_DEFAULT
;
431 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
432 if (!profile
->IsManaged()) {
433 provider_list
->push_back(
434 linked_ptr
<ExternalProviderInterface
>(
435 new ExternalProviderImpl(
437 new ExternalPrefLoader(
438 chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS
,
439 ExternalPrefLoader::NONE
),
441 Manifest::EXTERNAL_PREF
,
442 Manifest::EXTERNAL_PREF_DOWNLOAD
,
443 bundled_extension_creation_flags
)));
447 #if defined(OS_CHROMEOS)
448 if (!is_chromeos_demo_session
&& !is_chrome_os_public_session
) {
449 int external_apps_path_id
= profile
->IsManaged() ?
450 chrome::DIR_MANAGED_USERS_DEFAULT_APPS
:
451 chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS
;
452 provider_list
->push_back(
453 linked_ptr
<ExternalProviderInterface
>(new ExternalProviderImpl(
455 new ExternalPrefLoader(external_apps_path_id
,
456 ExternalPrefLoader::NONE
),
458 Manifest::EXTERNAL_PREF
,
459 Manifest::EXTERNAL_PREF_DOWNLOAD
,
460 bundled_extension_creation_flags
)));
463 int oem_extension_creation_flags
=
464 bundled_extension_creation_flags
| Extension::WAS_INSTALLED_BY_OEM
;
465 chromeos::ServicesCustomizationDocument
* customization
=
466 chromeos::ServicesCustomizationDocument::GetInstance();
467 provider_list
->push_back(linked_ptr
<ExternalProviderInterface
>(
468 new ExternalProviderImpl(service
,
469 customization
->CreateExternalLoader(profile
),
471 Manifest::EXTERNAL_PREF
,
472 Manifest::EXTERNAL_PREF_DOWNLOAD
,
473 oem_extension_creation_flags
)));
476 policy::AppPackUpdater
* app_pack_updater
= connector
->GetAppPackUpdater();
477 if (is_chromeos_demo_session
&& app_pack_updater
&&
478 !app_pack_updater
->created_external_loader()) {
479 provider_list
->push_back(
480 linked_ptr
<ExternalProviderInterface
>(
481 new ExternalProviderImpl(
483 app_pack_updater
->CreateExternalLoader(),
485 Manifest::EXTERNAL_PREF
,
486 Manifest::INVALID_LOCATION
,
487 Extension::NO_FLAGS
)));
491 if (!profile
->IsManaged() && !is_chromeos_demo_session
) {
493 provider_list
->push_back(
494 linked_ptr
<ExternalProviderInterface
>(
495 new ExternalProviderImpl(
497 new ExternalPrefLoader(chrome::DIR_EXTERNAL_EXTENSIONS
,
498 check_admin_permissions_on_mac
),
500 Manifest::EXTERNAL_PREF
,
501 Manifest::EXTERNAL_PREF_DOWNLOAD
,
502 bundled_extension_creation_flags
)));
505 // Define a per-user source of external extensions.
506 #if defined(OS_MACOSX)
507 provider_list
->push_back(
508 linked_ptr
<ExternalProviderInterface
>(
509 new ExternalProviderImpl(
511 new ExternalPrefLoader(chrome::DIR_USER_EXTERNAL_EXTENSIONS
,
512 ExternalPrefLoader::NONE
),
514 Manifest::EXTERNAL_PREF
,
515 Manifest::EXTERNAL_PREF_DOWNLOAD
,
516 Extension::NO_FLAGS
)));
520 provider_list
->push_back(
521 linked_ptr
<ExternalProviderInterface
>(
522 new ExternalProviderImpl(
524 new ExternalRegistryLoader
,
526 Manifest::EXTERNAL_REGISTRY
,
527 Manifest::EXTERNAL_PREF_DOWNLOAD
,
528 Extension::NO_FLAGS
)));
531 #if !defined(OS_CHROMEOS)
532 // The default apps are installed as INTERNAL but use the external
533 // extension installer codeflow.
534 provider_list
->push_back(
535 linked_ptr
<ExternalProviderInterface
>(
536 new default_apps::Provider(
539 new ExternalPrefLoader(chrome::DIR_DEFAULT_APPS
,
540 ExternalPrefLoader::NONE
),
542 Manifest::INVALID_LOCATION
,
543 Extension::FROM_WEBSTORE
|
544 Extension::WAS_INSTALLED_BY_DEFAULT
)));
547 provider_list
->push_back(
548 linked_ptr
<ExternalProviderInterface
>(
549 new ExternalProviderImpl(
551 new ExternalComponentLoader(profile
),
553 Manifest::INVALID_LOCATION
,
554 Manifest::EXTERNAL_COMPONENT
,
555 Extension::FROM_WEBSTORE
| Extension::WAS_INSTALLED_BY_DEFAULT
)));
559 } // namespace extensions