Enable brotli_bsdiff in update package generation

Brotli_bsdiff uses brotli to compress the bsdiff patches; and almost
always yields a smaller result than the old bsdiff with bz2. So there
is little point to try both methods in update_engine.

Example: walleye 4504078 -> 4585723 with bsdiff cache enabled.
               time      size
bz2 only:      ~8min     494M
brotli only:   ~12min    455M
bz2 & bro:     ~16min    454M

In this example, trying bz2 merely saves ~1M at the cost of 4 minutes
generation time. So in this CL, we only use brotli_bsdiff if the operation
is allowed; and later we can optionally implement a new mode in bsdiff
to pick the compressor who generates the smallest patches.

Bug: 34220646
Test: Generate and verify a walleye package
Change-Id: Ideae1acea95ff204c2ba5b7637945307743f0a51
diff --git a/payload_generator/delta_diff_utils.cc b/payload_generator/delta_diff_utils.cc
index bcbc3a5..877e13f 100644
--- a/payload_generator/delta_diff_utils.cc
+++ b/payload_generator/delta_diff_utils.cc
@@ -40,6 +40,7 @@
 #include <base/threading/simple_thread.h>
 #include <brillo/data_encoding.h>
 #include <bsdiff/bsdiff.h>
+#include <bsdiff/patch_writer_factory.h>
 
 #include "update_engine/common/hash_calculator.h"
 #include "update_engine/common/subprocess.h"
@@ -73,6 +74,8 @@
 // memory intensive, so we limit these operations to 150 MiB.
 const uint64_t kMaxPuffdiffDestinationSize = 150 * 1024 * 1024;  // bytes
 
+const int kBrotliCompressionQuality = 11;
+
 // Process a range of blocks from |range_start| to |range_end| in the extent at
 // position |*idx_p| of |extents|. If |do_remove| is true, this range will be
 // removed, which may cause the extent to be trimmed, split or removed entirely.
@@ -740,21 +743,33 @@
         TEST_AND_RETURN_FALSE(base::CreateTemporaryFile(&patch));
         ScopedPathUnlinker unlinker(patch.value());
 
+        std::unique_ptr<bsdiff::PatchWriterInterface> bsdiff_patch_writer;
+        InstallOperation_Type operation_type = InstallOperation::BSDIFF;
+        if (version.OperationAllowed(InstallOperation::BROTLI_BSDIFF)) {
+          bsdiff_patch_writer =
+              bsdiff::CreateBSDF2PatchWriter(patch.value(),
+                                             bsdiff::CompressorType::kBrotli,
+                                             kBrotliCompressionQuality);
+          operation_type = InstallOperation::BROTLI_BSDIFF;
+        } else {
+          bsdiff_patch_writer = bsdiff::CreateBsdiffPatchWriter(patch.value());
+          if (version.OperationAllowed(InstallOperation::SOURCE_BSDIFF)) {
+            operation_type = InstallOperation::SOURCE_BSDIFF;
+          }
+        }
+
         brillo::Blob bsdiff_delta;
         TEST_AND_RETURN_FALSE(0 == bsdiff::bsdiff(old_data.data(),
                                                   old_data.size(),
                                                   new_data.data(),
                                                   new_data.size(),
-                                                  patch.value().c_str(),
+                                                  bsdiff_patch_writer.get(),
                                                   nullptr));
 
         TEST_AND_RETURN_FALSE(utils::ReadFile(patch.value(), &bsdiff_delta));
         CHECK_GT(bsdiff_delta.size(), static_cast<brillo::Blob::size_type>(0));
         if (bsdiff_delta.size() < data_blob.size()) {
-          operation.set_type(
-              version.OperationAllowed(InstallOperation::SOURCE_BSDIFF)
-                  ? InstallOperation::SOURCE_BSDIFF
-                  : InstallOperation::BSDIFF);
+          operation.set_type(operation_type);
           data_blob = std::move(bsdiff_delta);
         }
       }