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 "chrome/browser/chromeos/drive/local_file_reader.h"
8 #include "base/files/file_path.h"
9 #include "base/profiler/scoped_tracker.h"
10 #include "base/sequenced_task_runner.h"
11 #include "net/base/completion_callback.h"
12 #include "net/base/io_buffer.h"
13 #include "net/base/net_errors.h"
18 LocalFileReader::LocalFileReader(
19 base::SequencedTaskRunner
* sequenced_task_runner
)
20 : file_stream_(sequenced_task_runner
),
21 weak_ptr_factory_(this) {
24 LocalFileReader::~LocalFileReader() {
27 void LocalFileReader::Open(const base::FilePath
& file_path
,
29 const net::CompletionCallback
& callback
) {
30 DCHECK(!callback
.is_null());
31 int flags
= base::File::FLAG_OPEN
| base::File::FLAG_READ
|
32 base::File::FLAG_ASYNC
;
34 int rv
= file_stream_
.Open(file_path
, flags
,
35 Bind(&LocalFileReader::DidOpen
,
36 weak_ptr_factory_
.GetWeakPtr(),
38 DCHECK_EQ(rv
, net::ERR_IO_PENDING
);
41 void LocalFileReader::Read(net::IOBuffer
* in_buffer
,
43 const net::CompletionCallback
& callback
) {
44 DCHECK(!callback
.is_null());
45 DCHECK(file_stream_
.IsOpen());
46 int rv
= file_stream_
.Read(in_buffer
, buffer_length
, callback
);
47 DCHECK_EQ(rv
, net::ERR_IO_PENDING
);
50 void LocalFileReader::DidOpen(const net::CompletionCallback
& callback
,
53 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
54 tracked_objects::ScopedTracker
tracking_profile(
55 FROM_HERE_WITH_EXPLICIT_FUNCTION("423948 LocalFileReader::DidOpen"));
58 return callback
.Run(error
);
60 int rv
= file_stream_
.Seek(base::File::FROM_BEGIN
, offset
,
61 Bind(&LocalFileReader::DidSeek
,
62 weak_ptr_factory_
.GetWeakPtr(),
64 DCHECK_EQ(rv
, net::ERR_IO_PENDING
);
67 void LocalFileReader::DidSeek(const net::CompletionCallback
& callback
,
70 callback
.Run(error
== offset
? net::OK
: net::ERR_FAILED
);