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 "base/macros.h" |
8 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
9 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
10 #include "chrome/browser/chromeos/file_system_provider/abort_callback.h" | 11 #include "chrome/browser/chromeos/file_system_provider/abort_callback.h" |
11 #include "chrome/browser/chromeos/file_system_provider/fileapi/provider_async_fi
le_util.h" | 12 #include "chrome/browser/chromeos/file_system_provider/fileapi/provider_async_fi
le_util.h" |
12 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h" | 13 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h" |
13 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" | 14 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" |
14 #include "chrome/browser/chromeos/file_system_provider/scoped_file_opener.h" | 15 #include "chrome/browser/chromeos/file_system_provider/scoped_file_opener.h" |
15 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
16 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
17 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
18 | 19 |
19 using content::BrowserThread; | 20 using content::BrowserThread; |
20 | 21 |
21 namespace chromeos { | 22 namespace chromeos { |
22 namespace file_system_provider { | 23 namespace file_system_provider { |
23 | 24 |
24 // Converts net::CompletionCallback to net::Int64CompletionCallback. | 25 // Converts net::CompletionCallback to net::Int64CompletionCallback. |
25 void Int64ToIntCompletionCallback(net::CompletionCallback callback, | 26 void Int64ToIntCompletionCallback(net::CompletionCallback callback, |
26 int64 result) { | 27 int64_t result) { |
27 callback.Run(static_cast<int>(result)); | 28 callback.Run(static_cast<int>(result)); |
28 } | 29 } |
29 | 30 |
30 class FileStreamReader::OperationRunner | 31 class FileStreamReader::OperationRunner |
31 : public base::RefCountedThreadSafe< | 32 : public base::RefCountedThreadSafe< |
32 FileStreamReader::OperationRunner, | 33 FileStreamReader::OperationRunner, |
33 content::BrowserThread::DeleteOnUIThread> { | 34 content::BrowserThread::DeleteOnUIThread> { |
34 public: | 35 public: |
35 OperationRunner() : file_handle_(0) {} | 36 OperationRunner() : file_handle_(0) {} |
36 | 37 |
(...skipping 21 matching lines...) Expand all Loading... |
58 base::Bind(&OperationRunner::OnOpenFileCompletedOnUIThread, this, | 59 base::Bind(&OperationRunner::OnOpenFileCompletedOnUIThread, this, |
59 callback))); | 60 callback))); |
60 } | 61 } |
61 | 62 |
62 // Requests reading contents of a file. In case of either success or a failure | 63 // Requests reading contents of a file. In case of either success or a failure |
63 // |callback| is executed. It can be called many times, until |has_more| is | 64 // |callback| is executed. It can be called many times, until |has_more| is |
64 // set to false. This function guarantees that it will succeed only if the | 65 // set to false. This function guarantees that it will succeed only if the |
65 // file has not been changed while reading. Must be called on UI thread. | 66 // file has not been changed while reading. Must be called on UI thread. |
66 void ReadFileOnUIThread( | 67 void ReadFileOnUIThread( |
67 scoped_refptr<net::IOBuffer> buffer, | 68 scoped_refptr<net::IOBuffer> buffer, |
68 int64 offset, | 69 int64_t offset, |
69 int length, | 70 int length, |
70 const ProvidedFileSystemInterface::ReadChunkReceivedCallback& callback) { | 71 const ProvidedFileSystemInterface::ReadChunkReceivedCallback& callback) { |
71 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 72 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
72 DCHECK(abort_callback_.is_null()); | 73 DCHECK(abort_callback_.is_null()); |
73 | 74 |
74 // If the file system got unmounted, then abort the reading operation. | 75 // If the file system got unmounted, then abort the reading operation. |
75 if (!file_system_.get()) { | 76 if (!file_system_.get()) { |
76 BrowserThread::PostTask( | 77 BrowserThread::PostTask( |
77 BrowserThread::IO, | 78 BrowserThread::IO, |
78 FROM_HERE, | 79 FROM_HERE, |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 base::WeakPtr<ProvidedFileSystemInterface> file_system_; | 190 base::WeakPtr<ProvidedFileSystemInterface> file_system_; |
190 base::FilePath file_path_; | 191 base::FilePath file_path_; |
191 scoped_ptr<ScopedFileOpener> file_opener_; | 192 scoped_ptr<ScopedFileOpener> file_opener_; |
192 int file_handle_; | 193 int file_handle_; |
193 | 194 |
194 DISALLOW_COPY_AND_ASSIGN(OperationRunner); | 195 DISALLOW_COPY_AND_ASSIGN(OperationRunner); |
195 }; | 196 }; |
196 | 197 |
197 FileStreamReader::FileStreamReader(storage::FileSystemContext* context, | 198 FileStreamReader::FileStreamReader(storage::FileSystemContext* context, |
198 const storage::FileSystemURL& url, | 199 const storage::FileSystemURL& url, |
199 int64 initial_offset, | 200 int64_t initial_offset, |
200 const base::Time& expected_modification_time) | 201 const base::Time& expected_modification_time) |
201 : url_(url), | 202 : url_(url), |
202 current_offset_(initial_offset), | 203 current_offset_(initial_offset), |
203 current_length_(0), | 204 current_length_(0), |
204 expected_modification_time_(expected_modification_time), | 205 expected_modification_time_(expected_modification_time), |
205 runner_(new OperationRunner), | 206 runner_(new OperationRunner), |
206 state_(NOT_INITIALIZED), | 207 state_(NOT_INITIALIZED), |
207 weak_ptr_factory_(this) { | 208 weak_ptr_factory_(this) {} |
208 } | |
209 | 209 |
210 FileStreamReader::~FileStreamReader() { | 210 FileStreamReader::~FileStreamReader() { |
211 // FileStreamReader doesn't have a Cancel() method like in FileStreamWriter. | 211 // FileStreamReader doesn't have a Cancel() method like in FileStreamWriter. |
212 // Therefore, aborting and/or closing an opened file is done from the | 212 // Therefore, aborting and/or closing an opened file is done from the |
213 // destructor. | 213 // destructor. |
214 BrowserThread::PostTask( | 214 BrowserThread::PostTask( |
215 BrowserThread::UI, FROM_HERE, | 215 BrowserThread::UI, FROM_HERE, |
216 base::Bind(&OperationRunner::CloseRunnerOnUIThread, runner_)); | 216 base::Bind(&OperationRunner::CloseRunnerOnUIThread, runner_)); |
217 | 217 |
218 // If a read is in progress, mark it as completed. | 218 // If a read is in progress, mark it as completed. |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 } | 346 } |
347 | 347 |
348 void FileStreamReader::OnReadCompleted(net::CompletionCallback callback, | 348 void FileStreamReader::OnReadCompleted(net::CompletionCallback callback, |
349 int result) { | 349 int result) { |
350 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 350 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
351 callback.Run(static_cast<int>(result)); | 351 callback.Run(static_cast<int>(result)); |
352 TRACE_EVENT_ASYNC_END0( | 352 TRACE_EVENT_ASYNC_END0( |
353 "file_system_provider", "FileStreamReader::Read", this); | 353 "file_system_provider", "FileStreamReader::Read", this); |
354 } | 354 } |
355 | 355 |
356 int64 FileStreamReader::GetLength( | 356 int64_t FileStreamReader::GetLength( |
357 const net::Int64CompletionCallback& callback) { | 357 const net::Int64CompletionCallback& callback) { |
358 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 358 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
359 | 359 |
360 switch (state_) { | 360 switch (state_) { |
361 case NOT_INITIALIZED: | 361 case NOT_INITIALIZED: |
362 // Lazily initialize with the first call to GetLength(). | 362 // Lazily initialize with the first call to GetLength(). |
363 Initialize(base::Bind(&FileStreamReader::GetLengthAfterInitialized, | 363 Initialize(base::Bind(&FileStreamReader::GetLengthAfterInitialized, |
364 weak_ptr_factory_.GetWeakPtr(), | 364 weak_ptr_factory_.GetWeakPtr(), |
365 callback), | 365 callback), |
366 callback); | 366 callback); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 callback.Run(net::ERR_UPLOAD_FILE_CHANGED); | 470 callback.Run(net::ERR_UPLOAD_FILE_CHANGED); |
471 return; | 471 return; |
472 } | 472 } |
473 | 473 |
474 DCHECK_EQ(base::File::FILE_OK, result); | 474 DCHECK_EQ(base::File::FILE_OK, result); |
475 callback.Run(*metadata->size); | 475 callback.Run(*metadata->size); |
476 } | 476 } |
477 | 477 |
478 } // namespace file_system_provider | 478 } // namespace file_system_provider |
479 } // namespace chromeos | 479 } // namespace chromeos |
OLD | NEW |