| 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/chromeos/file_system_provider/fileapi/file_stream_reade
r.h" | 5 #include "chrome/browser/chromeos/file_system_provider/fileapi/file_stream_reade
r.h" |
| 6 | 6 |
| 7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
| 8 #include "chrome/browser/chromeos/file_system_provider/fileapi/provider_async_fi
le_util.h" | 8 #include "chrome/browser/chromeos/file_system_provider/fileapi/provider_async_fi
le_util.h" |
| 9 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h" | 9 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h" |
| 10 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" | 10 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 // Closes a file. Ignores result, since it is called from a constructor. | 69 // Closes a file. Ignores result, since it is called from a constructor. |
| 70 // Must be called on UI thread. | 70 // Must be called on UI thread. |
| 71 void CloseFileOnUIThread(base::WeakPtr<ProvidedFileSystemInterface> file_system, | 71 void CloseFileOnUIThread(base::WeakPtr<ProvidedFileSystemInterface> file_system, |
| 72 int file_handle) { | 72 int file_handle) { |
| 73 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 73 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 74 if (file_system.get()) | 74 if (file_system.get()) |
| 75 file_system->CloseFile(file_handle, base::Bind(&EmptyStatusCallback)); | 75 file_system->CloseFile(file_handle, base::Bind(&EmptyStatusCallback)); |
| 76 } | 76 } |
| 77 | 77 |
| 78 // Requests reading contents of a file. In case of either success or a failure | 78 // Requests reading contents of a file. In case of either success or a failure |
| 79 // |callback| is executed. It can be called many times, until |has_next| is set | 79 // |callback| is executed. It can be called many times, until |has_more| is set |
| 80 // to false. This function guarantees that it will succeed only if the file has | 80 // to false. This function guarantees that it will succeed only if the file has |
| 81 // not been changed while reading. Must be called on UI thread. | 81 // not been changed while reading. Must be called on UI thread. |
| 82 void ReadFileOnUIThread( | 82 void ReadFileOnUIThread( |
| 83 base::WeakPtr<ProvidedFileSystemInterface> file_system, | 83 base::WeakPtr<ProvidedFileSystemInterface> file_system, |
| 84 int file_handle, | 84 int file_handle, |
| 85 net::IOBuffer* buffer, | 85 net::IOBuffer* buffer, |
| 86 int64 offset, | 86 int64 offset, |
| 87 int length, | 87 int length, |
| 88 const ProvidedFileSystemInterface::ReadChunkReceivedCallback& callback) { | 88 const ProvidedFileSystemInterface::ReadChunkReceivedCallback& callback) { |
| 89 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 89 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 90 | 90 |
| 91 // If the file system got unmounted, then abort the reading operation. | 91 // If the file system got unmounted, then abort the reading operation. |
| 92 if (!file_system.get()) { | 92 if (!file_system.get()) { |
| 93 callback.Run(0, false /* has_next */, base::File::FILE_ERROR_ABORT); | 93 callback.Run(0, false /* has_more */, base::File::FILE_ERROR_ABORT); |
| 94 return; | 94 return; |
| 95 } | 95 } |
| 96 | 96 |
| 97 file_system->ReadFile(file_handle, buffer, offset, length, callback); | 97 file_system->ReadFile(file_handle, buffer, offset, length, callback); |
| 98 } | 98 } |
| 99 | 99 |
| 100 // Forward the completion callback to IO thread. | 100 // Forward the completion callback to IO thread. |
| 101 void OnReadChunkReceivedOnUIThread( | 101 void OnReadChunkReceivedOnUIThread( |
| 102 const ProvidedFileSystemInterface::ReadChunkReceivedCallback& | 102 const ProvidedFileSystemInterface::ReadChunkReceivedCallback& |
| 103 chunk_received_callback, | 103 chunk_received_callback, |
| 104 int chunk_length, | 104 int chunk_length, |
| 105 bool has_next, | 105 bool has_more, |
| 106 base::File::Error result) { | 106 base::File::Error result) { |
| 107 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 107 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 108 BrowserThread::PostTask( | 108 BrowserThread::PostTask( |
| 109 BrowserThread::IO, | 109 BrowserThread::IO, |
| 110 FROM_HERE, | 110 FROM_HERE, |
| 111 base::Bind(chunk_received_callback, chunk_length, has_next, result)); | 111 base::Bind(chunk_received_callback, chunk_length, has_more, result)); |
| 112 } | 112 } |
| 113 | 113 |
| 114 // Requests metadata of a file. In case of either succes or a failure, | 114 // Requests metadata of a file. In case of either succes or a failure, |
| 115 // |callback is executed. Must be called on UI thread. | 115 // |callback is executed. Must be called on UI thread. |
| 116 void GetMetadataOnUIThread( | 116 void GetMetadataOnUIThread( |
| 117 base::WeakPtr<ProvidedFileSystemInterface> file_system, | 117 base::WeakPtr<ProvidedFileSystemInterface> file_system, |
| 118 const base::FilePath& file_path, | 118 const base::FilePath& file_path, |
| 119 const fileapi::AsyncFileUtil::GetFileInfoCallback& callback) { | 119 const fileapi::AsyncFileUtil::GetFileInfoCallback& callback) { |
| 120 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 120 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 121 | 121 |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 base::Bind( | 280 base::Bind( |
| 281 &OnGetMetadataReceivedOnUIThread, | 281 &OnGetMetadataReceivedOnUIThread, |
| 282 base::Bind(&FileStreamReader::OnGetMetadataForGetLengthReceived, | 282 base::Bind(&FileStreamReader::OnGetMetadataForGetLengthReceived, |
| 283 weak_ptr_factory_.GetWeakPtr(), | 283 weak_ptr_factory_.GetWeakPtr(), |
| 284 callback)))); | 284 callback)))); |
| 285 } | 285 } |
| 286 | 286 |
| 287 void FileStreamReader::OnReadChunkReceived( | 287 void FileStreamReader::OnReadChunkReceived( |
| 288 const net::CompletionCallback& callback, | 288 const net::CompletionCallback& callback, |
| 289 int chunk_length, | 289 int chunk_length, |
| 290 bool has_next, | 290 bool has_more, |
| 291 base::File::Error result) { | 291 base::File::Error result) { |
| 292 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 292 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 293 current_length_ += chunk_length; | 293 current_length_ += chunk_length; |
| 294 | 294 |
| 295 // If this is the last chunk with a success, then finalize. | 295 // If this is the last chunk with a success, then finalize. |
| 296 if (!has_next && result == base::File::FILE_OK) { | 296 if (!has_more && result == base::File::FILE_OK) { |
| 297 current_offset_ += current_length_; | 297 current_offset_ += current_length_; |
| 298 callback.Run(current_length_); | 298 callback.Run(current_length_); |
| 299 return; | 299 return; |
| 300 } | 300 } |
| 301 | 301 |
| 302 // In case of an error, abort. | 302 // In case of an error, abort. |
| 303 if (result != base::File::FILE_OK) { | 303 if (result != base::File::FILE_OK) { |
| 304 DCHECK(!has_next); | 304 DCHECK(!has_more); |
| 305 callback.Run(net::FileErrorToNetError(result)); | 305 callback.Run(net::FileErrorToNetError(result)); |
| 306 return; | 306 return; |
| 307 } | 307 } |
| 308 | 308 |
| 309 // More data is about to come, so do not call the callback yet. | 309 // More data is about to come, so do not call the callback yet. |
| 310 DCHECK(has_next); | 310 DCHECK(has_more); |
| 311 } | 311 } |
| 312 | 312 |
| 313 void FileStreamReader::OnGetMetadataForGetLengthReceived( | 313 void FileStreamReader::OnGetMetadataForGetLengthReceived( |
| 314 const net::Int64CompletionCallback& callback, | 314 const net::Int64CompletionCallback& callback, |
| 315 base::File::Error result, | 315 base::File::Error result, |
| 316 const base::File::Info& file_info) { | 316 const base::File::Info& file_info) { |
| 317 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 317 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 318 | 318 |
| 319 // In case of an error, abort. | 319 // In case of an error, abort. |
| 320 if (result != base::File::FILE_OK) { | 320 if (result != base::File::FILE_OK) { |
| 321 callback.Run(net::FileErrorToNetError(result)); | 321 callback.Run(net::FileErrorToNetError(result)); |
| 322 return; | 322 return; |
| 323 } | 323 } |
| 324 | 324 |
| 325 DCHECK_EQ(result, base::File::FILE_OK); | 325 DCHECK_EQ(result, base::File::FILE_OK); |
| 326 callback.Run(file_info.size); | 326 callback.Run(file_info.size); |
| 327 } | 327 } |
| 328 | 328 |
| 329 } // namespace file_system_provider | 329 } // namespace file_system_provider |
| 330 } // namespace chromeos | 330 } // namespace chromeos |
| OLD | NEW |