AU: Fall back to full updates after a number of failed delta update attempts.

Count each failed delta update attempt. Keep the count in persistent storage. If
the count exceeds a threshold (3), disable delta updates by setting delta_okay
to false in the update check request.

Once this CL is in, we have to ensure that we have a full update payload
available on the update server for each version otherwise we may orphan clients.

BUG=7221
TEST=unit tests, gmerged on device and tested with a mod'ed dev server

Change-Id: I7f7fa73f652f12f22cc8604dad6a26c802b8582d

Review URL: http://codereview.chromium.org/3617002
diff --git a/update_attempter.cc b/update_attempter.cc
index 73b93b8..a9c9261 100644
--- a/update_attempter.cc
+++ b/update_attempter.cc
@@ -25,6 +25,7 @@
 #include "update_engine/omaha_request_params.h"
 #include "update_engine/omaha_response_handler_action.h"
 #include "update_engine/postinstall_runner_action.h"
+#include "update_engine/prefs_interface.h"
 #include "update_engine/set_bootable_flag_action.h"
 #include "update_engine/update_check_scheduler.h"
 
@@ -36,6 +37,8 @@
 
 namespace chromeos_update_engine {
 
+const int UpdateAttempter::kMaxDeltaUpdateFailures = 3;
+
 const char* kUpdateCompletedMarker =
     "/var/run/update_engine_autoupdate_completed";
 
@@ -101,7 +104,8 @@
       download_progress_(0.0),
       last_checked_time_(0),
       new_version_("0.0.0.0"),
-      new_size_(0) {
+      new_size_(0),
+      is_full_update_(false) {
   if (utils::FileExists(kUpdateCompletedMarker))
     status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT;
 }
@@ -126,6 +130,7 @@
     LOG(ERROR) << "Unable to initialize Omaha request device params.";
     return;
   }
+  DisableDeltaUpdateIfNeeded();
   CHECK(!processor_->IsRunning());
   processor_->set_delegate(this);
 
@@ -251,6 +256,7 @@
   if (code == kActionCodeSuccess) {
     SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
     utils::WriteFile(kUpdateCompletedMarker, "", 0);
+    prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
 
     // Report the time it took to update the system.
     int64_t update_time = time(NULL) - last_checked_time_;
@@ -307,6 +313,14 @@
     }
   }
   if (code != kActionCodeSuccess) {
+    // If this was a delta update attempt and the current state is at or past
+    // the download phase, count the failure in case a switch to full update
+    // becomes necessary. Ignore network transfer timeouts and failures.
+    if (status_ >= UPDATE_STATUS_DOWNLOADING &&
+        !is_full_update_ &&
+        code != kActionCodeDownloadTransferError) {
+      MarkDeltaUpdateFailure();
+    }
     // On failure, schedule an error event to be sent to Omaha.
     CreatePendingErrorEvent(action, code);
     return;
@@ -326,6 +340,7 @@
     // TODO(adlr): put version in InstallPlan
     new_version_ = "0.0.0.0";
     new_size_ = plan.size;
+    is_full_update_ = plan.is_full_update;
     SetupPriorityManagement();
   } else if (type == DownloadAction::StaticType()) {
     SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
@@ -490,4 +505,24 @@
   return false;
 }
 
+void UpdateAttempter::DisableDeltaUpdateIfNeeded() {
+  int64_t delta_failures;
+  if (omaha_request_params_.delta_okay &&
+      prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) &&
+      delta_failures >= kMaxDeltaUpdateFailures) {
+    LOG(WARNING) << "Too many delta update failures, forcing full update.";
+    omaha_request_params_.delta_okay = false;
+  }
+}
+
+void UpdateAttempter::MarkDeltaUpdateFailure() {
+  CHECK(!is_full_update_);
+  int64_t delta_failures;
+  if (!prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) ||
+      delta_failures < 0) {
+    delta_failures = 0;
+  }
+  prefs_->SetInt64(kPrefsDeltaUpdateFailures, ++delta_failures);
+}
+
 }  // namespace chromeos_update_engine