Commit 2a6ea0f9 authored by Anand K. Mistry's avatar Anand K. Mistry Committed by Commit Bot

Move constants out of headers and into implementation files.

As a result, also use std::unique_ptr<char[]> instead of char[] class
members. This makes zip archiver more closely follow chromium style.

BUG=889703

Change-Id: I551557958beff280e344134f15be20a406fb5ea8
Reviewed-on: https://chromium-review.googlesource.com/1242643Reviewed-by: default avatarNoel Gordon <noel@chromium.org>
Commit-Queue: Anand Mistry <amistry@chromium.org>
Cr-Commit-Position: refs/heads/master@{#594621}
parent ecb6e0bb
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <algorithm> #include <algorithm>
#include <cerrno> #include <cerrno>
#include <cstring> #include <cstring>
#include <utility>
#include "base/time/time.h" #include "base/time/time.h"
#include "chrome/browser/resources/chromeos/zip_archiver/cpp/compressor_io_javascript_stream.h" #include "chrome/browser/resources/chromeos/zip_archiver/cpp/compressor_io_javascript_stream.h"
...@@ -15,6 +16,13 @@ ...@@ -15,6 +16,13 @@
namespace { namespace {
const char kCreateArchiveError[] = "Failed to create archive.";
const char kAddToArchiveError[] = "Failed to add entry to archive.";
const char kCloseArchiveError[] = "Failed to close archive.";
// We need at least 256KB for MiniZip.
const int64_t kMaximumDataChunkSize = 512 * 1024;
uint32_t UnixToDosdate(const int64_t datetime) { uint32_t UnixToDosdate(const int64_t datetime) {
tm tm_datetime; tm tm_datetime;
localtime_r(&datetime, &tm_datetime); localtime_r(&datetime, &tm_datetime);
...@@ -122,15 +130,11 @@ CompressorArchiveMinizip::CompressorArchiveMinizip( ...@@ -122,15 +130,11 @@ CompressorArchiveMinizip::CompressorArchiveMinizip(
: CompressorArchive(compressor_stream), : CompressorArchive(compressor_stream),
compressor_stream_(compressor_stream), compressor_stream_(compressor_stream),
zip_file_(nullptr), zip_file_(nullptr),
destination_buffer_(std::make_unique<char[]>(kMaximumDataChunkSize)),
offset_(0), offset_(0),
length_(0) { length_(0) {}
destination_buffer_ =
new char[compressor_stream_constants::kMaximumDataChunkSize];
}
CompressorArchiveMinizip::~CompressorArchiveMinizip() { CompressorArchiveMinizip::~CompressorArchiveMinizip() = default;
delete destination_buffer_;
}
bool CompressorArchiveMinizip::CreateArchive() { bool CompressorArchiveMinizip::CreateArchive() {
// Set up archive object. // Set up archive object.
...@@ -147,7 +151,7 @@ bool CompressorArchiveMinizip::CreateArchive() { ...@@ -147,7 +151,7 @@ bool CompressorArchiveMinizip::CreateArchive() {
zip_file_ = zipOpen2(nullptr /* pathname */, APPEND_STATUS_CREATE, zip_file_ = zipOpen2(nullptr /* pathname */, APPEND_STATUS_CREATE,
nullptr /* globalcomment */, &zip_funcs); nullptr /* globalcomment */, &zip_funcs);
if (!zip_file_) { if (!zip_file_) {
set_error_message(compressor_archive_constants::kCreateArchiveError); set_error_message(kCreateArchiveError);
return false /* Error */; return false /* Error */;
} }
return true /* Success */; return true /* Success */;
...@@ -204,7 +208,7 @@ bool CompressorArchiveMinizip::AddToArchive(const std::string& filename, ...@@ -204,7 +208,7 @@ bool CompressorArchiveMinizip::AddToArchive(const std::string& filename,
LANGUAGE_ENCODING_FLAG); // flagBase LANGUAGE_ENCODING_FLAG); // flagBase
if (open_result != ZIP_OK) { if (open_result != ZIP_OK) {
CloseArchive(true /* has_error */); CloseArchive(true /* has_error */);
set_error_message(compressor_archive_constants::kAddToArchiveError); set_error_message(kAddToArchiveError);
return false /* Error */; return false /* Error */;
} }
...@@ -212,12 +216,11 @@ bool CompressorArchiveMinizip::AddToArchive(const std::string& filename, ...@@ -212,12 +216,11 @@ bool CompressorArchiveMinizip::AddToArchive(const std::string& filename,
if (!is_directory) { if (!is_directory) {
int64_t remaining_size = file_size; int64_t remaining_size = file_size;
while (remaining_size > 0) { while (remaining_size > 0) {
int64_t chunk_size = std::min( int64_t chunk_size = std::min(remaining_size, kMaximumDataChunkSize);
remaining_size, compressor_stream_constants::kMaximumDataChunkSize);
PP_DCHECK(chunk_size > 0); PP_DCHECK(chunk_size > 0);
int64_t read_bytes = int64_t read_bytes =
compressor_stream_->Read(chunk_size, destination_buffer_); compressor_stream_->Read(chunk_size, destination_buffer_.get());
// Negative read_bytes indicates an error occurred when reading chunks. // Negative read_bytes indicates an error occurred when reading chunks.
// 0 just means there is no more data available, but here we need positive // 0 just means there is no more data available, but here we need positive
// length of bytes, so this is also an error here. // length of bytes, so this is also an error here.
...@@ -230,8 +233,8 @@ bool CompressorArchiveMinizip::AddToArchive(const std::string& filename, ...@@ -230,8 +233,8 @@ bool CompressorArchiveMinizip::AddToArchive(const std::string& filename,
break; break;
} }
if (zipWriteInFileInZip(zip_file_, destination_buffer_, read_bytes) != if (zipWriteInFileInZip(zip_file_, destination_buffer_.get(),
ZIP_OK) { read_bytes) != ZIP_OK) {
has_error = true; has_error = true;
break; break;
} }
...@@ -244,7 +247,7 @@ bool CompressorArchiveMinizip::AddToArchive(const std::string& filename, ...@@ -244,7 +247,7 @@ bool CompressorArchiveMinizip::AddToArchive(const std::string& filename,
if (has_error) { if (has_error) {
CloseArchive(true /* has_error */); CloseArchive(true /* has_error */);
set_error_message(compressor_archive_constants::kAddToArchiveError); set_error_message(kAddToArchiveError);
return false /* Error */; return false /* Error */;
} }
...@@ -258,12 +261,12 @@ bool CompressorArchiveMinizip::AddToArchive(const std::string& filename, ...@@ -258,12 +261,12 @@ bool CompressorArchiveMinizip::AddToArchive(const std::string& filename,
bool CompressorArchiveMinizip::CloseArchive(bool has_error) { bool CompressorArchiveMinizip::CloseArchive(bool has_error) {
if (zipClose(zip_file_, nullptr /* global_comment */) != ZIP_OK) { if (zipClose(zip_file_, nullptr /* global_comment */) != ZIP_OK) {
set_error_message(compressor_archive_constants::kCloseArchiveError); set_error_message(kCloseArchiveError);
return false /* Error */; return false /* Error */;
} }
if (!has_error) { if (!has_error) {
if (compressor_stream()->Flush() < 0) { if (compressor_stream()->Flush() < 0) {
set_error_message(compressor_archive_constants::kCloseArchiveError); set_error_message(kCloseArchiveError);
return false /* Error */; return false /* Error */;
} }
} }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_RESOURCES_CHROMEOS_ZIP_ARCHIVER_CPP_COMPRESSOR_ARCHIVE_MINIZIP_H_ #ifndef CHROME_BROWSER_RESOURCES_CHROMEOS_ZIP_ARCHIVER_CPP_COMPRESSOR_ARCHIVE_MINIZIP_H_
#define CHROME_BROWSER_RESOURCES_CHROMEOS_ZIP_ARCHIVER_CPP_COMPRESSOR_ARCHIVE_MINIZIP_H_ #define CHROME_BROWSER_RESOURCES_CHROMEOS_ZIP_ARCHIVER_CPP_COMPRESSOR_ARCHIVE_MINIZIP_H_
#include <memory>
#include <string> #include <string>
#include "chrome/browser/resources/chromeos/zip_archiver/cpp/compressor_archive.h" #include "chrome/browser/resources/chromeos/zip_archiver/cpp/compressor_archive.h"
...@@ -13,15 +14,6 @@ ...@@ -13,15 +14,6 @@
class CompressorStream; class CompressorStream;
// A namespace with constants used by CompressorArchiveMinizip.
namespace compressor_archive_constants {
const char kCreateArchiveError[] = "Failed to create archive.";
const char kAddToArchiveError[] = "Failed to add entry to archive.";
const char kCloseArchiveError[] = "Failed to close archive.";
} // namespace compressor_archive_constants
// A name space with custom functions passed to minizip. // A name space with custom functions passed to minizip.
namespace compressor_archive_functions { namespace compressor_archive_functions {
...@@ -98,7 +90,7 @@ class CompressorArchiveMinizip : public CompressorArchive { ...@@ -98,7 +90,7 @@ class CompressorArchiveMinizip : public CompressorArchive {
zipFile zip_file_; zipFile zip_file_;
// The buffer used to store the data read from JavaScript. // The buffer used to store the data read from JavaScript.
char* destination_buffer_; std::unique_ptr<char[]> destination_buffer_;
// The current offset of the zip archive file. // The current offset of the zip archive file.
int64_t offset_; int64_t offset_;
......
...@@ -11,6 +11,13 @@ ...@@ -11,6 +11,13 @@
#include "chrome/browser/resources/chromeos/zip_archiver/cpp/javascript_compressor_requestor_interface.h" #include "chrome/browser/resources/chromeos/zip_archiver/cpp/javascript_compressor_requestor_interface.h"
#include "ppapi/cpp/logging.h" #include "ppapi/cpp/logging.h"
namespace {
// We need at least 256KB for MiniZip.
const int64_t kMaximumDataChunkSize = 512 * 1024;
} // namespace
CompressorIOJavaScriptStream::CompressorIOJavaScriptStream( CompressorIOJavaScriptStream::CompressorIOJavaScriptStream(
JavaScriptCompressorRequestorInterface* requestor) JavaScriptCompressorRequestorInterface* requestor)
: requestor_(requestor), buffer_offset_(-1), buffer_data_length_(0) { : requestor_(requestor), buffer_offset_(-1), buffer_data_length_(0) {
...@@ -20,7 +27,7 @@ CompressorIOJavaScriptStream::CompressorIOJavaScriptStream( ...@@ -20,7 +27,7 @@ CompressorIOJavaScriptStream::CompressorIOJavaScriptStream(
pthread_mutex_lock(&shared_state_lock_); pthread_mutex_lock(&shared_state_lock_);
available_data_ = false; available_data_ = false;
buffer_ = new char[compressor_stream_constants::kMaximumDataChunkSize]; buffer_ = new char[kMaximumDataChunkSize];
pthread_mutex_unlock(&shared_state_lock_); pthread_mutex_unlock(&shared_state_lock_);
} }
...@@ -89,10 +96,9 @@ int64_t CompressorIOJavaScriptStream::Write(int64_t zip_offset, ...@@ -89,10 +96,9 @@ int64_t CompressorIOJavaScriptStream::Write(int64_t zip_offset,
// 4: The index to write is outside the buffer. // 4: The index to write is outside the buffer.
// If we want to write data outside the range, we first need to flush // If we want to write data outside the range, we first need to flush
// the buffer, and then cache the data in the buffer. // the buffer, and then cache the data in the buffer.
if (buffer_offset_ >= 0 && /* 1 */ if (buffer_offset_ >= 0 && /* 1 */
(current_offset != buffer_offset_ + buffer_data_length_ || /* 2 */ (current_offset != buffer_offset_ + buffer_data_length_ || /* 2 */
buffer_data_length_ + left_length > buffer_data_length_ + left_length > kMaximumDataChunkSize) && /* 3 */
compressor_stream_constants::kMaximumDataChunkSize) && /* 3 */
(current_offset < buffer_offset_ || (current_offset < buffer_offset_ ||
buffer_offset_ + buffer_data_length_ < buffer_offset_ + buffer_data_length_ <
current_offset + left_length) /* 4 */) { current_offset + left_length) /* 4 */) {
...@@ -104,8 +110,7 @@ int64_t CompressorIOJavaScriptStream::Write(int64_t zip_offset, ...@@ -104,8 +110,7 @@ int64_t CompressorIOJavaScriptStream::Write(int64_t zip_offset,
} }
// How many bytes we should copy to buffer_ in this iteration. // How many bytes we should copy to buffer_ in this iteration.
int64_t copy_length = std::min( int64_t copy_length = std::min(left_length, kMaximumDataChunkSize);
left_length, compressor_stream_constants::kMaximumDataChunkSize);
// Set up the buffer_offset_ if the buffer_ has no data. // Set up the buffer_offset_ if the buffer_ has no data.
if (buffer_offset_ == -1 /* initial state */) if (buffer_offset_ == -1 /* initial state */)
buffer_offset_ = current_offset; buffer_offset_ = current_offset;
......
...@@ -18,12 +18,6 @@ ...@@ -18,12 +18,6 @@
class JavaScriptCompressorRequestorInterface; class JavaScriptCompressorRequestorInterface;
// A namespace with constants used by CompressorArchiveMinizip.
namespace compressor_stream_constants {
// We need at least 256KB for MiniZip.
const int64_t kMaximumDataChunkSize = 512 * 1024;
} // namespace compressor_stream_constants
class CompressorIOJavaScriptStream : public CompressorStream { class CompressorIOJavaScriptStream : public CompressorStream {
public: public:
CompressorIOJavaScriptStream( CompressorIOJavaScriptStream(
......
...@@ -14,46 +14,6 @@ ...@@ -14,46 +14,6 @@
#include "chrome/browser/resources/chromeos/zip_archiver/cpp/volume_archive.h" #include "chrome/browser/resources/chromeos/zip_archiver/cpp/volume_archive.h"
// A namespace with constants used by VolumeArchiveMinizip.
namespace volume_archive_constants {
const char kArchiveReadNewError[] = "Could not allocate archive.";
const char kFileNotFound[] = "File not found for read data request.";
const char kVolumeReaderError[] = "VolumeReader failed to retrieve data.";
const char kArchiveOpenError[] = "Failed to open archive.";
const char kArchiveNextHeaderError[] =
"Failed to open current file in archive.";
const char kArchiveReadDataError[] = "Failed to read archive data.";
const char kArchiveReadFreeError[] = "Failed to close archive.";
// The size of the buffer used to skip unnecessary data. Should be positive and
// UINT16_MAX or less. unzReadCurrentFile in third_party/minizip/src/unzip.c
// supports to read a data up to UINT16_MAX at a time.
const int64_t kDummyBufferSize = UINT16_MAX; // ~64 KB
// The size of the buffer used by ReadInProgress to decompress data. Should be
// positive and UINT16_MAX or less. unzReadCurrentFile in
// third_party/minizip/src/unzip.c supports to read a data up to UINT16_MAX at a
// time.
const int64_t kDecompressBufferSize = UINT16_MAX; // ~64 KB.
// The maximum data chunk size for VolumeReader::Read requests.
// Should be positive.
const int64_t kMaximumDataChunkSize = 512 * 1024; // 512 KB.
// The minimum data chunk size for VolumeReader::Read requests.
// Should be positive.
const int64_t kMinimumDataChunkSize = 32 * 1024; // 16 KB.
// Maximum length of filename in zip archive.
const int kZipMaxPath = 256;
// The size of the static cache. We need at least 64KB to cache whole
// 'end of central directory' data.
const int64_t kStaticCacheSize = 128 * 1024;
} // namespace volume_archive_constants
class VolumeArchiveMinizip; class VolumeArchiveMinizip;
// A namespace with custom functions passed to minizip. // A namespace with custom functions passed to minizip.
...@@ -148,7 +108,7 @@ class VolumeArchiveMinizip : public VolumeArchive { ...@@ -148,7 +108,7 @@ class VolumeArchiveMinizip : public VolumeArchive {
// chunk is small, we load larger size of bytes from the archive and cache // chunk is small, we load larger size of bytes from the archive and cache
// them in dynamic_cache_. If the range of the next requested chunk is within // them in dynamic_cache_. If the range of the next requested chunk is within
// the cache, we don't read the archive and just return the data in the cache. // the cache, we don't read the archive and just return the data in the cache.
char dynamic_cache_[volume_archive_constants::kMaximumDataChunkSize]; std::unique_ptr<char[]> dynamic_cache_;
// The offset from which dynamic_cache_ has the data of the archive. // The offset from which dynamic_cache_ has the data of the archive.
int64_t dynamic_cache_offset_; int64_t dynamic_cache_offset_;
...@@ -163,7 +123,7 @@ class VolumeArchiveMinizip : public VolumeArchive { ...@@ -163,7 +123,7 @@ class VolumeArchiveMinizip : public VolumeArchive {
// cache a certain length of data from the end into static_cache_. The data // cache a certain length of data from the end into static_cache_. The data
// in this buffer is also used when the data in the central directory is // in this buffer is also used when the data in the central directory is
// requested by MiniZip later. // requested by MiniZip later.
char static_cache_[volume_archive_constants::kStaticCacheSize]; std::unique_ptr<char[]> static_cache_;
// The offset from which static_cache_ has the data of the archive. // The offset from which static_cache_ has the data of the archive.
int64_t static_cache_offset_; int64_t static_cache_offset_;
...@@ -197,7 +157,7 @@ class VolumeArchiveMinizip : public VolumeArchive { ...@@ -197,7 +157,7 @@ class VolumeArchiveMinizip : public VolumeArchive {
// offsets different from last_read_data_offset_. In this case some bytes // offsets different from last_read_data_offset_. In this case some bytes
// must be skipped. Because seeking is not possible inside compressed files, // must be skipped. Because seeking is not possible inside compressed files,
// the bytes will be discarded using this buffer. // the bytes will be discarded using this buffer.
char dummy_buffer_[volume_archive_constants::kDummyBufferSize]; std::unique_ptr<char[]> dummy_buffer_;
// The address where the decompressed data starting from // The address where the decompressed data starting from
// decompressed_offset_ is stored. It should point to a valid location // decompressed_offset_ is stored. It should point to a valid location
...@@ -207,8 +167,7 @@ class VolumeArchiveMinizip : public VolumeArchive { ...@@ -207,8 +167,7 @@ class VolumeArchiveMinizip : public VolumeArchive {
char* decompressed_data_; char* decompressed_data_;
// The actual buffer that contains the decompressed data. // The actual buffer that contains the decompressed data.
char decompressed_data_buffer_ std::unique_ptr<char[]> decompressed_data_buffer_;
[volume_archive_constants::kDecompressBufferSize];
// The size of valid data starting from decompressed_data_ that is stored // The size of valid data starting from decompressed_data_ that is stored
// inside decompressed_data_buffer_. // inside decompressed_data_buffer_.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment