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 */) { |
| 48 | for (auto& callback : callbacks_) { |
| 49 | callback->onStatusUpdate(static_cast<int>(status), progress); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | void BinderUpdateEngineAndroidService::SendPayloadApplicationComplete( |
| 54 | ErrorCode error_code) { |
| 55 | for (auto& callback : callbacks_) { |
| 56 | callback->onPayloadApplicationComplete(static_cast<int>(error_code)); |
| 57 | } |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 60 | Status BinderUpdateEngineAndroidService::bind( |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 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 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 71 | *return_value = true; |
| 72 | return Status::ok(); |
| 73 | } |
| 74 | |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 75 | Status BinderUpdateEngineAndroidService::applyPayload( |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 76 | const android::String16& url, |
Alex Deymo | 95b8f24 | 2016-01-28 16:06:57 -0800 | [diff] [blame] | 77 | int64_t payload_offset, |
| 78 | int64_t payload_size, |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 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 | } |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 92 | return Status::ok(); |
| 93 | } |
| 94 | |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 95 | Status BinderUpdateEngineAndroidService::suspend() { |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 96 | brillo::ErrorPtr error; |
| 97 | if (!service_delegate_->SuspendUpdate(&error)) |
| 98 | return ErrorPtrToStatus(error); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 99 | return Status::ok(); |
| 100 | } |
| 101 | |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 102 | Status BinderUpdateEngineAndroidService::resume() { |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 103 | brillo::ErrorPtr error; |
| 104 | if (!service_delegate_->ResumeUpdate(&error)) |
| 105 | return ErrorPtrToStatus(error); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 106 | return Status::ok(); |
| 107 | } |
| 108 | |
Alex Deymo | e97b39c | 2016-01-20 13:22:17 -0800 | [diff] [blame] | 109 | Status BinderUpdateEngineAndroidService::cancel() { |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 110 | brillo::ErrorPtr error; |
| 111 | if (!service_delegate_->CancelUpdate(&error)) |
| 112 | return ErrorPtrToStatus(error); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 113 | return Status::ok(); |
| 114 | } |
| 115 | |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 116 | void 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 Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 131 | } // namespace chromeos_update_engine |