blob: 0305727fe0ca2f4dd0601d14fdf32a6956bbee46 [file] [log] [blame]
Casey Dahlina93cd532016-01-14 16:55:11 -08001//
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
17#include "update_engine/binder_service_android.h"
18
Alex Deymof8bfcff2016-02-02 21:22:11 -080019#include <base/bind.h>
20#include <base/logging.h>
21#include <binderwrapper/binder_wrapper.h>
22#include <brillo/errors/error.h>
23#include <utils/String8.h>
24
Casey Dahlina93cd532016-01-14 16:55:11 -080025using android::binder::Status;
26using android::os::IUpdateEngineCallback;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070027using update_engine::UpdateEngineStatus;
Alex Deymof8bfcff2016-02-02 21:22:11 -080028
29namespace {
30Status ErrorPtrToStatus(const brillo::ErrorPtr& error) {
31 return Status::fromServiceSpecificError(
32 1, android::String8{error->GetMessage().c_str()});
33}
34} // namespace
Casey Dahlina93cd532016-01-14 16:55:11 -080035
36namespace chromeos_update_engine {
37
Alex Deymofa78f142016-01-26 21:36:16 -080038BinderUpdateEngineAndroidService::BinderUpdateEngineAndroidService(
Alex Deymof8bfcff2016-02-02 21:22:11 -080039 ServiceDelegateAndroidInterface* service_delegate)
40 : service_delegate_(service_delegate) {
Alex Deymofa78f142016-01-26 21:36:16 -080041}
42
43void BinderUpdateEngineAndroidService::SendStatusUpdate(
Aaron Wood7f92e2b2017-08-28 14:51:21 -070044 const UpdateEngineStatus& update_engine_status) {
45 last_status_ = static_cast<int>(update_engine_status.status);
46 last_progress_ = update_engine_status.progress;
Alex Deymof8bfcff2016-02-02 21:22:11 -080047 for (auto& callback : callbacks_) {
Alex Deymo0e061ae2016-02-09 17:49:03 -080048 callback->onStatusUpdate(last_status_, last_progress_);
Alex Deymof8bfcff2016-02-02 21:22:11 -080049 }
50}
51
52void BinderUpdateEngineAndroidService::SendPayloadApplicationComplete(
53 ErrorCode error_code) {
54 for (auto& callback : callbacks_) {
55 callback->onPayloadApplicationComplete(static_cast<int>(error_code));
56 }
Alex Deymofa78f142016-01-26 21:36:16 -080057}
58
Alex Deymoe97b39c2016-01-20 13:22:17 -080059Status BinderUpdateEngineAndroidService::bind(
Alex Deymof8bfcff2016-02-02 21:22:11 -080060 const android::sp<IUpdateEngineCallback>& callback, bool* return_value) {
61 callbacks_.emplace_back(callback);
62
Sen Jiangb7f73802017-07-18 15:29:26 -070063 const android::sp<IBinder>& callback_binder =
64 IUpdateEngineCallback::asBinder(callback);
Alex Deymof8bfcff2016-02-02 21:22:11 -080065 auto binder_wrapper = android::BinderWrapper::Get();
66 binder_wrapper->RegisterForDeathNotifications(
Sen Jiangb7f73802017-07-18 15:29:26 -070067 callback_binder,
Sen Jiang5caab192017-07-07 17:22:29 -070068 base::Bind(
69 base::IgnoreResult(&BinderUpdateEngineAndroidService::UnbindCallback),
70 base::Unretained(this),
Sen Jiangb7f73802017-07-18 15:29:26 -070071 base::Unretained(callback_binder.get())));
Alex Deymof8bfcff2016-02-02 21:22:11 -080072
Alex Deymo0e061ae2016-02-09 17:49:03 -080073 // Send an status update on connection (except when no update sent so far),
74 // since the status update is oneway and we don't need to wait for the
75 // response.
76 if (last_status_ != -1)
77 callback->onStatusUpdate(last_status_, last_progress_);
78
Casey Dahlina93cd532016-01-14 16:55:11 -080079 *return_value = true;
80 return Status::ok();
81}
82
Sen Jiang5caab192017-07-07 17:22:29 -070083Status BinderUpdateEngineAndroidService::unbind(
84 const android::sp<IUpdateEngineCallback>& callback, bool* return_value) {
Sen Jiangb7f73802017-07-18 15:29:26 -070085 const android::sp<IBinder>& callback_binder =
86 IUpdateEngineCallback::asBinder(callback);
Sen Jiang5caab192017-07-07 17:22:29 -070087 auto binder_wrapper = android::BinderWrapper::Get();
Sen Jiangb7f73802017-07-18 15:29:26 -070088 binder_wrapper->UnregisterForDeathNotifications(callback_binder);
Sen Jiang5caab192017-07-07 17:22:29 -070089
Sen Jiangb7f73802017-07-18 15:29:26 -070090 *return_value = UnbindCallback(callback_binder.get());
Sen Jiang5caab192017-07-07 17:22:29 -070091 return Status::ok();
92}
93
Alex Deymoe97b39c2016-01-20 13:22:17 -080094Status BinderUpdateEngineAndroidService::applyPayload(
Alex Deymof8bfcff2016-02-02 21:22:11 -080095 const android::String16& url,
Alex Deymo95b8f242016-01-28 16:06:57 -080096 int64_t payload_offset,
97 int64_t payload_size,
Alex Deymof8bfcff2016-02-02 21:22:11 -080098 const std::vector<android::String16>& header_kv_pairs) {
99 const std::string payload_url{android::String8{url}.string()};
100 std::vector<std::string> str_headers;
101 str_headers.reserve(header_kv_pairs.size());
102 for (const auto& header : header_kv_pairs) {
103 str_headers.emplace_back(android::String8{header}.string());
104 }
105
106 brillo::ErrorPtr error;
107 if (!service_delegate_->ApplyPayload(
108 payload_url, payload_offset, payload_size, str_headers, &error)) {
109 return ErrorPtrToStatus(error);
110 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800111 return Status::ok();
112}
113
Alex Deymoe97b39c2016-01-20 13:22:17 -0800114Status BinderUpdateEngineAndroidService::suspend() {
Alex Deymof8bfcff2016-02-02 21:22:11 -0800115 brillo::ErrorPtr error;
116 if (!service_delegate_->SuspendUpdate(&error))
117 return ErrorPtrToStatus(error);
Casey Dahlina93cd532016-01-14 16:55:11 -0800118 return Status::ok();
119}
120
Alex Deymoe97b39c2016-01-20 13:22:17 -0800121Status BinderUpdateEngineAndroidService::resume() {
Alex Deymof8bfcff2016-02-02 21:22:11 -0800122 brillo::ErrorPtr error;
123 if (!service_delegate_->ResumeUpdate(&error))
124 return ErrorPtrToStatus(error);
Casey Dahlina93cd532016-01-14 16:55:11 -0800125 return Status::ok();
126}
127
Alex Deymoe97b39c2016-01-20 13:22:17 -0800128Status BinderUpdateEngineAndroidService::cancel() {
Alex Deymof8bfcff2016-02-02 21:22:11 -0800129 brillo::ErrorPtr error;
130 if (!service_delegate_->CancelUpdate(&error))
131 return ErrorPtrToStatus(error);
Casey Dahlina93cd532016-01-14 16:55:11 -0800132 return Status::ok();
133}
134
Alex Deymo3b678db2016-02-09 11:50:06 -0800135Status BinderUpdateEngineAndroidService::resetStatus() {
136 brillo::ErrorPtr error;
137 if (!service_delegate_->ResetStatus(&error))
138 return ErrorPtrToStatus(error);
139 return Status::ok();
140}
141
Sen Jiangb7f73802017-07-18 15:29:26 -0700142bool BinderUpdateEngineAndroidService::UnbindCallback(const IBinder* callback) {
143 auto it = std::find_if(
144 callbacks_.begin(),
145 callbacks_.end(),
146 [&callback](const android::sp<IUpdateEngineCallback>& elem) {
147 return IUpdateEngineCallback::asBinder(elem).get() == callback;
148 });
Alex Deymof8bfcff2016-02-02 21:22:11 -0800149 if (it == callbacks_.end()) {
Sen Jiang5caab192017-07-07 17:22:29 -0700150 LOG(ERROR) << "Unable to unbind unknown callback.";
151 return false;
Alex Deymof8bfcff2016-02-02 21:22:11 -0800152 }
153 callbacks_.erase(it);
Sen Jiang5caab192017-07-07 17:22:29 -0700154 return true;
Alex Deymof8bfcff2016-02-02 21:22:11 -0800155}
156
Casey Dahlina93cd532016-01-14 16:55:11 -0800157} // namespace chromeos_update_engine