Add minimal profile creation version for default apps install on Chrome OS
[chromium-blink-merge.git] / chrome / browser / extensions / external_provider_impl.cc
blob12ab37ed85d7438b951be9c2668a21c4055e5555
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"
7 #include <set>
8 #include <vector>
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"
48 #else
49 #include "chrome/browser/extensions/default_apps.h"
50 #endif
52 #if defined(OS_WIN)
53 #include "chrome/browser/extensions/external_registry_loader_win.h"
54 #endif
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,
77 Profile* profile,
78 Manifest::Location crx_location,
79 Manifest::Location download_location,
80 int creation_flags)
81 : crx_location_(crx_location),
82 download_location_(download_location),
83 service_(service),
84 ready_(false),
85 loader_(loader),
86 profile_(profile),
87 creation_flags_(creation_flags),
88 auto_acknowledge_(false),
89 install_immediately_(false) {
90 loader_->Init(this);
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;
110 prefs_.reset(prefs);
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.";
124 continue;
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.";
131 continue;
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;
146 } else {
147 LOG(WARNING) << "Malformed extension dictionary for extension: "
148 << extension_id.c_str() << ". " << kExternalVersion
149 << " value must be a string.";
150 continue;
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.";
160 continue;
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 << ".";
168 continue;
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(),
176 &browser_locales);
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, &current_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;
188 break;
190 } else {
191 LOG(WARNING) << "Unrecognized locale '" << current_locale
192 << "' found as supported locale for extension: "
193 << extension_id;
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.";
202 continue;
206 int creation_flags = creation_flags_;
207 bool is_bookmark_app;
208 if (extension->GetBoolean(kIsBookmarkApp, &is_bookmark_app) &&
209 is_bookmark_app) {
210 creation_flags |= Extension::FROM_BOOKMARK;
212 bool is_from_webstore = false;
213 if (extension->GetBoolean(kIsFromWebstore, &is_from_webstore) &&
214 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;
224 if (!extension) {
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.";
229 continue;
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) &&
239 may_be_untrusted) {
240 creation_flags |= Extension::MAY_BE_UNTRUSTED;
243 if (!ExternalProviderImpl::HandleMinProfileVersion(extension, extension_id,
244 &unsupported_extensions))
245 continue;
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.";
254 continue;
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();
260 continue;
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.";
271 continue;
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 << "\".";
281 continue;
283 service_->OnExternalExtensionFileFound(extension_id, &version, path,
284 crx_location_, creation_flags,
285 auto_acknowledge_,
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.";
292 continue;
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.";
300 continue;
302 service_->OnExternalExtensionUpdateUrlFound(extension_id,
303 install_parameter,
304 update_url,
305 download_location_,
306 creation_flags,
307 auto_acknowledge_);
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() {
322 service_ = NULL;
325 bool ExternalProviderImpl::IsReady() const {
326 return ready_;
329 bool ExternalProviderImpl::HasExtension(
330 const std::string& id) const {
331 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
332 CHECK(prefs_.get());
333 CHECK(ready_);
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));
341 CHECK(prefs_.get());
342 CHECK(ready_);
343 base::DictionaryValue* extension = NULL;
344 if (!prefs_->GetDictionary(id, &extension))
345 return false;
347 Manifest::Location loc = Manifest::INVALID_LOCATION;
348 if (extension->HasKey(kExternalUpdateUrl)) {
349 loc = download_location_;
351 } else if (extension->HasKey(kExternalCrx)) {
352 loc = crx_location_;
354 std::string external_version;
355 if (!extension->GetString(kExternalVersion, &external_version))
356 return false;
358 if (version)
359 version->reset(new Version(external_version));
361 } else {
362 NOTREACHED(); // Chrome should not allow prefs to get into this state.
363 return false;
366 if (location)
367 *location = loc;
369 return true;
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;
377 if (profile_ &&
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: "
386 << extension_id
387 << " profile.created_by_version: " << profile_version.GetString()
388 << " min_profile_created_by_version: "
389 << min_profile_created_by_version;
390 return false;
393 return true;
396 // static
397 void ExternalProviderImpl::CreateExternalProviders(
398 VisitorInterface* service,
399 Profile* profile,
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;
413 if (user &&
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(
420 user->email());
421 if (broker) {
422 external_loader = broker->extension_loader();
423 crx_location = Manifest::EXTERNAL_POLICY;
424 } else {
425 NOTREACHED();
427 } else {
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);
435 #else
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);
442 #endif
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(
449 service,
450 external_loader,
451 profile,
452 crx_location,
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()));
474 #endif
475 return;
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,
483 profile,
484 crx_location,
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))
493 return;
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;
501 #else
502 check_admin_permissions_on_mac = ExternalPrefLoader::NONE;
503 #endif
505 #if !defined(OS_WIN)
506 int bundled_extension_creation_flags = Extension::NO_FLAGS;
507 #endif
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)));
527 // OEM default apps.
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),
535 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(
545 service,
546 new ExternalPrefLoader(
547 chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS,
548 ExternalPrefLoader::NONE,
549 NULL),
550 profile,
551 Manifest::EXTERNAL_PREF,
552 Manifest::EXTERNAL_PREF_DOWNLOAD,
553 bundled_extension_creation_flags)));
555 #endif
557 if (!profile->IsLegacySupervised()) {
558 #if defined(OS_WIN)
559 provider_list->push_back(
560 linked_ptr<ExternalProviderInterface>(
561 new ExternalProviderImpl(
562 service,
563 new ExternalRegistryLoader,
564 profile,
565 Manifest::EXTERNAL_REGISTRY,
566 Manifest::EXTERNAL_PREF_DOWNLOAD,
567 Extension::NO_FLAGS)));
568 #else
569 provider_list->push_back(
570 linked_ptr<ExternalProviderInterface>(
571 new ExternalProviderImpl(
572 service,
573 new ExternalPrefLoader(chrome::DIR_EXTERNAL_EXTENSIONS,
574 check_admin_permissions_on_mac,
575 NULL),
576 profile,
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(
586 service,
587 new ExternalPrefLoader(chrome::DIR_USER_EXTERNAL_EXTENSIONS,
588 ExternalPrefLoader::NONE,
589 NULL),
590 profile,
591 Manifest::EXTERNAL_PREF,
592 Manifest::EXTERNAL_PREF_DOWNLOAD,
593 Extension::NO_FLAGS)));
594 #endif
595 #endif
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(
603 profile,
604 service,
605 new ExternalPrefLoader(chrome::DIR_DEFAULT_APPS,
606 ExternalPrefLoader::NONE,
607 NULL),
608 Manifest::INTERNAL,
609 Manifest::INTERNAL,
610 Extension::FROM_WEBSTORE |
611 Extension::WAS_INSTALLED_BY_DEFAULT)));
612 #endif
615 provider_list->push_back(
616 linked_ptr<ExternalProviderInterface>(
617 new ExternalProviderImpl(
618 service,
619 new ExternalComponentLoader(profile),
620 profile,
621 Manifest::INVALID_LOCATION,
622 Manifest::EXTERNAL_COMPONENT,
623 Extension::FROM_WEBSTORE | Extension::WAS_INSTALLED_BY_DEFAULT)));
626 } // namespace extensions