blob: de03e7f746d4f5af55cc365a8ffe16f31cceea21 [file] [log] [blame]
[email protected]522c2452013-04-30 10:39:031// 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
lukasza76b4a982015-08-08 00:36:395#include "components/drive/local_file_reader.h"
[email protected]522c2452013-04-30 10:39:036
[email protected]522c2452013-04-30 10:39:037#include "base/bind.h"
[email protected]522c2452013-04-30 10:39:038#include "base/files/file_path.h"
[email protected]fb441962013-05-08 05:35:249#include "base/sequenced_task_runner.h"
[email protected]522c2452013-04-30 10:39:0310#include "net/base/completion_callback.h"
11#include "net/base/io_buffer.h"
12#include "net/base/net_errors.h"
13
14namespace drive {
15namespace util {
16
[email protected]300da8a42013-05-08 21:12:5317LocalFileReader::LocalFileReader(
18 base::SequencedTaskRunner* sequenced_task_runner)
[email protected]619a0398c2014-04-23 08:28:4519 : file_stream_(sequenced_task_runner),
[email protected]522c2452013-04-30 10:39:0320 weak_ptr_factory_(this) {
[email protected]522c2452013-04-30 10:39:0321}
22
[email protected]300da8a42013-05-08 21:12:5323LocalFileReader::~LocalFileReader() {
[email protected]522c2452013-04-30 10:39:0324}
25
[email protected]300da8a42013-05-08 21:12:5326void LocalFileReader::Open(const base::FilePath& file_path,
avibc5337b2015-12-25 23:16:3327 int64_t offset,
[email protected]300da8a42013-05-08 21:12:5328 const net::CompletionCallback& callback) {
[email protected]522c2452013-04-30 10:39:0329 DCHECK(!callback.is_null());
[email protected]b7617c72014-03-29 00:57:2430 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
[email protected]866534a92014-05-07 03:39:1531 base::File::FLAG_ASYNC;
[email protected]522c2452013-04-30 10:39:0332
[email protected]b7617c72014-03-29 00:57:2433 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]522c2452013-04-30 10:39:0338}
39
[email protected]300da8a42013-05-08 21:12:5340void LocalFileReader::Read(net::IOBuffer* in_buffer,
41 int buffer_length,
42 const net::CompletionCallback& callback) {
[email protected]522c2452013-04-30 10:39:0343 DCHECK(!callback.is_null());
[email protected]b7617c72014-03-29 00:57:2444 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]522c2452013-04-30 10:39:0347}
48
[email protected]b7617c72014-03-29 00:57:2449void LocalFileReader::DidOpen(const net::CompletionCallback& callback,
avibc5337b2015-12-25 23:16:3350 int64_t offset,
[email protected]b7617c72014-03-29 00:57:2451 int error) {
52 if (error != net::OK)
53 return callback.Run(error);
[email protected]522c2452013-04-30 10:39:0354
reillygaa435e72015-06-16 00:48:4855 int rv = file_stream_.Seek(
56 offset, Bind(&LocalFileReader::DidSeek, weak_ptr_factory_.GetWeakPtr(),
57 callback, offset));
[email protected]b7617c72014-03-29 00:57:2458 DCHECK_EQ(rv, net::ERR_IO_PENDING);
59}
[email protected]522c2452013-04-30 10:39:0360
[email protected]b7617c72014-03-29 00:57:2461void LocalFileReader::DidSeek(const net::CompletionCallback& callback,
avibc5337b2015-12-25 23:16:3362 int64_t offset,
63 int64_t error) {
[email protected]b7617c72014-03-29 00:57:2464 callback.Run(error == offset ? net::OK : net::ERR_FAILED);
[email protected]522c2452013-04-30 10:39:0365}
66
67} // namespace util
68} // namespace drive