Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 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 | |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 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 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 25 | using android::binder::Status; |
| 26 | using android::os::IUpdateEngineCallback; |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 27 | |
| 28 | namespace { |
| 29 | Status ErrorPtrToStatus(const brillo::ErrorPtr& error) { |
| 30 | return Status::fromServiceSpecificError( |
| 31 | 1, android::String8{error->GetMessage().c_str()}); |
| 32 | } |
| 33 | } // namespace |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 34 | |
| 35 | namespace chromeos_update_engine { |
| 36 | |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 37 | BinderUpdateEngineAndroidService::BinderUpdateEngineAndroidService( |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 38 | ServiceDelegateAndroidInterface* service_delegate) |
| 39 | : service_delegate_(service_delegate) { |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void BinderUpdateEngineAndroidService::SendStatusUpdate( |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 43 | int64_t /* last_checked_time */, |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 44 | double progress, |
| 45 | update_engine::UpdateStatus status, |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 46 | const std::string& /* new_version */, |
| 47 | int64_t /* new_size */) { |
Alex Deymo | 0e061ae | 2016-02-09 17:49:03 -0800 | [diff] [blame] | 48 | last_status_ = static_cast<int>(status); |
| 49 | last_progress_ = progress; |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 50 | for (auto& callback : callbacks_) { |
Alex Deymo | 0e061ae | 2016-02-09 17:49:03 -0800 | [diff] [blame] | 51 | callback->onStatusUpdate(last_status_, last_progress_); |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | void BinderUpdateEngineAndroidService::SendPayloadApplicationComplete( |
| 56 | ErrorCode error_code) { |
| 57 | for (auto& callback : callbacks_) { |
| 58 | callback->onPayloadApplicationComplete(static_cast<int>(error_code)); |
| 59 | } |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 62 | Status BinderUpdateEngineAndroidService::bind( |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 63 | 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 Deymo | 0e061ae | 2016-02-09 17:49:03 -0800 | [diff] [blame] | 73 | // 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 Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 79 | *return_value = true; |
| 80 | return Status::ok(); |
| 81 | } |
| 82 | |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 83 | Status BinderUpdateEngineAndroidService::applyPayload( |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 84 | const android::String16& url, |
Alex Deymo | 95b8f24 | 2016-01-28 16:06:57 -0800 | [diff] [blame] | 85 | int64_t payload_offset, |
| 86 | int64_t payload_size, |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 87 | 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 Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 100 | return Status::ok(); |
| 101 | } |
| 102 | |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 103 | Status BinderUpdateEngineAndroidService::suspend() { |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 104 | brillo::ErrorPtr error; |
| 105 | if (!service_delegate_->SuspendUpdate(&error)) |
| 106 | return ErrorPtrToStatus(error); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 107 | return Status::ok(); |
| 108 | } |
| 109 | |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 110 | Status BinderUpdateEngineAndroidService::resume() { |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 111 | brillo::ErrorPtr error; |
| 112 | if (!service_delegate_->ResumeUpdate(&error)) |
| 113 | return ErrorPtrToStatus(error); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 114 | return Status::ok(); |
| 115 | } |
| 116 | |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 117 | Status BinderUpdateEngineAndroidService::cancel() { |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 118 | brillo::ErrorPtr error; |
| 119 | if (!service_delegate_->CancelUpdate(&error)) |
| 120 | return ErrorPtrToStatus(error); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 121 | return Status::ok(); |
| 122 | } |
| 123 | |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 124 | Status BinderUpdateEngineAndroidService::resetStatus() { |
| 125 | brillo::ErrorPtr error; |
| 126 | if (!service_delegate_->ResetStatus(&error)) |
| 127 | return ErrorPtrToStatus(error); |
| 128 | return Status::ok(); |
| 129 | } |
| 130 | |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 131 | void 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 Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 146 | } // namespace chromeos_update_engine |