blob: 3bc7f6c47333a2e5baaccdbddcd6bb01c7fbd31b [file] [log] [blame]
Yifan Hong2562cf22020-07-21 19:28:44 -07001//
2// Copyright (C) 2020 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
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#include "update_engine/aosp/binder_service_stable_android.h"
Yifan Hong2562cf22020-07-21 19:28:44 -070018
19#include <memory>
20
21#include <base/bind.h>
22#include <base/logging.h>
23#include <binderwrapper/binder_wrapper.h>
Yifan Hong2562cf22020-07-21 19:28:44 -070024#include <utils/String8.h>
25
Amin Hassaniec7bc112020-10-29 16:47:58 -070026#include "update_engine/aosp/binder_service_android_common.h"
Yifan Hong2562cf22020-07-21 19:28:44 -070027
28using android::binder::Status;
29using android::os::IUpdateEngineStableCallback;
30using android::os::ParcelFileDescriptor;
31using std::string;
32using std::vector;
33using update_engine::UpdateEngineStatus;
34
35namespace chromeos_update_engine {
36
37BinderUpdateEngineAndroidStableService::BinderUpdateEngineAndroidStableService(
38 ServiceDelegateAndroidInterface* service_delegate)
39 : service_delegate_(service_delegate) {}
40
41void BinderUpdateEngineAndroidStableService::SendStatusUpdate(
42 const UpdateEngineStatus& update_engine_status) {
43 last_status_ = static_cast<int>(update_engine_status.status);
44 last_progress_ = update_engine_status.progress;
45 if (callback_) {
46 callback_->onStatusUpdate(last_status_, last_progress_);
47 }
48}
49
50void BinderUpdateEngineAndroidStableService::SendPayloadApplicationComplete(
51 ErrorCode error_code) {
52 if (callback_) {
53 callback_->onPayloadApplicationComplete(static_cast<int>(error_code));
54 }
55}
56
57Status BinderUpdateEngineAndroidStableService::bind(
58 const android::sp<IUpdateEngineStableCallback>& callback,
59 bool* return_value) {
60 // Reject binding if another callback is already bound.
61 if (callback_ != nullptr) {
62 LOG(ERROR) << "Another callback is already bound. Can't bind new callback.";
63 *return_value = false;
64 return Status::ok();
65 }
66
67 // See BinderUpdateEngineAndroidService::bind.
68 if (last_status_ != -1) {
69 auto status = callback->onStatusUpdate(last_status_, last_progress_);
70 if (!status.isOk()) {
71 LOG(ERROR) << "Failed to call onStatusUpdate() from callback: "
72 << status.toString8();
73 *return_value = false;
74 return Status::ok();
75 }
76 }
77
78 callback_ = callback;
79
80 const android::sp<IBinder>& callback_binder =
81 IUpdateEngineStableCallback::asBinder(callback);
82 auto binder_wrapper = android::BinderWrapper::Get();
83 binder_wrapper->RegisterForDeathNotifications(
84 callback_binder,
Kelvin Zhang2f6c25a2023-07-05 12:49:34 -070085 [this, callback = callback_binder.get()]() { UnbindCallback(callback); });
Yifan Hong2562cf22020-07-21 19:28:44 -070086
87 *return_value = true;
88 return Status::ok();
89}
90
91Status BinderUpdateEngineAndroidStableService::unbind(
92 const android::sp<IUpdateEngineStableCallback>& callback,
93 bool* return_value) {
94 const android::sp<IBinder>& callback_binder =
95 IUpdateEngineStableCallback::asBinder(callback);
96 auto binder_wrapper = android::BinderWrapper::Get();
97 binder_wrapper->UnregisterForDeathNotifications(callback_binder);
98
99 *return_value = UnbindCallback(callback_binder.get());
100 return Status::ok();
101}
102
103Status BinderUpdateEngineAndroidStableService::applyPayloadFd(
104 const ParcelFileDescriptor& pfd,
105 int64_t payload_offset,
106 int64_t payload_size,
107 const vector<android::String16>& header_kv_pairs) {
108 vector<string> str_headers = ToVecString(header_kv_pairs);
109
Daniel Zheng92f7d172023-06-22 14:31:37 -0700110 Error error;
Yifan Hong2562cf22020-07-21 19:28:44 -0700111 if (!service_delegate_->ApplyPayload(
112 pfd.get(), payload_offset, payload_size, str_headers, &error)) {
113 return ErrorPtrToStatus(error);
114 }
115 return Status::ok();
116}
117
118bool BinderUpdateEngineAndroidStableService::UnbindCallback(
119 const IBinder* callback) {
120 if (IUpdateEngineStableCallback::asBinder(callback_).get() != callback) {
121 LOG(ERROR) << "Unable to unbind unknown callback.";
122 return false;
123 }
124 callback_ = nullptr;
125 return true;
126}
127
128} // namespace chromeos_update_engine