Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: chrome/browser/extensions/api/image_writer_private/operation_linux.cc

Issue 149313003: Significantly cleans up the ImageWriter Operation class and subclasses. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removes self-reference that was causing the operation to not be deleted. Removes some extra loggin… Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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::WriteStart() { 21 bool Operation::OpenPlatformFiles() {
22 base::PlatformFileError result;
23
24 if (source_ == base::kInvalidPlatformFileValue) {
25 source_ = base::CreatePlatformFile(
26 image_path_,
27 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ,
28 NULL,
29 &result);
30
31 if (result != base::PLATFORM_FILE_OK) {
32 DLOG(ERROR) << "Failed to open source(" << result
33 << "): " << image_path_.value();
34 Error(error::kImageOpenError);
35 return false;
36 }
37
38 source_closer_.reset(&source_);
39 }
40
41 if (target_ == base::kInvalidPlatformFileValue) {
42 target_ = base::CreatePlatformFile(device_path_,
43 base::PLATFORM_FILE_OPEN |
44 base::PLATFORM_FILE_WRITE |
45 base::PLATFORM_FILE_READ,
46 NULL,
47 &result);
48
49 if (result != base::PLATFORM_FILE_OK) {
50 DLOG(ERROR) << "Failed to open target(" << result
51 << "): " << device_path_.value();
52 Error(error::kDeviceOpenError);
53 return false;
54 }
55
56 target_closer_.reset(&target_);
57 }
58
59 return true;
60 }
61
62 void Operation::Write(const base::Closure& continuation) {
22 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
23 if (IsCancelled()) { 64 if (IsCancelled()) {
24 return; 65 return;
25 } 66 }
26 67
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
35 SetStage(image_writer_api::STAGE_WRITE); 68 SetStage(image_writer_api::STAGE_WRITE);
36 69
37 // TODO (haven): Unmount partitions before writing. http://crbug.com/284834 70 // TODO (haven): Unmount partitions before writing. http://crbug.com/284834
38 71
39 scoped_ptr<image_writer_utils::ImageReader> reader( 72 if (!OpenPlatformFiles())
40 new image_writer_utils::ImageReader()); 73 return;
41 scoped_ptr<image_writer_utils::ImageWriter> writer(
42 new image_writer_utils::ImageWriter());
43 base::FilePath storage_path(storage_unit_id_);
44 74
45 if (reader->Open(image_path_)) { 75 int64 total_size;
46 if (!writer->Open(storage_path)) { 76 base::GetFileSize(image_path_, &total_size);
47 reader->Close(); 77
48 Error(error::kDeviceOpenError); 78 BrowserThread::PostTask(
49 return; 79 BrowserThread::FILE,
50 } 80 FROM_HERE,
51 } else { 81 base::Bind(&Operation::WriteChunk, this, 0, total_size, continuation));
52 Error(error::kImageOpenError); 82 }
83
84 void Operation::WriteChunk(const int64& bytes_written,
85 const int64& total_size,
86 const base::Closure& continuation) {
87 if (IsCancelled()) {
53 return; 88 return;
54 } 89 }
55 90
56 BrowserThread::PostTask( 91 scoped_ptr<char[]> buffer(new char[kBurningBlockSize]);
57 BrowserThread::FILE, 92 int64 len = base::ReadPlatformFile(
58 FROM_HERE, 93 source_, bytes_written, buffer.get(), kBurningBlockSize);
59 base::Bind(&Operation::WriteChunk,
60 this,
61 base::Passed(&reader),
62 base::Passed(&writer),
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());
72 return;
73 }
74
75 char buffer[kBurningBlockSize];
76 int64 image_size = reader->GetSize();
77 int len = reader->Read(buffer, kBurningBlockSize);
78 94
79 if (len > 0) { 95 if (len > 0) {
80 if (writer->Write(buffer, len) == len) { 96 if (base::WritePlatformFile(target_, bytes_written, buffer.get(), len) ==
81 int percent_prev = kProgressComplete * bytes_written / image_size; 97 len) {
82 int percent_curr = kProgressComplete * (bytes_written + len) / image_size; 98 int percent_curr = kProgressComplete * (bytes_written + len) / total_size;
83 99
84 if (percent_curr > percent_prev) { 100 SetProgress(percent_curr);
85 SetProgress(percent_curr);
86 }
87 101
88 BrowserThread::PostTask( 102 BrowserThread::PostTask(BrowserThread::FILE,
89 BrowserThread::FILE, 103 FROM_HERE,
90 FROM_HERE, 104 base::Bind(&Operation::WriteChunk,
91 base::Bind(&Operation::WriteChunk, 105 this,
92 this, 106 bytes_written + len,
93 base::Passed(&reader), 107 total_size,
94 base::Passed(&writer), 108 continuation));
95 bytes_written + len));
96 } else { 109 } else {
97 WriteCleanUp(reader.Pass(), writer.Pass());
98 Error(error::kDeviceWriteError); 110 Error(error::kDeviceWriteError);
99 } 111 }
100 } else if (len == 0) { 112 } else if (len == 0) {
101 if (bytes_written == image_size) { 113 WriteComplete(continuation);
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 } else { // len < 0
114 WriteCleanUp(reader.Pass(), writer.Pass());
115 Error(error::kImageReadError); 115 Error(error::kImageReadError);
116 } 116 }
117 } 117 }
118 118
119 bool Operation::WriteCleanUp( 119 void Operation::WriteComplete(const base::Closure& continuation) {
120 scoped_ptr<image_writer_utils::ImageReader> reader, 120 SetProgress(kProgressComplete);
121 scoped_ptr<image_writer_utils::ImageWriter> writer) { 121 continuation.Run();
122
123 bool success = true;
124 if (!reader->Close()) {
125 Error(error::kImageCloseError);
126 success = false;
127 }
128
129 if (!writer->Close()) {
130 Error(error::kDeviceCloseError);
131 success = false;
132 }
133 return success;
134 } 122 }
135 123
136 void Operation::WriteComplete() { 124 void Operation::VerifyWrite(const base::Closure& continuation) {
137
138 DVLOG(2) << "Completed write of " << image_path_.value();
139 SetProgress(kProgressComplete);
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 }
150 }
151
152 void Operation::VerifyWriteStart() {
153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
154 if (IsCancelled()) { 126 if (IsCancelled()) {
155 return; 127 return;
156 } 128 }
157 129
158 DVLOG(1) << "Starting verification stage.";
159
160 SetStage(image_writer_api::STAGE_VERIFYWRITE); 130 SetStage(image_writer_api::STAGE_VERIFYWRITE);
161 131
162 scoped_ptr<base::FilePath> image_path(new base::FilePath(image_path_)); 132 if (!OpenPlatformFiles())
tbarzic 2014/02/13 06:58:52 Is this needed? I think you should either reset fi
Drew Haven 2014/02/13 20:48:10 So this was to save us closing and reopening the f
133 return;
163 134
164 GetMD5SumOfFile( 135 int64 total_size;
165 image_path.Pass(), 136 base::GetFileSize(image_path_, &total_size);
166 -1, 137
167 0, // progress_offset 138 BrowserThread::PostTask(
168 50, // progress_scale 139 BrowserThread::FILE,
169 base::Bind(&Operation::VerifyWriteStage2, 140 FROM_HERE,
170 this)); 141 base::Bind(
142 &Operation::VerifyWriteChunk, this, 0, total_size, continuation));
171 } 143 }
172 144
173 void Operation::VerifyWriteStage2( 145 void Operation::VerifyWriteChunk(const int64& bytes_processed,
174 scoped_ptr<std::string> image_hash) { 146 const int64& total_size,
175 DVLOG(1) << "Building MD5 sum of device: " << storage_unit_id_; 147 const base::Closure& continuation) {
176 148 if (IsCancelled()) {
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);
182 return; 149 return;
183 } 150 }
184 151
185 GetMD5SumOfFile( 152 scoped_ptr<char[]> source_buffer(new char[kBurningBlockSize]);
186 device_path.Pass(), 153 scoped_ptr<char[]> target_buffer(new char[kBurningBlockSize]);
187 image_size, 154
188 50, // progress_offset 155 int64 source_bytes_read = base::ReadPlatformFile(
189 50, // progress_scale 156 source_, bytes_processed, source_buffer.get(), kBurningBlockSize);
190 base::Bind(&Operation::VerifyWriteCompare, 157
191 this, 158 if (source_bytes_read > 0) {
192 base::Passed(&image_hash))); 159 int64 target_bytes_read = base::ReadPlatformFile(
160 target_, bytes_processed, target_buffer.get(), source_bytes_read);
161 if (source_bytes_read == target_bytes_read &&
162 memcmp(source_buffer.get(), target_buffer.get(), source_bytes_read) ==
163 0) {
164 int percent_curr = kProgressComplete *
165 (bytes_processed + source_bytes_read) / total_size;
166
167 SetProgress(percent_curr);
168
169 BrowserThread::PostTask(BrowserThread::FILE,
170 FROM_HERE,
171 base::Bind(&Operation::VerifyWriteChunk,
172 this,
173 bytes_processed + source_bytes_read,
174 total_size,
175 continuation));
176 } else {
177 Error(error::kVerificationFailed);
178 }
179 } else if (source_bytes_read == 0) {
tbarzic 2014/02/13 06:58:52 what if target file is not completely read at this
Drew Haven 2014/02/13 20:48:10 So, the target file should be larger because it's
180 VerifyWriteComplete(continuation);
181 } else { // len < 0
182 Error(error::kImageReadError);
183 }
193 } 184 }
194 185
195 void Operation::VerifyWriteCompare( 186 void Operation::VerifyWriteComplete(const base::Closure& continuation) {
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);
202 return;
203 }
204
205 DVLOG(2) << "Completed write verification of " << image_path_.value();
206
207 SetProgress(kProgressComplete); 187 SetProgress(kProgressComplete);
208 188 continuation.Run();
209 Finish();
210 } 189 }
211 190
212 } // namespace image_writer 191 } // namespace image_writer
213 } // namespace extensions 192 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698