p2p: Use p2p for updates

This is the main patch for enabling use of p2p for consuming and/or
sharing updates via p2p. Refer to the ddoc and other documentation for
how this works.

BUG=chromium:260426,chromium:273110
TEST=New unit tests + unit tests pass + manual testing
Change-Id: I6bc3bddae1e041ccc176969a651396e8e89cb3f0
Reviewed-on: https://chromium-review.googlesource.com/64829
Reviewed-by: David Zeuthen <zeuthen@chromium.org>
Commit-Queue: David Zeuthen <zeuthen@chromium.org>
Tested-by: David Zeuthen <zeuthen@chromium.org>
diff --git a/omaha_response_handler_action.cc b/omaha_response_handler_action.cc
index 2acc803..7df4c8d 100644
--- a/omaha_response_handler_action.cc
+++ b/omaha_response_handler_action.cc
@@ -69,6 +69,16 @@
   install_plan_.download_url = current_url;
   install_plan_.version = response.version;
 
+  OmahaRequestParams* params = system_state_->request_params();
+
+  // If we're using p2p to download and there is a local peer, use it.
+  if (params->use_p2p_for_downloading() && !params->p2p_url().empty()) {
+    LOG(INFO) << "Replacing URL " << install_plan_.download_url
+              << " with local URL " << params->p2p_url()
+              << " since p2p is enabled.";
+    install_plan_.download_url = params->p2p_url();
+  }
+
   // Fill up the other properties based on the response.
   install_plan_.payload_size = response.size;
   install_plan_.payload_hash = response.hash;
@@ -98,7 +108,6 @@
       system_state_->hardware()->KernelDeviceOfBootDevice(
           install_plan_.install_path);
 
-  OmahaRequestParams* params = system_state_->request_params();
   if (params->to_more_stable_channel() && params->is_powerwash_allowed())
     install_plan_.powerwash_required = true;
 
@@ -139,6 +148,13 @@
     return false;
   }
 
+  // If we're using p2p, |install_plan_.download_url| may contain a
+  // HTTP URL even if |response.payload_urls| contain only HTTPS URLs.
+  if (!StartsWithASCII(install_plan_.download_url, "https://", false)) {
+    LOG(INFO) << "Mandating hash checks since download_url is not HTTPS.";
+    return true;
+  }
+
   // TODO(jaysri): VALIDATION: For official builds, we currently waive hash
   // checks for HTTPS until we have rolled out at least once and are confident
   // nothing breaks. chromium-os:37082 tracks turning this on for HTTPS