1// Copyright (c) 2012 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 CHROMEOS_DBUS_UPDATE_ENGINE_CLIENT_H_
6#define CHROMEOS_DBUS_UPDATE_ENGINE_CLIENT_H_
7
8#include "base/callback.h"
9#include "base/observer_list.h"
10#include "chromeos/chromeos_export.h"
11#include "chromeos/dbus/dbus_client.h"
12#include "chromeos/dbus/dbus_client_implementation_type.h"
13
14#include <string>
15
16namespace chromeos {
17
18// UpdateEngineClient is used to communicate with the update engine.
19class CHROMEOS_EXPORT UpdateEngineClient : public DBusClient {
20 public:
21  // Edges for state machine
22  //    IDLE->CHECKING_FOR_UPDATE
23  //    CHECKING_FOR_UPDATE->IDLE
24  //    CHECKING_FOR_UPDATE->UPDATE_AVAILABLE
25  //    ...
26  //    FINALIZING->UPDATE_NEED_REBOOT
27  // Any state can transition to REPORTING_ERROR_EVENT and then on to IDLE.
28  enum UpdateStatusOperation {
29    UPDATE_STATUS_ERROR = -1,
30    UPDATE_STATUS_IDLE = 0,
31    UPDATE_STATUS_CHECKING_FOR_UPDATE,
32    UPDATE_STATUS_UPDATE_AVAILABLE,
33    UPDATE_STATUS_DOWNLOADING,
34    UPDATE_STATUS_VERIFYING,
35    UPDATE_STATUS_FINALIZING,
36    UPDATE_STATUS_UPDATED_NEED_REBOOT,
37    UPDATE_STATUS_REPORTING_ERROR_EVENT,
38    UPDATE_STATUS_ATTEMPTING_ROLLBACK
39  };
40
41  // The status of the ongoing update attempt.
42  struct Status {
43    Status() : status(UPDATE_STATUS_IDLE),
44               download_progress(0.0),
45               last_checked_time(0),
46               new_size(0) {
47    }
48
49    UpdateStatusOperation status;
50    double download_progress;   // 0.0 - 1.0
51    int64_t last_checked_time;  // As reported by std::time().
52    std::string new_version;
53    int64_t new_size;           // Valid during DOWNLOADING, in bytes.
54  };
55
56  // The result code used for RequestUpdateCheck().
57  enum UpdateCheckResult {
58    UPDATE_RESULT_SUCCESS,
59    UPDATE_RESULT_FAILED,
60    UPDATE_RESULT_NOTIMPLEMENTED,
61  };
62
63  // Interface for observing changes from the update engine.
64  class Observer {
65   public:
66    // Called when the status is updated.
67    virtual void UpdateStatusChanged(const Status& status) {}
68  };
69
70  virtual ~UpdateEngineClient();
71
72  // Adds and removes the observer.
73  virtual void AddObserver(Observer* observer) = 0;
74  virtual void RemoveObserver(Observer* observer) = 0;
75  // Returns true if this object has the given observer.
76  virtual bool HasObserver(Observer* observer) = 0;
77
78  // Called once RequestUpdateCheck() is complete. Takes one parameter:
79  // - UpdateCheckResult: the result of the update check.
80  typedef base::Callback<void(UpdateCheckResult)> UpdateCheckCallback;
81
82  // Requests an update check and calls |callback| when completed.
83  virtual void RequestUpdateCheck(const UpdateCheckCallback& callback) = 0;
84
85  // Reboots if update has been performed.
86  virtual void RebootAfterUpdate() = 0;
87
88  // Starts Rollback.
89  virtual void Rollback() = 0;
90
91  // Called once CanRollbackCheck() is complete. Takes one parameter:
92  // - bool: the result of the rollback availability check.
93  typedef base::Callback<void(bool can_rollback)> RollbackCheckCallback;
94
95  // Checks if Rollback is available and calls |callback| when completed.
96  virtual void CanRollbackCheck(
97      const RollbackCheckCallback& callback) = 0;
98
99  // Called once GetChannel() is complete. Takes one parameter;
100  // - string: the channel name like "beta-channel".
101  typedef base::Callback<void(const std::string& channel_name)>
102      GetChannelCallback;
103
104  // Returns the last status the object received from the update engine.
105  //
106  // Ideally, the D-Bus client should be state-less, but there are clients
107  // that need this information.
108  virtual Status GetLastStatus() = 0;
109
110  // Changes the current channel of the device to the target
111  // channel. If the target channel is a less stable channel than the
112  // current channel, then the channel change happens immediately (at
113  // the next update check).  If the target channel is a more stable
114  // channel, then if |is_powerwash_allowed| is set to true, then also
115  // the change happens immediately but with a powerwash if
116  // required. Otherwise, the change takes effect eventually (when the
117  // version on the target channel goes above the version number of
118  // what the device currently has). |target_channel| should look like
119  // "dev-channel", "beta-channel" or "stable-channel".
120  virtual void SetChannel(const std::string& target_channel,
121                          bool is_powerwash_allowed) = 0;
122
123  // If |get_current_channel| is set to true, calls |callback| with
124  // the name of the channel that the device is currently
125  // on. Otherwise, it calls it with the name of the channel the
126  // device is supposed to be (in case of a pending channel
127  // change). On error, calls |callback| with an empty string.
128  virtual void GetChannel(bool get_current_channel,
129                          const GetChannelCallback& callback) = 0;
130
131  // Returns an empty UpdateCheckCallback that does nothing.
132  static UpdateCheckCallback EmptyUpdateCheckCallback();
133
134  // Creates the instance.
135  static UpdateEngineClient* Create(DBusClientImplementationType type);
136
137 protected:
138  // Create() should be used instead.
139  UpdateEngineClient();
140
141 private:
142  DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient);
143};
144
145}  // namespace chromeos
146
147#endif  // CHROMEOS_DBUS_UPDATE_ENGINE_CLIENT_H_
148