Remove all mentions of Set/Get Track and hard-coding of canary-channel.

This CL removes the deprecated functionality of Set/Get Track previously
used to change channels. Now that the UI is using Set/Get Channel we no
longer need these methods. I've also gone ahead and fixed the long standing
bug of needed to do an update check before testing channel change.

I've also added the testing of allow powerwash functionality to update_engine
client to better test this feature.

BUG=chromium:221850
TEST=unittests + update_engine_client with channel change and allowing
powerwash and not allowing powerwash. Confirmed the versions were as expected (
when going more stable we send version 0.0.0.0 when we allow powerwash vs the
actual version when not).

Change-Id: I87297f7e7e103bb25e62f2d896e155e758030ead
Reviewed-on: https://gerrit.chromium.org/gerrit/63674
Commit-Queue: Chris Sosa <sosa@chromium.org>
Reviewed-by: Chris Sosa <sosa@chromium.org>
Tested-by: Chris Sosa <sosa@chromium.org>
diff --git a/UpdateEngine.conf b/UpdateEngine.conf
index 5673bbe..5c259b4 100644
--- a/UpdateEngine.conf
+++ b/UpdateEngine.conf
@@ -27,15 +27,9 @@
            send_member="GetStatus"/>
     <allow send_destination="org.chromium.UpdateEngine"
            send_interface="org.chromium.UpdateEngineInterface"
-           send_member="GetTrack"/>
-    <allow send_destination="org.chromium.UpdateEngine"
-           send_interface="org.chromium.UpdateEngineInterface"
            send_member="RebootIfNeeded"/>
     <allow send_destination="org.chromium.UpdateEngine"
            send_interface="org.chromium.UpdateEngineInterface"
-           send_member="SetTrack"/>
-    <allow send_destination="org.chromium.UpdateEngine"
-           send_interface="org.chromium.UpdateEngineInterface"
            send_member="SetChannel"/>
     <allow send_destination="org.chromium.UpdateEngine"
            send_interface="org.chromium.UpdateEngineInterface"
diff --git a/dbus_service.cc b/dbus_service.cc
index bc84858..cf6bbae 100644
--- a/dbus_service.cc
+++ b/dbus_service.cc
@@ -154,30 +154,6 @@
   return TRUE;
 }
 
