Implement exponential backoff for throttling repeated AU downloads.

Today we retry the same payload over and over again every hour. Ideally,
we shouldn't require ever to re-download the same payload again. But
from experience we find that post-install or firmware updates may succeed
on a second attempt. So until we have code that can do such selective
retries of those steps, we currently re-download and re-apply the whole
payload. So instead of retrying over and over again, we backoff the
successive payload download attempts at 1 day, 2 days, 4 days, etc. with
an upper limit of 16 days.

Another subtle reason for which we depend on the payload retry mechanism
today is if we've failed downloading the payload via all the URLs that are
specified in the rule, we don't want to keep re-attempting the download.
This case is different from the case discussed above, because in this case
we haven't even downloaded a payload once completely. In this case also,
there's a need for throttling the amount of bytes we end up downloading
repeatedly for a particular operation that may fail. This is done by
treating the exhaustion of all URLs as equivalent to having downloaded
a full payload and subjecting it to the same backoff behavior.

We waive backoffs for dev/test images so as not to cause any delay in
our testing or development.

BUG=chromium-os:36806
TEST=Added new unit tests. Tested all scenarios on my ZGB.
Change-Id: I6bd0d3f296a3c0da0a8026fb71b24785d825e39c
Reviewed-on: https://gerrit.chromium.org/gerrit/40220
Commit-Queue: Jay Srinivasan <jaysri@chromium.org>
Reviewed-by: Jay Srinivasan <jaysri@chromium.org>
Tested-by: Jay Srinivasan <jaysri@chromium.org>
diff --git a/omaha_request_action.cc b/omaha_request_action.cc
index c98357b..679d326 100644
--- a/omaha_request_action.cc
+++ b/omaha_request_action.cc
@@ -31,7 +31,9 @@
 
 // List of custom pair tags that we interpret in the Omaha Response:
 static const char* kTagDeadline = "deadline";
+static const char* kTagDisablePayloadBackoff = "DisablePayloadBackoff";
 static const char* kTagDisplayVersion = "DisplayVersion";
+static const char* kTagIsDeltaPayload = "IsDelta";
 static const char* kTagMaxFailureCountPerUrl = "MaxFailureCountPerUrl";
 static const char* kTagMaxDaysToScatter = "MaxDaysToScatter";
 // Deprecated: "ManifestSignatureRsa"
@@ -597,9 +599,15 @@
       ParseInt(XmlGetProperty(pie_action_node, kTagMaxDaysToScatter));
 
   string max = XmlGetProperty(pie_action_node, kTagMaxFailureCountPerUrl);
-  if (!base::StringToInt(max, &output_object->max_failure_count_per_url))
+  if (!base::StringToUint(max, &output_object->max_failure_count_per_url))
     output_object->max_failure_count_per_url = kDefaultMaxFailureCountPerUrl;
 
+  output_object->is_delta_payload =
+      XmlGetProperty(pie_action_node, kTagIsDeltaPayload) == "true";
+
+  output_object->disable_payload_backoff =
+      XmlGetProperty(pie_action_node, kTagDisablePayloadBackoff) == "true";
+
   return true;
 }
 
@@ -691,9 +699,20 @@
 
   // Update the payload state with the current response. The payload state
   // will automatically reset all stale state if this response is different
-  // from what's stored already.
+  // from what's stored already. We are updating the payload state as late
+  // as possible in this method so that if a new release gets pushed and then
+  // got pulled back due to some issues, we don't want to clear our internal
+  // state unnecessarily.
   PayloadStateInterface* payload_state = system_state_->payload_state();
   payload_state->SetResponse(output_object);
+
+  if (payload_state->ShouldBackoffDownload()) {
+    output_object.update_exists = false;
+    LOG(INFO) << "Ignoring Omaha updates in order to backoff our retry "
+                 "attempts";
+    completer.set_code(kActionCodeOmahaUpdateDeferredForBackoff);
+    return;
+  }
 }
 
 bool OmahaRequestAction::ShouldDeferDownload(OmahaResponse* output_object) {