blob: 872f64c333e69a7aaaf8d3957d40e22b20418d1b [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),
69 base::Bind(&BinderUpdateEngineAndroidService::UnbindCallback,
70 base::Unretained(this),
71 base::Unretained(callback.get())));
72
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
Alex Deymoe97b39c2016-01-20 13:22:17 -080083Status BinderUpdateEngineAndroidService::applyPayload(
Alex Deymof8bfcff2016-02-02 21:22:11 -080084 const android::String16& url,
Alex Deymo95b8f242016-01-28 16:06:57 -080085 int64_t payload_offset,
86 int64_t payload_size,
Alex Deymof8bfcff2016-02-02 21:22:11 -080087 const std::vector<android::String16>& header_kv_pairs) {
88 const std::string payload_url{android::String8{url}.string()};
89 std::vector<std::string> str_headers;
90 str_headers.reserve(header_kv_pairs.size());
91 for (const auto& header : header_kv_pairs) {
92 str_headers.emplace_back(android::String8{header}.string());
93 }
94
95 brillo::ErrorPtr error;
96 if (!service_delegate_->ApplyPayload(
97 payload_url, payload_offset, payload_size, str_headers, &error)) {
98 return ErrorPtrToStatus(error);
99 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800100 return Status::ok();
101}
102
Alex Deymoe97b39c2016-01-20 13:22:17 -0800103Status BinderUpdateEngineAndroidService::suspend() {
Alex Deymof8bfcff2016-02-02 21:22:11 -0800104 brillo::ErrorPtr error;
105 if (!service_delegate_->SuspendUpdate(&error))
106 return ErrorPtrToStatus(error);
Casey Dahlina93cd532016-01-14 16:55:11 -0800107 return Status::ok();
108}
109
Alex Deymoe97b39c2016-01-20 13:22:17 -0800110Status BinderUpdateEngineAndroidService::resume() {
Alex Deymof8bfcff2016-02-02 21:22:11 -0800111 brillo::ErrorPtr error;
112 if (!service_delegate_->ResumeUpdate(&error))
113 return ErrorPtrToStatus(error);
Casey Dahlina93cd532016-01-14 16:55:11 -0800114 return Status::ok();
115}
116
Alex Deymoe97b39c2016-01-20 13:22:17 -0800117Status BinderUpdateEngineAndroidService::cancel() {
Alex Deymof8bfcff2016-02-02 21:22:11 -0800118 brillo::ErrorPtr error;
119 if (!service_delegate_->CancelUpdate(&error))
120 return ErrorPtrToStatus(error);
Casey Dahlina93cd532016-01-14 16:55:11 -0800121 return Status::ok();
122}
123
Alex Deymo3b678db2016-02-09 11:50:06 -0800124Status BinderUpdateEngineAndroidService::resetStatus() {
125 brillo::ErrorPtr error;
126 if (!service_delegate_->ResetStatus(&error))
127 return ErrorPtrToStatus(error);
128 return Status::ok();
129}
130
Alex Deymof8bfcff2016-02-02 21:22:11 -0800131void BinderUpdateEngineAndroidService::UnbindCallback(
132 IUpdateEngineCallback* callback) {
133 auto it =
134 std::find_if(callbacks_.begin(),
135 callbacks_.end(),
136 [&callback](const android::sp<IUpdateEngineCallback>& elem) {
137 return elem.get() == callback;
138 });
139 if (it == callbacks_.end()) {
140 LOG(ERROR) << "Got death notification for unknown callback.";
141 return;
142 }
143 callbacks_.erase(it);
144}
145
Casey Dahlina93cd532016-01-14 16:55:11 -0800146} // namespace chromeos_update_engine