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