[email protected] | 522c245 | 2013-04-30 10:39:03 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
lukasza | 76b4a98 | 2015-08-08 00:36:39 | [diff] [blame] | 5 | #include "components/drive/local_file_reader.h" |
[email protected] | 522c245 | 2013-04-30 10:39:03 | [diff] [blame] | 6 | |
[email protected] | 522c245 | 2013-04-30 10:39:03 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 522c245 | 2013-04-30 10:39:03 | [diff] [blame] | 8 | #include "base/files/file_path.h" |
[email protected] | fb44196 | 2013-05-08 05:35:24 | [diff] [blame] | 9 | #include "base/sequenced_task_runner.h" |
[email protected] | 522c245 | 2013-04-30 10:39:03 | [diff] [blame] | 10 | #include "net/base/completion_callback.h" |
| 11 | #include "net/base/io_buffer.h" |
| 12 | #include "net/base/net_errors.h" |
| 13 | |
| 14 | namespace drive { |
| 15 | namespace util { |
| 16 | |
[email protected] | 300da8a4 | 2013-05-08 21:12:53 | [diff] [blame] | 17 | LocalFileReader::LocalFileReader( |
| 18 | base::SequencedTaskRunner* sequenced_task_runner) |
[email protected] | 619a0398c | 2014-04-23 08:28:45 | [diff] [blame] | 19 | : file_stream_(sequenced_task_runner), |
[email protected] | 522c245 | 2013-04-30 10:39:03 | [diff] [blame] | 20 | weak_ptr_factory_(this) { |
[email protected] | 522c245 | 2013-04-30 10:39:03 | [diff] [blame] | 21 | } |
| 22 | |
[email protected] | 300da8a4 | 2013-05-08 21:12:53 | [diff] [blame] | 23 | LocalFileReader::~LocalFileReader() { |
[email protected] | 522c245 | 2013-04-30 10:39:03 | [diff] [blame] | 24 | } |
| 25 | |
[email protected] | 300da8a4 | 2013-05-08 21:12:53 | [diff] [blame] | 26 | void LocalFileReader::Open(const base::FilePath& file_path, |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 27 | int64_t offset, |
[email protected] | 300da8a4 | 2013-05-08 21:12:53 | [diff] [blame] | 28 | const net::CompletionCallback& callback) { |
[email protected] | 522c245 | 2013-04-30 10:39:03 | [diff] [blame] | 29 | DCHECK(!callback.is_null()); |
[email protected] | b7617c7 | 2014-03-29 00:57:24 | [diff] [blame] | 30 | int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | |
[email protected] | 866534a9 | 2014-05-07 03:39:15 | [diff] [blame] | 31 | base::File::FLAG_ASYNC; |
[email protected] | 522c245 | 2013-04-30 10:39:03 | [diff] [blame] | 32 | |
[email protected] | b7617c7 | 2014-03-29 00:57:24 | [diff] [blame] | 33 | int rv = file_stream_.Open(file_path, flags, |
| 34 | Bind(&LocalFileReader::DidOpen, |
| 35 | weak_ptr_factory_.GetWeakPtr(), |
| 36 | callback, offset)); |
| 37 | DCHECK_EQ(rv, net::ERR_IO_PENDING); |
[email protected] | 522c245 | 2013-04-30 10:39:03 | [diff] [blame] | 38 | } |
| 39 | |
[email protected] | 300da8a4 | 2013-05-08 21:12:53 | [diff] [blame] | 40 | void LocalFileReader::Read(net::IOBuffer* in_buffer, |
| 41 | int buffer_length, |
| 42 | const net::CompletionCallback& callback) { |
[email protected] | 522c245 | 2013-04-30 10:39:03 | [diff] [blame] | 43 | DCHECK(!callback.is_null()); |
[email protected] | b7617c7 | 2014-03-29 00:57:24 | [diff] [blame] | 44 | DCHECK(file_stream_.IsOpen()); |
| 45 | int rv = file_stream_.Read(in_buffer, buffer_length, callback); |
| 46 | DCHECK_EQ(rv, net::ERR_IO_PENDING); |
[email protected] | 522c245 | 2013-04-30 10:39:03 | [diff] [blame] | 47 | } |
| 48 | |
[email protected] | b7617c7 | 2014-03-29 00:57:24 | [diff] [blame] | 49 | void LocalFileReader::DidOpen(const net::CompletionCallback& callback, |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 50 | int64_t offset, |
[email protected] | b7617c7 | 2014-03-29 00:57:24 | [diff] [blame] | 51 | int error) { |
| 52 | if (error != net::OK) |
| 53 | return callback.Run(error); |
[email protected] | 522c245 | 2013-04-30 10:39:03 | [diff] [blame] | 54 | |
reillyg | aa435e7 | 2015-06-16 00:48:48 | [diff] [blame] | 55 | int rv = file_stream_.Seek( |
| 56 | offset, Bind(&LocalFileReader::DidSeek, weak_ptr_factory_.GetWeakPtr(), |
| 57 | callback, offset)); |
[email protected] | b7617c7 | 2014-03-29 00:57:24 | [diff] [blame] | 58 | DCHECK_EQ(rv, net::ERR_IO_PENDING); |
| 59 | } |
[email protected] | 522c245 | 2013-04-30 10:39:03 | [diff] [blame] | 60 | |
[email protected] | b7617c7 | 2014-03-29 00:57:24 | [diff] [blame] | 61 | void LocalFileReader::DidSeek(const net::CompletionCallback& callback, |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 62 | int64_t offset, |
| 63 | int64_t error) { |
[email protected] | b7617c7 | 2014-03-29 00:57:24 | [diff] [blame] | 64 | callback.Run(error == offset ? net::OK : net::ERR_FAILED); |
[email protected] | 522c245 | 2013-04-30 10:39:03 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | } // namespace util |
| 68 | } // namespace drive |