blob: 91a2970f86b5345216f1f99bf3f92ac54270b234 [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
66 auto binder_wrapper = android::BinderWrapper::Get();
67 binder_wrapper->RegisterForDeathNotifications(
68 IUpdateEngineCallback::asBinder(callback),
Sen Jiang5caab192017-07-07 17:22:29 -070069 base::Bind(
70 base::IgnoreResult(&BinderUpdateEngineAndroidService::UnbindCallback),
71 base::Unretained(this),
72 base::Unretained(callback.get())));
Alex Deymof8bfcff2016-02-02 21:22:11 -080073
Alex Deymo0e061ae2016-02-09 17:49:03 -080074 // Send an status update on connection (except when no update sent so far),
75 // since the status update is oneway and we don't need to wait for the
76 // response.
77 if (last_status_ != -1)
78 callback->onStatusUpdate(last_status_, last_progress_);
79
Casey Dahlina93cd532016-01-14 16:55:11 -080080 *return_value = true;
81 return Status::ok();
82}
83
Sen Jiang5caab192017-07-07 17:22:29 -070084Status BinderUpdateEngineAndroidService::unbind(
85 const android::sp<IUpdateEngineCallback>& callback, bool* return_value) {
86 auto binder_wrapper = android::BinderWrapper::Get();
87 binder_wrapper->UnregisterForDeathNotifications(
88 IUpdateEngineCallback::asBinder(callback));
89
90 *return_value = UnbindCallback(callback.get());
91 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 Jiang5caab192017-07-07 17:22:29 -0700142bool BinderUpdateEngineAndroidService::UnbindCallback(
Alex Deymof8bfcff2016-02-02 21:22:11 -0800143 IUpdateEngineCallback* callback) {
144 auto it =
145 std::find_if(callbacks_.begin(),
146 callbacks_.end(),
147 [&callback](const android::sp<IUpdateEngineCallback>& elem) {
148 return elem.get() == callback;
149 });
150 if (it == callbacks_.end()) {
Sen Jiang5caab192017-07-07 17:22:29 -0700151 LOG(ERROR) << "Unable to unbind unknown callback.";
152 return false;
Alex Deymof8bfcff2016-02-02 21:22:11 -0800153 }
154 callbacks_.erase(it);
Sen Jiang5caab192017-07-07 17:22:29 -0700155 return true;
Alex Deymof8bfcff2016-02-02 21:22:11 -0800156}
157
Casey Dahlina93cd532016-01-14 16:55:11 -0800158} // namespace chromeos_update_engine