forked from LineageOS/android_system_update_engine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
binder_service_android.cc
178 lines (154 loc) · 5.76 KB
/
binder_service_android.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "update_engine/binder_service_android.h"
#include <base/bind.h>
#include <base/logging.h>
#include <binderwrapper/binder_wrapper.h>
#include <brillo/errors/error.h>
#include <utils/String8.h>
using android::binder::Status;
using android::os::IUpdateEngineCallback;
using update_engine::UpdateEngineStatus;
namespace {
Status ErrorPtrToStatus(const brillo::ErrorPtr& error) {
return Status::fromServiceSpecificError(
1, android::String8{error->GetMessage().c_str()});
}
} // namespace
namespace chromeos_update_engine {
BinderUpdateEngineAndroidService::BinderUpdateEngineAndroidService(
ServiceDelegateAndroidInterface* service_delegate)
: service_delegate_(service_delegate) {
}
void BinderUpdateEngineAndroidService::SendStatusUpdate(
const UpdateEngineStatus& update_engine_status) {
last_status_ = static_cast<int>(update_engine_status.status);
last_progress_ = update_engine_status.progress;
for (auto& callback : callbacks_) {
callback->onStatusUpdate(last_status_, last_progress_);
}
}
void BinderUpdateEngineAndroidService::SendPayloadApplicationComplete(
ErrorCode error_code) {
for (auto& callback : callbacks_) {
callback->onPayloadApplicationComplete(static_cast<int>(error_code));
}
}
Status BinderUpdateEngineAndroidService::bind(
const android::sp<IUpdateEngineCallback>& callback, bool* return_value) {
callbacks_.emplace_back(callback);
const android::sp<IBinder>& callback_binder =
IUpdateEngineCallback::asBinder(callback);
auto binder_wrapper = android::BinderWrapper::Get();
binder_wrapper->RegisterForDeathNotifications(
callback_binder,
base::Bind(
base::IgnoreResult(&BinderUpdateEngineAndroidService::UnbindCallback),
base::Unretained(this),
base::Unretained(callback_binder.get())));
// Send an status update on connection (except when no update sent so far),
// since the status update is oneway and we don't need to wait for the
// response.
if (last_status_ != -1)
callback->onStatusUpdate(last_status_, last_progress_);
*return_value = true;
return Status::ok();
}
Status BinderUpdateEngineAndroidService::unbind(
const android::sp<IUpdateEngineCallback>& callback, bool* return_value) {
const android::sp<IBinder>& callback_binder =
IUpdateEngineCallback::asBinder(callback);
auto binder_wrapper = android::BinderWrapper::Get();
binder_wrapper->UnregisterForDeathNotifications(callback_binder);
*return_value = UnbindCallback(callback_binder.get());
return Status::ok();
}
Status BinderUpdateEngineAndroidService::applyPayload(
const android::String16& url,
int64_t payload_offset,
int64_t payload_size,
const std::vector<android::String16>& header_kv_pairs) {
const std::string payload_url{android::String8{url}.string()};
std::vector<std::string> str_headers;
str_headers.reserve(header_kv_pairs.size());
for (const auto& header : header_kv_pairs) {
str_headers.emplace_back(android::String8{header}.string());
}
brillo::ErrorPtr error;
if (!service_delegate_->ApplyPayload(
payload_url, payload_offset, payload_size, str_headers, &error)) {
return ErrorPtrToStatus(error);
}
return Status::ok();
}
Status BinderUpdateEngineAndroidService::suspend() {
brillo::ErrorPtr error;
if (!service_delegate_->SuspendUpdate(&error))
return ErrorPtrToStatus(error);
return Status::ok();
}
Status BinderUpdateEngineAndroidService::resume() {
brillo::ErrorPtr error;
if (!service_delegate_->ResumeUpdate(&error))
return ErrorPtrToStatus(error);
return Status::ok();
}
Status BinderUpdateEngineAndroidService::cancel() {
brillo::ErrorPtr error;
if (!service_delegate_->CancelUpdate(&error))
return ErrorPtrToStatus(error);
return Status::ok();
}
Status BinderUpdateEngineAndroidService::resetStatus() {
brillo::ErrorPtr error;
if (!service_delegate_->ResetStatus(&error))
return ErrorPtrToStatus(error);
return Status::ok();
}
Status BinderUpdateEngineAndroidService::verifyPayloadApplicable(
const android::String16& metadata_filename, bool* return_value) {
const std::string payload_metadata{
android::String8{metadata_filename}.string()};
LOG(INFO) << "Received a request of verifying payload metadata in "
<< payload_metadata << ".";
brillo::ErrorPtr error;
*return_value =
service_delegate_->VerifyPayloadApplicable(payload_metadata, &error);
if (error != nullptr)
return ErrorPtrToStatus(error);
return Status::ok();
}
bool BinderUpdateEngineAndroidService::UnbindCallback(const IBinder* callback) {
auto it = std::find_if(
callbacks_.begin(),
callbacks_.end(),
[&callback](const android::sp<IUpdateEngineCallback>& elem) {
return IUpdateEngineCallback::asBinder(elem).get() == callback;
});
if (it == callbacks_.end()) {
LOG(ERROR) << "Unable to unbind unknown callback.";
return false;
}
callbacks_.erase(it);
return true;
}
Status BinderUpdateEngineAndroidService::setPerformanceMode(bool enable) {
brillo::ErrorPtr error;
if (!service_delegate_->SetPerformanceMode(enable, &error))
return ErrorPtrToStatus(error);
return Status::ok();
}
} // namespace chromeos_update_engine