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 "chrome/browser/extensions/api/image_writer_private/operation.h" | 5 #include "chrome/browser/extensions/api/image_writer_private/operation.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/file_enumerator.h" | 8 #include "base/files/file_enumerator.h" |
9 #include "base/threading/worker_pool.h" | 9 #include "base/threading/worker_pool.h" |
10 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" | 10 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 #else | 34 #else |
35 device_path_(device_path), | 35 device_path_(device_path), |
36 #endif | 36 #endif |
37 stage_(image_writer_api::STAGE_UNKNOWN), | 37 stage_(image_writer_api::STAGE_UNKNOWN), |
38 progress_(0) { | 38 progress_(0) { |
39 } | 39 } |
40 | 40 |
41 Operation::~Operation() {} | 41 Operation::~Operation() {} |
42 | 42 |
43 void Operation::Cancel() { | 43 void Operation::Cancel() { |
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 44 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
45 | 45 |
46 stage_ = image_writer_api::STAGE_NONE; | 46 stage_ = image_writer_api::STAGE_NONE; |
47 | 47 |
48 CleanUp(); | 48 CleanUp(); |
49 } | 49 } |
50 | 50 |
51 void Operation::Abort() { | 51 void Operation::Abort() { |
52 Error(error::kAborted); | 52 Error(error::kAborted); |
53 } | 53 } |
54 | 54 |
(...skipping 26 matching lines...) Expand all Loading... |
81 } | 81 } |
82 | 82 |
83 AddCleanUpFunction( | 83 AddCleanUpFunction( |
84 base::Bind(base::IgnoreResult(&base::ScopedTempDir::Delete), | 84 base::Bind(base::IgnoreResult(&base::ScopedTempDir::Delete), |
85 base::Unretained(&temp_dir_))); | 85 base::Unretained(&temp_dir_))); |
86 | 86 |
87 StartImpl(); | 87 StartImpl(); |
88 } | 88 } |
89 | 89 |
90 void Operation::Unzip(const base::Closure& continuation) { | 90 void Operation::Unzip(const base::Closure& continuation) { |
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 91 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
92 if (IsCancelled()) { | 92 if (IsCancelled()) { |
93 return; | 93 return; |
94 } | 94 } |
95 | 95 |
96 if (image_path_.Extension() != FILE_PATH_LITERAL(".zip")) { | 96 if (image_path_.Extension() != FILE_PATH_LITERAL(".zip")) { |
97 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation); | 97 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation); |
98 return; | 98 return; |
99 } | 99 } |
100 | 100 |
101 SetStage(image_writer_api::STAGE_UNZIP); | 101 SetStage(image_writer_api::STAGE_UNZIP); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 BrowserThread::UI, | 218 BrowserThread::UI, |
219 FROM_HERE, | 219 FROM_HERE, |
220 base::Bind(&OperationManager::OnProgress, | 220 base::Bind(&OperationManager::OnProgress, |
221 manager_, | 221 manager_, |
222 extension_id_, | 222 extension_id_, |
223 stage_, | 223 stage_, |
224 progress_)); | 224 progress_)); |
225 } | 225 } |
226 | 226 |
227 bool Operation::IsCancelled() { | 227 bool Operation::IsCancelled() { |
228 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 228 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
229 | 229 |
230 return stage_ == image_writer_api::STAGE_NONE; | 230 return stage_ == image_writer_api::STAGE_NONE; |
231 } | 231 } |
232 | 232 |
233 void Operation::AddCleanUpFunction(const base::Closure& callback) { | 233 void Operation::AddCleanUpFunction(const base::Closure& callback) { |
234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 234 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
235 cleanup_functions_.push_back(callback); | 235 cleanup_functions_.push_back(callback); |
236 } | 236 } |
237 | 237 |
238 void Operation::CompleteAndContinue(const base::Closure& continuation) { | 238 void Operation::CompleteAndContinue(const base::Closure& continuation) { |
239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 239 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
240 SetProgress(kProgressComplete); | 240 SetProgress(kProgressComplete); |
241 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation); | 241 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation); |
242 } | 242 } |
243 | 243 |
244 #if !defined(OS_CHROMEOS) | 244 #if !defined(OS_CHROMEOS) |
245 void Operation::StartUtilityClient() { | 245 void Operation::StartUtilityClient() { |
246 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 246 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
247 if (!image_writer_client_) { | 247 if (!image_writer_client_) { |
248 image_writer_client_ = new ImageWriterUtilityClient(); | 248 image_writer_client_ = new ImageWriterUtilityClient(); |
249 AddCleanUpFunction(base::Bind(&Operation::StopUtilityClient, this)); | 249 AddCleanUpFunction(base::Bind(&Operation::StopUtilityClient, this)); |
250 } | 250 } |
251 } | 251 } |
252 | 252 |
253 void Operation::StopUtilityClient() { | 253 void Operation::StopUtilityClient() { |
254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 254 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
255 BrowserThread::PostTask( | 255 BrowserThread::PostTask( |
256 BrowserThread::IO, | 256 BrowserThread::IO, |
257 FROM_HERE, | 257 FROM_HERE, |
258 base::Bind(&ImageWriterUtilityClient::Shutdown, image_writer_client_)); | 258 base::Bind(&ImageWriterUtilityClient::Shutdown, image_writer_client_)); |
259 } | 259 } |
260 | 260 |
261 void Operation::WriteImageProgress(int64 total_bytes, int64 curr_bytes) { | 261 void Operation::WriteImageProgress(int64 total_bytes, int64 curr_bytes) { |
262 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 262 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
263 if (IsCancelled()) { | 263 if (IsCancelled()) { |
264 return; | 264 return; |
265 } | 265 } |
266 | 266 |
267 int progress = kProgressComplete * curr_bytes / total_bytes; | 267 int progress = kProgressComplete * curr_bytes / total_bytes; |
268 | 268 |
269 if (progress > GetProgress()) { | 269 if (progress > GetProgress()) { |
270 SetProgress(progress); | 270 SetProgress(progress); |
271 } | 271 } |
272 } | 272 } |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 // Skip closing the file. | 355 // Skip closing the file. |
356 return; | 356 return; |
357 } else { | 357 } else { |
358 // We didn't read the bytes we expected. | 358 // We didn't read the bytes we expected. |
359 Error(error::kHashReadError); | 359 Error(error::kHashReadError); |
360 } | 360 } |
361 } | 361 } |
362 } | 362 } |
363 | 363 |
364 void Operation::OnUnzipFailure() { | 364 void Operation::OnUnzipFailure() { |
365 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 365 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
366 Error(error::kUnzipGenericError); | 366 Error(error::kUnzipGenericError); |
367 } | 367 } |
368 | 368 |
369 void Operation::OnUnzipProgress(int64 total_bytes, int64 progress_bytes) { | 369 void Operation::OnUnzipProgress(int64 total_bytes, int64 progress_bytes) { |
370 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 370 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
371 | 371 |
372 int progress_percent = kProgressComplete * progress_bytes / total_bytes; | 372 int progress_percent = kProgressComplete * progress_bytes / total_bytes; |
373 SetProgress(progress_percent); | 373 SetProgress(progress_percent); |
374 } | 374 } |
375 | 375 |
376 void Operation::CleanUp() { | 376 void Operation::CleanUp() { |
377 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 377 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
378 for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin(); | 378 for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin(); |
379 it != cleanup_functions_.end(); | 379 it != cleanup_functions_.end(); |
380 ++it) { | 380 ++it) { |
381 it->Run(); | 381 it->Run(); |
382 } | 382 } |
383 cleanup_functions_.clear(); | 383 cleanup_functions_.clear(); |
384 } | 384 } |
385 | 385 |
386 } // namespace image_writer | 386 } // namespace image_writer |
387 } // namespace extensions | 387 } // namespace extensions |
OLD | NEW |