Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(494)

Unified Diff: chrome/browser/chromeos/app_mode/kiosk_external_updater.cc

Issue 491403003: Update cached kiosk app crx from usb stick. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove the refcounted implementation KioskExternalUpdater. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..61aab94faa5d415d3607c20b446d2db95b623483
--- /dev/null
+++ b/chrome/browser/chromeos/app_mode/kiosk_external_updater.cc
@@ -0,0 +1,543 @@
+// 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 "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";
+
+void ParseExternalUpdateManifest(
+ const base::FilePath& external_update_dir,
+ base::DictionaryValue* parsed_manifest,
+ KioskExternalUpdater::ExternalUpdateErrorCode* error_code) {
+ base::FilePath manifest =
+ external_update_dir.AppendASCII(kExternalUpdateManifest);
+ if (!base::PathExists(manifest)) {
+ *error_code = KioskExternalUpdater::ERROR_NO_MANIFEST;
+ return;
+ }
+
+ JSONFileValueSerializer serializer(manifest);
+ std::string error_msg;
+ base::Value* extensions = serializer.Deserialize(NULL, &error_msg);
+ if (!extensions) {
+ *error_code = KioskExternalUpdater::ERROR_INVALID_MANIFEST;
+ return;
+ }
+
+ base::DictionaryValue* dict_value = NULL;
+ if (!extensions->GetAsDictionary(&dict_value)) {
+ *error_code = KioskExternalUpdater::ERROR_INVALID_MANIFEST;
+ return;
+ }
+
+ parsed_manifest->Swap(dict_value);
+ *error_code = KioskExternalUpdater::ERROR_NONE;
+}
+
+void CacheExternalCrx(const base::FilePath& external_file,
xiyuan 2014/08/27 16:17:03 nit: Use a function name that reflects what it doe
jennyz 2014/08/28 22:41:06 Done.
+ const base::FilePath& target_file,
+ const base::FilePath& temp_dir,
+ bool* success) {
+ base::DeleteFile(temp_dir, true);
+ *success = base::CopyFile(external_file, target_file);
+}
+
+} // 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),
+ weak_factory_(this) {
+ // 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) {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ 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;
+ }
+
+ NotifyKioskUpdateProgress(
+ ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
+ IDS_KIOSK_EXTERNAL_UPDATE_IN_PROGRESS));
+
+ base::DictionaryValue* parsed_manifest = new base::DictionaryValue();
+ ExternalUpdateErrorCode* parsing_error = new ExternalUpdateErrorCode;
+ backend_task_runner_->PostTaskAndReply(
+ FROM_HERE,
+ base::Bind(&ParseExternalUpdateManifest,
+ base::FilePath(mount_info.mount_path),
+ parsed_manifest,
+ parsing_error),
+ base::Bind(&KioskExternalUpdater::ProcessParsedManifest,
+ weak_factory_.GetWeakPtr(),
+ base::Owned(parsing_error),
+ base::FilePath(mount_info.mount_path),
+ base::Owned(parsed_manifest)));
+ } 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 std::string& version,
+ const std::string& min_browser_version,
+ const base::FilePath& temp_dir) {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+
+ // User might pull out the usb stick before updating is completed.
+ if (CheckExternalUpdateInterrupted())
+ return;
+
+ if (!ShouldDoExternalUpdate(app_id, version, min_browser_version)) {
+ external_updates_[app_id].update_status = FAILED;
+ MaybeValidateNextExternalUpdate();
+ return;
+ }
+
+ // Copy the newer version from usb stick to cache.
+ std::string filename = app_id + "-" + version + ".crx";
+ base::FilePath new_cache_file = crx_cache_dir_.AppendASCII(filename);
xiyuan 2014/08/27 16:17:03 We should not assume the filename in ExternalCache
jennyz 2014/08/28 22:41:07 Changed to use LocalExtensionCache::PutExtension t
+
+ // User might pull out the usb stick before updating is completed.
+ if (CheckExternalUpdateInterrupted())
+ return;
+
+ bool* success = new bool;
+ backend_task_runner_->PostTaskAndReply(
+ FROM_HERE,
+ base::Bind(&CacheExternalCrx,
+ external_updates_[app_id].external_crx,
+ new_cache_file,
+ temp_dir,
+ success),
+ base::Bind(&KioskExternalUpdater::OnCacheExternalCrx,
+ weak_factory_.GetWeakPtr(),
+ app_id,
+ new_cache_file,
+ base::Owned(success)));
+}
+
+void KioskExternalUpdater::OnExternalUpdateUnpackFailure(
+ const std::string& app_id) {
+ // User might pull out the usb stick before updating is completed.
+ if (CheckExternalUpdateInterrupted())
+ return;
+
+ external_updates_[app_id].update_status = FAILED;
+ external_updates_[app_id].error =
+ ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
+ IDS_KIOSK_EXTERNAL_UPDATE_BAD_CRX);
+ MaybeValidateNextExternalUpdate();
+}
+
+void KioskExternalUpdater::ProcessParsedManifest(
+ ExternalUpdateErrorCode* parsing_error,
+ const base::FilePath& external_update_dir,
+ base::DictionaryValue* parsed_manifest) {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+
+ if (*parsing_error == ERROR_NO_MANIFEST) {
+ NotifyKioskUpdateProgress(
+ ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
+ IDS_KIOSK_EXTERNAL_UPDATE_NO_MANIFEST));
+ return;
+ } else if (*parsing_error == ERROR_INVALID_MANIFEST) {
+ NotifyKioskUpdateProgress(
+ ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
+ IDS_KIOSK_EXTERNAL_UPDATE_INVALID_MANIFEST));
+ return;
+ }
+
+ external_update_path_ = external_update_dir;
+ for (base::DictionaryValue::Iterator it(*parsed_manifest); !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_path_.AppendASCII(external_crx_str);
+ if (!base::PathExists(external_crx)) {
xiyuan 2014/08/27 16:17:03 The code now runs on UI thread and file access is
jennyz 2014/08/28 22:41:06 Yes, removed the check here, if the file does not
+ 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;
xiyuan 2014/08/27 16:17:03 We don't need to require the update manifest to ha
jennyz 2014/08/28 22:41:07 Done.
+ continue;
+ }
+ base::Version external_version(external_version_str);
+ base::Version cached_version(cached_version_str);
+ switch (cached_version.CompareTo(external_version)) {
xiyuan 2014/08/27 16:17:03 nit: suggest to change to use "if", switch on int
jennyz 2014/08/28 22:41:07 Done.
+ 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_.empty()) {
+ NotifyKioskUpdateProgress(
+ ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
+ IDS_KIOSK_EXTERNAL_UPDATE_NO_UPDATES));
+ return;
+ }
+
+ ValidateExternalUpdates();
+}
+
+bool KioskExternalUpdater::CheckExternalUpdateInterrupted() {
+ 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 true;
+ }
+
+ return false;
+}
+
+void KioskExternalUpdater::ValidateExternalUpdates() {
+ for (ExternalUpdateMap::iterator it = external_updates_.begin();
+ it != external_updates_.end();
+ ++it) {
+ if (it->second.update_status == PENDING) {
+ scoped_refptr<KioskExternalUpdateValidator> crx_validator =
+ new KioskExternalUpdateValidator(backend_task_runner_,
+ it->first,
+ it->second.external_crx,
+ crx_unpack_dir_,
+ weak_factory_.GetWeakPtr());
+ crx_validator->Start();
+ break;
+ }
+ }
+}
+
+void KioskExternalUpdater::OnCacheExternalCrx(const std::string& app_id,
+ const base::FilePath& target_file,
+ bool* cache_success) {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ if (!*cache_success) {
+ 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;
+ }
+
+ // Validate the next pending external update.
+ MaybeValidateNextExternalUpdate();
+}
+
+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 std::string& version,
+ const std::string& min_browser_version) {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+
+ 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);
+ const base::Version external_version(version);
+ ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
+ switch (existing_version.CompareTo(external_version)) {
xiyuan 2014/08/27 16:17:03 nit: change this to "if"
jennyz 2014/08/28 22:41:07 Done.
+ 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(version),
+ base::UTF8ToUTF16(existing_version_str));
+ return false;
+ }
+
+ // Check minimum browser version.
+ if (min_browser_version.empty()) {
xiyuan 2014/08/27 16:17:03 This should not be a requirement. Many apps do not
jennyz 2014/08/28 22:41:06 Done.
+ external_updates_[app_id].error = rb.GetLocalizedString(
+ IDS_KIOSK_EXTERNAL_UPDATE_INVALID_MIN_BROWSER_VERSION);
+ return false;
+ }
+
+ Version minimum_version(min_browser_version);
+ 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::NotifyKioskUpdateProgress(
+ const base::string16& message) {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ content::BrowserThread::PostTask(
xiyuan 2014/08/27 16:17:02 Why do we need to PostTask to UI when we are alrea
jennyz 2014/08/28 22:41:07 Removed the PostTask, making direct call now.
+ content::BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(&KioskExternalUpdater::ShowKioskUpdateProgress,
+ weak_factory_.GetWeakPtr(),
+ message));
+}
+
+void KioskExternalUpdater::MaybeValidateNextExternalUpdate() {
+ if (IsExternalUpdatePending())
+ ValidateExternalUpdates();
+ else
+ MayBeNotifyKioskAppUpdate();
+}
+
+void KioskExternalUpdater::MayBeNotifyKioskAppUpdate() {
+ if (IsExternalUpdatePending())
+ return;
+
+ NotifyKioskUpdateProgress(GetUpdateReportMessage());
+
+ content::BrowserThread::PostTask(
xiyuan 2014/08/27 16:17:03 Do we need PostTask here?
jennyz 2014/08/28 22:41:06 No need to PostTask, making direct call.
+ content::BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(&KioskExternalUpdater::NotifyKioskAppUpdateAvailable,
+ weak_factory_.GetWeakPtr()));
+}
+
+void KioskExternalUpdater::NotifyKioskAppUpdateAvailable() {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ 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(
xiyuan 2014/08/27 16:17:03 Do we need PostTask here?
jennyz 2014/08/28 22:41:06 Making direct call now.
+ content::BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(&KioskExternalUpdater::DismissKioskUpdateNotification,
+ weak_factory_.GetWeakPtr()));
+}
+
+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;
xiyuan 2014/08/27 16:17:02 nit: use base::ASCIIToUTF16 for this
jennyz 2014/08/28 22:41:06 Done.
+ } else { // FAILED
+ ++failed;
+ if (failed_apps.empty()) {
+ failed_apps = app_name + base::UTF8ToUTF16(": ") + it->second.error;
xiyuan 2014/08/27 16:17:02 nit: ASCIIToUTF16
jennyz 2014/08/28 22:41:07 Done.
+ } else {
+ failed_apps = failed_apps + base::UTF8ToUTF16("\n") + app_name +
+ base::UTF8ToUTF16(": ") + it->second.error;
xiyuan 2014/08/27 16:17:03 nit: ASCIIToUTF16
jennyz 2014/08/28 22:41:06 Done.
+ }
+ }
+ }
+
+ 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;
xiyuan 2014/08/27 16:17:03 nit: ASCIIToUTF16
jennyz 2014/08/28 22:41:07 Done.
+ }
+
+ 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;
xiyuan 2014/08/27 16:17:03 nit: ASCIIToUTF16
jennyz 2014/08/28 22:41:07 Done.
+ }
+ return message;
+}
+
+} // namespace chromeos

Powered by Google App Engine
This is Rietveld 408576698