summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/app_mode/kiosk_external_updater.h
blob: a76f81b1718f4a658e86b4a7b9445b60f62b1878 (plain)
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
// 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.

#ifndef CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_EXTERNAL_UPDATER_H_
#define CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_EXTERNAL_UPDATER_H_

#include <string>

#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/sequenced_task_runner.h"
#include "chrome/browser/chromeos/app_mode/kiosk_external_update_validator.h"
#include "chromeos/disks/disk_mount_manager.h"

namespace chromeos {

class KioskExternalUpdateNotification;

// Observes the disk mount/unmount events, scans the usb stick for external
// kiosk app updates, validates the external crx, and updates the cache.
class KioskExternalUpdater : public disks::DiskMountManager::Observer,
                             public KioskExternalUpdateValidatorDelegate {
 public:
  enum ExternalUpdateErrorCode {
    ERROR_NONE,
    ERROR_NO_MANIFEST,
    ERROR_INVALID_MANIFEST,
  };

  KioskExternalUpdater(
      const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner,
      const base::FilePath& crx_cache_dir,
      const base::FilePath& crx_unpack_dir);

  ~KioskExternalUpdater() override;

 private:
  enum ExternalUpdateStatus {
    PENDING,
    SUCCESS,
    FAILED,
  };
  struct ExternalUpdate {
    ExternalUpdate();
    ~ExternalUpdate();

    std::string app_name;
    extensions::CRXFileInfo external_crx;
    ExternalUpdateStatus update_status;
    base::string16 error;
  };

  // disks::DiskMountManager::Observer overrides.
  void OnDiskEvent(disks::DiskMountManager::DiskEvent event,
                   const disks::DiskMountManager::Disk* disk) override;
  void OnDeviceEvent(disks::DiskMountManager::DeviceEvent event,
                     const std::string& device_path) override;
  void OnMountEvent(
      disks::DiskMountManager::MountEvent event,
      MountError error_code,
      const disks::DiskMountManager::MountPointInfo& mount_info) override;
  void OnFormatEvent(disks::DiskMountManager::FormatEvent event,
                     FormatError error_code,
                     const std::string& device_path) override;

  // KioskExternalUpdateValidatorDelegate overrides:
  void OnExtenalUpdateUnpackSuccess(const std::string& app_id,
                                    const std::string& version,
                                    const std::string& min_browser_version,
                                    const base::FilePath& temp_dir) override;
  void OnExternalUpdateUnpackFailure(const std::string& app_id) override;

  // Processes the parsed external update manifest, check |parsing_error| for
  // any manifest parsing error.
  void ProcessParsedManifest(ExternalUpdateErrorCode* parsing_error,
                             const base::FilePath& external_update_dir,
                             base::DictionaryValue* parsed_manifest);

  // Returns true if |external_update_| is interrupted before the updating
  // completes.
  bool CheckExternalUpdateInterrupted();

  // Validates the external updates.
  void ValidateExternalUpdates();

  // Returns true if there are any external updates pending.
  bool IsExternalUpdatePending();

  // Returns true if all external updates specified in the manifest are
  // completed successfully.
  bool IsAllExternalUpdatesSucceeded();

  // Returns true if the app with |app_id| should be updated to
  // |external_extension|.
  bool ShouldDoExternalUpdate(const std::string& app_id,
                              const std::string& version,
                              const std::string& min_browser_version);

  // Installs the validated extension into cache.
  // |*crx_copied| indicates whether the |crx_file| is copied successfully.
  void PutValidatedExtension(bool* crx_copied,
                             const std::string& app_id,
                             const base::FilePath& crx_file,
                             const std::string& version);

  // Called upon completion of installing the validated external extension into
  // the local cache. |success| is true if the operation succeeded.
  void OnPutValidatedExtension(const std::string& app_id, bool success);

  void NotifyKioskUpdateProgress(const base::string16& message);

  void MaybeValidateNextExternalUpdate();

  // Notifies the kiosk update status with UI and KioskAppUpdateService, if
  // there is no kiosk external updates pending.
  void MayBeNotifyKioskAppUpdate();

  void NotifyKioskAppUpdateAvailable();

  // Dismisses the UI notification for kiosk updates.
  void DismissKioskUpdateNotification();

  // Return a detailed message for kiosk updating status.
  base::string16 GetUpdateReportMessage();

  // Task runner for executing file I/O tasks.
  const scoped_refptr<base::SequencedTaskRunner> backend_task_runner_;

  // The directory where kiosk crx files are cached.
  const base::FilePath crx_cache_dir_;

  // The directory used by SandBoxedUnpacker for unpack extensions.
  const base::FilePath crx_unpack_dir_;

  // The path where external crx files resides(usb stick mount path).
  base::FilePath external_update_path_;

  // map of app_id: ExternalUpdate
  typedef std::map<std::string, ExternalUpdate> ExternalUpdateMap;
  ExternalUpdateMap external_updates_;
  scoped_ptr<KioskExternalUpdateNotification> notification_;

  base::WeakPtrFactory<KioskExternalUpdater> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(KioskExternalUpdater);
};

}  // namespace chromeos

#endif  // CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_EXTERNAL_UPDATER_H_