AU: Provide a reboot_if_needed D-Bus API.

This will reboot if the current state is UPDATED_REBOOT_NEEDED.
Also add -reboot option to the update_engine_client.

BUG=4250
TEST=unit tests,gmerged on device and tried reboot requests

Review URL: http://codereview.chromium.org/3034026
diff --git a/update_engine_client.cc b/update_engine_client.cc
index bb4beaa..c8dda07 100644
--- a/update_engine_client.cc
+++ b/update_engine_client.cc
@@ -22,14 +22,12 @@
 using chromeos_update_engine::utils::GetGErrorMessage;
 using std::string;
 
-DEFINE_string(app_version, "",
-              "Force the current app version.");
-DEFINE_bool(check_for_update, false,
-            "Initiate check for updates.");
+DEFINE_string(app_version, "", "Force the current app version.");
+DEFINE_bool(check_for_update, false, "Initiate check for updates.");
 DEFINE_bool(force_update, false,
             "Force an update, even over an expensive network.");
-DEFINE_string(omaha_url, "",
-              "The URL of the Omaha update server.");
+DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
+DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
 DEFINE_bool(status, false, "Print the status to stdout.");
 DEFINE_bool(watch_for_updates, false,
             "Listen for status updates and print them to the screen.");
@@ -158,6 +156,21 @@
   return true;
 }
 
+bool RebootIfNeeded() {
+  DBusGProxy* proxy;
+  GError* error = NULL;
+
+  CHECK(GetProxy(&proxy));
+
+  gboolean rc =
+      org_chromium_UpdateEngineInterface_reboot_if_needed(proxy, &error);
+  // Reboot error code doesn't necessarily mean that a reboot
+  // failed. For example, D-Bus may be shutdown before we receive the
+  // result.
+  LOG_IF(INFO, !rc) << "Reboot error message: " << GetGErrorMessage(error);
+  return true;
+}
+
 }  // namespace {}
 
 int main(int argc, char** argv) {
@@ -177,6 +190,7 @@
   }
   if (FLAGS_force_update || FLAGS_check_for_update ||
       !FLAGS_app_version.empty() || !FLAGS_omaha_url.empty()) {
+    LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
     LOG(INFO) << "Initiating update check and install.";
     if (FLAGS_force_update) {
       LOG(INFO) << "Will not abort due to being on expensive network.";
@@ -187,10 +201,16 @@
     return 0;
   }
   if (FLAGS_watch_for_updates) {
+    LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
     LOG(INFO) << "Watching for status updates.";
     WatchForUpdates();  // Should never return.
     return 1;
   }
+  if (FLAGS_reboot) {
+    LOG(INFO) << "Requesting a reboot...";
+    CHECK(RebootIfNeeded());
+    return 0;
+  }
 
   LOG(INFO) << "No flags specified. Exiting.";
   return 0;