Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / local_file_reader.cc
blob6a9fe948215b3f9d3d8df2f7468c5e7a549a45bb
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"
7 #include "base/bind.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"
15 namespace drive {
16 namespace util {
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,
28 int64 offset,
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(),
37 callback, offset));
38 DCHECK_EQ(rv, net::ERR_IO_PENDING);
41 void LocalFileReader::Read(net::IOBuffer* in_buffer,
42 int buffer_length,
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,
51 int64 offset,
52 int error) {
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"));
57 if (error != net::OK)
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(),
63 callback, offset));
64 DCHECK_EQ(rv, net::ERR_IO_PENDING);
67 void LocalFileReader::DidSeek(const net::CompletionCallback& callback,
68 int64 offset,
69 int64 error) {
70 callback.Run(error == offset ? net::OK : net::ERR_FAILED);
73 } // namespace util
74 } // namespace drive