| 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/media_galleries/linux/snapshot_file_details.h" | 5 #include "chrome/browser/media_galleries/linux/snapshot_file_details.h" |
| 6 | 6 |
| 7 #include <limits> |
| 8 |
| 7 #include "base/numerics/safe_conversions.h" | 9 #include "base/numerics/safe_conversions.h" |
| 8 | 10 |
| 9 //////////////////////////////////////////////////////////////////////////////// | 11 //////////////////////////////////////////////////////////////////////////////// |
| 10 // SnapshotRequestInfo // | 12 // SnapshotRequestInfo // |
| 11 //////////////////////////////////////////////////////////////////////////////// | 13 //////////////////////////////////////////////////////////////////////////////// |
| 12 | 14 |
| 13 SnapshotRequestInfo::SnapshotRequestInfo( | 15 SnapshotRequestInfo::SnapshotRequestInfo( |
| 14 uint32 file_id, | 16 uint32_t file_id, |
| 15 const base::FilePath& snapshot_file_path, | 17 const base::FilePath& snapshot_file_path, |
| 16 const MTPDeviceAsyncDelegate::CreateSnapshotFileSuccessCallback& | 18 const MTPDeviceAsyncDelegate::CreateSnapshotFileSuccessCallback& |
| 17 success_callback, | 19 success_callback, |
| 18 const MTPDeviceAsyncDelegate::ErrorCallback& error_callback) | 20 const MTPDeviceAsyncDelegate::ErrorCallback& error_callback) |
| 19 : file_id(file_id), | 21 : file_id(file_id), |
| 20 snapshot_file_path(snapshot_file_path), | 22 snapshot_file_path(snapshot_file_path), |
| 21 success_callback(success_callback), | 23 success_callback(success_callback), |
| 22 error_callback(error_callback) { | 24 error_callback(error_callback) {} |
| 23 } | |
| 24 | 25 |
| 25 SnapshotRequestInfo::~SnapshotRequestInfo() { | 26 SnapshotRequestInfo::~SnapshotRequestInfo() { |
| 26 } | 27 } |
| 27 | 28 |
| 28 //////////////////////////////////////////////////////////////////////////////// | 29 //////////////////////////////////////////////////////////////////////////////// |
| 29 // SnapshotFileDetails // | 30 // SnapshotFileDetails // |
| 30 //////////////////////////////////////////////////////////////////////////////// | 31 //////////////////////////////////////////////////////////////////////////////// |
| 31 | 32 |
| 32 SnapshotFileDetails::SnapshotFileDetails( | 33 SnapshotFileDetails::SnapshotFileDetails( |
| 33 const SnapshotRequestInfo& request_info, | 34 const SnapshotRequestInfo& request_info, |
| 34 const base::File::Info& file_info) | 35 const base::File::Info& file_info) |
| 35 : request_info_(request_info), | 36 : request_info_(request_info), |
| 36 file_info_(file_info), | 37 file_info_(file_info), |
| 37 bytes_written_(0), | 38 bytes_written_(0), |
| 38 error_occurred_(false) { | 39 error_occurred_(false) { |
| 39 } | 40 } |
| 40 | 41 |
| 41 SnapshotFileDetails::~SnapshotFileDetails() { | 42 SnapshotFileDetails::~SnapshotFileDetails() { |
| 42 } | 43 } |
| 43 | 44 |
| 44 void SnapshotFileDetails::set_error_occurred(bool error) { | 45 void SnapshotFileDetails::set_error_occurred(bool error) { |
| 45 error_occurred_ = error; | 46 error_occurred_ = error; |
| 46 } | 47 } |
| 47 | 48 |
| 48 bool SnapshotFileDetails::AddBytesWritten(uint32 bytes_written) { | 49 bool SnapshotFileDetails::AddBytesWritten(uint32_t bytes_written) { |
| 49 if ((bytes_written == 0) || | 50 if ((bytes_written == 0) || |
| 50 (bytes_written_ > kuint32max - bytes_written) || | 51 (bytes_written_ > std::numeric_limits<uint32_t>::max() - bytes_written) || |
| 51 (bytes_written_ + bytes_written > file_info_.size)) | 52 (bytes_written_ + bytes_written > file_info_.size)) |
| 52 return false; | 53 return false; |
| 53 | 54 |
| 54 bytes_written_ += bytes_written; | 55 bytes_written_ += bytes_written; |
| 55 return true; | 56 return true; |
| 56 } | 57 } |
| 57 | 58 |
| 58 bool SnapshotFileDetails::IsSnapshotFileWriteComplete() const { | 59 bool SnapshotFileDetails::IsSnapshotFileWriteComplete() const { |
| 59 return !error_occurred_ && (bytes_written_ == file_info_.size); | 60 return !error_occurred_ && (bytes_written_ == file_info_.size); |
| 60 } | 61 } |
| 61 | 62 |
| 62 uint32 SnapshotFileDetails::BytesToRead() const { | 63 uint32_t SnapshotFileDetails::BytesToRead() const { |
| 63 // Read data in 1MB chunks. | 64 // Read data in 1MB chunks. |
| 64 static const uint32 kReadChunkSize = 1024 * 1024; | 65 static const uint32_t kReadChunkSize = 1024 * 1024; |
| 65 return std::min( | 66 return std::min( |
| 66 kReadChunkSize, | 67 kReadChunkSize, |
| 67 base::checked_cast<uint32>(file_info_.size) - bytes_written_); | 68 base::checked_cast<uint32_t>(file_info_.size) - bytes_written_); |
| 68 } | 69 } |
| OLD | NEW |