1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/app_mode/kiosk_external_updater.h"
#include <memory>
#include <string>
#include <utility>
#include "base/check.h"
#include "base/check_deref.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/json/json_file_value_serializer.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/scoped_refptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/values.h"
#include "base/version.h"
#include "base/version_info/version_info.h"
#include "chrome/browser/ash/app_mode/kiosk_chrome_app_manager.h"
#include "chrome/browser/ash/app_mode/kiosk_external_update_validator.h"
#include "chrome/browser/ash/notifications/kiosk_external_update_notification.h"
#include "chrome/grit/generated_resources.h"
#include "chromeos/ash/components/dbus/cros_disks/cros_disks_client.h"
#include "chromeos/ash/components/disks/disk_mount_manager.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/browser/crx_file_info.h"
#include "extensions/browser/sandboxed_unpacker.h"
#include "extensions/common/verifier_formats.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
namespace ash {
namespace {
constexpr base::FilePath::CharType kExternalUpdateManifest[] =
"external_update.json";
constexpr char kExternalCrx[] = "external_crx";
constexpr char kExternalVersion[] = "external_version";
std::pair<base::Value, KioskExternalUpdater::ErrorCode>
ParseExternalUpdateManifest(const base::FilePath& external_update_dir) {
base::FilePath manifest = external_update_dir.Append(kExternalUpdateManifest);
if (!base::PathExists(manifest)) {
return std::make_pair(base::Value(),
KioskExternalUpdater::ErrorCode::kNoManifest);
}
JSONFileValueDeserializer deserializer(manifest);
std::unique_ptr<base::Value> extensions =
deserializer.Deserialize(nullptr, nullptr);
if (!extensions) {
return std::make_pair(base::Value(),
KioskExternalUpdater::ErrorCode::kInvalidManifest);
}
return std::make_pair(base::Value::FromUniquePtrValue(std::move(extensions)),
KioskExternalUpdater::ErrorCode::kNone);
}
// Copies `external_crx_file` to `temp_crx_file`, and removes `temp_dir`
// created for unpacking `external_crx_file`.
bool CopyExternalCrxAndDeleteTempDir(const base::FilePath& external_crx_file,
const base::FilePath& temp_crx_file,
const base::FilePath& temp_dir) {
base::DeletePathRecursively(temp_dir);
return base::CopyFile(external_crx_file, temp_crx_file);
}
// Returns true if `version_1` < `version_2`, and
// if `update_for_same_version` is true and `version_1` = `version_2`.
bool ShouldUpdateForHigherVersion(const std::string& version_1,
const std::string& version_2,
bool update_for_same_version) {
const base::Version v1(version_1);
const base::Version v2(version_2);
if (!v1.IsValid() || !v2.IsValid()) {
return false;
}
int compare_result = v1.CompareTo(v2);
if (compare_result < 0) {
return true;
}
return update_for_same_version && compare_result == 0;
}
} // namespace
KioskExternalUpdater::ExternalUpdate::ExternalUpdate() = default;
KioskExternalUpdater::ExternalUpdate::ExternalUpdate(
const ExternalUpdate& other) = default;
KioskExternalUpdater::ExternalUpdate::~ExternalUpdate() = default;
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::OnMountEvent(
disks::DiskMountManager::MountEvent event,
MountError error_code,
const disks::DiskMountManager::MountPoint& mount_info) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (mount_info.mount_type != MountType::kDevice ||
error_code != MountError::kSuccess) {
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;
}
backend_task_runner_->PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&ParseExternalUpdateManifest,
base::FilePath(mount_info.mount_path)),
base::BindOnce(&KioskExternalUpdater::ProcessParsedManifest,
weak_factory_.GetWeakPtr(),
base::FilePath(mount_info.mount_path)));
return;
}
// unmounting a removable device case.
if (external_update_path_.value().empty()) {
// Clear any previously displayed message.
DismissKioskUpdateNotification();
} else if (external_update_path_.value() == mount_info.mount_path) {
DismissKioskUpdateNotification();
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::OnExternalUpdateUnpackSuccess(
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 = UpdateStatus::kFailed;
MaybeValidateNextExternalUpdate();
return;
}
// User might pull out the usb stick before updating is completed.
if (CheckExternalUpdateInterrupted()) {
return;
}
base::FilePath external_crx_path =
external_updates_[app_id].external_crx.path;
base::FilePath temp_crx_path =
crx_unpack_dir_.Append(external_crx_path.BaseName());
backend_task_runner_->PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&CopyExternalCrxAndDeleteTempDir, external_crx_path,
temp_crx_path, temp_dir),
base::BindOnce(&KioskExternalUpdater::PutValidatedExtension,
weak_factory_.GetWeakPtr(), app_id, temp_crx_path,
version));
}
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 = UpdateStatus::kFailed;
external_updates_[app_id].error =
ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
IDS_KIOSK_EXTERNAL_UPDATE_BAD_CRX);
MaybeValidateNextExternalUpdate();
}
void KioskExternalUpdater::ProcessParsedManifest(
const base::FilePath& external_update_dir,
const ParseManifestResult& result) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
auto& manager = CHECK_DEREF(KioskChromeAppManager::Get());
const base::Value& parsed_manifest = result.first;
ErrorCode parsing_error = result.second;
if (parsing_error == ErrorCode::kNoManifest) {
manager.OnKioskAppExternalUpdateComplete(false);
return;
}
if (parsing_error == ErrorCode::kInvalidManifest) {
NotifyKioskUpdateProgress(
ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
IDS_KIOSK_EXTERNAL_UPDATE_INVALID_MANIFEST));
manager.OnKioskAppExternalUpdateComplete(false);
return;
}
NotifyKioskUpdateProgress(
ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
IDS_KIOSK_EXTERNAL_UPDATE_IN_PROGRESS));
external_update_path_ = external_update_dir;
for (auto manifest : parsed_manifest.GetDict()) {
std::string app_id = manifest.first;
auto crx_info = manager.GetCachedCrx(app_id);
if (!crx_info.has_value()) {
LOG(WARNING) << "Can't find app in existing cache " << app_id;
continue;
}
auto& [_, cached_crx_version] = crx_info.value();
if (!manifest.second.is_dict()) {
LOG(ERROR) << "Found bad entry in manifest type "
<< manifest.second.type();
continue;
}
const base::Value::Dict& extension = manifest.second.GetDict();
const std::string* external_crx_str = extension.FindString(kExternalCrx);
if (!external_crx_str) {
LOG(ERROR) << "Can't find external crx in manifest " << app_id;
continue;
}
const std::string* external_version_str =
extension.FindString(kExternalVersion);
if (external_version_str) {
if (!ShouldUpdateForHigherVersion(cached_crx_version,
*external_version_str, false)) {
LOG(WARNING) << "External app " << app_id
<< " is at the same or lower version comparing to "
<< " the existing one.";
continue;
}
}
auto app = manager.GetApp(app_id);
CHECK(app.has_value());
ExternalUpdate update;
update.app_name = app->name;
update.external_crx = extensions::CRXFileInfo(
external_update_path_.AppendASCII(*external_crx_str),
extensions::GetExternalVerifierFormat());
update.external_crx.extension_id = app_id;
update.update_status = UpdateStatus::kPending;
external_updates_[app_id] = update;
}
if (external_updates_.empty()) {
NotifyKioskUpdateProgress(
ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
IDS_KIOSK_EXTERNAL_UPDATE_NO_UPDATES));
manager.OnKioskAppExternalUpdateComplete(false);
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 (const auto& it : external_updates_) {
const ExternalUpdate& update = it.second;
if (update.update_status == UpdateStatus::kPending) {
auto crx_validator = base::MakeRefCounted<KioskExternalUpdateValidator>(
backend_task_runner_, update.external_crx, crx_unpack_dir_,
weak_factory_.GetWeakPtr());
crx_validator->Start();
break;
}
}
}
bool KioskExternalUpdater::IsExternalUpdatePending() const {
for (const auto& it : external_updates_) {
if (it.second.update_status == UpdateStatus::kPending) {
return true;
}
}
return false;
}
bool KioskExternalUpdater::IsAllExternalUpdatesSucceeded() const {
for (const auto& it : external_updates_) {
if (it.second.update_status != UpdateStatus::kSuccess) {
return false;
}
}
return true;
}
bool KioskExternalUpdater::ShouldDoExternalUpdate(
const std::string& app_id,
const std::string& version,
const std::string& min_browser_version) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
auto& manager = CHECK_DEREF(KioskChromeAppManager::Get());
auto crx_info = manager.GetCachedCrx(app_id);
if (!crx_info.has_value()) {
return false;
}
auto [_, existing_version_str] = std::move(crx_info).value();
// Compare app version.
ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
if (!ShouldUpdateForHigherVersion(existing_version_str, version, false)) {
external_updates_[app_id].error = rb->GetLocalizedString(
IDS_KIOSK_EXTERNAL_UPDATE_SAME_OR_LOWER_APP_VERSION);
return false;
}
// Check minimum browser version.
if (!min_browser_version.empty() &&
!ShouldUpdateForHigherVersion(
min_browser_version, std::string(version_info::GetVersionNumber()),
true)) {
external_updates_[app_id].error = l10n_util::GetStringFUTF16(
IDS_KIOSK_EXTERNAL_UPDATE_REQUIRE_HIGHER_BROWSER_VERSION,
base::UTF8ToUTF16(min_browser_version));
return false;
}
return true;
}
void KioskExternalUpdater::PutValidatedExtension(const std::string& app_id,
const base::FilePath& crx_file,
const std::string& version,
bool crx_copied) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (CheckExternalUpdateInterrupted()) {
return;
}
if (!crx_copied) {
LOG(ERROR) << "Cannot copy external crx file to " << crx_file.value();
external_updates_[app_id].update_status = UpdateStatus::kFailed;
external_updates_[app_id].error = l10n_util::GetStringFUTF16(
IDS_KIOSK_EXTERNAL_UPDATE_FAILED_COPY_CRX_TO_TEMP,
base::UTF8ToUTF16(crx_file.value()));
MaybeValidateNextExternalUpdate();
return;
}
KioskChromeAppManager::Get()->PutValidatedExternalExtension(
app_id, crx_file, version,
base::BindOnce(&KioskExternalUpdater::OnPutValidatedExtension,
weak_factory_.GetWeakPtr()));
}
void KioskExternalUpdater::OnPutValidatedExtension(const std::string& app_id,
bool success) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (CheckExternalUpdateInterrupted()) {
return;
}
if (!success) {
external_updates_[app_id].update_status = UpdateStatus::kFailed;
external_updates_[app_id].error = l10n_util::GetStringFUTF16(
IDS_KIOSK_EXTERNAL_UPDATE_CANNOT_INSTALL_IN_LOCAL_CACHE,
base::UTF8ToUTF16(external_updates_[app_id].external_crx.path.value()));
} else {
external_updates_[app_id].update_status = UpdateStatus::kSuccess;
}
// Validate the next pending external update.
MaybeValidateNextExternalUpdate();
}
void KioskExternalUpdater::MaybeValidateNextExternalUpdate() {
if (IsExternalUpdatePending()) {
ValidateExternalUpdates();
} else {
MayBeNotifyKioskAppUpdate();
}
}
void KioskExternalUpdater::MayBeNotifyKioskAppUpdate() {
if (IsExternalUpdatePending()) {
return;
}
NotifyKioskUpdateProgress(GetUpdateReportMessage());
NotifyKioskAppUpdateAvailable();
KioskChromeAppManager::Get()->OnKioskAppExternalUpdateComplete(
IsAllExternalUpdatesSucceeded());
}
void KioskExternalUpdater::NotifyKioskAppUpdateAvailable() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
for (const auto& it : external_updates_) {
if (it.second.update_status == UpdateStatus::kSuccess) {
KioskChromeAppManager::Get()->OnKioskAppCacheUpdated(it.first);
}
}
}
void KioskExternalUpdater::NotifyKioskUpdateProgress(
const std::u16string& message) {
if (!notification_) {
notification_ = std::make_unique<KioskExternalUpdateNotification>(message);
} else {
notification_->ShowMessage(message);
}
}
void KioskExternalUpdater::DismissKioskUpdateNotification() {
if (notification_.get()) {
notification_.reset();
}
}
std::u16string KioskExternalUpdater::GetUpdateReportMessage() const {
DCHECK(!IsExternalUpdatePending());
int updated = 0;
int failed = 0;
std::u16string updated_apps;
std::u16string failed_apps;
for (const auto& it : external_updates_) {
const ExternalUpdate& update = it.second;
std::u16string app_name = base::UTF8ToUTF16(update.app_name);
if (update.update_status == UpdateStatus::kSuccess) {
++updated;
if (updated_apps.empty()) {
updated_apps = app_name;
} else {
updated_apps += u", " + app_name;
}
} else { // UpdateStatus::kFailed
++failed;
if (failed_apps.empty()) {
failed_apps = app_name + u": " + update.error;
} else {
failed_apps += u"\n" + app_name + u": " + update.error;
}
}
}
std::u16string message =
ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
IDS_KIOSK_EXTERNAL_UPDATE_COMPLETE);
if (updated) {
std::u16string success_app_msg = l10n_util::GetStringFUTF16(
IDS_KIOSK_EXTERNAL_UPDATE_SUCCESSFUL_UPDATED_APPS, updated_apps);
message += u"\n" + success_app_msg;
}
if (failed) {
std::u16string failed_app_msg =
ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
IDS_KIOSK_EXTERNAL_UPDATE_FAILED_UPDATED_APPS) +
u"\n" + failed_apps;
message += u"\n" + failed_app_msg;
}
return message;
}
} // namespace ash
|