blob: 3c679da7499c916c6a6351b84360217ce41bc6bc [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 */) {
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 }
Alex Deymofa78f142016-01-26 21:36:16 -080058}
59
Alex Deymoe97b39c2016-01-20 13:22:17 -080060Status BinderUpdateEngineAndroidService::bind(
Alex Deymof8bfcff2016-02-02 21:22:11 -080061 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
Casey Dahlina93cd532016-01-14 16:55:11 -080071 *return_value = true;
72 return Status::ok();
73}
74
Alex Deymoe97b39c2016-01-20 13:22:17 -080075Status BinderUpdateEngineAndroidService::applyPayload(
Alex Deymof8bfcff2016-02-02 21:22:11 -080076 const android::String16& url,
Alex Deymo95b8f242016-01-28 16:06:57 -080077 int64_t payload_offset,
78 int64_t payload_size,
Alex Deymof8bfcff2016-02-02 21:22:11 -080079 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 }
Casey Dahlina93cd532016-01-14 16:55:11 -080092 return Status::ok();
93}
94
Alex Deymoe97b39c2016-01-20 13:22:17 -080095Status BinderUpdateEngineAndroidService::suspend() {
Alex Deymof8bfcff2016-02-02 21:22:11 -080096 brillo::ErrorPtr error;
97 if (!service_delegate_->SuspendUpdate(&error))
98 return ErrorPtrToStatus(error);
Casey Dahlina93cd532016-01-14 16:55:11 -080099 return Status::ok();
100}
101
Alex Deymoe97b39c2016-01-20 13:22:17 -0800102Status BinderUpdateEngineAndroidService::resume() {
Alex Deymof8bfcff2016-02-02 21:22:11 -0800103 brillo::ErrorPtr error;
104 if (!service_delegate_->ResumeUpdate(&error))
105 return ErrorPtrToStatus(error);
Casey Dahlina93cd532016-01-14 16:55:11 -0800106 return Status::ok();
107}
108
Alex Deymoe97b39c2016-01-20 13:22:17 -0800109Status BinderUpdateEngineAndroidService::cancel() {
Alex Deymof8bfcff2016-02-02 21:22:11 -0800110 brillo::ErrorPtr error;
111 if (!service_delegate_->CancelUpdate(&error))
112 return ErrorPtrToStatus(error);
Casey Dahlina93cd532016-01-14 16:55:11 -0800113 return Status::ok();
114}
115
Alex Deymof8bfcff2016-02-02 21:22:11 -0800116void 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
Casey Dahlina93cd532016-01-14 16:55:11 -0800131} // namespace chromeos_update_engine