client_dbus.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // Copyright (C) 2015 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. #include "update_engine/client_library/client_dbus.h"
  17. #include <base/message_loop/message_loop.h>
  18. #include <dbus/bus.h>
  19. #include <update_engine/dbus-constants.h>
  20. #include <update_engine/proto_bindings/update_engine.pb.h>
  21. #include "update_engine/update_status_utils.h"
  22. using chromeos_update_engine::StringToUpdateStatus;
  23. using dbus::Bus;
  24. using org::chromium::UpdateEngineInterfaceProxy;
  25. using std::string;
  26. using std::vector;
  27. namespace update_engine {
  28. namespace internal {
  29. bool DBusUpdateEngineClient::Init() {
  30. Bus::Options options;
  31. options.bus_type = Bus::SYSTEM;
  32. scoped_refptr<Bus> bus{new Bus{options}};
  33. if (!bus->Connect())
  34. return false;
  35. proxy_.reset(new UpdateEngineInterfaceProxy{bus});
  36. return true;
  37. }
  38. bool DBusUpdateEngineClient::AttemptUpdate(const string& in_app_version,
  39. const string& in_omaha_url,
  40. bool at_user_request) {
  41. return proxy_->AttemptUpdateWithFlags(
  42. in_app_version,
  43. in_omaha_url,
  44. (at_user_request)
  45. ? 0
  46. : update_engine::UpdateAttemptFlags::kFlagNonInteractive,
  47. nullptr);
  48. }
  49. bool DBusUpdateEngineClient::AttemptInstall(
  50. const string& omaha_url, const vector<string>& dlc_module_ids) {
  51. // Convert parameters into protobuf.
  52. chromeos_update_engine::DlcParameters dlc_parameters;
  53. dlc_parameters.set_omaha_url(omaha_url);
  54. for (const auto& dlc_module_id : dlc_module_ids) {
  55. chromeos_update_engine::DlcInfo* dlc_info = dlc_parameters.add_dlc_infos();
  56. dlc_info->set_dlc_id(dlc_module_id);
  57. }
  58. string dlc_request;
  59. if (dlc_parameters.SerializeToString(&dlc_request)) {
  60. return proxy_->AttemptInstall(dlc_request, nullptr /* brillo::ErrorPtr* */);
  61. } else {
  62. LOG(ERROR) << "Fail to serialize a protobuf to a string.";
  63. return false;
  64. }
  65. }
  66. bool DBusUpdateEngineClient::GetStatus(int64_t* out_last_checked_time,
  67. double* out_progress,
  68. UpdateStatus* out_update_status,
  69. string* out_new_version,
  70. int64_t* out_new_size) const {
  71. string status_as_string;
  72. const bool success = proxy_->GetStatus(out_last_checked_time,
  73. out_progress,
  74. &status_as_string,
  75. out_new_version,
  76. out_new_size,
  77. nullptr);
  78. if (!success) {
  79. return false;
  80. }
  81. return StringToUpdateStatus(status_as_string, out_update_status);
  82. }
  83. bool DBusUpdateEngineClient::SetCohortHint(const string& cohort_hint) {
  84. return proxy_->SetCohortHint(cohort_hint, nullptr);
  85. }
  86. bool DBusUpdateEngineClient::GetCohortHint(string* cohort_hint) const {
  87. return proxy_->GetCohortHint(cohort_hint, nullptr);
  88. }
  89. bool DBusUpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) {
  90. return proxy_->SetUpdateOverCellularPermission(allowed, nullptr);
  91. }
  92. bool DBusUpdateEngineClient::GetUpdateOverCellularPermission(
  93. bool* allowed) const {
  94. return proxy_->GetUpdateOverCellularPermission(allowed, nullptr);
  95. }
  96. bool DBusUpdateEngineClient::SetP2PUpdatePermission(bool enabled) {
  97. return proxy_->SetP2PUpdatePermission(enabled, nullptr);
  98. }
  99. bool DBusUpdateEngineClient::GetP2PUpdatePermission(bool* enabled) const {
  100. return proxy_->GetP2PUpdatePermission(enabled, nullptr);
  101. }
  102. bool DBusUpdateEngineClient::Rollback(bool powerwash) {
  103. return proxy_->AttemptRollback(powerwash, nullptr);
  104. }
  105. bool DBusUpdateEngineClient::GetRollbackPartition(
  106. string* rollback_partition) const {
  107. return proxy_->GetRollbackPartition(rollback_partition, nullptr);
  108. }
  109. bool DBusUpdateEngineClient::GetPrevVersion(string* prev_version) const {
  110. return proxy_->GetPrevVersion(prev_version, nullptr);
  111. }
  112. void DBusUpdateEngineClient::RebootIfNeeded() {
  113. bool ret = proxy_->RebootIfNeeded(nullptr);
  114. if (!ret) {
  115. // Reboot error code doesn't necessarily mean that a reboot
  116. // failed. For example, D-Bus may be shutdown before we receive the
  117. // result.
  118. LOG(INFO) << "RebootIfNeeded() failure ignored.";
  119. }
  120. }
  121. bool DBusUpdateEngineClient::ResetStatus() {
  122. return proxy_->ResetStatus(nullptr);
  123. }
  124. void DBusUpdateEngineClient::DBusStatusHandlersRegistered(
  125. const string& interface, const string& signal_name, bool success) const {
  126. if (!success) {
  127. for (auto handler : handlers_) {
  128. handler->IPCError("Could not connect to" + signal_name + " on " +
  129. interface);
  130. }
  131. } else {
  132. StatusUpdateHandlersRegistered(nullptr);
  133. }
  134. }
  135. void DBusUpdateEngineClient::StatusUpdateHandlersRegistered(
  136. StatusUpdateHandler* handler) const {
  137. int64_t last_checked_time;
  138. double progress;
  139. UpdateStatus update_status;
  140. string new_version;
  141. int64_t new_size;
  142. if (!GetStatus(&last_checked_time,
  143. &progress,
  144. &update_status,
  145. &new_version,
  146. &new_size)) {
  147. handler->IPCError("Could not query current status");
  148. return;
  149. }
  150. std::vector<update_engine::StatusUpdateHandler*> just_handler = {handler};
  151. for (auto h : handler ? just_handler : handlers_) {
  152. h->HandleStatusUpdate(
  153. last_checked_time, progress, update_status, new_version, new_size);
  154. }
  155. }
  156. void DBusUpdateEngineClient::RunStatusUpdateHandlers(
  157. int64_t last_checked_time,
  158. double progress,
  159. const string& current_operation,
  160. const string& new_version,
  161. int64_t new_size) {
  162. UpdateStatus status;
  163. StringToUpdateStatus(current_operation, &status);
  164. for (auto handler : handlers_) {
  165. handler->HandleStatusUpdate(
  166. last_checked_time, progress, status, new_version, new_size);
  167. }
  168. }
  169. bool DBusUpdateEngineClient::UnregisterStatusUpdateHandler(
  170. StatusUpdateHandler* handler) {
  171. auto it = std::find(handlers_.begin(), handlers_.end(), handler);
  172. if (it != handlers_.end()) {
  173. handlers_.erase(it);
  174. return true;
  175. }
  176. return false;
  177. }
  178. bool DBusUpdateEngineClient::RegisterStatusUpdateHandler(
  179. StatusUpdateHandler* handler) {
  180. if (!base::MessageLoopForIO::current()) {
  181. LOG(FATAL) << "Cannot get UpdateEngineClient outside of message loop.";
  182. return false;
  183. }
  184. handlers_.push_back(handler);
  185. if (dbus_handler_registered_) {
  186. StatusUpdateHandlersRegistered(handler);
  187. return true;
  188. }
  189. proxy_->RegisterStatusUpdateSignalHandler(
  190. base::Bind(&DBusUpdateEngineClient::RunStatusUpdateHandlers,
  191. base::Unretained(this)),
  192. base::Bind(&DBusUpdateEngineClient::DBusStatusHandlersRegistered,
  193. base::Unretained(this)));
  194. dbus_handler_registered_ = true;
  195. return true;
  196. }
  197. bool DBusUpdateEngineClient::SetTargetChannel(const string& in_target_channel,
  198. bool allow_powerwash) {
  199. return proxy_->SetChannel(in_target_channel, allow_powerwash, nullptr);
  200. }
  201. bool DBusUpdateEngineClient::GetTargetChannel(string* out_channel) const {
  202. return proxy_->GetChannel(false, // Get the target channel.
  203. out_channel,
  204. nullptr);
  205. }
  206. bool DBusUpdateEngineClient::GetChannel(string* out_channel) const {
  207. return proxy_->GetChannel(true, // Get the current channel.
  208. out_channel,
  209. nullptr);
  210. }
  211. bool DBusUpdateEngineClient::GetLastAttemptError(
  212. int32_t* last_attempt_error) const {
  213. return proxy_->GetLastAttemptError(last_attempt_error, nullptr);
  214. }
  215. bool DBusUpdateEngineClient::GetEolStatus(int32_t* eol_status) const {
  216. return proxy_->GetEolStatus(eol_status, nullptr);
  217. }
  218. } // namespace internal
  219. } // namespace update_engine