Support for processing multiple URLs in update_engine.

Main changes:
1. Added a new PayloadState class which encapsulates all the persisted
state we use for multiple URLs, back-off (TBD), etc.
2. Added support for handling multiple URLs stored in the OmahaResponse in
OmahaRequestAction and OmahaResponseHandlerAction code.
3. Added support for picking the right URL in OmahaResponseHandlerAction
and putting it in the install_plan. This way, the rest of the code that
uses the install_plan is oblivious to the presence of multiple URLs :-)
4. Added support for advancing to next URL when an update fails. The full
error classification is a new work item (chromium-os:37206). Right now,
it's a basic round-robin on every error.
5. Updated the conditions for determining when hash checks are mandatory.
Previously since there was only one URL, if it was HTTPS, the checks were
waived. Now, even if there's one HTTP URL, we make hash checks mandatory
even if other HTTPS URLs are present.

6. Added new unit tests for PayloadState and the new logic added to other
places.

Noisy changes:
1. Instead of passing PrefsInterface to OmahaRequestAction and
OmahaResponseHandlerAction, we're now passing SystemState which will now
contain PrefsInterface and the newly added PayloadState object that these
actions need to do their work.
2. Renamed a bunch of setters/getters to set_x() and x() instead of SetX()
and GetX() methods - this was pending from Gilad's old CR. As I'm
adding new methods in the correct style, I went ahead and fixed it to
avoid the confusing styles.
3. Updated all existing unit tests to reflect these changes.

BUG=chromium-os:36807
TEST=All Single/Multiple URL scenarios work fine on my ZGB as expected.
TEST=Old and new unit tests run fine.

Change-Id: Id31f9ccb220471f3ec3a475f624dc03c16119144
Reviewed-on: https://gerrit.chromium.org/gerrit/39638
Commit-Ready: Jay Srinivasan <jaysri@chromium.org>
Reviewed-by: Jay Srinivasan <jaysri@chromium.org>
Tested-by: Jay Srinivasan <jaysri@chromium.org>
diff --git a/certificate_checker.cc b/certificate_checker.cc
index 1f19baf..7432c21 100644
--- a/certificate_checker.cc
+++ b/certificate_checker.cc
@@ -51,10 +51,7 @@
 }
 
 // static
-MetricsLibraryInterface* CertificateChecker::metrics_lib_ = NULL;
-
-// static
-PrefsInterface* CertificateChecker::prefs_ = NULL;
+SystemState* CertificateChecker::system_state_ = NULL;
 
 // static
 OpenSSLWrapper* CertificateChecker::openssl_wrapper_ = NULL;
@@ -105,13 +102,15 @@
   static const char kUMAActionCertChanged[] =
       "Updater.ServerCertificateChanged";
   static const char kUMAActionCertFailed[] = "Updater.ServerCertificateFailed";
+  TEST_AND_RETURN_FALSE(system_state_ != NULL);
+  TEST_AND_RETURN_FALSE(system_state_->prefs() != NULL);
   TEST_AND_RETURN_FALSE(server_to_check != kNone);
 
   // If pre-verification failed, we are not interested in the current
   // certificate. We store a report to UMA and just propagate the fail result.
   if (!preverify_ok) {
-    LOG_IF(WARNING, !prefs_->SetString(kReportToSendKey[server_to_check],
-                                       kUMAActionCertFailed))
+    LOG_IF(WARNING, !system_state_->prefs()->SetString(
+        kReportToSendKey[server_to_check], kUMAActionCertFailed))
         << "Failed to store UMA report on a failure to validate "
         << "certificate from update server.";
     return false;
@@ -140,8 +139,9 @@
                                     depth);
   string stored_digest;
   // If there's no stored certificate, we just store the current one and return.
-  if (!prefs_->GetString(storage_key, &stored_digest)) {
-    LOG_IF(WARNING, !prefs_->SetString(storage_key, digest_string))
+  if (!system_state_->prefs()->GetString(storage_key, &stored_digest)) {
+    LOG_IF(WARNING, !system_state_->prefs()->SetString(storage_key,
+                                                       digest_string))
         << "Failed to store server certificate on storage key " << storage_key;
     return true;
   }
@@ -149,11 +149,12 @@
   // Certificate changed, we store a report to UMA and store the most recent
   // certificate.
   if (stored_digest != digest_string) {
-    LOG_IF(WARNING, !prefs_->SetString(kReportToSendKey[server_to_check],
-                                       kUMAActionCertChanged))
+    LOG_IF(WARNING, !system_state_->prefs()->SetString(
+        kReportToSendKey[server_to_check], kUMAActionCertChanged))
         << "Failed to store UMA report on a change on the "
         << "certificate from update server.";
-    LOG_IF(WARNING, !prefs_->SetString(storage_key, digest_string))
+    LOG_IF(WARNING, !system_state_->prefs()->SetString(storage_key,
+                                                       digest_string))
         << "Failed to store server certificate on storage key " << storage_key;
   }
 
@@ -164,20 +165,22 @@
 // static
 void CertificateChecker::FlushReport() {
   // This check shouldn't be needed, but it is useful for testing.
-  TEST_AND_RETURN(metrics_lib_ && prefs_);
+  TEST_AND_RETURN(system_state_);
+  TEST_AND_RETURN(system_state_->metrics_lib());
+  TEST_AND_RETURN(system_state_->prefs());
 
   // We flush reports for both servers.
   for (size_t i = 0; i < arraysize(kReportToSendKey); i++) {
     string report_to_send;
-    if (prefs_->GetString(kReportToSendKey[i], &report_to_send) &&
-        !report_to_send.empty()) {
+    if (system_state_->prefs()->GetString(kReportToSendKey[i], &report_to_send)
+        && !report_to_send.empty()) {
       // There is a report to be sent. We send it and erase it.
-      LOG_IF(WARNING, !metrics_lib_->SendUserActionToUMA(report_to_send))
+      LOG(INFO) << "Found report #" << i << ". Sending it";
+      LOG_IF(WARNING, !system_state_->metrics_lib()->SendUserActionToUMA(
+          report_to_send))
           << "Failed to send server certificate report to UMA: "
           << report_to_send;
-      // Since prefs doesn't provide deletion, we just set it as an empty
-      // string.
-      LOG_IF(WARNING, !prefs_->SetString(kReportToSendKey[i], ""))
+      LOG_IF(WARNING, !system_state_->prefs()->Delete(kReportToSendKey[i]))
           << "Failed to erase server certificate report to be sent to UMA";
     }
   }