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#ifndef CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_EXTERNAL_UPDATER_H_
6#define CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_EXTERNAL_UPDATER_H_
7
8#include <string>
9
10#include "base/files/file_path.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/memory/weak_ptr.h"
13#include "base/observer_list.h"
14#include "base/sequenced_task_runner.h"
15#include "chrome/browser/chromeos/app_mode/kiosk_external_update_validator.h"
16#include "chromeos/disks/disk_mount_manager.h"
17
18namespace chromeos {
19
20class KioskExternalUpdateNotification;
21
22// Observes the disk mount/unmount events, scans the usb stick for external
23// kiosk app updates, validates the external crx, and updates the cache.
24class KioskExternalUpdater : public disks::DiskMountManager::Observer,
25                             public KioskExternalUpdateValidatorDelegate {
26 public:
27  enum ExternalUpdateErrorCode {
28    ERROR_NONE,
29    ERROR_NO_MANIFEST,
30    ERROR_INVALID_MANIFEST,
31  };
32
33  KioskExternalUpdater(
34      const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner,
35      const base::FilePath& crx_cache_dir,
36      const base::FilePath& crx_unpack_dir);
37
38  virtual ~KioskExternalUpdater();
39
40 private:
41  enum ExternalUpdateStatus {
42    PENDING,
43    SUCCESS,
44    FAILED,
45  };
46  struct ExternalUpdate {
47    ExternalUpdate();
48
49    std::string app_name;
50    base::FilePath external_crx;
51    ExternalUpdateStatus update_status;
52    base::string16 error;
53  };
54
55  // disks::DiskMountManager::Observer overrides.
56  virtual void OnDiskEvent(disks::DiskMountManager::DiskEvent event,
57                           const disks::DiskMountManager::Disk* disk) OVERRIDE;
58  virtual void OnDeviceEvent(disks::DiskMountManager::DeviceEvent event,
59                             const std::string& device_path) OVERRIDE;
60  virtual void OnMountEvent(
61      disks::DiskMountManager::MountEvent event,
62      MountError error_code,
63      const disks::DiskMountManager::MountPointInfo& mount_info) OVERRIDE;
64  virtual void OnFormatEvent(disks::DiskMountManager::FormatEvent event,
65                             FormatError error_code,
66                             const std::string& device_path) OVERRIDE;
67
68  // KioskExternalUpdateValidatorDelegate overrides:
69  virtual void OnExtenalUpdateUnpackSuccess(
70      const std::string& app_id,
71      const std::string& version,
72      const std::string& min_browser_version,
73      const base::FilePath& temp_dir) OVERRIDE;
74  virtual void OnExternalUpdateUnpackFailure(
75      const std::string& app_id) OVERRIDE;
76
77  // Processes the parsed external update manifest, check |parsing_error| for
78  // any manifest parsing error.
79  void ProcessParsedManifest(ExternalUpdateErrorCode* parsing_error,
80                             const base::FilePath& external_update_dir,
81                             base::DictionaryValue* parsed_manifest);
82
83  // Returns true if |external_update_| is interrupted before the updating
84  // completes.
85  bool CheckExternalUpdateInterrupted();
86
87  // Validates the external updates.
88  void ValidateExternalUpdates();
89
90  // Returns true if there are any external updates pending.
91  bool IsExternalUpdatePending();
92
93  // Returns true if all external updates specified in the manifest are
94  // completed successfully.
95  bool IsAllExternalUpdatesSucceeded();
96
97  // Returns true if the app with |app_id| should be updated to
98  // |external_extension|.
99  bool ShouldDoExternalUpdate(const std::string& app_id,
100                              const std::string& version,
101                              const std::string& min_browser_version);
102
103  // Installs the validated extension into cache.
104  // |*crx_copied| indicates whether the |crx_file| is copied successfully.
105  void PutValidatedExtension(bool* crx_copied,
106                             const std::string& app_id,
107                             const base::FilePath& crx_file,
108                             const std::string& version);
109
110  // Called upon completion of installing the validated external extension into
111  // the local cache. |success| is true if the operation succeeded.
112  void OnPutValidatedExtension(const std::string& app_id, bool success);
113
114  void NotifyKioskUpdateProgress(const base::string16& message);
115
116  void MaybeValidateNextExternalUpdate();
117
118  // Notifies the kiosk update status with UI and KioskAppUpdateService, if
119  // there is no kiosk external updates pending.
120  void MayBeNotifyKioskAppUpdate();
121
122  void NotifyKioskAppUpdateAvailable();
123
124  // Dismisses the UI notification for kiosk updates.
125  void DismissKioskUpdateNotification();
126
127  // Return a detailed message for kiosk updating status.
128  base::string16 GetUpdateReportMessage();
129
130  // Task runner for executing file I/O tasks.
131  const scoped_refptr<base::SequencedTaskRunner> backend_task_runner_;
132
133  // The directory where kiosk crx files are cached.
134  const base::FilePath crx_cache_dir_;
135
136  // The directory used by SandBoxedUnpacker for unpack extensions.
137  const base::FilePath crx_unpack_dir_;
138
139  // The path where external crx files resides(usb stick mount path).
140  base::FilePath external_update_path_;
141
142  // map of app_id: ExternalUpdate
143  typedef std::map<std::string, ExternalUpdate> ExternalUpdateMap;
144  ExternalUpdateMap external_updates_;
145  scoped_ptr<KioskExternalUpdateNotification> notification_;
146
147  base::WeakPtrFactory<KioskExternalUpdater> weak_factory_;
148
149  DISALLOW_COPY_AND_ASSIGN(KioskExternalUpdater);
150};
151
152}  // namespace chromeos
153
154#endif  // CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_EXTERNAL_UPDATER_H_
155