binder_service_brillo.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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/binder_service_brillo.h"
  17. #include <base/bind.h>
  18. #include <binderwrapper/binder_wrapper.h>
  19. #include <utils/String16.h>
  20. #include <utils/StrongPointer.h>
  21. #include "update_engine/update_status_utils.h"
  22. using android::sp;
  23. using android::String16;
  24. using android::String8;
  25. using android::binder::Status;
  26. using android::brillo::IUpdateEngineStatusCallback;
  27. using android::brillo::ParcelableUpdateEngineStatus;
  28. using brillo::ErrorPtr;
  29. using std::string;
  30. using update_engine::UpdateEngineStatus;
  31. namespace chromeos_update_engine {
  32. namespace {
  33. string NormalString(const String16& in) {
  34. return string{String8{in}.string()};
  35. }
  36. Status ToStatus(ErrorPtr* error) {
  37. return Status::fromServiceSpecificError(
  38. 1, String8{error->get()->GetMessage().c_str()});
  39. }
  40. } // namespace
  41. template <typename... Parameters, typename... Arguments>
  42. Status BinderUpdateEngineBrilloService::CallCommonHandler(
  43. bool (UpdateEngineService::*Handler)(ErrorPtr*, Parameters...),
  44. Arguments... arguments) {
  45. ErrorPtr error;
  46. if (((common_.get())->*Handler)(&error, arguments...))
  47. return Status::ok();
  48. return ToStatus(&error);
  49. }
  50. Status BinderUpdateEngineBrilloService::SetUpdateAttemptFlags(int flags) {
  51. return CallCommonHandler(&UpdateEngineService::SetUpdateAttemptFlags, flags);
  52. }
  53. Status BinderUpdateEngineBrilloService::AttemptUpdate(
  54. const String16& app_version,
  55. const String16& omaha_url,
  56. int flags,
  57. bool* out_result) {
  58. return CallCommonHandler(&UpdateEngineService::AttemptUpdate,
  59. NormalString(app_version),
  60. NormalString(omaha_url),
  61. flags,
  62. out_result);
  63. }
  64. Status BinderUpdateEngineBrilloService::AttemptRollback(bool powerwash) {
  65. return CallCommonHandler(&UpdateEngineService::AttemptRollback, powerwash);
  66. }
  67. Status BinderUpdateEngineBrilloService::CanRollback(bool* out_can_rollback) {
  68. return CallCommonHandler(&UpdateEngineService::CanRollback, out_can_rollback);
  69. }
  70. Status BinderUpdateEngineBrilloService::ResetStatus() {
  71. return CallCommonHandler(&UpdateEngineService::ResetStatus);
  72. }
  73. Status BinderUpdateEngineBrilloService::GetStatus(
  74. ParcelableUpdateEngineStatus* status) {
  75. UpdateEngineStatus update_engine_status;
  76. auto ret =
  77. CallCommonHandler(&UpdateEngineService::GetStatus, &update_engine_status);
  78. if (ret.isOk()) {
  79. *status = ParcelableUpdateEngineStatus(update_engine_status);
  80. }
  81. return ret;
  82. }
  83. Status BinderUpdateEngineBrilloService::RebootIfNeeded() {
  84. return CallCommonHandler(&UpdateEngineService::RebootIfNeeded);
  85. }
  86. Status BinderUpdateEngineBrilloService::SetChannel(
  87. const String16& target_channel, bool powerwash) {
  88. return CallCommonHandler(&UpdateEngineService::SetChannel,
  89. NormalString(target_channel),
  90. powerwash);
  91. }
  92. Status BinderUpdateEngineBrilloService::GetChannel(bool get_current_channel,
  93. String16* out_channel) {
  94. string channel_string;
  95. auto ret = CallCommonHandler(
  96. &UpdateEngineService::GetChannel, get_current_channel, &channel_string);
  97. *out_channel = String16(channel_string.c_str());
  98. return ret;
  99. }
  100. Status BinderUpdateEngineBrilloService::SetCohortHint(
  101. const String16& in_cohort_hint) {
  102. return CallCommonHandler(&UpdateEngineService::SetCohortHint,
  103. NormalString(in_cohort_hint));
  104. }
  105. Status BinderUpdateEngineBrilloService::GetCohortHint(
  106. String16* out_cohort_hint) {
  107. string cohort_hint;
  108. auto ret =
  109. CallCommonHandler(&UpdateEngineService::GetCohortHint, &cohort_hint);
  110. *out_cohort_hint = String16(cohort_hint.c_str());
  111. return ret;
  112. }
  113. Status BinderUpdateEngineBrilloService::SetP2PUpdatePermission(bool enabled) {
  114. return CallCommonHandler(&UpdateEngineService::SetP2PUpdatePermission,
  115. enabled);
  116. }
  117. Status BinderUpdateEngineBrilloService::GetP2PUpdatePermission(
  118. bool* out_p2p_permission) {
  119. return CallCommonHandler(&UpdateEngineService::GetP2PUpdatePermission,
  120. out_p2p_permission);
  121. }
  122. Status BinderUpdateEngineBrilloService::SetUpdateOverCellularPermission(
  123. bool enabled) {
  124. return CallCommonHandler(
  125. &UpdateEngineService::SetUpdateOverCellularPermission, enabled);
  126. }
  127. Status BinderUpdateEngineBrilloService::SetUpdateOverCellularTarget(
  128. const String16& target_version, int64_t target_size) {
  129. return CallCommonHandler(&UpdateEngineService::SetUpdateOverCellularTarget,
  130. NormalString(target_version),
  131. target_size);
  132. }
  133. Status BinderUpdateEngineBrilloService::GetUpdateOverCellularPermission(
  134. bool* out_cellular_permission) {
  135. return CallCommonHandler(
  136. &UpdateEngineService::GetUpdateOverCellularPermission,
  137. out_cellular_permission);
  138. }
  139. Status BinderUpdateEngineBrilloService::GetDurationSinceUpdate(
  140. int64_t* out_duration) {
  141. return CallCommonHandler(&UpdateEngineService::GetDurationSinceUpdate,
  142. out_duration);
  143. }
  144. Status BinderUpdateEngineBrilloService::GetPrevVersion(
  145. String16* out_prev_version) {
  146. string version_string;
  147. auto ret =
  148. CallCommonHandler(&UpdateEngineService::GetPrevVersion, &version_string);
  149. *out_prev_version = String16(version_string.c_str());
  150. return ret;
  151. }
  152. Status BinderUpdateEngineBrilloService::GetRollbackPartition(
  153. String16* out_rollback_partition) {
  154. string partition_string;
  155. auto ret = CallCommonHandler(&UpdateEngineService::GetRollbackPartition,
  156. &partition_string);
  157. if (ret.isOk()) {
  158. *out_rollback_partition = String16(partition_string.c_str());
  159. }
  160. return ret;
  161. }
  162. Status BinderUpdateEngineBrilloService::RegisterStatusCallback(
  163. const sp<IUpdateEngineStatusCallback>& callback) {
  164. callbacks_.emplace_back(callback);
  165. auto binder_wrapper = android::BinderWrapper::Get();
  166. binder_wrapper->RegisterForDeathNotifications(
  167. IUpdateEngineStatusCallback::asBinder(callback),
  168. base::Bind(&BinderUpdateEngineBrilloService::UnregisterStatusCallback,
  169. base::Unretained(this),
  170. base::Unretained(callback.get())));
  171. return Status::ok();
  172. }
  173. Status BinderUpdateEngineBrilloService::GetLastAttemptError(
  174. int* out_last_attempt_error) {
  175. return CallCommonHandler(&UpdateEngineService::GetLastAttemptError,
  176. out_last_attempt_error);
  177. }
  178. Status BinderUpdateEngineBrilloService::GetEolStatus(int* out_eol_status) {
  179. return CallCommonHandler(&UpdateEngineService::GetEolStatus, out_eol_status);
  180. }
  181. void BinderUpdateEngineBrilloService::UnregisterStatusCallback(
  182. IUpdateEngineStatusCallback* callback) {
  183. auto it = callbacks_.begin();
  184. while (it != callbacks_.end() && it->get() != callback)
  185. it++;
  186. if (it == callbacks_.end()) {
  187. LOG(ERROR) << "Got death notification for unknown callback.";
  188. return;
  189. }
  190. LOG(INFO) << "Erasing orphan callback";
  191. callbacks_.erase(it);
  192. }
  193. void BinderUpdateEngineBrilloService::SendStatusUpdate(
  194. const UpdateEngineStatus& update_engine_status) {
  195. ParcelableUpdateEngineStatus parcelable_status(update_engine_status);
  196. for (auto& callback : callbacks_) {
  197. callback->HandleStatusUpdate(parcelable_status);
  198. }
  199. }
  200. } // namespace chromeos_update_engine