Enhanced channel changing behavior

This CL adds a new DBUS API to UpdateEngine called SetTargetChannel to
change the current channel of the device with an option to indicate
whether to do eventually or immediately.

The API will be called with the option to do it immediately in a
subsequent CL in Chrome UI. For now the old API (set_track) has been
wired up to call the new API to produce the old behavior (i.e. change
eventually). The old API will be removed after Chrome UI code stops
using it.

It's the UI's responsibility to ask the user for confirmation for the
powerwash that may happen in some cases and call the API with the
appropriate value whether or not the powerwash should happen.

For now, we're restricting the changing of channels to only those
devices that are on canary-channel or running test builds. This
restriction will be lifted off once the UI work is ready to give
warning to the users about the powerwash that may happen when they move
to a more stable channel.

We also enforce ReleaseChannelDelegated and ReleaseChannel policies
correctly now as follows:

* If ReleaseChannelDelegated is false, SetTargetChannel will fail as we
need to honor (only) the ReleaseChannel value in this case.
* If ReleaseChannelDelegated is true, we'll allow the SetTargetChannel
call to specify. In this case, we'll ignore the value of ReleaseChannel,
if any.

BUG=chromium-os:39095
TEST=Tested on ZGB by going from canary to dev-channel with and without
powerwash.
TEST=Existing unit tests have been updated and they pass.
TEST=New unit tests have been added.

Change-Id: Ifbf806a06e1c30d2f318e94d73735d1812049abd
Reviewed-on: https://gerrit.chromium.org/gerrit/44619
Commit-Queue: Jay Srinivasan <jaysri@chromium.org>
Reviewed-by: Jay Srinivasan <jaysri@chromium.org>
Tested-by: Jay Srinivasan <jaysri@chromium.org>
diff --git a/update_engine_client.cc b/update_engine_client.cc
index f3b5eea..e28801c 100644
--- a/update_engine_client.cc
+++ b/update_engine_client.cc
@@ -26,10 +26,12 @@
 DEFINE_bool(check_for_update, false, "Initiate check for updates.");
 DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
 DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
-DEFINE_bool(show_track, false, "Show the update track.");
+DEFINE_bool(show_channel, false, "Show the current and target channels.");
 DEFINE_bool(status, false, "Print the status to stdout.");
 DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
-DEFINE_string(track, "", "Permanently change the update track.");
+DEFINE_string(channel, "",
+    "Set the target channel. The device will be powerwashed if the target "
+    "channel is more stable than the current channel.");
 DEFINE_bool(update, false, "Forces an update and waits for its completion. "
             "Exit status is 0 if the update succeeded, and 1 otherwise.");
 DEFINE_bool(watch_for_updates, false,
@@ -205,35 +207,38 @@
   return true;
 }
 
-void SetTrack(const string& track) {
+void SetTargetChannel(const string& target_channel) {
   DBusGProxy* proxy;
   GError* error = NULL;
 
   CHECK(GetProxy(&proxy));
 
   gboolean rc =
-      org_chromium_UpdateEngineInterface_set_track(proxy,
-                                                   track.c_str(),
-                                                   &error);
-  CHECK_EQ(rc, true) << "Error setting the track: "
+      org_chromium_UpdateEngineInterface_set_channel(proxy,
+                                                     target_channel.c_str(),
+                                                     true, // OK to Powerwash
+                                                     &error);
+  CHECK_EQ(rc, true) << "Error setting the channel: "
                      << GetAndFreeGError(&error);
-  LOG(INFO) << "Track permanently set to: " << track;
+  LOG(INFO) << "Channel permanently set to: " << target_channel;
 }
 
-string GetTrack() {
+string GetChannel(bool get_current_channel) {
   DBusGProxy* proxy;
   GError* error = NULL;
 
   CHECK(GetProxy(&proxy));
 
-  char* track = NULL;
+  char* channel = NULL;
   gboolean rc =
-      org_chromium_UpdateEngineInterface_get_track(proxy,
-                                                   &track,
-                                                   &error);
-  CHECK_EQ(rc, true) << "Error getting the track: " << GetAndFreeGError(&error);
-  string output = track;
-  g_free(track);
+      org_chromium_UpdateEngineInterface_get_channel(proxy,
+                                                     get_current_channel,
+                                                     &channel,
+                                                     &error);
+  CHECK_EQ(rc, true) << "Error getting the channel: "
+                     << GetAndFreeGError(&error);
+  string output = channel;
+  g_free(channel);
   return output;
 }
 
@@ -294,14 +299,18 @@
     return 0;
   }
 
-  // First, update the track if requested.
-  if (!FLAGS_track.empty()) {
-    SetTrack(FLAGS_track);
-  }
+  // First, update the target channel if requested.
+  if (!FLAGS_channel.empty())
+    SetTargetChannel(FLAGS_channel);
 
-  // Show the track if requested.
-  if (FLAGS_show_track) {
-    LOG(INFO) << "Track: " << GetTrack();
+  // Show the current and target channels if requested.
+  if (FLAGS_show_channel) {
+    string current_channel = GetChannel(true);
+    LOG(INFO) << "Current Channel: " << current_channel;
+
+    string target_channel = GetChannel(false);
+    if (!target_channel.empty())
+      LOG(INFO) << "Target Channel (pending update): " << target_channel;
   }
 
   // Initiate an update check, if necessary.