-gboolean update_engine_service_set_track(UpdateEngineService* self,
-                                         gchar* track,
-                                         GError **error) {
-  // track == target channel.
-  // TODO(jaysri): Remove this method once chromium:219292 is fixed.
-  // Since UI won't be ready for now, preserve the existing
-  // behavior for set_track by calling SetTargetChannel directly without the
-  // policy checks instead of calling update_engine_service_set_channel.
-  LOG(INFO) << "Setting destination track to: " << track;
-  if (!self->system_state_->request_params()->SetTargetChannel(track, false)) {
-    *error = NULL;
-    return FALSE;
-  }
-
-  return TRUE;
-}
-
-gboolean update_engine_service_get_track(UpdateEngineService* self,
-                                         gchar** track,
-                                         GError **error) {
-  // track == target channel.
-  return update_engine_service_get_channel(self, false, track, error);
-}
-
 gboolean update_engine_service_set_channel(UpdateEngineService* self,
                                            gchar* target_channel,
                                            bool is_powerwash_allowed,
@@ -187,14 +163,21 @@
 
   const policy::DevicePolicy* device_policy =
       self->system_state_->device_policy();
+
+  // The device_policy is loaded in a lazy way before an update check. Load it
+  // now from the libchromeos cache if it wasn't already loaded.
   if (!device_policy) {
-    LOG(INFO) << "Cannot set target channel until device policy/settings are "
-                 "known";
-    return FALSE;
+    chromeos_update_engine::UpdateAttempter* update_attempter =
+        self->system_state_->update_attempter();
+    if (update_attempter) {
+      update_attempter->RefreshDevicePolicy();
+      device_policy = self->system_state_->device_policy();
+    }
   }
 
   bool delegated = false;
-  if (device_policy->GetReleaseChannelDelegated(&delegated) && !delegated) {
+  if (device_policy &&
+      device_policy->GetReleaseChannelDelegated(&delegated) && !delegated) {
     LOG(INFO) << "Cannot set target channel explicitly when channel "
                  "policy/settings is not delegated";
     return FALSE;
@@ -239,7 +222,7 @@
         self->system_state_->update_attempter();
     if (update_attempter) {
       update_attempter->RefreshDevicePolicy();
-      device_policy = self->system_state_->device_policy();;
+      device_policy = self->system_state_->device_policy();
     }
   }
 
diff --git a/dbus_service.h b/dbus_service.h
index b9bf08f..8349622 100644
--- a/dbus_service.h
+++ b/dbus_service.h
@@ -69,16 +69,6 @@
 gboolean update_engine_service_reboot_if_needed(UpdateEngineService* self,
                                                 GError **error);
 
-// TODO(jaysri): Deprecate set_track and get_track once Chrome is modified to
-// use set_channel and get_channel.
-gboolean update_engine_service_set_track(UpdateEngineService* self,
-                                         gchar* track,
-                                         GError **error);
-
-gboolean update_engine_service_get_track(UpdateEngineService* self,
-                                         gchar** track,
-                                         GError **error);
-
 // Changes the current channel of the device to the target channel. If the
 // target channel is a less stable channel than the current channel, then the
 // channel change happens immediately (at the next update check).  If the
diff --git a/omaha_request_params.cc b/omaha_request_params.cc
index 38262a9..9a729f0 100644
--- a/omaha_request_params.cc
+++ b/omaha_request_params.cc
@@ -131,23 +131,6 @@
             << ". Current channel = " << current_channel_
             << ", existing target channel = " << target_channel_
             << ", download channel = " << download_channel_;
-
-  if (current_channel_ == "canary-channel") {
-    // TODO(jaysri): chromium-os:39751: We don't have the UI warnings yet. So,
-    // enable the powerwash-on-changing-to-more-stable-channel behavior for now
-    // only on canary-channel devices.
-    is_powerwash_allowed = true;
-    LOG(INFO) << "Is Powerwash Allowed set to true as we are in canary-channel";
-  } else if (!utils::IsOfficialBuild() &&
-             current_channel_ == "testimage-channel") {
-    // Also, allow test builds to have the powerwash behavior so we can always
-    // test channel changing behavior on them, without having to first get them
-    // on an official channel.
-    is_powerwash_allowed = true;
-    LOG(INFO) << "Is Powerwash Allowed set to true as we are running an "
-                 "unofficial build";
-  }
-
   TEST_AND_RETURN_FALSE(IsValidChannel(new_target_channel));
   FilePath kFile(root_ + kStatefulPartition + "/etc/lsb-release");
   string file_data;
diff --git a/update_engine.xml b/update_engine.xml
index cafe26b..eada639 100644
--- a/update_engine.xml
+++ b/update_engine.xml
@@ -25,13 +25,6 @@
     </method>
     <method name="RebootIfNeeded">
     </method>
-    <!-- Deprecate GetTrack and SetTrack after updating the clients -->
-    <method name="GetTrack">
-      <arg type="s" name="track" direction="out" />
-    </method>
-    <method name="SetTrack">
-      <arg type="s" name="track" />
-    </method>
     <method name="SetChannel">
       <arg type="s" name="target_channel" />
       <arg type="b" name="is_powerwash_allowed" />
diff --git a/update_engine_client.cc b/update_engine_client.cc
index 3455a52..e959534 100644
--- a/update_engine_client.cc
+++ b/update_engine_client.cc
@@ -33,7 +33,8 @@
 DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
 DEFINE_bool(rollback, false, "Perform a rollback to the previous partition.");
 DEFINE_bool(show_channel, false, "Show the current and target channels.");
-DEFINE_bool(powerwash, true, "When performing a rollback, do a powerwash.");
+DEFINE_bool(powerwash, true, "When performing rollback or channel change, "
+    "do a powerwash or allow it respectively.");
 DEFINE_bool(status, false, "Print the status to stdout.");
 DEFINE_bool(update, false, "Forces an update and waits for its completion. "
             "Exit status is 0 if the update succeeded, and 1 otherwise.");
@@ -230,7 +231,7 @@
   return true;
 }
 
-void SetTargetChannel(const string& target_channel) {
+void SetTargetChannel(const string& target_channel, bool allow_powerwash) {
   DBusGProxy* proxy;
   GError* error = NULL;
 
@@ -239,7 +240,7 @@
   gboolean rc =
       org_chromium_UpdateEngineInterface_set_channel(proxy,
                                                      target_channel.c_str(),
-                                                     true, // OK to Powerwash
+                                                     allow_powerwash,
                                                      &error);
   CHECK_EQ(rc, true) << "Error setting the channel: "
                      << GetAndFreeGError(&error);
@@ -372,9 +373,14 @@
               << (allowed ? "ENABLED" : "DISABLED");
   }
 
+  if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) {
+    LOG(FATAL) << "powerwash flag only makes sense rollback or channel change";
+    return 1;
+  }
+
   // First, update the target channel if requested.
   if (!FLAGS_channel.empty())
-    SetTargetChannel(FLAGS_channel);
+    SetTargetChannel(FLAGS_channel, FLAGS_powerwash);
 
   // Show the current and target channels if requested.
   if (FLAGS_show_channel) {
@@ -389,11 +395,6 @@
   bool do_update_request = FLAGS_check_for_update | FLAGS_update |
       !FLAGS_app_version.empty() | !FLAGS_omaha_url.empty();
 
-  if (!FLAGS_powerwash && !FLAGS_rollback) {
-    LOG(FATAL) << "Skipping powerwash only works with rollback";
-    return 1;
-  }
-
   if (do_update_request && FLAGS_rollback) {
     LOG(FATAL) << "Update should not be requested with rollback!";
     return 1;