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.
5 #include "components/drive/local_file_reader.h"
8 #include "base/files/file_path.h"
9 #include "base/sequenced_task_runner.h"
10 #include "net/base/completion_callback.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/net_errors.h"
17 LocalFileReader::LocalFileReader(
18 base::SequencedTaskRunner
* sequenced_task_runner
)
19 : file_stream_(sequenced_task_runner
),
20 weak_ptr_factory_(this) {
23 LocalFileReader::~LocalFileReader() {
26 void LocalFileReader::Open(const base::FilePath
& file_path
,
28 const net::CompletionCallback
& callback
) {
29 DCHECK(!callback
.is_null());
30 int flags
= base::File::FLAG_OPEN
| base::File::FLAG_READ
|
31 base::File::FLAG_ASYNC
;
33 int rv
= file_stream_
.Open(file_path
, flags
,
34 Bind(&LocalFileReader::DidOpen
,
35 weak_ptr_factory_
.GetWeakPtr(),
37 DCHECK_EQ(rv
, net::ERR_IO_PENDING
);
40 void LocalFileReader::Read(net::IOBuffer
* in_buffer
,
42 const net::CompletionCallback
& callback
) {
43 DCHECK(!callback
.is_null());
44 DCHECK(file_stream_
.IsOpen());
45 int rv
= file_stream_
.Read(in_buffer
, buffer_length
, callback
);
46 DCHECK_EQ(rv
, net::ERR_IO_PENDING
);
49 void LocalFileReader::DidOpen(const net::CompletionCallback
& callback
,
53 return callback
.Run(error
);
55 int rv
= file_stream_
.Seek(
56 offset
, Bind(&LocalFileReader::DidSeek
, weak_ptr_factory_
.GetWeakPtr(),
58 DCHECK_EQ(rv
, net::ERR_IO_PENDING
);
61 void LocalFileReader::DidSeek(const net::CompletionCallback
& callback
,
64 callback
.Run(error
== offset
? net::OK
: net::ERR_FAILED
);