Skip to content

Commit

Permalink
Add support to unzip from a string representation (youtube#699)
Browse files Browse the repository at this point in the history
This is needed for the in-memory updates feature, where the CRX package
will be kept in memory until it's unpacked. Separate commits will have
the changes to download the CRX to memory and verify a CRX from memory.

b/158043520
  • Loading branch information
hlwarriner committed Jun 28, 2023
1 parent ea48ac6 commit e977b5b
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 11 deletions.
16 changes: 16 additions & 0 deletions cobalt/updater/unzipper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

#include "cobalt/updater/unzipper.h"

#include <string>
#include <utility>

#include "base/callback.h"
#include "base/files/file_path.h"
#include "starboard/time.h"
Expand All @@ -40,6 +42,20 @@ class UnzipperImpl : public update_client::Unzipper {
LOG(INFO) << "Unzip took " << time_unzip_took / kSbTimeMillisecond
<< " milliseconds.";
}

#if defined(IN_MEMORY_UPDATES)
void Unzip(const std::string& zip_str, const base::FilePath& output_path,
UnzipCompleteCallback callback) override {
SbTimeMonotonic time_before_unzip = SbTimeGetMonotonicNow();
std::move(callback).Run(zip::Unzip(zip_str, output_path));
SbTimeMonotonic time_unzip_took =
SbTimeGetMonotonicNow() - time_before_unzip;
LOG(INFO) << "Unzip from string";
LOG(INFO) << "output_path = " << output_path;
LOG(INFO) << "Unzip took " << time_unzip_took / kSbTimeMillisecond
<< " milliseconds.";
}
#endif
};

} // namespace
Expand Down
7 changes: 7 additions & 0 deletions components/update_client/unzip/unzip_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class UnzipperImpl : public Unzipper {
UnzipCompleteCallback callback) override {
unzip::Unzip(callback_.Run(), zip_file, destination, std::move(callback));
}
#if defined(IN_MEMORY_UPDATES)
void Unzip(const std::string& zip_str,
const base::FilePath& destination,
UnzipCompleteCallback callback) override {
unzip::Unzip(callback_.Run(), zip_str, destination, std::move(callback));
}
#endif

private:
const UnzipChromiumFactory::Callback callback_;
Expand Down
11 changes: 10 additions & 1 deletion components/update_client/unzip/unzip_impl_cobalt.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Copyright 2019 The Cobalt Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/update_client/unzip/unzip_impl_cobalt.h"

#include <string>
#include <utility>

#include "base/callback.h"
#include "base/files/file_path.h"
#include "starboard/time.h"
Expand All @@ -23,6 +25,13 @@ class UnzipperImpl : public update_client::Unzipper {
UnzipCompleteCallback callback) override {
std::move(callback).Run(zip::Unzip(zip_path, output_path));
}
#if defined(IN_MEMORY_UPDATES)
void Unzip(const std::string& zip_str,
const base::FilePath& output_path,
UnzipCompleteCallback callback) override {
std::move(callback).Run(zip::Unzip(zip_str, output_path));
}
#endif
};

} // namespace
Expand Down
6 changes: 6 additions & 0 deletions components/update_client/unzipper.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ class Unzipper {
const base::FilePath& destination,
UnzipCompleteCallback callback) = 0;

#if defined(IN_MEMORY_UPDATES)
virtual void Unzip(const std::string& zip_str,
const base::FilePath& destination,
UnzipCompleteCallback callback) = 0;
#endif

protected:
Unzipper() = default;

Expand Down
11 changes: 11 additions & 0 deletions starboard/evergreen/shared/platform_configuration/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ config("platform_configuration") {
# By default, <EGL/eglplatform.h> pulls in some X11 headers that have some
# nasty macros (|Status|, for example) that conflict with Chromium base.
"MESA_EGL_NO_X11_HEADERS",

# During Evergreen updates the CRX package is kept in-memory, instead of
# on the file system, before getting unpacked.
# TODO(b/158043520): we need to make significant customizations to Chromium
# code to implement this feature and this macro allows us to switch back
# to the legacy behavior during development to verify the customizations
# are made cleanly. Once the feature is complete we may want to remove
# existing customizations that enable the legacy behavior for Cobalt builds,
# change IN_MEMORY_UPDATES references to USE_COBALT_CUSTOMIZATIONS
# references, and remove this macro.
"IN_MEMORY_UPDATES",
]

