1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/drive/local_file_reader.h"
#include <errno.h>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/files/file_path.h"
#include "base/location.h"
#include "base/platform_file.h"
#include "base/sequenced_task_runner.h"
#include "base/task_runner_util.h"
#include "net/base/completion_callback.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
namespace drive {
namespace util {
namespace {
// Opens the file at |file_path| and seeks to the |offset| from begin.
// Returns the net::Error code. If succeeded, |platform_file| is set to point
// the opened file.
// This function should run on the blocking pool.
int OpenAndSeekOnBlockingPool(const base::FilePath& file_path,
int64 offset,
base::PlatformFile* platform_file) {
DCHECK(platform_file);
DCHECK_EQ(base::kInvalidPlatformFileValue, *platform_file);
// First of all, open the file.
const int open_flags = base::PLATFORM_FILE_OPEN |
base::PLATFORM_FILE_READ |
base::PLATFORM_FILE_ASYNC;
base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED;
base::PlatformFile file =
base::CreatePlatformFile(file_path, open_flags, NULL, &error);
if (file == base::kInvalidPlatformFileValue) {
DCHECK_NE(base::PLATFORM_FILE_OK, error);
return net::PlatformFileErrorToNetError(error);
}
// If succeeded, seek to the |offset| from begin.
int64 position = base::SeekPlatformFile(
file, base::PLATFORM_FILE_FROM_BEGIN, offset);
if (position < 0) {
// If failed, close the file and return an error.
base::ClosePlatformFile(file);
return net::ERR_FAILED;
}
*platform_file = file;
return net::OK;
}
// Reads the data from the |platform_file| and copies it to the |buffer| at
// most |buffer_length| size. Returns the number of copied bytes if succeeded,
// or the net::Error code.
// This function should run on the blocking pool.
int ReadOnBlockingPool(base::PlatformFile platform_file,
scoped_refptr<net::IOBuffer> buffer,
int buffer_length) {
DCHECK_NE(base::kInvalidPlatformFileValue, platform_file);
int result = base::ReadPlatformFileCurPosNoBestEffort(
platform_file, buffer->data(), buffer_length);
return result < 0 ? net::MapSystemError(errno) : result;
}
// Posts a task to close the |platform_file| into |task_runner|.
// Or, if |platform_file| is kInvalidPlatformFileValue, does nothing.
void PostCloseIfNeeded(base::TaskRunner* task_runner,
base::PlatformFile platform_file) {
DCHECK(task_runner);
if (platform_file != base::kInvalidPlatformFileValue) {
task_runner->PostTask(
FROM_HERE,
base::Bind(
base::IgnoreResult(&base::ClosePlatformFile), platform_file));
}
}
} // namespace
class LocalFileReader::ScopedPlatformFile {
public:
explicit ScopedPlatformFile(base::TaskRunner* task_runner)
: task_runner_(task_runner),
platform_file_(base::kInvalidPlatformFileValue) {
DCHECK(task_runner);
}
~ScopedPlatformFile() {
PostCloseIfNeeded(task_runner_.get(), platform_file_);
}
base::PlatformFile* ptr() { return &platform_file_; }
base::PlatformFile release() {
base::PlatformFile result = platform_file_;
platform_file_ = base::kInvalidPlatformFileValue;
return result;
}
private:
scoped_refptr<base::TaskRunner> task_runner_;
base::PlatformFile platform_file_;
DISALLOW_COPY_AND_ASSIGN(ScopedPlatformFile);
};
LocalFileReader::LocalFileReader(
base::SequencedTaskRunner* sequenced_task_runner)
: sequenced_task_runner_(sequenced_task_runner),
platform_file_(base::kInvalidPlatformFileValue),
weak_ptr_factory_(this) {
DCHECK(sequenced_task_runner_.get());
}
LocalFileReader::~LocalFileReader() {
PostCloseIfNeeded(sequenced_task_runner_.get(), platform_file_);
}
void LocalFileReader::Open(const base::FilePath& file_path,
int64 offset,
const net::CompletionCallback& callback) {
DCHECK(!callback.is_null());
DCHECK_EQ(base::kInvalidPlatformFileValue, platform_file_);
ScopedPlatformFile* platform_file =
new ScopedPlatformFile(sequenced_task_runner_.get());
base::PostTaskAndReplyWithResult(
sequenced_task_runner_.get(),
FROM_HERE,
base::Bind(
&OpenAndSeekOnBlockingPool, file_path, offset, platform_file->ptr()),
base::Bind(&LocalFileReader::OpenAfterBlockingPoolTask,
weak_ptr_factory_.GetWeakPtr(),
callback,
base::Owned(platform_file)));
}
void LocalFileReader::Read(net::IOBuffer* in_buffer,
int buffer_length,
const net::CompletionCallback& callback) {
DCHECK(!callback.is_null());
DCHECK_NE(base::kInvalidPlatformFileValue, platform_file_);
scoped_refptr<net::IOBuffer> buffer(in_buffer);
base::PostTaskAndReplyWithResult(
sequenced_task_runner_.get(),
FROM_HERE,
base::Bind(&ReadOnBlockingPool, platform_file_, buffer, buffer_length),
callback);
}
void LocalFileReader::OpenAfterBlockingPoolTask(
const net::CompletionCallback& callback,
ScopedPlatformFile* platform_file,
int open_result) {
DCHECK(!callback.is_null());
DCHECK(platform_file);
DCHECK_EQ(base::kInvalidPlatformFileValue, platform_file_);
if (open_result == net::OK) {
DCHECK_NE(base::kInvalidPlatformFileValue, *platform_file->ptr());
platform_file_ = platform_file->release();
}
callback.Run(open_result);
}
} // namespace util
} // namespace drive
|