| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/files/file_enumerator.h" | 6 #include "base/files/file_enumerator.h" |
| 7 #include "base/threading/worker_pool.h" | 7 #include "base/threading/worker_pool.h" |
| 8 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" | 8 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" |
| 9 #include "chrome/browser/extensions/api/image_writer_private/operation.h" | 9 #include "chrome/browser/extensions/api/image_writer_private/operation.h" |
| 10 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h
" | 10 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h
" |
| 11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "third_party/zlib/google/zip.h" | 12 #include "third_party/zlib/google/zip.h" |
| 13 | 13 |
| 14 namespace extensions { | 14 namespace extensions { |
| 15 namespace image_writer { | 15 namespace image_writer { |
| 16 | 16 |
| 17 using content::BrowserThread; | 17 using content::BrowserThread; |
| 18 | 18 |
| 19 const int kBurningBlockSize = 8 * 1024; // 8 KiB | 19 const int kBurningBlockSize = 8 * 1024; // 8 KiB |
| 20 | 20 |
| 21 void Operation::Write(const base::Closure& continuation) { | 21 void Operation::WriteStart() { |
| 22 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 22 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 23 if (IsCancelled()) { | 23 if (IsCancelled()) { |
| 24 return; | 24 return; |
| 25 } | 25 } |
| 26 | 26 |
| 27 if (image_path_.empty()) { |
| 28 Error(error::kImageNotFound); |
| 29 return; |
| 30 } |
| 31 |
| 32 DVLOG(1) << "Starting write of " << image_path_.value() |
| 33 << " to " << storage_unit_id_; |
| 34 |
| 27 SetStage(image_writer_api::STAGE_WRITE); | 35 SetStage(image_writer_api::STAGE_WRITE); |
| 28 | 36 |
| 29 // TODO (haven): Unmount partitions before writing. http://crbug.com/284834 | 37 // TODO (haven): Unmount partitions before writing. http://crbug.com/284834 |
| 30 | 38 |
| 31 base::PlatformFileError result; | 39 scoped_ptr<image_writer_utils::ImageReader> reader( |
| 32 image_file_ = base::CreatePlatformFile( | 40 new image_writer_utils::ImageReader()); |
| 33 image_path_, | 41 scoped_ptr<image_writer_utils::ImageWriter> writer( |
| 34 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, | 42 new image_writer_utils::ImageWriter()); |
| 35 NULL, | 43 base::FilePath storage_path(storage_unit_id_); |
| 36 &result); | 44 |
| 37 if (result != base::PLATFORM_FILE_OK) { | 45 if (reader->Open(image_path_)) { |
| 46 if (!writer->Open(storage_path)) { |
| 47 reader->Close(); |
| 48 Error(error::kDeviceOpenError); |
| 49 return; |
| 50 } |
| 51 } else { |
| 38 Error(error::kImageOpenError); | 52 Error(error::kImageOpenError); |
| 39 return; | 53 return; |
| 40 } | 54 } |
| 41 | 55 |
| 42 device_file_ = base::CreatePlatformFile( | 56 BrowserThread::PostTask( |
| 43 device_path_, | 57 BrowserThread::FILE, |
| 44 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, | 58 FROM_HERE, |
| 45 NULL, | 59 base::Bind(&Operation::WriteChunk, |
| 46 &result); | 60 this, |
| 47 if (result != base::PLATFORM_FILE_OK) { | 61 base::Passed(&reader), |
| 48 Error(error::kDeviceOpenError); | 62 base::Passed(&writer), |
| 49 base::ClosePlatformFile(image_file_); | 63 0)); |
| 64 } |
| 65 |
| 66 void Operation::WriteChunk( |
| 67 scoped_ptr<image_writer_utils::ImageReader> reader, |
| 68 scoped_ptr<image_writer_utils::ImageWriter> writer, |
| 69 int64 bytes_written) { |
| 70 if (IsCancelled()) { |
| 71 WriteCleanUp(reader.Pass(), writer.Pass()); |
| 50 return; | 72 return; |
| 51 } | 73 } |
| 52 | 74 |
| 53 int64 total_size; | 75 char buffer[kBurningBlockSize]; |
| 54 base::GetFileSize(image_path_, &total_size); | 76 int64 image_size = reader->GetSize(); |
| 77 int len = reader->Read(buffer, kBurningBlockSize); |
| 55 | 78 |
| 56 BrowserThread::PostTask( | 79 if (len > 0) { |
| 57 BrowserThread::FILE, | 80 if (writer->Write(buffer, len) == len) { |
| 58 FROM_HERE, | 81 int percent_prev = kProgressComplete * bytes_written / image_size; |
| 59 base::Bind(&Operation::WriteChunk, this, 0, total_size, continuation)); | 82 int percent_curr = kProgressComplete * (bytes_written + len) / image_size; |
| 83 |
| 84 if (percent_curr > percent_prev) { |
| 85 SetProgress(percent_curr); |
| 86 } |
| 87 |
| 88 BrowserThread::PostTask( |
| 89 BrowserThread::FILE, |
| 90 FROM_HERE, |
| 91 base::Bind(&Operation::WriteChunk, |
| 92 this, |
| 93 base::Passed(&reader), |
| 94 base::Passed(&writer), |
| 95 bytes_written + len)); |
| 96 } else { |
| 97 WriteCleanUp(reader.Pass(), writer.Pass()); |
| 98 Error(error::kDeviceWriteError); |
| 99 } |
| 100 } else if (len == 0) { |
| 101 if (bytes_written == image_size) { |
| 102 if (WriteCleanUp(reader.Pass(), writer.Pass())) { |
| 103 BrowserThread::PostTask( |
| 104 BrowserThread::FILE, |
| 105 FROM_HERE, |
| 106 base::Bind(&Operation::WriteComplete, |
| 107 this)); |
| 108 } |
| 109 } else { |
| 110 WriteCleanUp(reader.Pass(), writer.Pass()); |
| 111 Error(error::kImageReadError); |
| 112 } |
| 113 } else { // len < 0 |
| 114 WriteCleanUp(reader.Pass(), writer.Pass()); |
| 115 Error(error::kImageReadError); |
| 116 } |
| 60 } | 117 } |
| 61 | 118 |
| 62 void Operation::WriteChunk(const int64& bytes_written, | 119 bool Operation::WriteCleanUp( |
| 63 const int64& total_size, | 120 scoped_ptr<image_writer_utils::ImageReader> reader, |
| 64 const base::Closure& continuation) { | 121 scoped_ptr<image_writer_utils::ImageWriter> writer) { |
| 65 if (!IsCancelled()) { | |
| 66 scoped_ptr<char[]> buffer(new char[kBurningBlockSize]); | |
| 67 int64 len = base::ReadPlatformFile( | |
| 68 image_file_, bytes_written, buffer.get(), kBurningBlockSize); | |
| 69 | 122 |
| 70 if (len > 0) { | 123 bool success = true; |
| 71 if (base::WritePlatformFile( | 124 if (!reader->Close()) { |
| 72 device_file_, bytes_written, buffer.get(), len) == len) { | 125 Error(error::kImageCloseError); |
| 73 int percent_curr = | 126 success = false; |
| 74 kProgressComplete * (bytes_written + len) / total_size; | |
| 75 | |
| 76 SetProgress(percent_curr); | |
| 77 | |
| 78 BrowserThread::PostTask(BrowserThread::FILE, | |
| 79 FROM_HERE, | |
| 80 base::Bind(&Operation::WriteChunk, | |
| 81 this, | |
| 82 bytes_written + len, | |
| 83 total_size, | |
| 84 continuation)); | |
| 85 return; | |
| 86 } else { | |
| 87 Error(error::kDeviceWriteError); | |
| 88 } | |
| 89 } else if (len == 0) { | |
| 90 WriteComplete(continuation); | |
| 91 } else { // len < 0 | |
| 92 Error(error::kImageReadError); | |
| 93 } | |
| 94 } | 127 } |
| 95 | 128 |
| 96 base::ClosePlatformFile(image_file_); | 129 if (!writer->Close()) { |
| 97 base::ClosePlatformFile(device_file_); | 130 Error(error::kDeviceCloseError); |
| 131 success = false; |
| 132 } |
| 133 return success; |
| 98 } | 134 } |
| 99 | 135 |
| 100 void Operation::WriteComplete(const base::Closure& continuation) { | 136 void Operation::WriteComplete() { |
| 137 |
| 138 DVLOG(2) << "Completed write of " << image_path_.value(); |
| 101 SetProgress(kProgressComplete); | 139 SetProgress(kProgressComplete); |
| 102 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation); | 140 |
| 141 if (verify_write_) { |
| 142 BrowserThread::PostTask(BrowserThread::FILE, |
| 143 FROM_HERE, |
| 144 base::Bind(&Operation::VerifyWriteStart, this)); |
| 145 } else { |
| 146 BrowserThread::PostTask(BrowserThread::FILE, |
| 147 FROM_HERE, |
| 148 base::Bind(&Operation::Finish, this)); |
| 149 } |
| 103 } | 150 } |
| 104 | 151 |
| 105 void Operation::VerifyWrite(const base::Closure& continuation) { | 152 void Operation::VerifyWriteStart() { |
| 106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 107 if (IsCancelled()) { | 154 if (IsCancelled()) { |
| 108 return; | 155 return; |
| 109 } | 156 } |
| 110 | 157 |
| 158 DVLOG(1) << "Starting verification stage."; |
| 159 |
| 111 SetStage(image_writer_api::STAGE_VERIFYWRITE); | 160 SetStage(image_writer_api::STAGE_VERIFYWRITE); |
| 112 | 161 |
| 113 base::PlatformFileError result; | 162 scoped_ptr<base::FilePath> image_path(new base::FilePath(image_path_)); |
| 114 | 163 |
| 115 image_file_ = base::CreatePlatformFile( | 164 GetMD5SumOfFile( |
| 116 image_path_, | 165 image_path.Pass(), |
| 117 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, | 166 -1, |
| 118 NULL, | 167 0, // progress_offset |
| 119 &result); | 168 50, // progress_scale |
| 120 if (result != base::PLATFORM_FILE_OK) { | 169 base::Bind(&Operation::VerifyWriteStage2, |
| 121 Error(error::kImageOpenError); | 170 this)); |
| 171 } |
| 172 |
| 173 void Operation::VerifyWriteStage2( |
| 174 scoped_ptr<std::string> image_hash) { |
| 175 DVLOG(1) << "Building MD5 sum of device: " << storage_unit_id_; |
| 176 |
| 177 int64 image_size; |
| 178 scoped_ptr<base::FilePath> device_path(new base::FilePath(storage_unit_id_)); |
| 179 |
| 180 if (!base::GetFileSize(image_path_, &image_size)){ |
| 181 Error(error::kImageSizeError); |
| 122 return; | 182 return; |
| 123 } | 183 } |
| 124 | 184 |
| 125 device_file_ = base::CreatePlatformFile( | 185 GetMD5SumOfFile( |
| 126 device_path_, | 186 device_path.Pass(), |
| 127 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, | 187 image_size, |
| 128 NULL, | 188 50, // progress_offset |
| 129 &result); | 189 50, // progress_scale |
| 130 if (result != base::PLATFORM_FILE_OK) { | 190 base::Bind(&Operation::VerifyWriteCompare, |
| 131 Error(error::kDeviceOpenError); | 191 this, |
| 132 base::ClosePlatformFile(image_file_); | 192 base::Passed(&image_hash))); |
| 193 } |
| 194 |
| 195 void Operation::VerifyWriteCompare( |
| 196 scoped_ptr<std::string> image_hash, |
| 197 scoped_ptr<std::string> device_hash) { |
| 198 DVLOG(1) << "Comparing hashes: " << *image_hash << " vs " << *device_hash; |
| 199 |
| 200 if (*image_hash != *device_hash) { |
| 201 Error(error::kVerificationFailed); |
| 133 return; | 202 return; |
| 134 } | 203 } |
| 135 | 204 |
| 136 int64 total_size; | 205 DVLOG(2) << "Completed write verification of " << image_path_.value(); |
| 137 base::GetFileSize(image_path_, &total_size); | |
| 138 | 206 |
| 139 BrowserThread::PostTask( | 207 SetProgress(kProgressComplete); |
| 140 BrowserThread::FILE, | |
| 141 FROM_HERE, | |
| 142 base::Bind( | |
| 143 &Operation::VerifyWriteChunk, this, 0, total_size, continuation)); | |
| 144 } | |
| 145 | 208 |
| 146 void Operation::VerifyWriteChunk(const int64& bytes_processed, | 209 Finish(); |
| 147 const int64& total_size, | |
| 148 const base::Closure& continuation) { | |
| 149 if (!IsCancelled()) { | |
| 150 scoped_ptr<char[]> source_buffer(new char[kBurningBlockSize]); | |
| 151 scoped_ptr<char[]> target_buffer(new char[kBurningBlockSize]); | |
| 152 | |
| 153 int64 image_bytes_read = base::ReadPlatformFile( | |
| 154 image_file_, bytes_processed, source_buffer.get(), kBurningBlockSize); | |
| 155 | |
| 156 if (image_bytes_read > 0) { | |
| 157 int64 device_bytes_read = base::ReadPlatformFile( | |
| 158 device_file_, bytes_processed, target_buffer.get(), image_bytes_read); | |
| 159 if (image_bytes_read == device_bytes_read && | |
| 160 memcmp(source_buffer.get(), target_buffer.get(), image_bytes_read) == | |
| 161 0) { | |
| 162 int percent_curr = kProgressComplete * | |
| 163 (bytes_processed + image_bytes_read) / total_size; | |
| 164 | |
| 165 SetProgress(percent_curr); | |
| 166 | |
| 167 BrowserThread::PostTask(BrowserThread::FILE, | |
| 168 FROM_HERE, | |
| 169 base::Bind(&Operation::VerifyWriteChunk, | |
| 170 this, | |
| 171 bytes_processed + image_bytes_read, | |
| 172 total_size, | |
| 173 continuation)); | |
| 174 return; | |
| 175 } else { | |
| 176 Error(error::kVerificationFailed); | |
| 177 } | |
| 178 } else if (image_bytes_read == 0) { | |
| 179 VerifyWriteComplete(continuation); | |
| 180 } else { // len < 0 | |
| 181 Error(error::kImageReadError); | |
| 182 } | |
| 183 } | |
| 184 | |
| 185 base::ClosePlatformFile(image_file_); | |
| 186 base::ClosePlatformFile(device_file_); | |
| 187 } | |
| 188 | |
| 189 void Operation::VerifyWriteComplete(const base::Closure& continuation) { | |
| 190 SetProgress(kProgressComplete); | |
| 191 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation); | |
| 192 } | 210 } |
| 193 | 211 |
| 194 } // namespace image_writer | 212 } // namespace image_writer |
| 195 } // namespace extensions | 213 } // namespace extensions |
| OLD | NEW |