blob: e179c6259c6f167340300aeddb9659ce6e257566 [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;
Alex Deymof8bfcff2016-02-02 21:22:11 -080027
28namespace {
29Status ErrorPtrToStatus(const brillo::ErrorPtr& error) {
30 return Status::fromServiceSpecificError(
31 1, android::String8{error->GetMessage().c_str()});
32}
33} // namespace
Casey Dahlina93cd532016-01-14 16:55:11 -080034
35namespace chromeos_update_engine {
36
Alex Deymofa78f142016-01-26 21:36:16 -080037BinderUpdateEngineAndroidService::BinderUpdateEngineAndroidService(
Alex Deymof8bfcff2016-02-02 21:22:11 -080038 ServiceDelegateAndroidInterface* service_delegate)
39 : service_delegate_(service_delegate) {
Alex Deymofa78f142016-01-26 21:36:16 -080040}
41
42void BinderUpdateEngineAndroidService::SendStatusUpdate(
Alex Deymof8bfcff2016-02-02 21:22:11 -080043 int64_t /* last_checked_time */,
Alex Deymofa78f142016-01-26 21:36:16 -080044 double progress,
45 update_engine::UpdateStatus status,
Alex Deymof8bfcff2016-02-02 21:22:11 -080046 const std::string& /* new_version */,
47 int64_t /* new_size */) {
Alex Deymo0e061ae2016-02-09 17:49:03 -080048 last_status_ = static_cast<int>(status);
49 last_progress_ = progress;
Alex Deymof8bfcff2016-02-02 21:22:11 -080050 for (auto& callback : callbacks_) {
Alex Deymo0e061ae2016-02-09 17:49:03 -080051 callback->onStatusUpdate(last_status_, last_progress_);
Alex Deymof8bfcff2016-02-02 21:22:11 -080052 }
53}
54
55void BinderUpdateEngineAndroidService::SendPayloadApplicationComplete(
56 ErrorCode error_code) {
57 for (auto& callback : callbacks_) {
58 callback->onPayloadApplicationComplete(static_cast<int>(error_code));
59 }
Alex Deymofa78f142016-01-26 21:36:16 -080060}
61
Alex Deymoe97b39c2016-01-20 13:22:17 -080062Status BinderUpdateEngineAndroidService::bind(
Alex Deymof8bfcff2016-02-02 21:22:11 -080063 const android::sp<IUpdateEngineCallback>& callback, bool* return_value) {
64 callbacks_.emplace_back(callback);
65
Sen Jiangb7f73802017-07-18 15:29:26 -070066 const android::sp<IBinder>& callback_binder =
67 IUpdateEngineCallback::asBinder(callback);
Alex Deymof8bfcff2016-02-02 21:22:11 -080068 auto binder_wrapper = android::BinderWrapper::Get();
69 binder_wrapper->RegisterForDeathNotifications(
Sen Jiangb7f73802017-07-18 15:29:26 -070070 callback_binder,
Sen Jiang5caab192017-07-07 17:22:29 -070071 base::Bind(
72 base::IgnoreResult(&BinderUpdateEngineAndroidService::UnbindCallback),
73 base::Unretained(this),
Sen Jiangb7f73802017-07-18 15:29:26 -070074 base::Unretained(callback_binder.get())));
Alex Deymof8bfcff2016-02-02 21:22:11 -080075
Alex Deymo0e061ae2016-02-09 17:49:03 -080076 // Send an status update on connection (except when no update sent so far),
77 // since the status update is oneway and we don't need to wait for the
78 // response.
79 if (last_status_ != -1)
80 callback->onStatusUpdate(last_status_, last_progress_);
81
Casey Dahlina93cd532016-01-14 16:55:11 -080082 *return_value = true;
83 return Status::ok();
84}
85
Sen Jiang5caab192017-07-07 17:22:29 -070086Status BinderUpdateEngineAndroidService::unbind(
87 const android::sp<IUpdateEngineCallback>& callback, bool* return_value) {
Sen Jiangb7f73802017-07-18 15:29:26 -070088 const android::sp<IBinder>& callback_binder =
89 IUpdateEngineCallback::asBinder(callback);
Sen Jiang5caab192017-07-07 17:22:29 -070090 auto binder_wrapper = android::BinderWrapper::Get();
Sen Jiangb7f73802017-07-18 15:29:26 -070091 binder_wrapper->UnregisterForDeathNotifications(callback_binder);
Sen Jiang5caab192017-07-07 17:22:29 -070092
Sen Jiangb7f73802017-07-18 15:29:26 -070093 *return_value = UnbindCallback(callback_binder.get());
Sen Jiang5caab192017-07-07 17:22:29 -070094 return Status::ok();
95}
96
Alex Deymoe97b39c2016-01-20 13:22:17 -080097Status BinderUpdateEngineAndroidService::applyPayload(
Alex Deymof8bfcff2016-02-02 21:22:11 -080098 const android::String16& url,
Alex Deymo95b8f242016-01-28 16:06:57 -080099 int64_t payload_offset,
100 int64_t payload_size,
Alex Deymof8bfcff2016-02-02 21:22:11 -0800101 const std::vector<android::String16>& header_kv_pairs) {
102 const std::string payload_url{android::String8{url}.string()};
103 std::vector<std::string> str_headers;
104 str_headers.reserve(header_kv_pairs.size());
105 for (const auto& header : header_kv_pairs) {
106 str_headers.emplace_back(android::String8{header}.string());
107 }
108
109 brillo::ErrorPtr error;
110 if (!service_delegate_->ApplyPayload(
111 payload_url, payload_offset, payload_size, str_headers, &error)) {
112 return ErrorPtrToStatus(error);
113 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800114 return Status::ok();
115}
116
Alex Deymoe97b39c2016-01-20 13:22:17 -0800117Status BinderUpdateEngineAndroidService::suspend() {
Alex Deymof8bfcff2016-02-02 21:22:11 -0800118 brillo::ErrorPtr error;
119 if (!service_delegate_->SuspendUpdate(&error))
120 return ErrorPtrToStatus(error);
Casey Dahlina93cd532016-01-14 16:55:11 -0800121 return Status::ok();
122}
123
Alex Deymoe97b39c2016-01-20 13:22:17 -0800124Status BinderUpdateEngineAndroidService::resume() {
Alex Deymof8bfcff2016-02-02 21:22:11 -0800125 brillo::ErrorPtr error;
126 if (!service_delegate_->ResumeUpdate(&error))
127 return ErrorPtrToStatus(error);
Casey Dahlina93cd532016-01-14 16:55:11 -0800128 return Status::ok();
129}
130
Alex Deymoe97b39c2016-01-20 13:22:17 -0800131Status BinderUpdateEngineAndroidService::cancel() {
Alex Deymof8bfcff2016-02-02 21:22:11 -0800132 brillo::ErrorPtr error;
133 if (!service_delegate_->CancelUpdate(&error))
134 return ErrorPtrToStatus(error);
Casey Dahlina93cd532016-01-14 16:55:11 -0800135 return Status::ok();
136}
137
Alex Deymo3b678db2016-02-09 11:50:06 -0800138Status BinderUpdateEngineAndroidService::resetStatus() {
139 brillo::ErrorPtr error;
140 if (!service_delegate_->ResetStatus(&error))
141 return ErrorPtrToStatus(error);
142 return Status::ok();
143}
144
Sen Jiangb7f73802017-07-18 15:29:26 -0700145bool BinderUpdateEngineAndroidService::UnbindCallback(const IBinder* callback) {
146 auto it = std::find_if(
147 callbacks_.begin(),
148 callbacks_.end(),
149 [&callback](const android::sp<IUpdateEngineCallback>& elem) {
150 return IUpdateEngineCallback::asBinder(elem).get() == callback;
151 });
Alex Deymof8bfcff2016-02-02 21:22:11 -0800152 if (it == callbacks_.end()) {
Sen Jiang5caab192017-07-07 17:22:29 -0700153 LOG(ERROR) << "Unable to unbind unknown callback.";
154 return false;
Alex Deymof8bfcff2016-02-02 21:22:11 -0800155 }
156 callbacks_.erase(it);
Sen Jiang5caab192017-07-07 17:22:29 -0700157 return true;
Alex Deymof8bfcff2016-02-02 21:22:11 -0800158}
159
Casey Dahlina93cd532016-01-14 16:55:11 -0800160} // namespace chromeos_update_engine