Index: chrome/browser/extensions/api/image_writer_private/operation_linux.cc |
diff --git a/chrome/browser/extensions/api/image_writer_private/operation_linux.cc b/chrome/browser/extensions/api/image_writer_private/operation_linux.cc |
index df34834b6e8579852ed0fe2c7bcbc586ed073f04..73697e6a3f5db98cb451772fcf6dae26fc428cde 100644 |
--- a/chrome/browser/extensions/api/image_writer_private/operation_linux.cc |
+++ b/chrome/browser/extensions/api/image_writer_private/operation_linux.cc |
@@ -9,204 +9,71 @@ |
#include "chrome/browser/extensions/api/image_writer_private/operation.h" |
#include "chrome/browser/extensions/api/image_writer_private/operation_manager.h" |
#include "content/public/browser/browser_thread.h" |
-#include "third_party/zlib/google/zip.h" |
namespace extensions { |
namespace image_writer { |
using content::BrowserThread; |
-const int kBurningBlockSize = 8 * 1024; // 8 KiB |
- |
void Operation::WriteStart() { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
if (IsCancelled()) { |
return; |
} |
- if (image_path_.empty()) { |
- Error(error::kImageNotFound); |
- return; |
- } |
- |
- DVLOG(1) << "Starting write of " << image_path_.value() |
- << " to " << storage_unit_id_; |
- |
SetStage(image_writer_api::STAGE_WRITE); |
- // TODO (haven): Unmount partitions before writing. http://crbug.com/284834 |
- |
- scoped_ptr<image_writer_utils::ImageReader> reader( |
- new image_writer_utils::ImageReader()); |
- scoped_ptr<image_writer_utils::ImageWriter> writer( |
- new image_writer_utils::ImageWriter()); |
base::FilePath storage_path(storage_unit_id_); |
- if (reader->Open(image_path_)) { |
- if (!writer->Open(storage_path)) { |
- reader->Close(); |
- Error(error::kDeviceOpenError); |
- return; |
- } |
- } else { |
- Error(error::kImageOpenError); |
- return; |
- } |
- |
- BrowserThread::PostTask( |
- BrowserThread::FILE, |
- FROM_HERE, |
- base::Bind(&Operation::WriteChunk, |
- this, |
- base::Passed(&reader), |
- base::Passed(&writer), |
- 0)); |
-} |
- |
-void Operation::WriteChunk( |
- scoped_ptr<image_writer_utils::ImageReader> reader, |
- scoped_ptr<image_writer_utils::ImageWriter> writer, |
- int64 bytes_written) { |
- if (IsCancelled()) { |
- WriteCleanUp(reader.Pass(), writer.Pass()); |
- return; |
- } |
+ StartUtilityClient(); |
- char buffer[kBurningBlockSize]; |
- int64 image_size = reader->GetSize(); |
- int len = reader->Read(buffer, kBurningBlockSize); |
- |
- if (len > 0) { |
- if (writer->Write(buffer, len) == len) { |
- int percent_prev = kProgressComplete * bytes_written / image_size; |
- int percent_curr = kProgressComplete * (bytes_written + len) / image_size; |
- |
- if (percent_curr > percent_prev) { |
- SetProgress(percent_curr); |
- } |
- |
- BrowserThread::PostTask( |
- BrowserThread::FILE, |
- FROM_HERE, |
- base::Bind(&Operation::WriteChunk, |
- this, |
- base::Passed(&reader), |
- base::Passed(&writer), |
- bytes_written + len)); |
- } else { |
- WriteCleanUp(reader.Pass(), writer.Pass()); |
- Error(error::kDeviceWriteError); |
- } |
- } else if (len == 0) { |
- if (bytes_written == image_size) { |
- if (WriteCleanUp(reader.Pass(), writer.Pass())) { |
- BrowserThread::PostTask( |
- BrowserThread::FILE, |
- FROM_HERE, |
- base::Bind(&Operation::WriteComplete, |
- this)); |
- } |
- } else { |
- WriteCleanUp(reader.Pass(), writer.Pass()); |
- Error(error::kImageReadError); |
- } |
- } else { // len < 0 |
- WriteCleanUp(reader.Pass(), writer.Pass()); |
+ int64 file_size; |
+ if (!base::GetFileSize(image_path_, &file_size)) { |
Error(error::kImageReadError); |
- } |
-} |
- |
-bool Operation::WriteCleanUp( |
- scoped_ptr<image_writer_utils::ImageReader> reader, |
- scoped_ptr<image_writer_utils::ImageWriter> writer) { |
- |
- bool success = true; |
- if (!reader->Close()) { |
- Error(error::kImageCloseError); |
- success = false; |
+ return; |
} |
- if (!writer->Close()) { |
- Error(error::kDeviceCloseError); |
- success = false; |
- } |
- return success; |
-} |
- |
-void Operation::WriteComplete() { |
- |
- DVLOG(2) << "Completed write of " << image_path_.value(); |
- SetProgress(kProgressComplete); |
- |
- if (verify_write_) { |
- BrowserThread::PostTask(BrowserThread::FILE, |
- FROM_HERE, |
- base::Bind(&Operation::VerifyWriteStart, this)); |
- } else { |
- BrowserThread::PostTask(BrowserThread::FILE, |
- FROM_HERE, |
- base::Bind(&Operation::Finish, this)); |
- } |
+ BrowserThread::PostTask( |
+ BrowserThread::IO, |
+ FROM_HERE, |
+ base::Bind(&ImageWriterUtilityClient::Write, |
+ image_writer_client_, |
+ base::Bind(&Operation::WriteImageProgress, this, file_size), |
+ base::Bind(&Operation::VerifyWriteStart, this), |
+ base::Bind(&Operation::Error, this), |
+ image_path_, |
+ storage_path)); |
} |
void Operation::VerifyWriteStart() { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
+ |
if (IsCancelled()) { |
return; |
} |
- DVLOG(1) << "Starting verification stage."; |
- |
SetStage(image_writer_api::STAGE_VERIFYWRITE); |
- scoped_ptr<base::FilePath> image_path(new base::FilePath(image_path_)); |
- |
- GetMD5SumOfFile( |
- image_path.Pass(), |
- -1, |
- 0, // progress_offset |
- 50, // progress_scale |
- base::Bind(&Operation::VerifyWriteStage2, |
- this)); |
-} |
- |
-void Operation::VerifyWriteStage2( |
- scoped_ptr<std::string> image_hash) { |
- DVLOG(1) << "Building MD5 sum of device: " << storage_unit_id_; |
- |
- int64 image_size; |
- scoped_ptr<base::FilePath> device_path(new base::FilePath(storage_unit_id_)); |
- |
- if (!base::GetFileSize(image_path_, &image_size)){ |
- Error(error::kImageSizeError); |
- return; |
- } |
- |
- GetMD5SumOfFile( |
- device_path.Pass(), |
- image_size, |
- 50, // progress_offset |
- 50, // progress_scale |
- base::Bind(&Operation::VerifyWriteCompare, |
- this, |
- base::Passed(&image_hash))); |
-} |
+ base::FilePath storage_path(storage_unit_id_); |
-void Operation::VerifyWriteCompare( |
- scoped_ptr<std::string> image_hash, |
- scoped_ptr<std::string> device_hash) { |
- DVLOG(1) << "Comparing hashes: " << *image_hash << " vs " << *device_hash; |
+ StartUtilityClient(); |
- if (*image_hash != *device_hash) { |
- Error(error::kVerificationFailed); |
+ int64 file_size; |
+ if (!base::GetFileSize(image_path_, &file_size)) { |
+ Error(error::kImageReadError); |
return; |
} |
- DVLOG(2) << "Completed write verification of " << image_path_.value(); |
- |
- SetProgress(kProgressComplete); |
- |
- Finish(); |
+ BrowserThread::PostTask( |
+ BrowserThread::IO, |
+ FROM_HERE, |
+ base::Bind(&ImageWriterUtilityClient::Verify, |
+ image_writer_client_, |
+ base::Bind(&Operation::WriteImageProgress, this, file_size), |
+ base::Bind(&Operation::Finish, this), |
+ base::Bind(&Operation::Error, this), |
+ image_path_, |
+ storage_path)); |
} |
} // namespace image_writer |