• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
25 using android::binder::Status;
26 using android::os::IUpdateEngineCallback;
27 using update_engine::UpdateEngineStatus;
28 
29 namespace {
ErrorPtrToStatus(const brillo::ErrorPtr & error)30 Status ErrorPtrToStatus(const brillo::ErrorPtr& error) {
31   return Status::fromServiceSpecificError(
32       1, android::String8{error->GetMessage().c_str()});
33 }
34 }  // namespace
35 
36 namespace chromeos_update_engine {
37 
BinderUpdateEngineAndroidService(ServiceDelegateAndroidInterface * service_delegate)38 BinderUpdateEngineAndroidService::BinderUpdateEngineAndroidService(
39     ServiceDelegateAndroidInterface* service_delegate)
40     : service_delegate_(service_delegate) {}
41 
SendStatusUpdate(const UpdateEngineStatus & update_engine_status)42 void BinderUpdateEngineAndroidService::SendStatusUpdate(
43     const UpdateEngineStatus& update_engine_status) {
44   last_status_ = static_cast<int>(update_engine_status.status);
45   last_progress_ = update_engine_status.progress;
46   for (auto& callback : callbacks_) {
47     callback->onStatusUpdate(last_status_, last_progress_);
48   }
49 }
50 
SendPayloadApplicationComplete(ErrorCode error_code)51 void BinderUpdateEngineAndroidService::SendPayloadApplicationComplete(
52     ErrorCode error_code) {
53   for (auto& callback : callbacks_) {
54     callback->onPayloadApplicationComplete(static_cast<int>(error_code));
55   }
56 }
57 
bind(const android::sp<IUpdateEngineCallback> & callback,bool * return_value)58 Status BinderUpdateEngineAndroidService::bind(
59     const android::sp<IUpdateEngineCallback>& callback, bool* return_value) {
60   callbacks_.emplace_back(callback);
61 
62   const android::sp<IBinder>& callback_binder =
63       IUpdateEngineCallback::asBinder(callback);
64   auto binder_wrapper = android::BinderWrapper::Get();
65   binder_wrapper->RegisterForDeathNotifications(
66       callback_binder,
67       base::Bind(
68           base::IgnoreResult(&BinderUpdateEngineAndroidService::UnbindCallback),
69           base::Unretained(this),
70           base::Unretained(callback_binder.get())));
71 
72   // Send an status update on connection (except when no update sent so far),
73   // since the status update is oneway and we don't need to wait for the
74   // response.
75   if (last_status_ != -1)
76     callback->onStatusUpdate(last_status_, last_progress_);
77 
78   *return_value = true;
79   return Status::ok();
80 }
81 
unbind(const android::sp<IUpdateEngineCallback> & callback,bool * return_value)82 Status BinderUpdateEngineAndroidService::unbind(
83     const android::sp<IUpdateEngineCallback>& callback, bool* return_value) {
84   const android::sp<IBinder>& callback_binder =
85       IUpdateEngineCallback::asBinder(callback);
86   auto binder_wrapper = android::BinderWrapper::Get();
87   binder_wrapper->UnregisterForDeathNotifications(callback_binder);
88 
89   *return_value = UnbindCallback(callback_binder.get());
90   return Status::ok();
91 }
92 
applyPayload(const android::String16 & url,int64_t payload_offset,int64_t payload_size,const std::vector<android::String16> & header_kv_pairs)93 Status BinderUpdateEngineAndroidService::applyPayload(
94     const android::String16& url,
95     int64_t payload_offset,
96     int64_t payload_size,
97     const std::vector<android::String16>& header_kv_pairs) {
98   const std::string payload_url{android::String8{url}.string()};
99   std::vector<std::string> str_headers;
100   str_headers.reserve(header_kv_pairs.size());
101   for (const auto& header : header_kv_pairs) {
102     str_headers.emplace_back(android::String8{header}.string());
103   }
104 
105   brillo::ErrorPtr error;
106   if (!service_delegate_->ApplyPayload(
107           payload_url, payload_offset, payload_size, str_headers, &error)) {
108     return ErrorPtrToStatus(error);
109   }
110   return Status::ok();
111 }
112 
suspend()113 Status BinderUpdateEngineAndroidService::suspend() {
114   brillo::ErrorPtr error;
115   if (!service_delegate_->SuspendUpdate(&error))
116     return ErrorPtrToStatus(error);
117   return Status::ok();
118 }
119 
resume()120 Status BinderUpdateEngineAndroidService::resume() {
121   brillo::ErrorPtr error;
122   if (!service_delegate_->ResumeUpdate(&error))
123     return ErrorPtrToStatus(error);
124   return Status::ok();
125 }
126 
cancel()127 Status BinderUpdateEngineAndroidService::cancel() {
128   brillo::ErrorPtr error;
129   if (!service_delegate_->CancelUpdate(&error))
130     return ErrorPtrToStatus(error);
131   return Status::ok();
132 }
133 
resetStatus()134 Status BinderUpdateEngineAndroidService::resetStatus() {
135   brillo::ErrorPtr error;
136   if (!service_delegate_->ResetStatus(&error))
137     return ErrorPtrToStatus(error);
138   return Status::ok();
139 }
140 
verifyPayloadApplicable(const android::String16 & metadata_filename,bool * return_value)141 Status BinderUpdateEngineAndroidService::verifyPayloadApplicable(
142     const android::String16& metadata_filename, bool* return_value) {
143   const std::string payload_metadata{
144       android::String8{metadata_filename}.string()};
145   LOG(INFO) << "Received a request of verifying payload metadata in "
146             << payload_metadata << ".";
147   brillo::ErrorPtr error;
148   *return_value =
149       service_delegate_->VerifyPayloadApplicable(payload_metadata, &error);
150   if (error != nullptr)
151     return ErrorPtrToStatus(error);
152   return Status::ok();
153 }
154 
UnbindCallback(const IBinder * callback)155 bool BinderUpdateEngineAndroidService::UnbindCallback(const IBinder* callback) {
156   auto it = std::find_if(
157       callbacks_.begin(),
158       callbacks_.end(),
159       [&callback](const android::sp<IUpdateEngineCallback>& elem) {
160         return IUpdateEngineCallback::asBinder(elem).get() == callback;
161       });
162   if (it == callbacks_.end()) {
163     LOG(ERROR) << "Unable to unbind unknown callback.";
164     return false;
165   }
166   callbacks_.erase(it);
167   return true;
168 }
169 
170 }  // namespace chromeos_update_engine
171