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