Chromium Code Reviews| Index: chrome/browser/chromeos/app_mode/kiosk_external_updater.cc |
| diff --git a/chrome/browser/chromeos/app_mode/kiosk_external_updater.cc b/chrome/browser/chromeos/app_mode/kiosk_external_updater.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..83a7262c3f43eabf167922b9618140a8ec367361 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/app_mode/kiosk_external_updater.cc |
| @@ -0,0 +1,504 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/app_mode/kiosk_external_updater.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/file_util.h" |
| +#include "base/files/file_enumerator.h" |
| +#include "base/json/json_file_value_serializer.h" |
| +#include "base/location.h" |
| +#include "base/logging.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "base/version.h" |
| +#include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h" |
| +#include "chrome/browser/chromeos/ui/kiosk_external_update_notification.h" |
| +#include "chrome/browser/extensions/sandboxed_unpacker.h" |
| +#include "chrome/common/chrome_version_info.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "extensions/common/extension.h" |
| +#include "extensions/common/manifest_constants.h" |
| +#include "grit/chromium_strings.h" |
| +#include "grit/generated_resources.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| + |
| +namespace chromeos { |
| + |
| +namespace { |
| + |
| +const char kExternalUpdateManifest[] = "external_update.json"; |
| +const char kExternalCrx[] = "external_crx"; |
| +const char kExternalVersion[] = "external_version"; |
| + |
| +} // namespace |
| + |
| +KioskExternalUpdater::ExternalUpdate::ExternalUpdate() { |
| +} |
| + |
| +KioskExternalUpdater::KioskExternalUpdater( |
| + const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner, |
| + const base::FilePath& crx_cache_dir, |
| + const base::FilePath& crx_unpack_dir) |
| + : backend_task_runner_(backend_task_runner), |
| + crx_cache_dir_(crx_cache_dir), |
| + crx_unpack_dir_(crx_unpack_dir) { |
| + // Subscribe to DiskMountManager. |
| + DCHECK(disks::DiskMountManager::GetInstance()); |
| + disks::DiskMountManager::GetInstance()->AddObserver(this); |
| +} |
| + |
| +KioskExternalUpdater::~KioskExternalUpdater() { |
| + if (disks::DiskMountManager::GetInstance()) |
| + disks::DiskMountManager::GetInstance()->RemoveObserver(this); |
| +} |
| + |
| +void KioskExternalUpdater::OnDiskEvent( |
| + disks::DiskMountManager::DiskEvent event, |
| + const disks::DiskMountManager::Disk* disk) { |
| +} |
| + |
| +void KioskExternalUpdater::OnDeviceEvent( |
| + disks::DiskMountManager::DeviceEvent event, |
| + const std::string& device_path) { |
| +} |
| + |
| +void KioskExternalUpdater::OnMountEvent( |
| + disks::DiskMountManager::MountEvent event, |
| + MountError error_code, |
| + const disks::DiskMountManager::MountPointInfo& mount_info) { |
| + if (mount_info.mount_type != MOUNT_TYPE_DEVICE || |
| + error_code != MOUNT_ERROR_NONE) { |
| + return; |
| + } |
| + |
| + if (event == disks::DiskMountManager::MOUNTING) { |
| + // If multiple disks have been mounted, skip the rest of them if kiosk |
| + // update has already been found. |
| + if (!external_update_path_.empty()) { |
| + LOG(WARNING) << "*** external update path already found, skip " |
| + << mount_info.mount_path; |
| + return; |
| + } |
| + |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + ShowKioskUpdateProgressOnUI( |
| + ui::ResourceBundle::GetSharedInstance().GetLocalizedString( |
| + IDS_KIOSK_EXTERNAL_UPDATE_IN_PROGRESS)); |
| + |
| + backend_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&KioskExternalUpdater::LoadExternalUpdateManifest, |
|
xiyuan
2014/08/23 18:11:57
We can break LoadExternalUpdateManifest up into tw
|
| + this, |
| + base::FilePath(mount_info.mount_path))); |
| + } else { // unmounting a removable device. |
| + if (external_update_path_.value().empty()) { |
| + // Clear any previously displayed message. |
| + DismissKioskUpdateNotificationOnUIThread(); |
| + } else if (external_update_path_.value() == mount_info.mount_path) { |
| + DismissKioskUpdateNotificationOnUIThread(); |
| + if (IsExternalUpdatePending()) { |
| + LOG(ERROR) << "External kiosk update is not completed when the usb " |
| + "stick is unmoutned."; |
| + } |
| + external_updates_.clear(); |
| + external_update_path_.clear(); |
| + } |
| + } |
| +} |
| + |
| +void KioskExternalUpdater::OnFormatEvent( |
| + disks::DiskMountManager::FormatEvent event, |
| + FormatError error_code, |
| + const std::string& device_path) { |
| +} |
| + |
| +void KioskExternalUpdater::OnExtenalUpdateUnpackSuccess( |
| + const std::string& app_id, |
| + const base::FilePath& temp_dir, |
| + const extensions::Extension* extension) { |
| + if (external_updates_.empty()) { |
| + // This could happen if user pulls out the usb stick before the updating |
| + // operation is completed. |
| + LOG(ERROR) << "external_updates_ has been cleared before external " |
| + << "updating completes."; |
| + return; |
| + } |
| + |
| + if (!ShouldDoExternalUpdate(app_id, extension)) { |
| + external_updates_[app_id].update_status = FAILED; |
| + MayBeNotifyKioskAppUpdate(); |
| + return; |
| + } |
| + |
| + // Cleans upt he temp_dir for unpacking. |
|
xiyuan
2014/08/23 18:11:57
nit: upt he -> up the
jennyz
2014/08/27 00:58:41
Done.
|
| + base::DeleteFile(temp_dir, true); |
| + |
| + // Copy the newer version from usb stick to cache. |
| + std::string filename = app_id + "-" + extension->VersionString() + ".crx"; |
| + base::FilePath new_cache_file = crx_cache_dir_.AppendASCII(filename); |
| + CacheExternalCrx(app_id, new_cache_file); |
| +} |
| + |
| +void KioskExternalUpdater::OnExternalUpdateUnpackFailure( |
| + const std::string& app_id) { |
| + if (external_updates_.empty()) { |
| + // This could happen if user pulls out the usb stick before the updating |
| + // operation is completed. |
| + LOG(ERROR) << "external_updates_ has been cleared before external " |
| + << "updating completes."; |
| + return; |
| + } |
| + |
| + external_updates_[app_id].update_status = FAILED; |
| + external_updates_[app_id].error = |
| + ui::ResourceBundle::GetSharedInstance().GetLocalizedString( |
| + IDS_KIOSK_EXTERNAL_UPDATE_BAD_CRX); |
| + MayBeNotifyKioskAppUpdate(); |
| +} |
| + |
| +void KioskExternalUpdater::LoadExternalUpdateManifest( |
| + const base::FilePath& external_update_dir) { |
| + base::FilePath manifest = |
| + external_update_dir.AppendASCII(kExternalUpdateManifest); |
| + if (!base::PathExists(manifest)) { |
| + LOG(WARNING) << "*** Can't find kiosk external update manifest file " |
| + << manifest.value(); |
| + ShowKioskUpdateProgressOnUI( |
| + ui::ResourceBundle::GetSharedInstance().GetLocalizedString( |
| + IDS_KIOSK_EXTERNAL_UPDATE_NO_MANIFEST)); |
| + return; |
| + } |
| + |
| + external_update_path_ = external_update_dir; |
| + |
| + JSONFileValueSerializer serializer(manifest); |
| + std::string error_msg; |
| + base::Value* extensions = serializer.Deserialize(NULL, &error_msg); |
| + if (!extensions) { |
| + ShowKioskUpdateProgressOnUI( |
| + ui::ResourceBundle::GetSharedInstance().GetLocalizedString( |
| + IDS_KIOSK_EXTERNAL_UPDATE_INVALID_MANIFEST)); |
| + LOG(ERROR) << "Unable to deserialize json data: " << error_msg |
| + << " in manifest file " << manifest.value() << "."; |
| + return; |
| + } |
| + |
| + base::DictionaryValue* update_prefs = NULL; |
| + if (!extensions->GetAsDictionary(&update_prefs)) { |
| + ShowKioskUpdateProgressOnUI( |
| + ui::ResourceBundle::GetSharedInstance().GetLocalizedString( |
| + IDS_KIOSK_EXTERNAL_UPDATE_INVALID_MANIFEST)); |
| + LOG(ERROR) << "Invalid manifest file, expected dictionary data:" |
| + << manifest.value(); |
| + return; |
| + } |
| + |
| + for (base::DictionaryValue::Iterator it(*update_prefs); !it.IsAtEnd(); |
| + it.Advance()) { |
| + std::string app_id = it.key(); |
| + std::string cached_version_str; |
| + base::FilePath cached_crx; |
| + if (!KioskAppManager::Get()->GetCachedCrx( |
| + app_id, &cached_crx, &cached_version_str)) { |
| + LOG(WARNING) << "Can't find app in existing cache " << app_id; |
| + continue; |
| + } |
| + |
| + const base::DictionaryValue* extension = NULL; |
| + if (!it.value().GetAsDictionary(&extension)) { |
| + LOG(ERROR) << "Found bad entry in manifest type " << it.value().GetType(); |
| + continue; |
| + } |
| + |
| + std::string external_crx_str; |
| + if (!extension->GetString(kExternalCrx, &external_crx_str)) { |
| + LOG(ERROR) << "Can't find external crx in manifest " << app_id; |
| + continue; |
| + } |
| + // Validate path first. |
| + base::FilePath external_crx = |
| + external_update_dir.AppendASCII(external_crx_str); |
| + if (!base::PathExists(external_crx)) { |
| + LOG(ERROR) << "External crx does not exist " << external_crx.value(); |
| + continue; |
| + } |
| + |
| + std::string external_version_str; |
| + if (!extension->GetString(kExternalVersion, &external_version_str)) { |
| + LOG(ERROR) << "Can't find external version in manifest " << app_id; |
| + continue; |
| + } |
| + base::Version external_version(external_version_str); |
| + base::Version cached_version(cached_version_str); |
| + switch (cached_version.CompareTo(external_version)) { |
| + case -1: // cached version is older, we should upgrade |
| + break; |
| + case 0: // cached version is same, do nothing |
| + LOG(WARNING) << "External app " << app_id |
| + << "is at the same version with manifest"; |
| + continue; |
| + case 1: // cached version is newer, do nothing. |
| + LOG(WARNING) << "Found external version of extension " << app_id |
| + << "that is older than current version. Current version " |
| + << "is: " << cached_version_str << ". New " |
| + << "version is: " << external_version_str |
| + << ". Keeping current version."; |
| + continue; |
| + } |
| + |
| + ExternalUpdate update; |
| + KioskAppManager::App app; |
| + if (KioskAppManager::Get()->GetApp(app_id, &app)) { |
| + update.app_name = app.name; |
| + } else { |
| + NOTREACHED(); |
| + } |
| + update.external_crx = external_crx; |
| + update.update_status = PENDING; |
| + external_updates_[app_id] = update; |
| + } |
| + |
| + if (!external_updates_.size()) { |
|
xiyuan
2014/08/23 18:11:57
nit: if (external_updates_.empty())
jennyz
2014/08/27 00:58:42
Done.
|
| + ShowKioskUpdateProgressOnUI( |
| + ui::ResourceBundle::GetSharedInstance().GetLocalizedString( |
| + IDS_KIOSK_EXTERNAL_UPDATE_NO_UPDATES)); |
| + return; |
| + } |
| + |
| + ValidateExternalUpdates(); |
| +} |
| + |
| +void KioskExternalUpdater::ValidateExternalUpdates() { |
| + DCHECK(backend_task_runner_->RunsTasksOnCurrentThread()); |
| + for (ExternalUpdateMap::iterator it = external_updates_.begin(); |
| + it != external_updates_.end(); |
| + ++it) { |
| + scoped_refptr<KioskExternalUpdateValidator> crx_validator = |
|
xiyuan
2014/08/23 18:11:57
To not ref-counting updater, we should keep track
jennyz
2014/08/27 00:58:41
KioskExternalUpdater is not ref counted, and the v
|
| + new KioskExternalUpdateValidator(backend_task_runner_, |
| + it->first, |
| + it->second.external_crx, |
| + crx_unpack_dir_, |
| + this); |
| + crx_validator->Start(); |
| + } |
| +} |
| + |
| +void KioskExternalUpdater::CacheExternalCrx(const std::string& app_id, |
| + const base::FilePath& target_file) { |
| + if (!backend_task_runner_->RunsTasksOnCurrentThread()) { |
| + if (!backend_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&KioskExternalUpdater::CacheExternalCrx, |
| + this, |
| + app_id, |
| + target_file))) { |
| + NOTREACHED(); |
| + } |
| + return; |
| + } |
| + |
| + // User might pull out the usb stick before updating is completed. |
| + if (external_updates_.empty()) { |
| + LOG(ERROR) << "External_updates_ has been cleared before the external " |
| + << "update completes."; |
| + return; |
| + } |
| + |
| + if (!base::CopyFile(external_updates_[app_id].external_crx, target_file)) { |
| + external_updates_[app_id].update_status = FAILED; |
| + external_updates_[app_id].error = l10n_util::GetStringFUTF16( |
| + IDS_KIOSK_EXTERNAL_UPDATE_CANNOT_COPY_CRX, |
| + base::UTF8ToUTF16(external_updates_[app_id].external_crx.value()), |
| + base::UTF8ToUTF16(target_file.value())); |
| + } else { |
| + external_updates_[app_id].update_status = SUCCESS; |
| + } |
| + |
| + MayBeNotifyKioskAppUpdate(); |
| +} |
| + |
| +bool KioskExternalUpdater::IsExternalUpdatePending() { |
| + for (ExternalUpdateMap::iterator it = external_updates_.begin(); |
| + it != external_updates_.end(); |
| + ++it) { |
| + if (it->second.update_status == PENDING) { |
| + return true; |
| + } |
| + } |
| + return false; |
| +} |
| + |
| +bool KioskExternalUpdater::ShouldDoExternalUpdate( |
| + const std::string& app_id, |
| + const extensions::Extension* extenral_extension) { |
| + // User might have pulled out the usb stick before external updating is done. |
| + if (external_updates_.empty()) { |
| + LOG(ERROR) << "external_updates_ has been cleared before external " |
| + << "updating completes."; |
| + return false; |
| + } |
| + |
| + std::string existing_version_str; |
| + base::FilePath existing_path; |
| + bool cached = KioskAppManager::Get()->GetCachedCrx( |
| + app_id, &existing_path, &existing_version_str); |
| + DCHECK(cached); |
| + |
| + // Compare app version. |
| + const base::Version existing_version(existing_version_str); |
| + ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| + switch (existing_version.CompareTo(*extenral_extension->version())) { |
| + case -1: // existing version is older, we should upgrade |
| + break; |
| + case 0: // existing version is same, no update |
| + external_updates_[app_id].error = |
| + rb.GetLocalizedString(IDS_KIOSK_EXTERNAL_UPDATE_SAME_APP_VERSION); |
| + return false; |
| + case 1: // existing version is newer, no update |
| + external_updates_[app_id].error = l10n_util::GetStringFUTF16( |
| + IDS_KIOSK_EXTERNAL_UPDATE_EXISTING_VERSION_NEWER, |
| + base::UTF8ToUTF16(extenral_extension->VersionString()), |
| + base::UTF8ToUTF16(existing_version_str)); |
| + return false; |
| + } |
| + |
| + // Check minimum browser version. |
| + std::string minimum_version_string; |
| + if (!extenral_extension->manifest()->GetString( |
| + extensions::manifest_keys::kMinimumChromeVersion, |
| + &minimum_version_string)) { |
| + external_updates_[app_id].error = rb.GetLocalizedString( |
| + IDS_KIOSK_EXTERNAL_UPDATE_INVALID_MIN_BROWSER_VERSION); |
| + return false; |
| + } |
| + |
| + Version minimum_version(minimum_version_string); |
| + if (!minimum_version.IsValid()) { |
| + external_updates_[app_id].error = rb.GetLocalizedString( |
| + IDS_KIOSK_EXTERNAL_UPDATE_INVALID_MIN_BROWSER_VERSION); |
| + return false; |
| + } |
| + |
| + chrome::VersionInfo current_version_info; |
| + Version current_version(current_version_info.Version()); |
| + if (!current_version.IsValid()) { |
| + NOTREACHED(); |
| + return false; |
| + } |
| + |
| + base::string16 error; |
| + if (current_version.CompareTo(minimum_version) < 0) { |
| + external_updates_[app_id].error = l10n_util::GetStringFUTF16( |
| + IDS_KIOSK_EXTERNAL_UPDATE_REQUIRE_HIGHER_BROWSER_VERSION, |
| + l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)); |
| + return false; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +void KioskExternalUpdater::ShowKioskUpdateProgressOnUI( |
|
xiyuan
2014/08/23 18:11:57
nit: OnUI is misleading. Other places uses this su
jennyz
2014/08/27 00:58:41
Renamed.
|
| + const base::string16& message) { |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind( |
| + &KioskExternalUpdater::ShowKioskUpdateProgress, this, message)); |
| +} |
| + |
| +void KioskExternalUpdater::MayBeNotifyKioskAppUpdate() { |
| + if (IsExternalUpdatePending()) |
| + return; |
| + |
| + ShowKioskUpdateProgressOnUI(GetUpdateReportMessage()); |
| + |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&KioskExternalUpdater::NotifyKioskAppUpdateAvailable, this)); |
| +} |
| + |
| +void KioskExternalUpdater::NotifyKioskAppUpdateAvailable() { |
| + /// This should be done on UI thread??? |
|
xiyuan
2014/08/23 18:11:58
Yes, this has to be called on UI.
jennyz
2014/08/27 00:58:42
Acknowledged.
|
| + for (ExternalUpdateMap::iterator it = external_updates_.begin(); |
| + it != external_updates_.end(); |
| + ++it) { |
| + if (it->second.update_status == SUCCESS) { |
| + KioskAppManager::Get()->OnKioskAppCacheUpdated(it->first); |
| + } |
| + } |
| +} |
| + |
| +void KioskExternalUpdater::ShowKioskUpdateProgress( |
| + const base::string16& message) { |
| + if (!notification_) |
| + notification_.reset(new KioskExternalUpdateNotification(message)); |
| + else |
| + notification_->ShowMessage(message); |
| +} |
| + |
| +void KioskExternalUpdater::DismissKioskUpdateNotificationOnUIThread() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&KioskExternalUpdater::DismissKioskUpdateNotification, this)); |
| +} |
| + |
| +void KioskExternalUpdater::DismissKioskUpdateNotification() { |
| + if (notification_.get()) { |
| + notification_->Dismiss(); |
| + notification_.reset(); |
| + } |
| +} |
| + |
| +base::string16 KioskExternalUpdater::GetUpdateReportMessage() { |
| + DCHECK(!IsExternalUpdatePending()); |
| + int updated = 0; |
| + int failed = 0; |
| + base::string16 updated_apps; |
| + base::string16 failed_apps; |
| + for (ExternalUpdateMap::iterator it = external_updates_.begin(); |
| + it != external_updates_.end(); |
| + ++it) { |
| + base::string16 app_name = base::UTF8ToUTF16(it->second.app_name); |
| + if (it->second.update_status == SUCCESS) { |
| + ++updated; |
| + if (updated_apps.empty()) |
| + updated_apps = app_name; |
| + else |
| + updated_apps = updated_apps + base::UTF8ToUTF16(", ") + app_name; |
| + } else { // FAILED |
| + ++failed; |
| + if (failed_apps.empty()) { |
| + failed_apps = app_name + base::UTF8ToUTF16(": ") + it->second.error; |
| + } else { |
| + failed_apps = failed_apps + base::UTF8ToUTF16("\n") + app_name + |
| + base::UTF8ToUTF16(": ") + it->second.error; |
| + } |
| + } |
| + } |
| + |
| + base::string16 message; |
| + message = ui::ResourceBundle::GetSharedInstance().GetLocalizedString( |
| + IDS_KIOSK_EXTERNAL_UPDATE_COMPLETE); |
| + base::string16 success_app_msg; |
| + if (updated) { |
| + success_app_msg = l10n_util::GetStringFUTF16( |
| + IDS_KIOSK_EXTERNAL_UPDATE_SUCCESSFUL_UPDATED_APPS, updated_apps); |
| + message = message + base::UTF8ToUTF16("\n") + success_app_msg; |
| + } |
| + |
| + base::string16 failed_app_msg; |
| + if (failed) { |
| + failed_app_msg = ui::ResourceBundle::GetSharedInstance().GetLocalizedString( |
| + IDS_KIOSK_EXTERNAL_UPDATE_FAILED_UPDATED_APPS) + |
| + base::UTF8ToUTF16("\n") + failed_apps; |
| + message = message + base::UTF8ToUTF16("\n") + failed_app_msg; |
| + } |
| + return message; |
| +} |
| + |
| +} // namespace chromeos |