if (is_debug) {
Expand Down
67 changes: 59 additions & 8 deletions third_party/zlib/google/zip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ bool Unzip(const base::FilePath& src_file, const base::FilePath& dest_dir) {
return UnzipWithFilterCallback(
src_file, dest_dir, base::BindRepeating(&ExcludeNoFilesFilter), true);
}
#if defined(IN_MEMORY_UPDATES)
bool Unzip(const std::string& src_str, const base::FilePath& dest_dir) {
return UnzipWithFilterCallback(
src_str, dest_dir, base::BindRepeating(&ExcludeNoFilesFilter), true);
}
#endif


bool UnzipWithFilterCallback(const base::FilePath& src_file,
const base::FilePath& dest_dir,
Expand All @@ -200,17 +207,24 @@ bool UnzipWithFilterCallback(const base::FilePath& src_file,
#endif
}

#if defined(IN_MEMORY_UPDATES)
bool UnzipWithFilterCallback(const std::string& src_str,
const base::FilePath& dest_dir,
const FilterCallback& filter_cb,
bool log_skipped_files) {
return UnzipWithFilterAndWriters(
src_str, base::BindRepeating(&CreateFilePathWriterDelegate, dest_dir),
base::BindRepeating(&CreateDirectory, dest_dir), filter_cb,
log_skipped_files);
}
#endif

#if defined(STARBOARD)
bool UnzipWithFilterAndWriters(const base::FilePath& src_file,
bool UnzipWithFilterAndWriters(ZipReader& reader,
const WriterFactory& writer_factory,
const DirectoryCreator& directory_creator,
const FilterCallback& filter_cb,
bool log_skipped_files) {
ZipReader reader;
if (!reader.Open(src_file)) {
DLOG(WARNING) << "Failed to open src_file " << src_file;
return false;
}
while (reader.HasMore()) {
if (!reader.OpenCurrentEntryInZip()) {
DLOG(WARNING) << "Failed to open the current file in zip";
Expand Down Expand Up @@ -244,7 +258,44 @@ bool UnzipWithFilterAndWriters(const base::FilePath& src_file,
}
return true;
}
#else

bool UnzipWithFilterAndWriters(const base::FilePath& src_file,
const WriterFactory& writer_factory,
const DirectoryCreator& directory_creator,
const FilterCallback& filter_cb,
bool log_skipped_files) {
ZipReader reader;
if (!reader.Open(src_file)) {
DLOG(WARNING) << "Failed to open src_file " << src_file;
return false;
}
return UnzipWithFilterAndWriters(reader,
writer_factory,
directory_creator,
filter_cb,
log_skipped_files);
}

#if defined(IN_MEMORY_UPDATES)
bool UnzipWithFilterAndWriters(const std::string& src_str,
const WriterFactory& writer_factory,
const DirectoryCreator& directory_creator,
const FilterCallback& filter_cb,
bool log_skipped_files) {
ZipReader reader;
if (!reader.OpenFromString(src_str)) {
DLOG(WARNING) << "Failed to open src_str";
return false;
}
return UnzipWithFilterAndWriters(reader,
writer_factory,
directory_creator,
filter_cb,
log_skipped_files);
}
#endif // defined(IN_MEMORY_UPDATES)
#else // defined(STARBOARD)

bool UnzipWithFilterAndWriters(const base::PlatformFile& src_file,
const WriterFactory& writer_factory,
const DirectoryCreator& directory_creator,
Expand Down Expand Up @@ -288,7 +339,7 @@ bool UnzipWithFilterAndWriters(const base::PlatformFile& src_file,
}
return true;
}
#endif
#endif // STARBOARD

bool ZipWithFilterCallback(const base::FilePath& src_dir,
const base::FilePath& dest_file,
Expand Down
24 changes: 22 additions & 2 deletions third_party/zlib/google/zip.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ bool UnzipWithFilterCallback(const base::FilePath& zip_file,
const FilterCallback& filter_cb,
bool log_skipped_files);

#if defined(IN_MEMORY_UPDATES)
// An overload for a zip represented as a string.
bool UnzipWithFilterCallback(const std::string& zip_str,
const base::FilePath& dest_dir,
const FilterCallback& filter_cb,
bool log_skipped_files);
#endif

// Unzip the contents of zip_file, using the writers provided by writer_factory.
// For each file in zip_file, include it only if the callback |filter_cb|
// returns true. Otherwise omit it.
Expand All @@ -174,16 +182,28 @@ bool UnzipWithFilterAndWriters(const base::FilePath& zip_file,
const DirectoryCreator& directory_creator,
const FilterCallback& filter_cb,
bool log_skipped_files);
#else
#if defined(IN_MEMORY_UPDATES)
// An overload for a zip represented as a string.
bool UnzipWithFilterAndWriters(const std::string& zip_str,
const WriterFactory& writer_factory,
const DirectoryCreator& directory_creator,
const FilterCallback& filter_cb,
bool log_skipped_files);
#endif // defined(IN_MEMORY_UPDATES)
#else // defined(STARBOARD)
bool UnzipWithFilterAndWriters(const base::PlatformFile& zip_file,
const WriterFactory& writer_factory,
const DirectoryCreator& directory_creator,
const FilterCallback& filter_cb,
bool log_skipped_files);
#endif
#endif // defined(STARBOARD)

// Unzip the contents of zip_file into dest_dir.
bool Unzip(const base::FilePath& zip_file, const base::FilePath& dest_dir);
#if defined(IN_MEMORY_UPDATES)
// Unzip the contents of zip_str into dest_dir.
bool Unzip(const std::string& zip_str, const base::FilePath& dest_dir);
#endif

} // namespace zip

Expand Down
Loading

0 comments on commit e977b5b

Please sign in to comment.