binder_service_android.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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_android.h"
  17. #include <base/bind.h>
  18. #include <base/logging.h>
  19. #include <binderwrapper/binder_wrapper.h>
  20. #include <brillo/errors/error.h>
  21. #include <utils/String8.h>
  22. using android::binder::Status;
  23. using android::os::IUpdateEngineCallback;
  24. using update_engine::UpdateEngineStatus;
  25. namespace {
  26. Status ErrorPtrToStatus(const brillo::ErrorPtr& error) {
  27. return Status::fromServiceSpecificError(
  28. 1, android::String8{error->GetMessage().c_str()});
  29. }
  30. } // namespace
  31. namespace chromeos_update_engine {
  32. BinderUpdateEngineAndroidService::BinderUpdateEngineAndroidService(
  33. ServiceDelegateAndroidInterface* service_delegate)
  34. : service_delegate_(service_delegate) {}
  35. void BinderUpdateEngineAndroidService::SendStatusUpdate(
  36. const UpdateEngineStatus& update_engine_status) {
  37. last_status_ = static_cast<int>(update_engine_status.status);
  38. last_progress_ = update_engine_status.progress;
  39. for (auto& callback : callbacks_) {
  40. callback->onStatusUpdate(last_status_, last_progress_);
  41. }
  42. }
  43. void BinderUpdateEngineAndroidService::SendPayloadApplicationComplete(
  44. ErrorCode error_code) {
  45. for (auto& callback : callbacks_) {
  46. callback->onPayloadApplicationComplete(static_cast<int>(error_code));
  47. }
  48. }
  49. Status BinderUpdateEngineAndroidService::bind(
  50. const android::sp<IUpdateEngineCallback>& callback, bool* return_value) {
  51. callbacks_.emplace_back(callback);
  52. const android::sp<IBinder>& callback_binder =
  53. IUpdateEngineCallback::asBinder(callback);
  54. auto binder_wrapper = android::BinderWrapper::Get();
  55. binder_wrapper->RegisterForDeathNotifications(
  56. callback_binder,
  57. base::Bind(
  58. base::IgnoreResult(&BinderUpdateEngineAndroidService::UnbindCallback),
  59. base::Unretained(this),
  60. base::Unretained(callback_binder.get())));
  61. // Send an status update on connection (except when no update sent so far),
  62. // since the status update is oneway and we don't need to wait for the
  63. // response.
  64. if (last_status_ != -1)
  65. callback->onStatusUpdate(last_status_, last_progress_);
  66. *return_value = true;
  67. return Status::ok();
  68. }
  69. Status BinderUpdateEngineAndroidService::unbind(
  70. const android::sp<IUpdateEngineCallback>& callback, bool* return_value) {
  71. const android::sp<IBinder>& callback_binder =
  72. IUpdateEngineCallback::asBinder(callback);
  73. auto binder_wrapper = android::BinderWrapper::Get();
  74. binder_wrapper->UnregisterForDeathNotifications(callback_binder);
  75. *return_value = UnbindCallback(callback_binder.get());
  76. return Status::ok();
  77. }
  78. Status BinderUpdateEngineAndroidService::applyPayload(
  79. const android::String16& url,
  80. int64_t payload_offset,
  81. int64_t payload_size,
  82. const std::vector<android::String16>& header_kv_pairs) {
  83. const std::string payload_url{android::String8{url}.string()};
  84. std::vector<std::string> str_headers;
  85. str_headers.reserve(header_kv_pairs.size());
  86. for (const auto& header : header_kv_pairs) {
  87. str_headers.emplace_back(android::String8{header}.string());
  88. }
  89. brillo::ErrorPtr error;
  90. if (!service_delegate_->ApplyPayload(
  91. payload_url, payload_offset, payload_size, str_headers, &error)) {
  92. return ErrorPtrToStatus(error);
  93. }
  94. return Status::ok();
  95. }
  96. Status BinderUpdateEngineAndroidService::suspend() {
  97. brillo::ErrorPtr error;
  98. if (!service_delegate_->SuspendUpdate(&error))
  99. return ErrorPtrToStatus(error);
  100. return Status::ok();
  101. }
  102. Status BinderUpdateEngineAndroidService::resume() {
  103. brillo::ErrorPtr error;
  104. if (!service_delegate_->ResumeUpdate(&error))
  105. return ErrorPtrToStatus(error);
  106. return Status::ok();
  107. }
  108. Status BinderUpdateEngineAndroidService::cancel() {
  109. brillo::ErrorPtr error;
  110. if (!service_delegate_->CancelUpdate(&error))
  111. return ErrorPtrToStatus(error);
  112. return Status::ok();
  113. }
  114. Status BinderUpdateEngineAndroidService::resetStatus() {
  115. brillo::ErrorPtr error;
  116. if (!service_delegate_->ResetStatus(&error))
  117. return ErrorPtrToStatus(error);
  118. return Status::ok();
  119. }
  120. Status BinderUpdateEngineAndroidService::verifyPayloadApplicable(
  121. const android::String16& metadata_filename, bool* return_value) {
  122. const std::string payload_metadata{
  123. android::String8{metadata_filename}.string()};
  124. LOG(INFO) << "Received a request of verifying payload metadata in "
  125. << payload_metadata << ".";
  126. brillo::ErrorPtr error;
  127. *return_value =
  128. service_delegate_->VerifyPayloadApplicable(payload_metadata, &error);
  129. if (error != nullptr)
  130. return ErrorPtrToStatus(error);
  131. return Status::ok();
  132. }
  133. bool BinderUpdateEngineAndroidService::UnbindCallback(const IBinder* callback) {
  134. auto it = std::find_if(
  135. callbacks_.begin(),
  136. callbacks_.end(),
  137. [&callback](const android::sp<IUpdateEngineCallback>& elem) {
  138. return IUpdateEngineCallback::asBinder(elem).get() == callback;
  139. });
  140. if (it == callbacks_.end()) {
  141. LOG(ERROR) << "Unable to unbind unknown callback.";
  142. return false;
  143. }
  144. callbacks_.erase(it);
  145. return true;
  146. }
  147. } // namespace chromeos_update_engine