update_engine: NextPayload() resets URL index

This change fixes a bug/assumption that |PayloadState| used to make in
regards to URL index related to payloads. When a URL index is
incremented, there is no gurauntee that subsequent payloads will have
the same number of candidate URLs, hence it is critical to reset the URL
index back to 0 for subsequent payloads. This fix also allows candidate
URLs to not be skipped over for multi-package/payload request/responses.
The max number of times a URL is allowed to fail is reduced from 10 to 3
to allow preferred URLs to always be used as the intial URL for payloads.

BUG=chromium:928805
TEST=FEATURES=test emerge-$B update_engine # filter PayloadStateTest

Change-Id: I67732b2b7da08f580d1b554fd85eb06b3bf1f761
Reviewed-on: https://chromium-review.googlesource.com/c/aosp/platform/system/update_engine/+/2188552
Tested-by: Jae Hoon Kim <kimjae@chromium.org>
Commit-Queue: Andrew Lassalle <andrewlassalle@chromium.org>
Reviewed-by: Amin Hassani <ahassani@chromium.org>
diff --git a/payload_state.cc b/payload_state.cc
index 355552e..5facdff 100644
--- a/payload_state.cc
+++ b/payload_state.cc
@@ -469,9 +469,7 @@
 
 void PayloadState::IncrementUrlIndex() {
   size_t next_url_index = url_index_ + 1;
-  size_t max_url_size = 0;
-  for (const auto& urls : candidate_urls_)
-    max_url_size = std::max(max_url_size, urls.size());
+  size_t max_url_size = candidate_urls_[payload_index_].size();
   if (next_url_index < max_url_size) {
     LOG(INFO) << "Incrementing the URL index for next attempt";
     SetUrlIndex(next_url_index);
@@ -902,6 +900,7 @@
 bool PayloadState::NextPayload() {
   if (payload_index_ + 1 >= candidate_urls_.size())
     return false;
+  SetUrlIndex(0);
   SetPayloadIndex(payload_index_ + 1);
   return true;
 }