AU: Announce the update engine status on startup.

This is to ensure that the system is in a consistent state in case update-engine
crashes.

BUG=chromium-os:15069
TEST=press "Check for update" in Chrome, restart update-engine while downloading
an update, see the status go back to 'up to date' (rather than get stuck in
'Downloading')

Change-Id: I1e89502a9fa134ec3a81e50a9036ce20b46eb9b0
Reviewed-on: http://gerrit.chromium.org/gerrit/1144
Tested-by: Darin Petkov <petkov@chromium.org>
Reviewed-by: Andrew de los Reyes <adlr@chromium.org>
diff --git a/main.cc b/main.cc
index 7d6babf..52e9c9e 100644
--- a/main.cc
+++ b/main.cc
@@ -40,8 +40,12 @@
 namespace chromeos_update_engine {
 
 gboolean UpdateBootFlags(void* arg) {
-  UpdateAttempter* attempter = reinterpret_cast<UpdateAttempter*>(arg);
-  attempter->UpdateBootFlags();
+  reinterpret_cast<UpdateAttempter*>(arg)->UpdateBootFlags();
+  return FALSE;  // Don't call this callback again
+}
+
+gboolean BroadcastStatus(void* arg) {
+  reinterpret_cast<UpdateAttempter*>(arg)->BroadcastStatus();
   return FALSE;  // Don't call this callback again
 }
 
@@ -192,6 +196,10 @@
                         &chromeos_update_engine::UpdateBootFlags,
                         &update_attempter);
 
+  // Broadcast the update engine status on startup to ensure consistent system
+  // state on crashes.
+  g_idle_add(&chromeos_update_engine::BroadcastStatus, &update_attempter);
+
   // Run the main loop until exit time:
   g_main_loop_run(loop);
 
diff --git a/update_attempter.cc b/update_attempter.cc
index 474f24c..e0e3b60 100644
--- a/update_attempter.cc
+++ b/update_attempter.cc
@@ -510,13 +510,10 @@
   reinterpret_cast<UpdateAttempter*>(p)->CompleteUpdateBootFlags(return_code);
 }
 
-void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
-  status_ = status;
-  if (update_check_scheduler_) {
-    update_check_scheduler_->SetUpdateStatus(status_);
-  }
-  if (!dbus_service_)
+void UpdateAttempter::BroadcastStatus() {
+  if (!dbus_service_) {
     return;
+  }
   last_notify_time_ = TimeTicks::Now();
   update_engine_service_emit_status_update(
       dbus_service_,
@@ -527,6 +524,14 @@
       new_size_);
 }
 
+void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
+  status_ = status;
+  if (update_check_scheduler_) {
+    update_check_scheduler_->SetUpdateStatus(status_);
+  }
+  BroadcastStatus();
+}
+
 void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action,
                                               ActionExitCode code) {
   if (error_event_.get()) {
diff --git a/update_attempter.h b/update_attempter.h
index 3bbd2ef..f4b82ff 100644
--- a/update_attempter.h
+++ b/update_attempter.h
@@ -126,6 +126,9 @@
   void SetDownloadStatus(bool active);
   void BytesReceived(uint64_t bytes_received, uint64_t total);
 
+  // Broadcasts the current status over D-Bus.
+  void BroadcastStatus();
+
  private:
   friend class UpdateAttempterTest;
   FRIEND_TEST(UpdateAttempterTest, ActionCompletedDownloadTest);