1 //
2 // Copyright (C) 2010 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef UPDATE_ENGINE_DBUS_SERVICE_H_
18 #define UPDATE_ENGINE_DBUS_SERVICE_H_
19 
20 #include <inttypes.h>
21 
22 #include <string>
23 
24 #include <base/memory/ref_counted.h>
25 #include <brillo/errors/error.h>
26 
27 #include "update_engine/common_service.h"
28 #include "update_engine/service_observer_interface.h"
29 #include "update_engine/update_attempter.h"
30 
31 #include "dbus_bindings/org.chromium.UpdateEngineInterface.h"
32 
33 namespace chromeos_update_engine {
34 
35 class DBusUpdateEngineService
36     : public org::chromium::UpdateEngineInterfaceInterface {
37  public:
38   explicit DBusUpdateEngineService(SystemState* system_state);
39   virtual ~DBusUpdateEngineService() = default;
40 
41   // Implementation of org::chromium::UpdateEngineInterfaceInterface.
42   bool AttemptUpdate(brillo::ErrorPtr* error,
43                      const std::string& in_app_version,
44                      const std::string& in_omaha_url) override;
45 
46   bool AttemptUpdateWithFlags(brillo::ErrorPtr* error,
47                               const std::string& in_app_version,
48                               const std::string& in_omaha_url,
49                               int32_t in_flags_as_int) override;
50 
51   bool AttemptRollback(brillo::ErrorPtr* error, bool in_powerwash) override;
52 
53   // Checks if the system rollback is available by verifying if the secondary
54   // system partition is valid and bootable.
55   bool CanRollback(brillo::ErrorPtr* error, bool* out_can_rollback) override;
56 
57   // Resets the status of the update_engine to idle, ignoring any applied
58   // update. This is used for development only.
59   bool ResetStatus(brillo::ErrorPtr* error) override;
60 
61   // Returns the current status of the Update Engine. If an update is in
62   // progress, the number of operations, size to download and overall progress
63   // is reported.
64   bool GetStatus(brillo::ErrorPtr* error,
65                  int64_t* out_last_checked_time,
66                  double* out_progress,
67                  std::string* out_current_operation,
68                  std::string* out_new_version,
69                  int64_t* out_new_size) override;
70 
71   // Reboots the device if an update is applied and a reboot is required.
72   bool RebootIfNeeded(brillo::ErrorPtr* error) override;
73 
74   // Changes the current channel of the device to the target channel. If the
75   // target channel is a less stable channel than the current channel, then the
76   // channel change happens immediately (at the next update check).  If the
77   // target channel is a more stable channel, then if is_powerwash_allowed is
78   // set to true, then also the change happens immediately but with a powerwash
79   // if required. Otherwise, the change takes effect eventually (when the
80   // version on the target channel goes above the version number of what the
81   // device currently has).
82   bool SetChannel(brillo::ErrorPtr* error,
83                   const std::string& in_target_channel,
84                   bool in_is_powerwash_allowed) override;
85 
86   // If get_current_channel is set to true, populates |channel| with the name of
87   // the channel that the device is currently on. Otherwise, it populates it
88   // with the name of the channel the device is supposed to be (in case of a
89   // pending channel change).
90   bool GetChannel(brillo::ErrorPtr* error,
91                   bool in_get_current_channel,
92                   std::string* out_channel) override;
93 
94   // Enables or disables the sharing and consuming updates over P2P feature
95   // according to the |enabled| argument passed.
96   bool SetP2PUpdatePermission(brillo::ErrorPtr* error,
97                               bool in_enabled) override;
98 
99   // Returns the current value for the P2P enabled setting. This involves both
100   // sharing and consuming updates over P2P.
101   bool GetP2PUpdatePermission(brillo::ErrorPtr* error,
102                               bool* out_enabled) override;
103 
104   // If there's no device policy installed, sets the update over cellular
105   // networks permission to the |allowed| value. Otherwise, this method returns
106   // with an error since this setting is overridden by the applied policy.
107   bool SetUpdateOverCellularPermission(brillo::ErrorPtr* error,
108                                        bool in_allowed) override;
109 
110   // Returns the current value of the update over cellular network setting,
111   // either forced by the device policy if the device is enrolled or the current
112   // user preference otherwise.
113   bool GetUpdateOverCellularPermission(brillo::ErrorPtr* error,
114                                        bool* out_allowed) override;
115 
116   // Returns the duration since the last successful update, as the
117   // duration on the wallclock. Returns an error if the device has not
118   // updated.
119   bool GetDurationSinceUpdate(brillo::ErrorPtr* error,
120                               int64_t* out_usec_wallclock) override;
121 
122   // Returns the version string of OS that was used before the last reboot
123   // into an updated version. This is available only when rebooting into an
124   // update from previous version, otherwise an empty string is returned.
125   bool GetPrevVersion(brillo::ErrorPtr* error,
126                       std::string* out_prev_version) override;
127 
128   // Returns the name of kernel partition that can be rolled back into.
129   bool GetRollbackPartition(brillo::ErrorPtr* error,
130                             std::string* out_rollback_partition_name) override;
131 
132   // Returns the last UpdateAttempt error. If not updated yet, default success
133   // ErrorCode will be returned.
134   bool GetLastAttemptError(brillo::ErrorPtr* error,
135                            int32_t* out_last_attempt_error) override;
136  private:
137   std::unique_ptr<UpdateEngineService> common_;
138 };
139 
140 // The UpdateEngineAdaptor class runs the UpdateEngineInterface in the fixed
141 // object path, without an ObjectManager notifying the interfaces, since it is
142 // all static and clients don't expect it to be implemented.
143 class UpdateEngineAdaptor : public org::chromium::UpdateEngineInterfaceAdaptor,
144                             public ServiceObserverInterface {
145  public:
146   UpdateEngineAdaptor(SystemState* system_state,
147                       const scoped_refptr<dbus::Bus>& bus);
148   ~UpdateEngineAdaptor() = default;
149 
150   // Register the DBus object with the update engine service asynchronously.
151   // Calls |copmletion_callback| when done passing a boolean indicating if the
152   // registration succeeded.
153   void RegisterAsync(const base::Callback<void(bool)>& completion_callback);
154 
155   // Takes ownership of the well-known DBus name and returns whether it
156   // succeeded.
157   bool RequestOwnership();
158 
159   // ServiceObserverInterface overrides.
160   void SendStatusUpdate(int64_t last_checked_time,
161                         double progress,
162                         update_engine::UpdateStatus status,
163                         const std::string& new_version,
164                         int64_t new_size) override;
165 
SendPayloadApplicationComplete(ErrorCode error_code)166   void SendPayloadApplicationComplete(ErrorCode error_code) override {}
167 
168   // Channel tracking changes are ignored.
SendChannelChangeUpdate(const std::string & tracking_channel)169   void SendChannelChangeUpdate(const std::string& tracking_channel) override {}
170 
171  private:
172   scoped_refptr<dbus::Bus> bus_;
173   DBusUpdateEngineService dbus_service_;
174   brillo::dbus_utils::DBusObject dbus_object_;
175 };
176 
177 }  // namespace chromeos_update_engine
178 
179 #endif  // UPDATE_ENGINE_DBUS_SERVICE_H_
180