| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/fileapi/mtp_file_stream_reader.h" | 5 #include "chrome/browser/media_galleries/fileapi/mtp_file_stream_reader.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/numerics/safe_conversions.h" | 9 #include "base/numerics/safe_conversions.h" |
| 10 #include "base/platform_file.h" | 10 #include "base/platform_file.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 base::File::Error file_error) { | 34 base::File::Error file_error) { |
| 35 callback.Run(net::FileErrorToNetError(file_error)); | 35 callback.Run(net::FileErrorToNetError(file_error)); |
| 36 } | 36 } |
| 37 | 37 |
| 38 void CallInt64CompletionCallbackWithPlatformFileError( | 38 void CallInt64CompletionCallbackWithPlatformFileError( |
| 39 const net::Int64CompletionCallback& callback, | 39 const net::Int64CompletionCallback& callback, |
| 40 base::File::Error file_error) { | 40 base::File::Error file_error) { |
| 41 callback.Run(net::FileErrorToNetError(file_error)); | 41 callback.Run(net::FileErrorToNetError(file_error)); |
| 42 } | 42 } |
| 43 | 43 |
| 44 void ReadBytes(const fileapi::FileSystemURL& url, net::IOBuffer* buf, | 44 void ReadBytes( |
| 45 int64 offset, int buf_len, const base::File::Info& file_info, | 45 const fileapi::FileSystemURL& url, net::IOBuffer* buf, int64 offset, |
| 46 const net::CompletionCallback& callback) { | 46 int buf_len, |
| 47 const MTPDeviceAsyncDelegate::ReadBytesSuccessCallback& success_callback, |
| 48 const net::CompletionCallback& error_callback) { |
| 47 MTPDeviceAsyncDelegate* delegate = GetMTPDeviceDelegate(url); | 49 MTPDeviceAsyncDelegate* delegate = GetMTPDeviceDelegate(url); |
| 48 if (!delegate) { | 50 if (!delegate) { |
| 49 callback.Run(net::ERR_FAILED); | 51 error_callback.Run(net::ERR_FAILED); |
| 50 return; | 52 return; |
| 51 } | 53 } |
| 52 | 54 |
| 53 DCHECK_GE(file_info.size, offset); | |
| 54 int bytes_to_read = std::min( | |
| 55 buf_len, | |
| 56 base::saturated_cast<int>(file_info.size - offset)); | |
| 57 | |
| 58 delegate->ReadBytes( | 55 delegate->ReadBytes( |
| 59 url.path(), | 56 url.path(), |
| 60 make_scoped_refptr(buf), | 57 make_scoped_refptr(buf), |
| 61 offset, | 58 offset, |
| 62 bytes_to_read, | 59 buf_len, |
| 63 callback, | 60 success_callback, |
| 64 base::Bind(&CallCompletionCallbackWithPlatformFileError, callback)); | 61 base::Bind(&CallCompletionCallbackWithPlatformFileError, error_callback)); |
| 65 } | 62 } |
| 66 | 63 |
| 67 } // namespace | 64 } // namespace |
| 68 | 65 |
| 69 MTPFileStreamReader::MTPFileStreamReader( | 66 MTPFileStreamReader::MTPFileStreamReader( |
| 70 fileapi::FileSystemContext* file_system_context, | 67 fileapi::FileSystemContext* file_system_context, |
| 71 const fileapi::FileSystemURL& url, | 68 const fileapi::FileSystemURL& url, |
| 72 int64 initial_offset, | 69 int64 initial_offset, |
| 73 const base::Time& expected_modification_time) | 70 const base::Time& expected_modification_time) |
| 74 : file_system_context_(file_system_context), | 71 : file_system_context_(file_system_context), |
| 75 url_(url), | 72 url_(url), |
| 76 current_offset_(initial_offset), | 73 current_offset_(initial_offset), |
| 77 expected_modification_time_(expected_modification_time), | 74 expected_modification_time_(expected_modification_time), |
| 78 media_header_validated_(false), | 75 media_header_validated_(false), |
| 79 weak_factory_(this) { | 76 weak_factory_(this) { |
| 80 } | 77 } |
| 81 | 78 |
| 82 MTPFileStreamReader::~MTPFileStreamReader() { | 79 MTPFileStreamReader::~MTPFileStreamReader() { |
| 83 } | 80 } |
| 84 | 81 |
| 85 int MTPFileStreamReader::Read(net::IOBuffer* buf, int buf_len, | 82 int MTPFileStreamReader::Read(net::IOBuffer* buf, int buf_len, |
| 86 const net::CompletionCallback& callback) { | 83 const net::CompletionCallback& callback) { |
| 87 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 84 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 88 | 85 |
| 89 MTPDeviceAsyncDelegate* delegate = GetMTPDeviceDelegate(url_); | 86 MTPDeviceAsyncDelegate* delegate = GetMTPDeviceDelegate(url_); |
| 90 if (!delegate) | 87 if (!delegate) |
| 91 return net::ERR_FAILED; | 88 return net::ERR_FAILED; |
| 92 | 89 |
| 93 delegate->GetFileInfo( | 90 if (!media_header_validated_) { |
| 94 url_.path(), | 91 scoped_refptr<net::IOBuffer> header_buf( |
| 95 base::Bind(&MTPFileStreamReader::OnFileInfoForRead, | 92 new net::IOBuffer(net::kMaxBytesToSniff)); |
| 96 weak_factory_.GetWeakPtr(), | 93 ReadBytes(url_, header_buf, 0, net::kMaxBytesToSniff, |
| 97 make_scoped_refptr(buf), buf_len, callback), | 94 base::Bind(&MTPFileStreamReader::FinishValidateMediaHeader, |
| 98 base::Bind(&CallCompletionCallbackWithPlatformFileError, callback)); | 95 weak_factory_.GetWeakPtr(), header_buf, |
| 96 make_scoped_refptr(buf), buf_len, callback), |
| 97 callback); |
| 98 return net::ERR_IO_PENDING; |
| 99 } |
| 100 |
| 101 ReadBytes(url_, buf, current_offset_, buf_len, |
| 102 base::Bind(&MTPFileStreamReader::FinishRead, |
| 103 weak_factory_.GetWeakPtr(), callback), |
| 104 callback); |
| 99 | 105 |
| 100 return net::ERR_IO_PENDING; | 106 return net::ERR_IO_PENDING; |
| 101 } | 107 } |
| 102 | 108 |
| 103 int64 MTPFileStreamReader::GetLength( | 109 int64 MTPFileStreamReader::GetLength( |
| 104 const net::Int64CompletionCallback& callback) { | 110 const net::Int64CompletionCallback& callback) { |
| 105 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 111 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 106 | 112 |
| 107 MTPDeviceAsyncDelegate* delegate = GetMTPDeviceDelegate(url_); | 113 MTPDeviceAsyncDelegate* delegate = GetMTPDeviceDelegate(url_); |
| 108 if (!delegate) | 114 if (!delegate) |
| 109 return net::ERR_FAILED; | 115 return net::ERR_FAILED; |
| 110 | 116 |
| 111 delegate->GetFileInfo( | 117 delegate->GetFileInfo( |
| 112 url_.path(), | 118 url_.path(), |
| 113 base::Bind(&MTPFileStreamReader::FinishGetLength, | 119 base::Bind(&MTPFileStreamReader::FinishGetLength, |
| 114 weak_factory_.GetWeakPtr(), callback), | 120 weak_factory_.GetWeakPtr(), callback), |
| 115 base::Bind(&CallInt64CompletionCallbackWithPlatformFileError, | 121 base::Bind(&CallInt64CompletionCallbackWithPlatformFileError, |
| 116 callback)); | 122 callback)); |
| 117 | 123 |
| 118 return net::ERR_IO_PENDING; | 124 return net::ERR_IO_PENDING; |
| 119 } | 125 } |
| 120 | 126 |
| 121 void MTPFileStreamReader::OnFileInfoForRead( | |
| 122 net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback, | |
| 123 const base::File::Info& file_info) { | |
| 124 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 125 | |
| 126 if (!VerifySnapshotTime(expected_modification_time_, file_info)) { | |
| 127 callback.Run(net::ERR_UPLOAD_FILE_CHANGED); | |
| 128 return; | |
| 129 } | |
| 130 | |
| 131 if (!media_header_validated_) { | |
| 132 int header_bytes = std::min(net::kMaxBytesToSniff, | |
| 133 base::saturated_cast<int>(file_info.size)); | |
| 134 scoped_refptr<net::IOBuffer> header_buf(new net::IOBuffer(header_bytes)); | |
| 135 ReadBytes(url_, header_buf, 0, header_bytes, file_info, | |
| 136 base::Bind(&MTPFileStreamReader::FinishValidateMediaHeader, | |
| 137 weak_factory_.GetWeakPtr(), header_buf, file_info, | |
| 138 make_scoped_refptr(buf), buf_len, callback)); | |
| 139 return; | |
| 140 } | |
| 141 | |
| 142 ReadBytes(url_, buf, current_offset_, buf_len, file_info, | |
| 143 base::Bind(&MTPFileStreamReader::FinishRead, | |
| 144 weak_factory_.GetWeakPtr(), callback)); | |
| 145 } | |
| 146 | |
| 147 void MTPFileStreamReader::FinishValidateMediaHeader( | 127 void MTPFileStreamReader::FinishValidateMediaHeader( |
| 148 net::IOBuffer* header_buf, | 128 net::IOBuffer* header_buf, |
| 149 const base::File::Info& file_info, | |
| 150 net::IOBuffer* buf, int buf_len, | 129 net::IOBuffer* buf, int buf_len, |
| 151 const net::CompletionCallback& callback, | 130 const net::CompletionCallback& callback, |
| 131 const base::File::Info& file_info, |
| 152 int header_bytes_read) { | 132 int header_bytes_read) { |
| 153 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 133 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 154 DCHECK_GE(header_bytes_read, 0); | 134 DCHECK_GE(header_bytes_read, 0); |
| 155 base::File::Error error = NativeMediaFileUtil::BufferIsMediaHeader( | 135 base::File::Error error = NativeMediaFileUtil::BufferIsMediaHeader( |
| 156 header_buf, header_bytes_read); | 136 header_buf, header_bytes_read); |
| 157 if (error != base::File::FILE_OK) { | 137 if (error != base::File::FILE_OK) { |
| 158 CallCompletionCallbackWithPlatformFileError(callback, error); | 138 CallCompletionCallbackWithPlatformFileError(callback, error); |
| 159 return; | 139 return; |
| 160 } | 140 } |
| 161 | 141 |
| 162 media_header_validated_ = true; | 142 media_header_validated_ = true; |
| 163 | 143 |
| 164 // Complete originally requested read. | 144 // Complete originally requested read. |
| 165 ReadBytes(url_, buf, current_offset_, buf_len, file_info, | 145 ReadBytes(url_, buf, current_offset_, buf_len, |
| 166 base::Bind(&MTPFileStreamReader::FinishRead, | 146 base::Bind(&MTPFileStreamReader::FinishRead, |
| 167 weak_factory_.GetWeakPtr(), callback)); | 147 weak_factory_.GetWeakPtr(), callback), |
| 148 callback); |
| 168 } | 149 } |
| 169 | 150 |
| 170 void MTPFileStreamReader::FinishRead(const net::CompletionCallback& callback, | 151 void MTPFileStreamReader::FinishRead(const net::CompletionCallback& callback, |
| 152 const base::File::Info& file_info, |
| 171 int bytes_read) { | 153 int bytes_read) { |
| 172 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 154 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 155 |
| 156 if (!VerifySnapshotTime(expected_modification_time_, file_info)) { |
| 157 callback.Run(net::ERR_UPLOAD_FILE_CHANGED); |
| 158 return; |
| 159 } |
| 160 |
| 173 DCHECK_GE(bytes_read, 0); | 161 DCHECK_GE(bytes_read, 0); |
| 174 current_offset_ += bytes_read; | 162 current_offset_ += bytes_read; |
| 175 callback.Run(bytes_read); | 163 callback.Run(bytes_read); |
| 176 } | 164 } |
| 177 | 165 |
| 178 void MTPFileStreamReader::FinishGetLength( | 166 void MTPFileStreamReader::FinishGetLength( |
| 179 const net::Int64CompletionCallback& callback, | 167 const net::Int64CompletionCallback& callback, |
| 180 const base::File::Info& file_info) { | 168 const base::File::Info& file_info) { |
| 181 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 169 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 182 | 170 |
| 183 if (!VerifySnapshotTime(expected_modification_time_, file_info)) { | 171 if (!VerifySnapshotTime(expected_modification_time_, file_info)) { |
| 184 callback.Run(net::ERR_UPLOAD_FILE_CHANGED); | 172 callback.Run(net::ERR_UPLOAD_FILE_CHANGED); |
| 185 return; | 173 return; |
| 186 } | 174 } |
| 187 | 175 |
| 188 callback.Run(file_info.size); | 176 callback.Run(file_info.size); |
| 189 } | 177 } |
| OLD | NEW |