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..f370ce2ed0c6e319fdf3159f0c79b55d0a701c68 100644 |
--- a/chrome/browser/extensions/api/image_writer_private/operation_linux.cc |
+++ b/chrome/browser/extensions/api/image_writer_private/operation_linux.cc |
@@ -18,7 +18,7 @@ using content::BrowserThread; |
const int kBurningBlockSize = 8 * 1024; // 8 KiB |
-void Operation::WriteStart() { |
+void Operation::Write(const base::Closure& continuation) { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
if (IsCancelled()) { |
return; |
@@ -29,57 +29,79 @@ void Operation::WriteStart() { |
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 |
+ base::PlatformFileError result; |
- 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 { |
+ base::PlatformFile source = base::CreatePlatformFile( |
+ image_path_, |
+ base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, |
+ NULL, |
+ &result); |
+ |
+ if (result != base::PLATFORM_FILE_OK) { |
+ DLOG(ERROR) << "Failed to open source: " << result; |
Error(error::kImageOpenError); |
return; |
} |
+ AddCleanUpFunction(base::Bind( |
+ base::IgnoreResult(&base::ClosePlatformFile), |
+ source)); |
+ |
+ base::PlatformFile target = base::CreatePlatformFile( |
+ device_path_, |
+ base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
+ NULL, |
+ &result); |
+ |
+ if (result != base::PLATFORM_FILE_OK) { |
+ DLOG(ERROR) << "Failed to open target: " << result; |
+ Error(error::kDeviceOpenError); |
+ return; |
+ } |
+ |
+ AddCleanUpFunction(base::Bind( |
+ base::IgnoreResult(&base::ClosePlatformFile), |
+ target)); |
+ |
+ int64 total_size; |
+ base::GetFileSize(image_path_, &total_size); |
+ |
BrowserThread::PostTask( |
BrowserThread::FILE, |
FROM_HERE, |
base::Bind(&Operation::WriteChunk, |
this, |
- base::Passed(&reader), |
- base::Passed(&writer), |
- 0)); |
+ source, |
+ target, |
+ 0, |
+ total_size, |
+ continuation)); |
} |
void Operation::WriteChunk( |
- scoped_ptr<image_writer_utils::ImageReader> reader, |
- scoped_ptr<image_writer_utils::ImageWriter> writer, |
- int64 bytes_written) { |
+ base::PlatformFile source, |
+ base::PlatformFile target, |
+ const int64& bytes_written, |
+ const int64& total_size, |
+ const base::Closure& continuation) { |
if (IsCancelled()) { |
- WriteCleanUp(reader.Pass(), writer.Pass()); |
return; |
} |
- char buffer[kBurningBlockSize]; |
- int64 image_size = reader->GetSize(); |
- int len = reader->Read(buffer, kBurningBlockSize); |
+ scoped_ptr<char[]> buffer(new char[kBurningBlockSize]); |
+ int64 len = base::ReadPlatformFile(source, |
+ bytes_written, |
+ buffer.get(), |
+ 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 (base::WritePlatformFile(target, bytes_written, buffer.get(), len) == |
+ len) { |
+ int percent_prev = kProgressComplete * bytes_written / total_size; |
+ int percent_curr = kProgressComplete * (bytes_written + len) / total_size; |
if (percent_curr > percent_prev) { |
SetProgress(percent_curr); |
@@ -90,92 +112,47 @@ void Operation::WriteChunk( |
FROM_HERE, |
base::Bind(&Operation::WriteChunk, |
this, |
- base::Passed(&reader), |
- base::Passed(&writer), |
- bytes_written + len)); |
+ source, |
+ target, |
+ bytes_written + len, |
+ total_size, |
+ continuation)); |
} 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); |
- } |
+ WriteComplete(continuation); |
} else { // len < 0 |
- WriteCleanUp(reader.Pass(), writer.Pass()); |
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; |
- } |
- |
- if (!writer->Close()) { |
- Error(error::kDeviceCloseError); |
- success = false; |
- } |
- return success; |
-} |
- |
-void Operation::WriteComplete() { |
- |
- DVLOG(2) << "Completed write of " << image_path_.value(); |
+void Operation::WriteComplete(const base::Closure& continuation) { |
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)); |
- } |
+ continuation.Run(); |
} |
-void Operation::VerifyWriteStart() { |
+void Operation::VerifyWrite(const base::Closure& continuation) { |
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, |
+ image_path_, |
+ -1L, |
0, // progress_offset |
50, // progress_scale |
base::Bind(&Operation::VerifyWriteStage2, |
- this)); |
+ this, |
+ continuation)); |
} |
-void Operation::VerifyWriteStage2( |
- scoped_ptr<std::string> image_hash) { |
- DVLOG(1) << "Building MD5 sum of device: " << storage_unit_id_; |
- |
+void Operation::VerifyWriteStage2(const base::Closure& continuation, |
+ const std::string& image_hash) { |
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); |
@@ -183,30 +160,28 @@ void Operation::VerifyWriteStage2( |
} |
GetMD5SumOfFile( |
- device_path.Pass(), |
+ device_path_, |
image_size, |
50, // progress_offset |
50, // progress_scale |
base::Bind(&Operation::VerifyWriteCompare, |
this, |
- base::Passed(&image_hash))); |
+ continuation, |
+ image_hash)); |
} |
void Operation::VerifyWriteCompare( |
- scoped_ptr<std::string> image_hash, |
- scoped_ptr<std::string> device_hash) { |
- DVLOG(1) << "Comparing hashes: " << *image_hash << " vs " << *device_hash; |
- |
- if (*image_hash != *device_hash) { |
+ const base::Closure& continuation, |
+ const std::string& image_hash, |
+ const std::string& device_hash) { |
+ if (image_hash != device_hash) { |
Error(error::kVerificationFailed); |
return; |
} |
- DVLOG(2) << "Completed write verification of " << image_path_.value(); |
- |
SetProgress(kProgressComplete); |
- Finish(); |
+ continuation.Run(); |
} |
} // namespace image_writer |