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