Christopher Wiley | 16daa08 | 2015-10-01 17:18:40 -0700 | [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/client_library/client_impl.h" |
| 18 | |
| 19 | #include <dbus/bus.h> |
| 20 | #include <update_engine/dbus-constants.h> |
| 21 | |
| 22 | #include "update_engine/update_status_utils.h" |
| 23 | |
| 24 | using chromeos_update_engine::StringToUpdateStatus; |
| 25 | using dbus::Bus; |
| 26 | using org::chromium::UpdateEngineInterfaceProxy; |
| 27 | using std::string; |
| 28 | |
| 29 | namespace update_engine { |
| 30 | namespace internal { |
| 31 | |
| 32 | UpdateEngineClientImpl::UpdateEngineClientImpl() { |
| 33 | Bus::Options options; |
| 34 | options.bus_type = Bus::SYSTEM; |
| 35 | scoped_refptr<Bus> bus{new Bus{options}}; |
| 36 | proxy_.reset(new UpdateEngineInterfaceProxy{bus}); |
| 37 | } |
| 38 | |
| 39 | bool UpdateEngineClientImpl::AttemptUpdate(const string& in_app_version, |
| 40 | const string& in_omaha_url, |
| 41 | bool at_user_request) { |
| 42 | return proxy_->AttemptUpdateWithFlags( |
Casey Dahlin | 97c8705 | 2016-01-06 14:33:55 -0800 | [diff] [blame^] | 43 | in_app_version, in_omaha_url, |
| 44 | (at_user_request) ? 0 : kAttemptUpdateFlagNonInteractive, nullptr); |
Christopher Wiley | 16daa08 | 2015-10-01 17:18:40 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | bool UpdateEngineClientImpl::GetStatus(int64_t* out_last_checked_time, |
| 48 | double* out_progress, |
| 49 | UpdateStatus* out_update_status, |
| 50 | string* out_new_version, |
| 51 | int64_t* out_new_size) { |
| 52 | string status_as_string; |
Casey Dahlin | 97c8705 | 2016-01-06 14:33:55 -0800 | [diff] [blame^] | 53 | const bool success = |
| 54 | proxy_->GetStatus(out_last_checked_time, out_progress, &status_as_string, |
| 55 | out_new_version, out_new_size, nullptr); |
Christopher Wiley | 16daa08 | 2015-10-01 17:18:40 -0700 | [diff] [blame] | 56 | if (!success) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | return StringToUpdateStatus(status_as_string, out_update_status); |
| 61 | } |
| 62 | |
Casey Dahlin | ef36113 | 2015-12-17 13:02:37 -0800 | [diff] [blame] | 63 | bool UpdateEngineClientImpl::SetUpdateOverCellularPermission(bool allowed) { |
| 64 | return proxy_->SetUpdateOverCellularPermission(allowed, nullptr); |
| 65 | } |
| 66 | |
Casey Dahlin | 97c8705 | 2016-01-06 14:33:55 -0800 | [diff] [blame^] | 67 | bool UpdateEngineClientImpl::GetUpdateOverCellularPermission(bool* allowed) { |
Casey Dahlin | ef36113 | 2015-12-17 13:02:37 -0800 | [diff] [blame] | 68 | return proxy_->GetUpdateOverCellularPermission(allowed, nullptr); |
| 69 | } |
| 70 | |
| 71 | bool UpdateEngineClientImpl::SetP2PUpdatePermission(bool enabled) { |
| 72 | return proxy_->SetP2PUpdatePermission(enabled, nullptr); |
| 73 | } |
| 74 | |
| 75 | bool UpdateEngineClientImpl::GetP2PUpdatePermission(bool* enabled) { |
| 76 | return proxy_->GetP2PUpdatePermission(enabled, nullptr); |
| 77 | } |
| 78 | |
| 79 | bool UpdateEngineClientImpl::Rollback(bool powerwash) { |
| 80 | return proxy_->AttemptRollback(powerwash, nullptr); |
| 81 | } |
| 82 | |
| 83 | bool UpdateEngineClientImpl::GetRollbackPartition(string* rollback_partition) { |
| 84 | return proxy_->GetRollbackPartition(rollback_partition, nullptr); |
| 85 | } |
| 86 | |
| 87 | bool UpdateEngineClientImpl::GetPrevVersion(string* prev_version) { |
| 88 | return proxy_->GetPrevVersion(prev_version, nullptr); |
| 89 | } |
| 90 | |
| 91 | void UpdateEngineClientImpl::RebootIfNeeded() { |
| 92 | bool ret = proxy_->RebootIfNeeded(nullptr); |
| 93 | if (!ret) { |
| 94 | // Reboot error code doesn't necessarily mean that a reboot |
| 95 | // failed. For example, D-Bus may be shutdown before we receive the |
| 96 | // result. |
| 97 | LOG(INFO) << "RebootIfNeeded() failure ignored."; |
| 98 | } |
| 99 | } |
| 100 | |
Casey Dahlin | e844c1a | 2015-12-16 14:30:58 -0800 | [diff] [blame] | 101 | bool UpdateEngineClientImpl::ResetStatus() { |
| 102 | return proxy_->ResetStatus(nullptr); |
| 103 | } |
| 104 | |
Casey Dahlin | 97c8705 | 2016-01-06 14:33:55 -0800 | [diff] [blame^] | 105 | void UpdateEngineClientImpl::StatusUpdateHandlerRegistered( |
| 106 | StatusUpdateHandler* handler, const std::string& interface, |
| 107 | const std::string& signal_name, bool success) { |
| 108 | if (!success) { |
| 109 | handler->IPCError("Could not connect to" + signal_name); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | int64_t last_checked_time; |
| 114 | double progress; |
| 115 | UpdateStatus update_status; |
| 116 | string new_version; |
| 117 | int64_t new_size; |
| 118 | |
| 119 | if (GetStatus(&last_checked_time, &progress, &update_status, &new_version, |
| 120 | &new_size)) { |
| 121 | handler->HandleStatusUpdate(last_checked_time, progress, update_status, |
| 122 | new_version, new_size); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | handler->IPCError("Could not query current status"); |
| 127 | } |
| 128 | |
| 129 | void UpdateEngineClientImpl::RunStatusUpdateHandler( |
| 130 | StatusUpdateHandler* h, int64_t last_checked_time, double progress, |
| 131 | const std::string& current_operation, const std::string& new_version, |
| 132 | int64_t new_size) { |
| 133 | UpdateStatus status; |
| 134 | StringToUpdateStatus(current_operation, &status); |
| 135 | |
| 136 | h->HandleStatusUpdate(last_checked_time, progress, status, new_version, |
| 137 | new_size); |
| 138 | } |
| 139 | |
| 140 | void UpdateEngineClientImpl::RegisterStatusUpdateHandler( |
| 141 | StatusUpdateHandler* handler) { |
| 142 | proxy_->RegisterStatusUpdateSignalHandler( |
| 143 | base::Bind(&UpdateEngineClientImpl::RunStatusUpdateHandler, |
| 144 | base::Unretained(this), base::Unretained(handler)), |
| 145 | base::Bind(&UpdateEngineClientImpl::StatusUpdateHandlerRegistered, |
| 146 | base::Unretained(this), base::Unretained(handler))); |
| 147 | } |
| 148 | |
Casey Dahlin | 87ab88e | 2015-12-16 17:58:05 -0800 | [diff] [blame] | 149 | bool UpdateEngineClientImpl::SetTargetChannel(const string& in_target_channel, |
| 150 | bool allow_powerwash) { |
Casey Dahlin | 97c8705 | 2016-01-06 14:33:55 -0800 | [diff] [blame^] | 151 | return proxy_->SetChannel(in_target_channel, allow_powerwash, nullptr); |
Christopher Wiley | 16daa08 | 2015-10-01 17:18:40 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | bool UpdateEngineClientImpl::GetTargetChannel(string* out_channel) { |
Casey Dahlin | 97c8705 | 2016-01-06 14:33:55 -0800 | [diff] [blame^] | 155 | return proxy_->GetChannel(false, // Get the target channel. |
| 156 | out_channel, nullptr); |
Christopher Wiley | 16daa08 | 2015-10-01 17:18:40 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | bool UpdateEngineClientImpl::GetChannel(string* out_channel) { |
Casey Dahlin | 97c8705 | 2016-01-06 14:33:55 -0800 | [diff] [blame^] | 160 | return proxy_->GetChannel(true, // Get the current channel. |
| 161 | out_channel, nullptr); |
Christopher Wiley | 16daa08 | 2015-10-01 17:18:40 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | } // namespace internal |
| 165 | } // namespace update_engine |