binder_service_android.cc revision f8bfcff8debbcbbb572fdd61e640efe2a3df31dc
1//
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
19#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
25using android::binder::Status;
26using android::os::IUpdateEngineCallback;
27
28namespace {
29Status ErrorPtrToStatus(const brillo::ErrorPtr& error) {
30  return Status::fromServiceSpecificError(
31      1, android::String8{error->GetMessage().c_str()});
32}
33}  // namespace
34
35namespace chromeos_update_engine {
36
37BinderUpdateEngineAndroidService::BinderUpdateEngineAndroidService(
38    ServiceDelegateAndroidInterface* service_delegate)
39    : service_delegate_(service_delegate) {
40}
41
42void BinderUpdateEngineAndroidService::SendStatusUpdate(
43    int64_t /* last_checked_time */,
44    double progress,
45    update_engine::UpdateStatus status,
46    const std::string& /* new_version  */,
47    int64_t /* new_size */) {
48  for (auto& callback : callbacks_) {
49    callback->onStatusUpdate(static_cast<int>(status), progress);
50  }
51}
52
53void BinderUpdateEngineAndroidService::SendPayloadApplicationComplete(
54    ErrorCode error_code) {
55  for (auto& callback : callbacks_) {
56    callback->onPayloadApplicationComplete(static_cast<int>(error_code));
57  }
58}
59
60Status BinderUpdateEngineAndroidService::bind(
61    const android::sp<IUpdateEngineCallback>& callback, bool* return_value) {
62  callbacks_.emplace_back(callback);
63
64  auto binder_wrapper = android::BinderWrapper::Get();
65  binder_wrapper->RegisterForDeathNotifications(
66      IUpdateEngineCallback::asBinder(callback),
67      base::Bind(&BinderUpdateEngineAndroidService::UnbindCallback,
68                 base::Unretained(this),
69                 base::Unretained(callback.get())));
70
71  *return_value = true;
72  return Status::ok();
73}
74
75Status BinderUpdateEngineAndroidService::applyPayload(
76    const android::String16& url,
77    int64_t payload_offset,
78    int64_t payload_size,
79    const std::vector<android::String16>& header_kv_pairs) {
80  const std::string payload_url{android::String8{url}.string()};
81  std::vector<std::string> str_headers;
82  str_headers.reserve(header_kv_pairs.size());
83  for (const auto& header : header_kv_pairs) {
84    str_headers.emplace_back(android::String8{header}.string());
85  }
86
87  brillo::ErrorPtr error;
88  if (!service_delegate_->ApplyPayload(
89          payload_url, payload_offset, payload_size, str_headers, &error)) {
90    return ErrorPtrToStatus(error);
91  }
92  return Status::ok();
93}
94
95Status BinderUpdateEngineAndroidService::suspend() {
96  brillo::ErrorPtr error;
97  if (!service_delegate_->SuspendUpdate(&error))
98    return ErrorPtrToStatus(error);
99  return Status::ok();
100}
101
102Status BinderUpdateEngineAndroidService::resume() {
103  brillo::ErrorPtr error;
104  if (!service_delegate_->ResumeUpdate(&error))
105    return ErrorPtrToStatus(error);
106  return Status::ok();
107}
108
109Status BinderUpdateEngineAndroidService::cancel() {
110  brillo::ErrorPtr error;
111  if (!service_delegate_->CancelUpdate(&error))
112    return ErrorPtrToStatus(error);
113  return Status::ok();
114}
115
116void BinderUpdateEngineAndroidService::UnbindCallback(
117    IUpdateEngineCallback* callback) {
118  auto it =
119      std::find_if(callbacks_.begin(),
120                   callbacks_.end(),
121                   [&callback](const android::sp<IUpdateEngineCallback>& elem) {
122                     return elem.get() == callback;
123                   });
124  if (it == callbacks_.end()) {
125    LOG(ERROR) << "Got death notification for unknown callback.";
126    return;
127  }
128  callbacks_.erase(it);
129}
130
131}  // namespace chromeos_update_engine
132