AU: improve abstraction of DeltaPerformer.ParsePayloadMetadata()

This method used to take a manifest object pointer to be filled in.
However, since the class it belongs to has a private member for this
purpose, it makes little sense to pass a pointer to a member (and not
use this member directly in the parsing method). This was likely done to
allow shortcuts when other logic needed to parse metadata, such as the
PayloadSigner. Instead, ParsePayloadMetadata() now populates its
object's manifest, and we provide a method for copying the content of
a parsed manifest for use by external entities.

Note that we still require to pass in a buffer (vector) with the payload
data to be parsed, instead of parsing the object's own buffer_ member.
This feels like a reasonable compromise, meant to facilitate direct use
of the parsing logic (PayloadSigner, unit tests).

Also exposes (and fixes) a hidden dependency on an internal member when
discarding download buffer content. Minor cosmetic fixes (indentation
etc).

BUG=chromium:229726
TEST=Unit tests
TEST=Updated an x86-alex via image_to_live.

Change-Id: Ic0a9c830986981eb02a553f924e18767a69c3082
Reviewed-on: https://chromium-review.googlesource.com/183715
Tested-by: Gilad Arnold <garnold@chromium.org>
Reviewed-by: Don Garrett <dgarrett@chromium.org>
Commit-Queue: Gilad Arnold <garnold@chromium.org>
diff --git a/delta_performer.h b/delta_performer.h
index 8f5a982..403ab8f 100644
--- a/delta_performer.h
+++ b/delta_performer.h
@@ -62,6 +62,7 @@
         install_plan_(install_plan),
         fd_(-1),
         kernel_fd_(-1),
+        manifest_parsed_(false),
         manifest_valid_(false),
         metadata_size_(0),
         next_operation_num_(0),
@@ -151,20 +152,12 @@
   static bool ResetUpdateProgress(PrefsInterface* prefs, bool quick);
 
   // Attempts to parse the update metadata starting from the beginning of
-  // |payload| into |manifest|. On success, sets |*metadata_size_p| to the total
-  // metadata length in bytes (including the delta magic and metadata size
-  // fields), and returns kMetadataParseSuccess. Returns
+  // |payload|. On success, returns kMetadataParseSuccess. Returns
   // kMetadataParseInsufficientData if more data is needed to parse the complete
   // metadata. Returns kMetadataParseError if the metadata can't be parsed given
   // the payload.
-  //
-  // IMPORTANT! Reads the value of |*metadata_size_p| to determine whether the
-  // payload header (which includes the manifest length) was already processed.
-  // Therefore |*metadata_size_p| must be zero when it is first called.
-  MetadataParseResult ParsePayloadMetadata(
-      const std::vector<char>& payload,
-      DeltaArchiveManifest* manifest,
-      ErrorCode* error);
+  MetadataParseResult ParsePayloadMetadata(const std::vector<char>& payload,
+                                           ErrorCode* error);
 
   void set_public_key_path(const std::string& public_key_path) {
     public_key_path_ = public_key_path;
@@ -186,6 +179,10 @@
   // and the manifest. Is the header was not yet parsed, returns zero.
   uint64_t GetMetadataSize() const;
 
+  // If the manifest was successfully parsed, copies it to |*out_manifest_p|.
+  // Returns true on success.
+  bool GetManifest(DeltaArchiveManifest* out_manifest_p) const;
+
  private:
   friend class DeltaPerformerTest;
   FRIEND_TEST(DeltaPerformerTest, IsIdempotentOperationTest);
@@ -264,8 +261,9 @@
       const DeltaArchiveManifest_InstallOperation& operation);
 
   // Updates the hash calculator with the bytes in |buffer_|. Then discard the
-  // content, ensuring that memory is being deallocated.
-  void DiscardBuffer();
+  // content, ensuring that memory is being deallocated. If |do_advance_offset|,
+  // advances the internal offset counter accordingly.
+  void DiscardBuffer(bool do_advance_offset);
 
   // Checkpoints the update progress into persistent storage to allow this
   // update attempt to be resumed after reboot.
@@ -306,6 +304,7 @@
   std::string kernel_path_;  // Path that kernel_fd_ refers to.
 
   DeltaArchiveManifest manifest_;
+  bool manifest_parsed_;
   bool manifest_valid_;
   uint64_t metadata_size_;