Fix compilation error triggered with -Werror=sign-compare
[chromium-blink-merge.git] / chromeos / dbus / update_engine_client.cc
blobfd1bc2a251e990d3f876ba692f187354d1d94c3f
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chromeos/dbus/update_engine_client.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/command_line.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/string_util.h"
12 #include "chromeos/chromeos_switches.h"
13 #include "dbus/bus.h"
14 #include "dbus/message.h"
15 #include "dbus/object_path.h"
16 #include "dbus/object_proxy.h"
17 #include "third_party/cros_system_api/dbus/service_constants.h"
19 namespace chromeos {
21 namespace {
23 const char kReleaseChannelDev[] = "dev-channel";
24 const char kReleaseChannelBeta[] = "beta-channel";
25 const char kReleaseChannelStable[] = "stable-channel";
27 // Delay between successive state transitions during AU.
28 const int kStateTransitionDefaultDelayMs = 3000;
30 // Delay between successive notifications about downloading progress
31 // during fake AU.
32 const int kStateTransitionDownloadingDelayMs = 250;
34 // Size of parts of a "new" image which are downloaded each
35 // |kStateTransitionDownloadingDelayMs| during fake AU.
36 const int64_t kDownloadSizeDelta = 1 << 19;
38 // Returns UPDATE_STATUS_ERROR on error.
39 UpdateEngineClient::UpdateStatusOperation UpdateStatusFromString(
40 const std::string& str) {
41 VLOG(1) << "UpdateStatusFromString got " << str << " as input.";
42 if (str == update_engine::kUpdateStatusIdle)
43 return UpdateEngineClient::UPDATE_STATUS_IDLE;
44 if (str == update_engine::kUpdateStatusCheckingForUpdate)
45 return UpdateEngineClient::UPDATE_STATUS_CHECKING_FOR_UPDATE;
46 if (str == update_engine::kUpdateStatusUpdateAvailable)
47 return UpdateEngineClient::UPDATE_STATUS_UPDATE_AVAILABLE;
48 if (str == update_engine::kUpdateStatusDownloading)
49 return UpdateEngineClient::UPDATE_STATUS_DOWNLOADING;
50 if (str == update_engine::kUpdateStatusVerifying)
51 return UpdateEngineClient::UPDATE_STATUS_VERIFYING;
52 if (str == update_engine::kUpdateStatusFinalizing)
53 return UpdateEngineClient::UPDATE_STATUS_FINALIZING;
54 if (str == update_engine::kUpdateStatusUpdatedNeedReboot)
55 return UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT;
56 if (str == update_engine::kUpdateStatusReportingErrorEvent)
57 return UpdateEngineClient::UPDATE_STATUS_REPORTING_ERROR_EVENT;
58 if (str == update_engine::kUpdateStatusAttemptingRollback)
59 return UpdateEngineClient::UPDATE_STATUS_ATTEMPTING_ROLLBACK;
60 return UpdateEngineClient::UPDATE_STATUS_ERROR;
63 // Used in UpdateEngineClient::EmptyUpdateCheckCallback().
64 void EmptyUpdateCheckCallbackBody(
65 UpdateEngineClient::UpdateCheckResult unused_result) {
68 bool IsValidChannel(const std::string& channel) {
69 return channel == kReleaseChannelDev ||
70 channel == kReleaseChannelBeta ||
71 channel == kReleaseChannelStable;
74 } // namespace
76 // The UpdateEngineClient implementation used in production.
77 class UpdateEngineClientImpl : public UpdateEngineClient {
78 public:
79 UpdateEngineClientImpl()
80 : update_engine_proxy_(NULL), last_status_(), weak_ptr_factory_(this) {}
82 virtual ~UpdateEngineClientImpl() {
85 // UpdateEngineClient implementation:
86 virtual void AddObserver(Observer* observer) OVERRIDE {
87 observers_.AddObserver(observer);
90 virtual void RemoveObserver(Observer* observer) OVERRIDE {
91 observers_.RemoveObserver(observer);
94 virtual bool HasObserver(Observer* observer) OVERRIDE {
95 return observers_.HasObserver(observer);
98 virtual void RequestUpdateCheck(
99 const UpdateCheckCallback& callback) OVERRIDE {
100 dbus::MethodCall method_call(
101 update_engine::kUpdateEngineInterface,
102 update_engine::kAttemptUpdate);
103 dbus::MessageWriter writer(&method_call);
104 writer.AppendString(""); // Unused.
105 writer.AppendString(""); // Unused.
107 VLOG(1) << "Requesting an update check";
108 update_engine_proxy_->CallMethod(
109 &method_call,
110 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
111 base::Bind(&UpdateEngineClientImpl::OnRequestUpdateCheck,
112 weak_ptr_factory_.GetWeakPtr(),
113 callback));
116 virtual void RebootAfterUpdate() OVERRIDE {
117 dbus::MethodCall method_call(
118 update_engine::kUpdateEngineInterface,
119 update_engine::kRebootIfNeeded);
121 VLOG(1) << "Requesting a reboot";
122 update_engine_proxy_->CallMethod(
123 &method_call,
124 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
125 base::Bind(&UpdateEngineClientImpl::OnRebootAfterUpdate,
126 weak_ptr_factory_.GetWeakPtr()));
129 virtual void Rollback() OVERRIDE {
130 VLOG(1) << "Requesting a rollback";
131 dbus::MethodCall method_call(
132 update_engine::kUpdateEngineInterface,
133 update_engine::kAttemptRollback);
134 dbus::MessageWriter writer(&method_call);
135 writer.AppendBool(true /* powerwash */);
137 update_engine_proxy_->CallMethod(
138 &method_call,
139 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
140 base::Bind(&UpdateEngineClientImpl::OnRollback,
141 weak_ptr_factory_.GetWeakPtr()));
145 virtual void CanRollbackCheck(
146 const RollbackCheckCallback& callback) OVERRIDE {
147 dbus::MethodCall method_call(
148 update_engine::kUpdateEngineInterface,
149 update_engine::kCanRollback);
151 VLOG(1) << "Requesting to get rollback availability status";
152 update_engine_proxy_->CallMethod(
153 &method_call,
154 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
155 base::Bind(&UpdateEngineClientImpl::OnCanRollbackCheck,
156 weak_ptr_factory_.GetWeakPtr(),
157 callback));
160 virtual Status GetLastStatus() OVERRIDE {
161 return last_status_;
164 virtual void SetChannel(const std::string& target_channel,
165 bool is_powerwash_allowed) OVERRIDE {
166 if (!IsValidChannel(target_channel)) {
167 LOG(ERROR) << "Invalid channel name: " << target_channel;
168 return;
171 dbus::MethodCall method_call(
172 update_engine::kUpdateEngineInterface,
173 update_engine::kSetChannel);
174 dbus::MessageWriter writer(&method_call);
175 writer.AppendString(target_channel);
176 writer.AppendBool(is_powerwash_allowed);
178 VLOG(1) << "Requesting to set channel: "
179 << "target_channel=" << target_channel << ", "
180 << "is_powerwash_allowed=" << is_powerwash_allowed;
181 update_engine_proxy_->CallMethod(
182 &method_call,
183 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
184 base::Bind(&UpdateEngineClientImpl::OnSetChannel,
185 weak_ptr_factory_.GetWeakPtr()));
188 virtual void GetChannel(bool get_current_channel,
189 const GetChannelCallback& callback) OVERRIDE {
190 dbus::MethodCall method_call(
191 update_engine::kUpdateEngineInterface,
192 update_engine::kGetChannel);
193 dbus::MessageWriter writer(&method_call);
194 writer.AppendBool(get_current_channel);
196 VLOG(1) << "Requesting to get channel, get_current_channel="
197 << get_current_channel;
198 update_engine_proxy_->CallMethod(
199 &method_call,
200 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
201 base::Bind(&UpdateEngineClientImpl::OnGetChannel,
202 weak_ptr_factory_.GetWeakPtr(),
203 callback));
206 protected:
207 virtual void Init(dbus::Bus* bus) OVERRIDE {
208 update_engine_proxy_ = bus->GetObjectProxy(
209 update_engine::kUpdateEngineServiceName,
210 dbus::ObjectPath(update_engine::kUpdateEngineServicePath));
212 // Monitor the D-Bus signal for brightness changes. Only the power
213 // manager knows the actual brightness level. We don't cache the
214 // brightness level in Chrome as it will make things less reliable.
215 update_engine_proxy_->ConnectToSignal(
216 update_engine::kUpdateEngineInterface,
217 update_engine::kStatusUpdate,
218 base::Bind(&UpdateEngineClientImpl::StatusUpdateReceived,
219 weak_ptr_factory_.GetWeakPtr()),
220 base::Bind(&UpdateEngineClientImpl::StatusUpdateConnected,
221 weak_ptr_factory_.GetWeakPtr()));
223 // Get update engine status for the initial status. Update engine won't
224 // send StatusUpdate signal unless there is a status change. If chrome
225 // crashes after UPDATE_STATUS_UPDATED_NEED_REBOOT status is set,
226 // restarted chrome would not get this status. See crbug.com/154104.
227 GetUpdateEngineStatus();
230 private:
231 void GetUpdateEngineStatus() {
232 dbus::MethodCall method_call(
233 update_engine::kUpdateEngineInterface,
234 update_engine::kGetStatus);
235 update_engine_proxy_->CallMethodWithErrorCallback(
236 &method_call,
237 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
238 base::Bind(&UpdateEngineClientImpl::OnGetStatus,
239 weak_ptr_factory_.GetWeakPtr()),
240 base::Bind(&UpdateEngineClientImpl::OnGetStatusError,
241 weak_ptr_factory_.GetWeakPtr()));
244 // Called when a response for RequestUpdateCheck() is received.
245 void OnRequestUpdateCheck(const UpdateCheckCallback& callback,
246 dbus::Response* response) {
247 if (!response) {
248 LOG(ERROR) << "Failed to request update check";
249 callback.Run(UPDATE_RESULT_FAILED);
250 return;
252 callback.Run(UPDATE_RESULT_SUCCESS);
255 // Called when a response for RebootAfterUpdate() is received.
256 void OnRebootAfterUpdate(dbus::Response* response) {
257 if (!response) {
258 LOG(ERROR) << "Failed to request rebooting after update";
259 return;
263 // Called when a response for Rollback() is received.
264 void OnRollback(dbus::Response* response) {
265 if (!response) {
266 LOG(ERROR) << "Failed to rollback";
267 return;
271 // Called when a response for CanRollbackCheck() is received.
272 void OnCanRollbackCheck(const RollbackCheckCallback& callback,
273 dbus::Response* response) {
274 if (!response) {
275 LOG(ERROR) << "Failed to request rollback availability status";
276 callback.Run(false);
277 return;
279 dbus::MessageReader reader(response);
280 bool can_rollback;
281 if (!reader.PopBool(&can_rollback)) {
282 LOG(ERROR) << "Incorrect response: " << response->ToString();
283 callback.Run(false);
284 return;
286 VLOG(1) << "Rollback availability status received: " << can_rollback;
287 callback.Run(can_rollback);
290 // Called when a response for GetStatus is received.
291 void OnGetStatus(dbus::Response* response) {
292 if (!response) {
293 LOG(ERROR) << "Failed to get response for GetStatus request.";
294 return;
297 dbus::MessageReader reader(response);
298 std::string current_operation;
299 Status status;
300 if (!(reader.PopInt64(&status.last_checked_time) &&
301 reader.PopDouble(&status.download_progress) &&
302 reader.PopString(&current_operation) &&
303 reader.PopString(&status.new_version) &&
304 reader.PopInt64(&status.new_size))) {
305 LOG(ERROR) << "GetStatus had incorrect response: "
306 << response->ToString();
307 return;
309 status.status = UpdateStatusFromString(current_operation);
310 last_status_ = status;
311 FOR_EACH_OBSERVER(Observer, observers_, UpdateStatusChanged(status));
314 // Called when GetStatus call failed.
315 void OnGetStatusError(dbus::ErrorResponse* error) {
316 LOG(ERROR) << "GetStatus request failed with error: "
317 << (error ? error->ToString() : "");
320 // Called when a response for SetReleaseChannel() is received.
321 void OnSetChannel(dbus::Response* response) {
322 if (!response) {
323 LOG(ERROR) << "Failed to request setting channel";
324 return;
326 VLOG(1) << "Succeeded to set channel";
329 // Called when a response for GetChannel() is received.
330 void OnGetChannel(const GetChannelCallback& callback,
331 dbus::Response* response) {
332 if (!response) {
333 LOG(ERROR) << "Failed to request getting channel";
334 callback.Run("");
335 return;
337 dbus::MessageReader reader(response);
338 std::string channel;
339 if (!reader.PopString(&channel)) {
340 LOG(ERROR) << "Incorrect response: " << response->ToString();
341 callback.Run("");
342 return;
344 VLOG(1) << "The channel received: " << channel;
345 callback.Run(channel);
348 // Called when a status update signal is received.
349 void StatusUpdateReceived(dbus::Signal* signal) {
350 VLOG(1) << "Status update signal received: " << signal->ToString();
351 dbus::MessageReader reader(signal);
352 int64 last_checked_time = 0;
353 double progress = 0.0;
354 std::string current_operation;
355 std::string new_version;
356 int64_t new_size = 0;
357 if (!(reader.PopInt64(&last_checked_time) &&
358 reader.PopDouble(&progress) &&
359 reader.PopString(&current_operation) &&
360 reader.PopString(&new_version) &&
361 reader.PopInt64(&new_size))) {
362 LOG(ERROR) << "Status changed signal had incorrect parameters: "
363 << signal->ToString();
364 return;
366 Status status;
367 status.last_checked_time = last_checked_time;
368 status.download_progress = progress;
369 status.status = UpdateStatusFromString(current_operation);
370 status.new_version = new_version;
371 status.new_size = new_size;
373 last_status_ = status;
374 FOR_EACH_OBSERVER(Observer, observers_, UpdateStatusChanged(status));
377 // Called when the status update signal is initially connected.
378 void StatusUpdateConnected(const std::string& interface_name,
379 const std::string& signal_name,
380 bool success) {
381 LOG_IF(WARNING, !success)
382 << "Failed to connect to status updated signal.";
385 dbus::ObjectProxy* update_engine_proxy_;
386 ObserverList<Observer> observers_;
387 Status last_status_;
389 // Note: This should remain the last member so it'll be destroyed and
390 // invalidate its weak pointers before any other members are destroyed.
391 base::WeakPtrFactory<UpdateEngineClientImpl> weak_ptr_factory_;
393 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClientImpl);
396 // The UpdateEngineClient implementation used on Linux desktop,
397 // which does nothing.
398 class UpdateEngineClientStubImpl : public UpdateEngineClient {
399 // UpdateEngineClient implementation:
400 virtual void Init(dbus::Bus* bus) OVERRIDE {}
401 virtual void AddObserver(Observer* observer) OVERRIDE {}
402 virtual void RemoveObserver(Observer* observer) OVERRIDE {}
403 virtual bool HasObserver(Observer* observer) OVERRIDE { return false; }
405 virtual void RequestUpdateCheck(
406 const UpdateCheckCallback& callback) OVERRIDE {
407 callback.Run(UPDATE_RESULT_NOTIMPLEMENTED);
409 virtual void RebootAfterUpdate() OVERRIDE {}
410 virtual void Rollback() OVERRIDE {}
411 virtual void CanRollbackCheck(
412 const RollbackCheckCallback& callback) OVERRIDE {
413 callback.Run(true);
415 virtual Status GetLastStatus() OVERRIDE { return Status(); }
416 virtual void SetChannel(const std::string& target_channel,
417 bool is_powerwash_allowed) OVERRIDE {
418 VLOG(1) << "Requesting to set channel: "
419 << "target_channel=" << target_channel << ", "
420 << "is_powerwash_allowed=" << is_powerwash_allowed;
422 virtual void GetChannel(bool get_current_channel,
423 const GetChannelCallback& callback) OVERRIDE {
424 VLOG(1) << "Requesting to get channel, get_current_channel="
425 << get_current_channel;
426 callback.Run(kReleaseChannelBeta);
430 // The UpdateEngineClient implementation used on Linux desktop, which
431 // tries to emulate real update engine client.
432 class UpdateEngineClientFakeImpl : public UpdateEngineClientStubImpl {
433 public:
434 UpdateEngineClientFakeImpl() : weak_factory_(this) {
437 virtual ~UpdateEngineClientFakeImpl() {
440 // UpdateEngineClient implementation:
441 virtual void AddObserver(Observer* observer) OVERRIDE {
442 if (observer)
443 observers_.AddObserver(observer);
446 virtual void RemoveObserver(Observer* observer) OVERRIDE {
447 if (observer)
448 observers_.RemoveObserver(observer);
451 virtual bool HasObserver(Observer* observer) OVERRIDE {
452 return observers_.HasObserver(observer);
455 virtual void RequestUpdateCheck(
456 const UpdateCheckCallback& callback) OVERRIDE {
457 if (last_status_.status != UPDATE_STATUS_IDLE) {
458 callback.Run(UPDATE_RESULT_FAILED);
459 return;
461 callback.Run(UPDATE_RESULT_SUCCESS);
462 last_status_.status = UPDATE_STATUS_CHECKING_FOR_UPDATE;
463 last_status_.download_progress = 0.0;
464 last_status_.last_checked_time = 0;
465 last_status_.new_size = 0;
466 base::MessageLoop::current()->PostDelayedTask(
467 FROM_HERE,
468 base::Bind(&UpdateEngineClientFakeImpl::StateTransition,
469 weak_factory_.GetWeakPtr()),
470 base::TimeDelta::FromMilliseconds(kStateTransitionDefaultDelayMs));
473 virtual Status GetLastStatus() OVERRIDE { return last_status_; }
475 private:
476 void StateTransition() {
477 UpdateStatusOperation next_status = UPDATE_STATUS_ERROR;
478 int delay_ms = kStateTransitionDefaultDelayMs;
479 switch (last_status_.status) {
480 case UPDATE_STATUS_ERROR:
481 case UPDATE_STATUS_IDLE:
482 case UPDATE_STATUS_UPDATED_NEED_REBOOT:
483 case UPDATE_STATUS_REPORTING_ERROR_EVENT:
484 case UPDATE_STATUS_ATTEMPTING_ROLLBACK:
485 return;
486 case UPDATE_STATUS_CHECKING_FOR_UPDATE:
487 next_status = UPDATE_STATUS_UPDATE_AVAILABLE;
488 break;
489 case UPDATE_STATUS_UPDATE_AVAILABLE:
490 next_status = UPDATE_STATUS_DOWNLOADING;
491 break;
492 case UPDATE_STATUS_DOWNLOADING:
493 if (last_status_.download_progress >= 1.0) {
494 next_status = UPDATE_STATUS_VERIFYING;
495 } else {
496 next_status = UPDATE_STATUS_DOWNLOADING;
497 last_status_.download_progress += 0.01;
498 last_status_.new_size = kDownloadSizeDelta;
499 delay_ms = kStateTransitionDownloadingDelayMs;
501 break;
502 case UPDATE_STATUS_VERIFYING:
503 next_status = UPDATE_STATUS_FINALIZING;
504 break;
505 case UPDATE_STATUS_FINALIZING:
506 next_status = UPDATE_STATUS_IDLE;
507 break;
509 last_status_.status = next_status;
510 FOR_EACH_OBSERVER(Observer, observers_, UpdateStatusChanged(last_status_));
511 if (last_status_.status != UPDATE_STATUS_IDLE) {
512 base::MessageLoop::current()->PostDelayedTask(
513 FROM_HERE,
514 base::Bind(&UpdateEngineClientFakeImpl::StateTransition,
515 weak_factory_.GetWeakPtr()),
516 base::TimeDelta::FromMilliseconds(delay_ms));
520 ObserverList<Observer> observers_;
521 Status last_status_;
523 base::WeakPtrFactory<UpdateEngineClientFakeImpl> weak_factory_;
525 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClientFakeImpl);
528 UpdateEngineClient::UpdateEngineClient() {
531 UpdateEngineClient::~UpdateEngineClient() {
534 // static
535 UpdateEngineClient::UpdateCheckCallback
536 UpdateEngineClient::EmptyUpdateCheckCallback() {
537 return base::Bind(&EmptyUpdateCheckCallbackBody);
540 // static
541 UpdateEngineClient* UpdateEngineClient::Create(
542 DBusClientImplementationType type) {
543 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
544 return new UpdateEngineClientImpl();
545 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
546 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestAutoUpdateUI))
547 return new UpdateEngineClientFakeImpl();
548 else
549 return new UpdateEngineClientStubImpl();
552 } // namespace chromeos