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/payload_state.h b/payload_state.h
new file mode 100644
index 0000000..6861e70
--- /dev/null
+++ b/payload_state.h
@@ -0,0 +1,99 @@
+// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__
+#define CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__
+
+#include "base/file_path.h"
+
+#include "update_engine/action_processor.h"
+#include "update_engine/prefs_interface.h"
+
+namespace chromeos_update_engine {
+
+// Forward declaration here because we get a circular dependency if
+// we include omaha_request_action.h directly.
+struct OmahaResponse;
+
+// Encapsulates all the payload state required for download. This includes the
+// state necessary for handling multiple URLs in Omaha response, the back-off
+// state, etc. All state is persisted so that we use the most recently saved
+// value when resuming the update_engine process. All state is also cached in
+// memory so that we ensure we always make progress based on last known good
+// state even when there's any issue in reading/writing from the file system.
+class PayloadState {
+ public:
+
+  PayloadState() : prefs_(NULL), num_urls_(0), url_index_(0) {}
+
+  // Initializes a payload state object using |prefs| for storing the
+  // persisted state. It also performs the initial loading of all persisted
+  // state into memory and dumps the initial state for debugging purposes.
+  // Note: the other methods should be called only after calling Initialize
+  // on this object.
+  bool Initialize(PrefsInterface* prefs);
+
+  // Logs the current payload state.
+  void LogPayloadState();
+
+  // Sets the internal payload state based on the given Omaha response. This
+  // response could be the same or different from the one for which we've stored
+  // the internal state. If it's different, then this method resets all the
+  // internal state corresponding to the old response. Since the Omaha response
+  // has a lot of fields that are not related to payload state, it uses only
+  // a subset of the fields in the Omaha response to compare equality.
+  void SetResponse(const OmahaResponse& response);
+
+  // Updates the payload state when the current update attempt has failed.
+  void UpdateFailed(ActionExitCode error);
+
+  // Returns the internally stored subset of the response state as a string.
+  // This is logically a private method, but exposed here for unit tests.
+  std::string GetResponse() {
+    return response_;
+  }
+
+  // Returns the current URL index.
+  uint32_t GetUrlIndex() {
+    return url_index_;
+  }
+
+ private:
+  // Sets the stored response_ value from the currently persisted value for
+  // the response. Returns the same value.
+  std::string LoadResponse();
+
+  // Sets the url_index_ value from the currently persisted value for
+  // URL index. Returns the same value.
+  uint32_t LoadUrlIndex();
+
+  // Sets the current URL index.
+  void SetUrlIndex(uint32_t url_index);
+
+  // Interface object with which we read/write persisted state. This must
+  // be set by calling the Initialize method before calling any other method.
+  PrefsInterface* prefs_;
+
+  // Cached value of the latest subset of the Omaha response with which we're
+  // working off currently. This value is persisted so we load it off the next
+  // time when update_engine restarts. The rest of the state in this class will
+  // be cleared when we set a new  response.
+  std::string response_;
+
+  // The number of urls in the current response. Not persisted.
+  uint32_t num_urls_;
+
+  // Cached value of the index of the current URL, to be used in case we are
+  // unable to read from the persisted store for any reason. This type is
+  // different from the one in the accessor methods because PrefsInterface
+  // supports only int64_t but we want to provide a stronger abstraction of
+  // uint32_t.
+  int64_t url_index_;
+
+  DISALLOW_COPY_AND_ASSIGN(PayloadState);
+};
+
+}  // namespace chromeos_update_engine
+
+#endif  // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__