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

Side by Side 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: Minor optimization in test. Created 6 years, 3 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 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.
4
5 #include "chrome/browser/chromeos/app_mode/kiosk_external_updater.h"
6
7 #include "base/bind.h"
8 #include "base/file_util.h"
9 #include "base/files/file_enumerator.h"
10 #include "base/json/json_file_value_serializer.h"
11 #include "base/location.h"
12 #include "base/logging.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/version.h"
15 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
16 #include "chrome/browser/chromeos/ui/kiosk_external_update_notification.h"
17 #include "chrome/browser/extensions/sandboxed_unpacker.h"
18 #include "chrome/common/chrome_version_info.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "extensions/common/extension.h"
21 #include "grit/chromium_strings.h"
22 #include "grit/generated_resources.h"
23 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/base/resource/resource_bundle.h"
25
26 namespace chromeos {
27
28 namespace {
29
30 const char kExternalUpdateManifest[] = "external_update.json";
31 const char kExternalCrx[] = "external_crx";
32 const char kExternalVersion[] = "external_version";
33
34 void ParseExternalUpdateManifest(
35 const base::FilePath& external_update_dir,
36 base::DictionaryValue* parsed_manifest,
37 KioskExternalUpdater::ExternalUpdateErrorCode* error_code) {
38 base::FilePath manifest =
39 external_update_dir.AppendASCII(kExternalUpdateManifest);
40 if (!base::PathExists(manifest)) {
41 *error_code = KioskExternalUpdater::ERROR_NO_MANIFEST;
42 return;
43 }
44
45 JSONFileValueSerializer serializer(manifest);
46 std::string error_msg;
47 base::Value* extensions = serializer.Deserialize(NULL, &error_msg);
48 if (!extensions) {
49 *error_code = KioskExternalUpdater::ERROR_INVALID_MANIFEST;
50 return;
51 }
52
53 base::DictionaryValue* dict_value = NULL;
54 if (!extensions->GetAsDictionary(&dict_value)) {
55 *error_code = KioskExternalUpdater::ERROR_INVALID_MANIFEST;
56 return;
57 }
58
59 parsed_manifest->Swap(dict_value);
60 *error_code = KioskExternalUpdater::ERROR_NONE;
61 }
62
63 // Copies |external_crx_file| to |temp_crx_file|, and removes |temp_dir|
64 // created for unpacking |external_crx_file|.
65 void CopyExternalCrxAndDeleteTempDir(const base::FilePath& external_crx_file,
66 const base::FilePath& temp_crx_file,
67 const base::FilePath& temp_dir,
68 bool* success) {
69 base::DeleteFile(temp_dir, true);
70 *success = base::CopyFile(external_crx_file, temp_crx_file);
71 }
72
73 // Returns true if |version_1| < |version_2|, and
74 // if |update_for_same_version| is true and |version_1| = |version_2|.
75 bool ShouldUpdateForHigherVersion(const std::string& version_1,
76 const std::string& version_2,
77 bool update_for_same_version) {
78 const base::Version v1(version_1);
79 const base::Version v2(version_2);
80 if (!v1.IsValid() || !v2.IsValid())
81 return false;
82 int compare_result = v1.CompareTo(v2);
83 if (compare_result < 0)
84 return true;
85 else if (update_for_same_version && compare_result == 0)
86 return true;
87 else
88 return false;
89 }
90
91 } // namespace
92
93 KioskExternalUpdater::ExternalUpdate::ExternalUpdate() {
94 }
95
96 KioskExternalUpdater::KioskExternalUpdater(
97 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner,
98 const base::FilePath& crx_cache_dir,
99 const base::FilePath& crx_unpack_dir)
100 : backend_task_runner_(backend_task_runner),
101 crx_cache_dir_(crx_cache_dir),
102 crx_unpack_dir_(crx_unpack_dir),
103 weak_factory_(this) {
104 // Subscribe to DiskMountManager.
105 DCHECK(disks::DiskMountManager::GetInstance());
106 disks::DiskMountManager::GetInstance()->AddObserver(this);
107 }
108
109 KioskExternalUpdater::~KioskExternalUpdater() {
110 if (disks::DiskMountManager::GetInstance())
111 disks::DiskMountManager::GetInstance()->RemoveObserver(this);
112 }
113
114 void KioskExternalUpdater::OnDiskEvent(
115 disks::DiskMountManager::DiskEvent event,
116 const disks::DiskMountManager::Disk* disk) {
117 }
118
119 void KioskExternalUpdater::OnDeviceEvent(
120 disks::DiskMountManager::DeviceEvent event,
121 const std::string& device_path) {
122 }
123
124 void KioskExternalUpdater::OnMountEvent(
125 disks::DiskMountManager::MountEvent event,
126 MountError error_code,
127 const disks::DiskMountManager::MountPointInfo& mount_info) {
128 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
129 if (mount_info.mount_type != MOUNT_TYPE_DEVICE ||
130 error_code != MOUNT_ERROR_NONE) {
131 return;
132 }
133
134 if (event == disks::DiskMountManager::MOUNTING) {
135 // If multiple disks have been mounted, skip the rest of them if kiosk
136 // update has already been found.
137 if (!external_update_path_.empty()) {
138 LOG(WARNING) << "External update path already found, skip "
139 << mount_info.mount_path;
140 return;
141 }
142
143 NotifyKioskUpdateProgress(
144 ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
145 IDS_KIOSK_EXTERNAL_UPDATE_IN_PROGRESS));
146
147 base::DictionaryValue* parsed_manifest = new base::DictionaryValue();
148 ExternalUpdateErrorCode* parsing_error = new ExternalUpdateErrorCode;
149 backend_task_runner_->PostTaskAndReply(
150 FROM_HERE,
151 base::Bind(&ParseExternalUpdateManifest,
152 base::FilePath(mount_info.mount_path),
153 parsed_manifest,
154 parsing_error),
155 base::Bind(&KioskExternalUpdater::ProcessParsedManifest,
156 weak_factory_.GetWeakPtr(),
157 base::Owned(parsing_error),
158 base::FilePath(mount_info.mount_path),
159 base::Owned(parsed_manifest)));
160 } else { // unmounting a removable device.
161 if (external_update_path_.value().empty()) {
162 // Clear any previously displayed message.
163 DismissKioskUpdateNotification();
164 } else if (external_update_path_.value() == mount_info.mount_path) {
165 DismissKioskUpdateNotification();
166 if (IsExternalUpdatePending()) {
167 LOG(ERROR) << "External kiosk update is not completed when the usb "
168 "stick is unmoutned.";
169 }
170 external_updates_.clear();
171 external_update_path_.clear();
172 }
173 }
174 }
175
176 void KioskExternalUpdater::OnFormatEvent(
177 disks::DiskMountManager::FormatEvent event,
178 FormatError error_code,
179 const std::string& device_path) {
180 }
181
182 void KioskExternalUpdater::OnExtenalUpdateUnpackSuccess(
183 const std::string& app_id,
184 const std::string& version,
185 const std::string& min_browser_version,
186 const base::FilePath& temp_dir) {
187 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
188
189 // User might pull out the usb stick before updating is completed.
190 if (CheckExternalUpdateInterrupted())
191 return;
192
193 if (!ShouldDoExternalUpdate(app_id, version, min_browser_version)) {
194 external_updates_[app_id].update_status = FAILED;
195 MaybeValidateNextExternalUpdate();
196 return;
197 }
198
199 // User might pull out the usb stick before updating is completed.
200 if (CheckExternalUpdateInterrupted())
201 return;
202
203 base::FilePath external_crx_path = external_updates_[app_id].external_crx;
204 base::FilePath temp_crx_path =
205 crx_unpack_dir_.Append(external_crx_path.BaseName());
206 bool* success = new bool;
207 backend_task_runner_->PostTaskAndReply(
208 FROM_HERE,
209 base::Bind(&CopyExternalCrxAndDeleteTempDir,
210 external_crx_path,
211 temp_crx_path,
212 temp_dir,
213 success),
214 base::Bind(&KioskExternalUpdater::PutValidatedExtension,
215 weak_factory_.GetWeakPtr(),
216 base::Owned(success),
217 app_id,
218 temp_crx_path,
219 version));
220 }
221
222 void KioskExternalUpdater::OnExternalUpdateUnpackFailure(
223 const std::string& app_id) {
224 // User might pull out the usb stick before updating is completed.
225 if (CheckExternalUpdateInterrupted())
226 return;
227
228 external_updates_[app_id].update_status = FAILED;
229 external_updates_[app_id].error =
230 ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
231 IDS_KIOSK_EXTERNAL_UPDATE_BAD_CRX);
232 MaybeValidateNextExternalUpdate();
233 }
234
235 void KioskExternalUpdater::ProcessParsedManifest(
236 ExternalUpdateErrorCode* parsing_error,
237 const base::FilePath& external_update_dir,
238 base::DictionaryValue* parsed_manifest) {
239 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
240
241 if (*parsing_error == ERROR_NO_MANIFEST) {
242 NotifyKioskUpdateProgress(
243 ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
244 IDS_KIOSK_EXTERNAL_UPDATE_NO_MANIFEST));
245 return;
246 } else if (*parsing_error == ERROR_INVALID_MANIFEST) {
247 NotifyKioskUpdateProgress(
248 ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
249 IDS_KIOSK_EXTERNAL_UPDATE_INVALID_MANIFEST));
250 return;
251 }
252
253 external_update_path_ = external_update_dir;
254 for (base::DictionaryValue::Iterator it(*parsed_manifest); !it.IsAtEnd();
255 it.Advance()) {
256 std::string app_id = it.key();
257 std::string cached_version_str;
258 base::FilePath cached_crx;
259 if (!KioskAppManager::Get()->GetCachedCrx(
260 app_id, &cached_crx, &cached_version_str)) {
261 LOG(WARNING) << "Can't find app in existing cache " << app_id;
262 continue;
263 }
264
265 const base::DictionaryValue* extension = NULL;
266 if (!it.value().GetAsDictionary(&extension)) {
267 LOG(ERROR) << "Found bad entry in manifest type " << it.value().GetType();
268 continue;
269 }
270
271 std::string external_crx_str;
272 if (!extension->GetString(kExternalCrx, &external_crx_str)) {
273 LOG(ERROR) << "Can't find external crx in manifest " << app_id;
274 continue;
275 }
276
277 std::string external_version_str;
278 if (extension->GetString(kExternalVersion, &external_version_str)) {
279 if (!ShouldUpdateForHigherVersion(
280 cached_version_str, external_version_str, false)) {
281 LOG(WARNING) << "External app " << app_id
282 << " is at the same or lower version comparing to "
283 << " the existing one.";
284 continue;
285 }
286 }
287
288 ExternalUpdate update;
289 KioskAppManager::App app;
290 if (KioskAppManager::Get()->GetApp(app_id, &app)) {
291 update.app_name = app.name;
292 } else {
293 NOTREACHED();
294 }
295 update.external_crx = external_update_path_.AppendASCII(external_crx_str);
296 update.update_status = PENDING;
297 external_updates_[app_id] = update;
298 }
299
300 if (external_updates_.empty()) {
301 NotifyKioskUpdateProgress(
302 ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
303 IDS_KIOSK_EXTERNAL_UPDATE_NO_UPDATES));
304 return;
305 }
306
307 ValidateExternalUpdates();
308 }
309
310 bool KioskExternalUpdater::CheckExternalUpdateInterrupted() {
311 if (external_updates_.empty()) {
312 // This could happen if user pulls out the usb stick before the updating
313 // operation is completed.
314 LOG(ERROR) << "external_updates_ has been cleared before external "
315 << "updating completes.";
316 return true;
317 }
318
319 return false;
320 }
321
322 void KioskExternalUpdater::ValidateExternalUpdates() {
323 for (ExternalUpdateMap::iterator it = external_updates_.begin();
324 it != external_updates_.end();
325 ++it) {
326 if (it->second.update_status == PENDING) {
327 scoped_refptr<KioskExternalUpdateValidator> crx_validator =
328 new KioskExternalUpdateValidator(backend_task_runner_,
329 it->first,
330 it->second.external_crx,
331 crx_unpack_dir_,
332 weak_factory_.GetWeakPtr());
333 crx_validator->Start();
334 break;
335 }
336 }
337 }
338
339 bool KioskExternalUpdater::IsExternalUpdatePending() {
340 for (ExternalUpdateMap::iterator it = external_updates_.begin();
341 it != external_updates_.end();
342 ++it) {
343 if (it->second.update_status == PENDING) {
344 return true;
345 }
346 }
347 return false;
348 }
349
350 bool KioskExternalUpdater::ShouldDoExternalUpdate(
351 const std::string& app_id,
352 const std::string& version,
353 const std::string& min_browser_version) {
354 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
355
356 std::string existing_version_str;
357 base::FilePath existing_path;
358 bool cached = KioskAppManager::Get()->GetCachedCrx(
359 app_id, &existing_path, &existing_version_str);
360 DCHECK(cached);
361
362 // Compare app version.
363 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
364 if (!ShouldUpdateForHigherVersion(existing_version_str, version, false)) {
365 external_updates_[app_id].error = rb.GetLocalizedString(
366 IDS_KIOSK_EXTERNAL_UPDATE_SAME_OR_LOWER_APP_VERSION);
367 return false;
368 }
369
370 // Check minimum browser version.
371 if (!min_browser_version.empty()) {
372 chrome::VersionInfo current_version_info;
373 if (!ShouldUpdateForHigherVersion(
374 min_browser_version, current_version_info.Version(), true)) {
375 external_updates_[app_id].error = l10n_util::GetStringFUTF16(
376 IDS_KIOSK_EXTERNAL_UPDATE_REQUIRE_HIGHER_BROWSER_VERSION,
377 base::UTF8ToUTF16(min_browser_version));
378 return false;
379 }
380 }
381
382 return true;
383 }
384
385 void KioskExternalUpdater::PutValidatedExtension(bool* crx_copied,
386 const std::string& app_id,
387 const base::FilePath& crx_file,
388 const std::string& version) {
389 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
390 if (CheckExternalUpdateInterrupted())
391 return;
392
393 if (!*crx_copied) {
394 LOG(ERROR) << "Cannot copy external crx file to " << crx_file.value();
395 external_updates_[app_id].update_status = FAILED;
396 external_updates_[app_id].error = l10n_util::GetStringFUTF16(
397 IDS_KIOSK_EXTERNAL_UPDATE_FAILED_COPY_CRX_TO_TEMP,
398 base::UTF8ToUTF16(crx_file.value()));
399 MaybeValidateNextExternalUpdate();
400 return;
401 }
402
403 chromeos::KioskAppManager::Get()->PutValidatedExternalExtension(
404 app_id,
405 crx_file,
406 version,
407 base::Bind(&KioskExternalUpdater::OnPutValidatedExtension,
408 weak_factory_.GetWeakPtr()));
409 }
410
411 void KioskExternalUpdater::OnPutValidatedExtension(const std::string& app_id,
412 bool success) {
413 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
414 if (CheckExternalUpdateInterrupted())
415 return;
416
417 if (!success) {
418 external_updates_[app_id].update_status = FAILED;
419 external_updates_[app_id].error = l10n_util::GetStringFUTF16(
420 IDS_KIOSK_EXTERNAL_UPDATE_CANNOT_INSTALL_IN_LOCAL_CACHE,
421 base::UTF8ToUTF16(external_updates_[app_id].external_crx.value()));
422 } else {
423 external_updates_[app_id].update_status = SUCCESS;
424 }
425
426 // Validate the next pending external update.
427 MaybeValidateNextExternalUpdate();
428 }
429
430 void KioskExternalUpdater::MaybeValidateNextExternalUpdate() {
431 if (IsExternalUpdatePending())
432 ValidateExternalUpdates();
433 else
434 MayBeNotifyKioskAppUpdate();
435 }
436
437 void KioskExternalUpdater::MayBeNotifyKioskAppUpdate() {
438 if (IsExternalUpdatePending())
439 return;
440
441 NotifyKioskUpdateProgress(GetUpdateReportMessage());
442 NotifyKioskAppUpdateAvailable();
443 }
444
445 void KioskExternalUpdater::NotifyKioskAppUpdateAvailable() {
446 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
447 for (ExternalUpdateMap::iterator it = external_updates_.begin();
448 it != external_updates_.end();
449 ++it) {
450 if (it->second.update_status == SUCCESS) {
451 KioskAppManager::Get()->OnKioskAppCacheUpdated(it->first);
452 }
453 }
454 }
455
456 void KioskExternalUpdater::NotifyKioskUpdateProgress(
457 const base::string16& message) {
458 if (!notification_)
459 notification_.reset(new KioskExternalUpdateNotification(message));
460 else
461 notification_->ShowMessage(message);
462 }
463
464 void KioskExternalUpdater::DismissKioskUpdateNotification() {
465 if (notification_.get()) {
466 notification_.reset();
467 }
468 }
469
470 base::string16 KioskExternalUpdater::GetUpdateReportMessage() {
471 DCHECK(!IsExternalUpdatePending());
472 int updated = 0;
473 int failed = 0;
474 base::string16 updated_apps;
475 base::string16 failed_apps;
476 for (ExternalUpdateMap::iterator it = external_updates_.begin();
477 it != external_updates_.end();
478 ++it) {
479 base::string16 app_name = base::UTF8ToUTF16(it->second.app_name);
480 if (it->second.update_status == SUCCESS) {
481 ++updated;
482 if (updated_apps.empty())
483 updated_apps = app_name;
484 else
485 updated_apps = updated_apps + base::ASCIIToUTF16(", ") + app_name;
486 } else { // FAILED
487 ++failed;
488 if (failed_apps.empty()) {
489 failed_apps = app_name + base::ASCIIToUTF16(": ") + it->second.error;
490 } else {
491 failed_apps = failed_apps + base::ASCIIToUTF16("\n") + app_name +
492 base::ASCIIToUTF16(": ") + it->second.error;
493 }
494 }
495 }
496
497 base::string16 message;
498 message = ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
499 IDS_KIOSK_EXTERNAL_UPDATE_COMPLETE);
500 base::string16 success_app_msg;
501 if (updated) {
502 success_app_msg = l10n_util::GetStringFUTF16(
503 IDS_KIOSK_EXTERNAL_UPDATE_SUCCESSFUL_UPDATED_APPS, updated_apps);
504 message = message + base::ASCIIToUTF16("\n") + success_app_msg;
505 }
506
507 base::string16 failed_app_msg;
508 if (failed) {
509 failed_app_msg = ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
510 IDS_KIOSK_EXTERNAL_UPDATE_FAILED_UPDATED_APPS) +
511 base::ASCIIToUTF16("\n") + failed_apps;
512 message = message + base::ASCIIToUTF16("\n") + failed_app_msg;
513 }
514 return message;
515 }
516
517 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/app_mode/kiosk_external_updater.h ('k') | chrome/browser/chromeos/extensions/external_cache.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698