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