Cancel the current download if user chooses a different channel.

In my earlier CL, to keep the implementation simple, we disallowed changing
a channel until the previous change completed in its entirety. Given that
the UI is not going to be updated for M27, such a restriction turned out
to be very confusing when playing around with channel changing. So, we
decided to implement a simple form of canceling the download if the
user selected a different channel while we're downloading the bits. This
implementation can easily be extended to support a general form of cancel
in the future, if required.

This CL also adds validation of libchromeos API calls when interpreting
the policy values. It also cleans up some bogus error messages that were
logged earlier when we abort a download.

BUG=chromium:222617
TEST=All scenarios pass on ZGB. Unit Tests pass.

Change-Id: I7cd691fe461d9ce47314299f6e2598944650ee33
Reviewed-on: https://gerrit.chromium.org/gerrit/46095
Commit-Queue: Jay Srinivasan <jaysri@chromium.org>
Reviewed-by: Jay Srinivasan <jaysri@chromium.org>
Tested-by: Jay Srinivasan <jaysri@chromium.org>
diff --git a/delta_performer.cc b/delta_performer.cc
index c299d79..b0c8c17 100644
--- a/delta_performer.cc
+++ b/delta_performer.cc
@@ -27,6 +27,7 @@
 #include "update_engine/prefs_interface.h"
 #include "update_engine/subprocess.h"
 #include "update_engine/terminator.h"
+#include "update_engine/update_attempter.h"
 
 using std::min;
 using std::string;
@@ -262,10 +263,9 @@
   fd_ = -2;  // Set to invalid so that calls to Open() will fail.
   path_ = "";
   if (!buffer_.empty()) {
-    LOG(ERROR) << "Called Close() while buffer not empty!";
-    if (err >= 0) {
+    LOG(INFO) << "Discarding " << buffer_.size() << " unused downloaded bytes";
+    if (err >= 0)
       err = 1;
-    }
   }
   return -err;
 }
@@ -457,6 +457,12 @@
   }
 
   while (next_operation_num_ < num_total_operations_) {
+    // Check if we should cancel the current attempt for any reason.
+    // In this case, *error will have already been populated with the reason
+    // why we're cancelling.
+    if (system_state_->update_attempter()->ShouldCancel(error))
+      return false;
+
     const bool is_kernel_partition =
         (next_operation_num_ >= num_rootfs_operations_);
     const DeltaArchiveManifest_InstallOperation &op =