| 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 <utility> |
| 8 |
| 7 #include "base/files/file_enumerator.h" | 9 #include "base/files/file_enumerator.h" |
| 8 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 9 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 10 #include "base/threading/worker_pool.h" | 12 #include "base/threading/worker_pool.h" |
| 11 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 12 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" | 14 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" |
| 13 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h
" | 15 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h
" |
| 14 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 15 #include "third_party/zlib/google/zip_reader.h" | 17 #include "third_party/zlib/google/zip_reader.h" |
| 16 | 18 |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 } | 305 } |
| 304 | 306 |
| 305 if (file_size <= 0) { | 307 if (file_size <= 0) { |
| 306 file_size = file.GetLength(); | 308 file_size = file.GetLength(); |
| 307 if (file_size < 0) { | 309 if (file_size < 0) { |
| 308 Error(error::kImageOpenError); | 310 Error(error::kImageOpenError); |
| 309 return; | 311 return; |
| 310 } | 312 } |
| 311 } | 313 } |
| 312 | 314 |
| 313 BrowserThread::PostTask(BrowserThread::FILE, | 315 BrowserThread::PostTask( |
| 314 FROM_HERE, | 316 BrowserThread::FILE, FROM_HERE, |
| 315 base::Bind(&Operation::MD5Chunk, | 317 base::Bind(&Operation::MD5Chunk, this, Passed(std::move(file)), 0, |
| 316 this, | 318 file_size, progress_offset, progress_scale, callback)); |
| 317 Passed(file.Pass()), | |
| 318 0, | |
| 319 file_size, | |
| 320 progress_offset, | |
| 321 progress_scale, | |
| 322 callback)); | |
| 323 } | 319 } |
| 324 | 320 |
| 325 void Operation::MD5Chunk( | 321 void Operation::MD5Chunk( |
| 326 base::File file, | 322 base::File file, |
| 327 int64_t bytes_processed, | 323 int64_t bytes_processed, |
| 328 int64_t bytes_total, | 324 int64_t bytes_total, |
| 329 int progress_offset, | 325 int progress_offset, |
| 330 int progress_scale, | 326 int progress_scale, |
| 331 const base::Callback<void(const std::string&)>& callback) { | 327 const base::Callback<void(const std::string&)>& callback) { |
| 332 if (IsCancelled()) | 328 if (IsCancelled()) |
| (...skipping 14 matching lines...) Expand all Loading... |
| 347 int len = file.Read(bytes_processed, buffer.get(), read_size); | 343 int len = file.Read(bytes_processed, buffer.get(), read_size); |
| 348 | 344 |
| 349 if (len == read_size) { | 345 if (len == read_size) { |
| 350 // Process data. | 346 // Process data. |
| 351 base::MD5Update(&md5_context_, base::StringPiece(buffer.get(), len)); | 347 base::MD5Update(&md5_context_, base::StringPiece(buffer.get(), len)); |
| 352 int percent_curr = | 348 int percent_curr = |
| 353 ((bytes_processed + len) * progress_scale) / bytes_total + | 349 ((bytes_processed + len) * progress_scale) / bytes_total + |
| 354 progress_offset; | 350 progress_offset; |
| 355 SetProgress(percent_curr); | 351 SetProgress(percent_curr); |
| 356 | 352 |
| 357 BrowserThread::PostTask(BrowserThread::FILE, | 353 BrowserThread::PostTask( |
| 358 FROM_HERE, | 354 BrowserThread::FILE, FROM_HERE, |
| 359 base::Bind(&Operation::MD5Chunk, | 355 base::Bind(&Operation::MD5Chunk, this, Passed(std::move(file)), |
| 360 this, | 356 bytes_processed + len, bytes_total, progress_offset, |
| 361 Passed(file.Pass()), | 357 progress_scale, callback)); |
| 362 bytes_processed + len, | |
| 363 bytes_total, | |
| 364 progress_offset, | |
| 365 progress_scale, | |
| 366 callback)); | |
| 367 // Skip closing the file. | 358 // Skip closing the file. |
| 368 return; | 359 return; |
| 369 } else { | 360 } else { |
| 370 // We didn't read the bytes we expected. | 361 // We didn't read the bytes we expected. |
| 371 Error(error::kHashReadError); | 362 Error(error::kHashReadError); |
| 372 } | 363 } |
| 373 } | 364 } |
| 374 } | 365 } |
| 375 | 366 |
| 376 void Operation::OnUnzipFailure() { | 367 void Operation::OnUnzipFailure() { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 390 for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin(); | 381 for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin(); |
| 391 it != cleanup_functions_.end(); | 382 it != cleanup_functions_.end(); |
| 392 ++it) { | 383 ++it) { |
| 393 it->Run(); | 384 it->Run(); |
| 394 } | 385 } |
| 395 cleanup_functions_.clear(); | 386 cleanup_functions_.clear(); |
| 396 } | 387 } |
| 397 | 388 |
| 398 } // namespace image_writer | 389 } // namespace image_writer |
| 399 } // namespace extensions | 390 } // namespace extensions |
| OLD | NEW |