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

Side by Side Diff: chrome/browser/extensions/api/image_writer_private/operation.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 all the ScopedPlatformFileCloser stuff and just uses normal PlatformFiles. 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 12
13 namespace extensions { 13 namespace extensions {
14 namespace image_writer { 14 namespace image_writer {
15 15
16 using content::BrowserThread; 16 using content::BrowserThread;
17 17
18 namespace {
19
20 const int kMD5BufferSize = 1024; 18 const int kMD5BufferSize = 1024;
21 19 #if defined(OS_CHROMEOS)
22 void RemoveTempDirectory(const base::FilePath path) { 20 // Chrome OS only has a 1 GB temporary partition. This is too small to hold our
23 base::DeleteFile(path, true); 21 // unzipped image. Fortunately we mount part of the temporary partition under
24 } 22 // /var/tmp.
25 23 const char kChromeOSTempRoot[] = "/var/tmp";
26 } // namespace 24 #endif
27 25
28 Operation::Operation(base::WeakPtr<OperationManager> manager, 26 Operation::Operation(base::WeakPtr<OperationManager> manager,
29 const ExtensionId& extension_id, 27 const ExtensionId& extension_id,
30 const std::string& storage_unit_id) 28 const std::string& device_path)
31 : manager_(manager), 29 : manager_(manager),
32 extension_id_(extension_id), 30 extension_id_(extension_id),
33 storage_unit_id_(storage_unit_id), 31 #if defined(OS_WIN)
34 verify_write_(true), 32 device_path_(base::FilePath::FromUTF8Unsafe(device_path)),
33 #else
34 device_path_(device_path),
35 #endif
36 #if defined(OS_LINUX) && !defined(CHROMEOS)
37 image_file_(base::kInvalidPlatformFileValue),
38 device_file_(base::kInvalidPlatformFileValue),
39 #endif
35 stage_(image_writer_api::STAGE_UNKNOWN), 40 stage_(image_writer_api::STAGE_UNKNOWN),
36 progress_(0) { 41 progress_(0) {
37 } 42 }
38 43
39 Operation::~Operation() { 44 Operation::~Operation() {}
40 }
41 45
42 void Operation::Cancel() { 46 void Operation::Cancel() {
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
44 48
45 DVLOG(1) << "Cancelling image writing operation for ext: " << extension_id_;
46
47 stage_ = image_writer_api::STAGE_NONE; 49 stage_ = image_writer_api::STAGE_NONE;
48 50
49 CleanUp(); 51 CleanUp();
50 } 52 }
51 53
52 void Operation::Abort() { 54 void Operation::Abort() {
53 Error(error::kAborted); 55 Error(error::kAborted);
54 } 56 }
55 57
56 int Operation::GetProgress() { 58 int Operation::GetProgress() {
57 return progress_; 59 return progress_;
58 } 60 }
59 61
60 image_writer_api::Stage Operation::GetStage() { 62 image_writer_api::Stage Operation::GetStage() {
61 return stage_; 63 return stage_;
62 } 64 }
63 65
66 void Operation::Start() {
67 #if defined(OS_CHROMEOS)
68 if (!temp_dir_.CreateUniqueTempDirUnderPath(
69 base::FilePath(kChromeOSTempRoot))) {
70 #else
71 if (!temp_dir_.CreateUniqueTempDir()) {
72 #endif
73 Error(error::kTempDirError);
74 return;
75 }
76
77 AddCleanUpFunction(
78 base::Bind(base::IgnoreResult(&base::ScopedTempDir::Delete),
79 base::Unretained(&temp_dir_)));
80
81 StartImpl();
82 }
83
84 void Operation::Unzip(const base::Closure& continuation) {
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
86 if (IsCancelled()) {
87 return;
88 }
89
90 if (image_path_.Extension() != FILE_PATH_LITERAL(".zip")) {
91 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation);
92 return;
93 }
94
95 SetStage(image_writer_api::STAGE_UNZIP);
96
97 if (!(zip_reader_.Open(image_path_) && zip_reader_.AdvanceToNextEntry() &&
98 zip_reader_.OpenCurrentEntryInZip())) {
99 Error(error::kUnzipGenericError);
100 return;
101 }
102
103 if (zip_reader_.HasMore()) {
104 Error(error::kUnzipInvalidArchive);
105 return;
106 }
107
108 // Create a new target to unzip to. The original file is opened by the
109 // zip_reader_.
110 zip::ZipReader::EntryInfo* entry_info = zip_reader_.current_entry_info();
111 if (entry_info) {
112 image_path_ = temp_dir_.path().Append(entry_info->file_path().BaseName());
113 } else {
114 Error(error::kTempDirError);
115 return;
116 }
117
118 zip_reader_.ExtractCurrentEntryToFilePathAsync(
119 image_path_,
120 base::Bind(&Operation::OnUnzipSuccess, this, continuation),
121 base::Bind(&Operation::OnUnzipFailure, this),
122 base::Bind(&Operation::OnUnzipProgress,
123 this,
124 zip_reader_.current_entry_info()->original_size()));
125 }
126
127 void Operation::Finish() {
128 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) {
129 BrowserThread::PostTask(
130 BrowserThread::FILE, FROM_HERE, base::Bind(&Operation::Finish, this));
131 return;
132 }
133
134 CleanUp();
135
136 BrowserThread::PostTask(
137 BrowserThread::UI,
138 FROM_HERE,
139 base::Bind(&OperationManager::OnComplete, manager_, extension_id_));
140 }
141
64 void Operation::Error(const std::string& error_message) { 142 void Operation::Error(const std::string& error_message) {
65 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { 143 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) {
66 BrowserThread::PostTask(BrowserThread::FILE, 144 BrowserThread::PostTask(BrowserThread::FILE,
67 FROM_HERE, 145 FROM_HERE,
68 base::Bind(&Operation::Error, this, error_message)); 146 base::Bind(&Operation::Error, this, error_message));
69 return; 147 return;
70 } 148 }
71 149
72 BrowserThread::PostTask( 150 BrowserThread::PostTask(
73 BrowserThread::UI, 151 BrowserThread::UI,
(...skipping 12 matching lines...) Expand all
86 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { 164 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) {
87 BrowserThread::PostTask( 165 BrowserThread::PostTask(
88 BrowserThread::FILE, 166 BrowserThread::FILE,
89 FROM_HERE, 167 FROM_HERE,
90 base::Bind(&Operation::SetProgress, 168 base::Bind(&Operation::SetProgress,
91 this, 169 this,
92 progress)); 170 progress));
93 return; 171 return;
94 } 172 }
95 173
174 if (progress <= progress_) {
175 return;
176 }
177
96 if (IsCancelled()) { 178 if (IsCancelled()) {
97 return; 179 return;
98 } 180 }
99 181
100 progress_ = progress; 182 progress_ = progress;
101 183
102 BrowserThread::PostTask( 184 BrowserThread::PostTask(
103 BrowserThread::UI, 185 BrowserThread::UI,
104 FROM_HERE, 186 FROM_HERE,
105 base::Bind(&OperationManager::OnProgress, 187 base::Bind(&OperationManager::OnProgress,
(...skipping 24 matching lines...) Expand all
130 BrowserThread::PostTask( 212 BrowserThread::PostTask(
131 BrowserThread::UI, 213 BrowserThread::UI,
132 FROM_HERE, 214 FROM_HERE,
133 base::Bind(&OperationManager::OnProgress, 215 base::Bind(&OperationManager::OnProgress,
134 manager_, 216 manager_,
135 extension_id_, 217 extension_id_,
136 stage_, 218 stage_,
137 progress_)); 219 progress_));
138 } 220 }
139 221
140 void Operation::Finish() {
141 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) {
142 BrowserThread::PostTask(BrowserThread::FILE,
143 FROM_HERE,
144 base::Bind(&Operation::Finish, this));
145 return;
146 }
147 DVLOG(1) << "Write operation complete.";
148
149 CleanUp();
150
151 BrowserThread::PostTask(
152 BrowserThread::UI,
153 FROM_HERE,
154 base::Bind(&OperationManager::OnComplete,
155 manager_,
156 extension_id_));
157 }
158
159 bool Operation::IsCancelled() { 222 bool Operation::IsCancelled() {
160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
161 224
162 return stage_ == image_writer_api::STAGE_NONE; 225 return stage_ == image_writer_api::STAGE_NONE;
163 } 226 }
164 227
165 void Operation::AddCleanUpFunction(base::Closure callback) { 228 void Operation::AddCleanUpFunction(const base::Closure& callback) {
166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
167
168 cleanup_functions_.push_back(callback); 230 cleanup_functions_.push_back(callback);
169 } 231 }
170 232
171 void Operation::CleanUp() {
172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
173 for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin();
174 it != cleanup_functions_.end();
175 ++it) {
176 it->Run();
177 }
178 cleanup_functions_.clear();
179 }
180
181 void Operation::UnzipStart(scoped_ptr<base::FilePath> zip_path) {
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
183 if (IsCancelled()) {
184 return;
185 }
186
187 DVLOG(1) << "Starting unzip stage for " << zip_path->value();
188
189 SetStage(image_writer_api::STAGE_UNZIP);
190
191 base::FilePath tmp_dir;
192 if (!base::CreateTemporaryDirInDir(zip_path->DirName(),
193 FILE_PATH_LITERAL("image_writer"),
194 &tmp_dir)) {
195 Error(error::kTempDirError);
196 return;
197 }
198
199 AddCleanUpFunction(base::Bind(&RemoveTempDirectory, tmp_dir));
200
201 if (!base::CreateTemporaryFileInDir(tmp_dir, &image_path_)) {
202 DLOG(ERROR) << "Failed create temporary unzip target in "
203 << tmp_dir.value();
204 Error(error::kTempDirError);
205 return;
206 }
207
208 if (!(zip_reader_.Open(*zip_path) &&
209 zip_reader_.AdvanceToNextEntry() &&
210 zip_reader_.OpenCurrentEntryInZip())) {
211 DLOG(ERROR) << "Failed to open zip file.";
212 Error(error::kUnzipGenericError);
213 return;
214 }
215
216 if (zip_reader_.HasMore()) {
217 DLOG(ERROR) << "Image zip has more than one file.";
218 Error(error::kUnzipInvalidArchive);
219 return;
220 }
221
222 zip_reader_.ExtractCurrentEntryToFilePathAsync(
223 image_path_,
224 base::Bind(&Operation::OnUnzipSuccess, this),
225 base::Bind(&Operation::OnUnzipFailure, this),
226 base::Bind(&Operation::OnUnzipProgress,
227 this,
228 zip_reader_.current_entry_info()->original_size()));
229 }
230
231 void Operation::GetMD5SumOfFile( 233 void Operation::GetMD5SumOfFile(
232 scoped_ptr<base::FilePath> file_path, 234 const base::FilePath& file_path,
233 int64 file_size, 235 int64 file_size,
234 int progress_offset, 236 int progress_offset,
235 int progress_scale, 237 int progress_scale,
236 const base::Callback<void(scoped_ptr<std::string>)>& callback) { 238 const base::Callback<void(const std::string&)>& callback) {
237 if (IsCancelled()) { 239 if (IsCancelled()) {
238 return; 240 return;
239 } 241 }
240 242
241 base::MD5Init(&md5_context_); 243 base::MD5Init(&md5_context_);
242 scoped_ptr<image_writer_utils::ImageReader> reader(
243 new image_writer_utils::ImageReader());
244 244
245 if (!reader->Open(*file_path)) { 245 base::PlatformFile file = base::CreatePlatformFile(
246 file_path,
247 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ,
248 NULL,
249 NULL);
250 if (file == base::kInvalidPlatformFileValue) {
246 Error(error::kImageOpenError); 251 Error(error::kImageOpenError);
247 return; 252 return;
248 } 253 }
254
249 if (file_size <= 0) { 255 if (file_size <= 0) {
250 file_size = reader->GetSize(); 256 if (!base::GetFileSize(file_path, &file_size)) {
257 Error(error::kImageOpenError);
258 return;
259 }
251 } 260 }
252 261
253 BrowserThread::PostTask(BrowserThread::FILE, 262 BrowserThread::PostTask(BrowserThread::FILE,
254 FROM_HERE, 263 FROM_HERE,
255 base::Bind(&Operation::MD5Chunk, 264 base::Bind(&Operation::MD5Chunk,
256 this, 265 this,
257 base::Passed(&reader), 266 file,
258 0, 267 0,
259 file_size, 268 file_size,
260 progress_offset, 269 progress_offset,
261 progress_scale, 270 progress_scale,
262 callback)); 271 callback));
263 } 272 }
264 273
265 void Operation::MD5Chunk( 274 void Operation::MD5Chunk(
266 scoped_ptr<image_writer_utils::ImageReader> reader, 275 const base::PlatformFile& file,
267 int64 bytes_processed, 276 int64 bytes_processed,
268 int64 bytes_total, 277 int64 bytes_total,
269 int progress_offset, 278 int progress_offset,
270 int progress_scale, 279 int progress_scale,
271 const base::Callback<void(scoped_ptr<std::string>)>& callback) { 280 const base::Callback<void(const std::string&)>& callback) {
272 if (IsCancelled()) { 281 if (IsCancelled()) {
273 reader->Close(); 282 base::ClosePlatformFile(file);
274 return; 283 return;
275 } 284 }
276 285
277 char buffer[kMD5BufferSize]; 286 CHECK_LE(bytes_processed, bytes_total);
278 int len;
279 287
280 if (bytes_total - bytes_processed <= kMD5BufferSize) { 288 scoped_ptr<char[]> buffer(new char[kMD5BufferSize]);
281 len = reader->Read(buffer, bytes_total - bytes_processed); 289 int read_size = std::min(bytes_total - bytes_processed,
290 static_cast<int64>(kMD5BufferSize));
291
292 if (read_size == 0) {
293 // Nothing to read, we are done.
294 base::MD5Digest digest;
295 base::MD5Final(&digest, &md5_context_);
296 callback.Run(base::MD5DigestToBase16(digest));
282 } else { 297 } else {
283 len = reader->Read(buffer, kMD5BufferSize); 298 int len =
299 base::ReadPlatformFile(file, bytes_processed, buffer.get(), read_size);
300
301 if (len == read_size) {
302 // Process data.
303 base::MD5Update(&md5_context_, base::StringPiece(buffer.get(), len));
304 int percent_curr =
305 ((bytes_processed + len) * progress_scale) / bytes_total +
306 progress_offset;
307 SetProgress(percent_curr);
308
309 BrowserThread::PostTask(BrowserThread::FILE,
310 FROM_HERE,
311 base::Bind(&Operation::MD5Chunk,
312 this,
313 file,
314 bytes_processed + len,
315 bytes_total,
316 progress_offset,
317 progress_scale,
318 callback));
319 // Skip closing the file.
320 return;
321 } else {
322 // We didn't read the bytes we expected.
323 Error(error::kHashReadError);
324 }
284 } 325 }
285 326 base::ClosePlatformFile(file);
286 if (len > 0) {
287 base::MD5Update(&md5_context_, base::StringPiece(buffer, len));
288 int percent_prev = (bytes_processed * progress_scale + progress_offset) /
289 (bytes_total);
290 int percent_curr = ((bytes_processed + len) * progress_scale +
291 progress_offset) /
292 (bytes_total);
293 if (percent_curr > percent_prev) {
294 SetProgress(progress_);
295 }
296
297 BrowserThread::PostTask(BrowserThread::FILE,
298 FROM_HERE,
299 base::Bind(&Operation::MD5Chunk,
300 this,
301 base::Passed(&reader),
302 bytes_processed + len,
303 bytes_total,
304 progress_offset,
305 progress_scale,
306 callback));
307 } else if (len == 0) {
308 if (bytes_processed + len < bytes_total) {
309 reader->Close();
310 Error(error::kHashReadError);
311 } else {
312 base::MD5Digest digest;
313 base::MD5Final(&digest, &md5_context_);
314 scoped_ptr<std::string> hash(
315 new std::string(base::MD5DigestToBase16(digest)));
316 callback.Run(hash.Pass());
317 }
318 } else { // len < 0
319 reader->Close();
320 Error(error::kHashReadError);
321 }
322 } 327 }
323 328
324 void Operation::OnUnzipSuccess() { 329 void Operation::OnUnzipSuccess(const base::Closure& continuation) {
325 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 330 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
326 SetProgress(kProgressComplete); 331 SetProgress(kProgressComplete);
327 WriteStart(); 332 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation);
tbarzic 2014/02/13 23:42:47 potential future cleanup: Could we use (Sequenced)
Drew Haven 2014/02/14 02:44:30 I'll have to look at how that works. I don't unde
328 } 333 }
329 334
330 void Operation::OnUnzipFailure() { 335 void Operation::OnUnzipFailure() {
331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
332 Error(error::kUnzipGenericError); 337 Error(error::kUnzipGenericError);
333 } 338 }
334 339
335 void Operation::OnUnzipProgress(int64 total_bytes, int64 progress_bytes) { 340 void Operation::OnUnzipProgress(int64 total_bytes, int64 progress_bytes) {
336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 341 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
337 342
338 int progress_percent = 100 * progress_bytes / total_bytes; 343 int progress_percent = 100 * progress_bytes / total_bytes;
339 SetProgress(progress_percent); 344 SetProgress(progress_percent);
340 } 345 }
341 346
347 void Operation::CleanUp() {
348 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
349 for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin();
350 it != cleanup_functions_.end();
351 ++it) {
352 it->Run();
353 }
354 cleanup_functions_.clear();
355 }
356
342 } // namespace image_writer 357 } // namespace image_writer
343 } // namespace extensions 358 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698