1*635a8641SAndroid Build Coastguard Worker // Copyright 2014 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker
5*635a8641SAndroid Build Coastguard Worker #include "base/files/file_proxy.h"
6*635a8641SAndroid Build Coastguard Worker
7*635a8641SAndroid Build Coastguard Worker #include <utility>
8*635a8641SAndroid Build Coastguard Worker
9*635a8641SAndroid Build Coastguard Worker #include "base/bind.h"
10*635a8641SAndroid Build Coastguard Worker #include "base/bind_helpers.h"
11*635a8641SAndroid Build Coastguard Worker #include "base/files/file.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/files/file_util.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/location.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/task_runner.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/task_runner_util.h"
17*635a8641SAndroid Build Coastguard Worker
18*635a8641SAndroid Build Coastguard Worker namespace {
19*635a8641SAndroid Build Coastguard Worker
FileDeleter(base::File file)20*635a8641SAndroid Build Coastguard Worker void FileDeleter(base::File file) {
21*635a8641SAndroid Build Coastguard Worker }
22*635a8641SAndroid Build Coastguard Worker
23*635a8641SAndroid Build Coastguard Worker } // namespace
24*635a8641SAndroid Build Coastguard Worker
25*635a8641SAndroid Build Coastguard Worker namespace base {
26*635a8641SAndroid Build Coastguard Worker
27*635a8641SAndroid Build Coastguard Worker class FileHelper {
28*635a8641SAndroid Build Coastguard Worker public:
FileHelper(FileProxy * proxy,File file)29*635a8641SAndroid Build Coastguard Worker FileHelper(FileProxy* proxy, File file)
30*635a8641SAndroid Build Coastguard Worker : file_(std::move(file)),
31*635a8641SAndroid Build Coastguard Worker error_(File::FILE_ERROR_FAILED),
32*635a8641SAndroid Build Coastguard Worker task_runner_(proxy->task_runner()),
33*635a8641SAndroid Build Coastguard Worker proxy_(AsWeakPtr(proxy)) {
34*635a8641SAndroid Build Coastguard Worker }
35*635a8641SAndroid Build Coastguard Worker
PassFile()36*635a8641SAndroid Build Coastguard Worker void PassFile() {
37*635a8641SAndroid Build Coastguard Worker if (proxy_)
38*635a8641SAndroid Build Coastguard Worker proxy_->SetFile(std::move(file_));
39*635a8641SAndroid Build Coastguard Worker else if (file_.IsValid())
40*635a8641SAndroid Build Coastguard Worker task_runner_->PostTask(FROM_HERE,
41*635a8641SAndroid Build Coastguard Worker BindOnce(&FileDeleter, std::move(file_)));
42*635a8641SAndroid Build Coastguard Worker }
43*635a8641SAndroid Build Coastguard Worker
44*635a8641SAndroid Build Coastguard Worker protected:
45*635a8641SAndroid Build Coastguard Worker File file_;
46*635a8641SAndroid Build Coastguard Worker File::Error error_;
47*635a8641SAndroid Build Coastguard Worker
48*635a8641SAndroid Build Coastguard Worker private:
49*635a8641SAndroid Build Coastguard Worker scoped_refptr<TaskRunner> task_runner_;
50*635a8641SAndroid Build Coastguard Worker WeakPtr<FileProxy> proxy_;
51*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(FileHelper);
52*635a8641SAndroid Build Coastguard Worker };
53*635a8641SAndroid Build Coastguard Worker
54*635a8641SAndroid Build Coastguard Worker namespace {
55*635a8641SAndroid Build Coastguard Worker
56*635a8641SAndroid Build Coastguard Worker class GenericFileHelper : public FileHelper {
57*635a8641SAndroid Build Coastguard Worker public:
GenericFileHelper(FileProxy * proxy,File file)58*635a8641SAndroid Build Coastguard Worker GenericFileHelper(FileProxy* proxy, File file)
59*635a8641SAndroid Build Coastguard Worker : FileHelper(proxy, std::move(file)) {
60*635a8641SAndroid Build Coastguard Worker }
61*635a8641SAndroid Build Coastguard Worker
Close()62*635a8641SAndroid Build Coastguard Worker void Close() {
63*635a8641SAndroid Build Coastguard Worker file_.Close();
64*635a8641SAndroid Build Coastguard Worker error_ = File::FILE_OK;
65*635a8641SAndroid Build Coastguard Worker }
66*635a8641SAndroid Build Coastguard Worker
SetTimes(Time last_access_time,Time last_modified_time)67*635a8641SAndroid Build Coastguard Worker void SetTimes(Time last_access_time, Time last_modified_time) {
68*635a8641SAndroid Build Coastguard Worker bool rv = file_.SetTimes(last_access_time, last_modified_time);
69*635a8641SAndroid Build Coastguard Worker error_ = rv ? File::FILE_OK : File::FILE_ERROR_FAILED;
70*635a8641SAndroid Build Coastguard Worker }
71*635a8641SAndroid Build Coastguard Worker
SetLength(int64_t length)72*635a8641SAndroid Build Coastguard Worker void SetLength(int64_t length) {
73*635a8641SAndroid Build Coastguard Worker if (file_.SetLength(length))
74*635a8641SAndroid Build Coastguard Worker error_ = File::FILE_OK;
75*635a8641SAndroid Build Coastguard Worker }
76*635a8641SAndroid Build Coastguard Worker
Flush()77*635a8641SAndroid Build Coastguard Worker void Flush() {
78*635a8641SAndroid Build Coastguard Worker if (file_.Flush())
79*635a8641SAndroid Build Coastguard Worker error_ = File::FILE_OK;
80*635a8641SAndroid Build Coastguard Worker }
81*635a8641SAndroid Build Coastguard Worker
Reply(FileProxy::StatusCallback callback)82*635a8641SAndroid Build Coastguard Worker void Reply(FileProxy::StatusCallback callback) {
83*635a8641SAndroid Build Coastguard Worker PassFile();
84*635a8641SAndroid Build Coastguard Worker if (!callback.is_null())
85*635a8641SAndroid Build Coastguard Worker std::move(callback).Run(error_);
86*635a8641SAndroid Build Coastguard Worker }
87*635a8641SAndroid Build Coastguard Worker
88*635a8641SAndroid Build Coastguard Worker private:
89*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(GenericFileHelper);
90*635a8641SAndroid Build Coastguard Worker };
91*635a8641SAndroid Build Coastguard Worker
92*635a8641SAndroid Build Coastguard Worker class CreateOrOpenHelper : public FileHelper {
93*635a8641SAndroid Build Coastguard Worker public:
CreateOrOpenHelper(FileProxy * proxy,File file)94*635a8641SAndroid Build Coastguard Worker CreateOrOpenHelper(FileProxy* proxy, File file)
95*635a8641SAndroid Build Coastguard Worker : FileHelper(proxy, std::move(file)) {
96*635a8641SAndroid Build Coastguard Worker }
97*635a8641SAndroid Build Coastguard Worker
RunWork(const FilePath & file_path,int file_flags)98*635a8641SAndroid Build Coastguard Worker void RunWork(const FilePath& file_path, int file_flags) {
99*635a8641SAndroid Build Coastguard Worker file_.Initialize(file_path, file_flags);
100*635a8641SAndroid Build Coastguard Worker error_ = file_.IsValid() ? File::FILE_OK : file_.error_details();
101*635a8641SAndroid Build Coastguard Worker }
102*635a8641SAndroid Build Coastguard Worker
Reply(FileProxy::StatusCallback callback)103*635a8641SAndroid Build Coastguard Worker void Reply(FileProxy::StatusCallback callback) {
104*635a8641SAndroid Build Coastguard Worker DCHECK(!callback.is_null());
105*635a8641SAndroid Build Coastguard Worker PassFile();
106*635a8641SAndroid Build Coastguard Worker std::move(callback).Run(error_);
107*635a8641SAndroid Build Coastguard Worker }
108*635a8641SAndroid Build Coastguard Worker
109*635a8641SAndroid Build Coastguard Worker private:
110*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(CreateOrOpenHelper);
111*635a8641SAndroid Build Coastguard Worker };
112*635a8641SAndroid Build Coastguard Worker
113*635a8641SAndroid Build Coastguard Worker class CreateTemporaryHelper : public FileHelper {
114*635a8641SAndroid Build Coastguard Worker public:
CreateTemporaryHelper(FileProxy * proxy,File file)115*635a8641SAndroid Build Coastguard Worker CreateTemporaryHelper(FileProxy* proxy, File file)
116*635a8641SAndroid Build Coastguard Worker : FileHelper(proxy, std::move(file)) {
117*635a8641SAndroid Build Coastguard Worker }
118*635a8641SAndroid Build Coastguard Worker
RunWork(uint32_t additional_file_flags)119*635a8641SAndroid Build Coastguard Worker void RunWork(uint32_t additional_file_flags) {
120*635a8641SAndroid Build Coastguard Worker // TODO(darin): file_util should have a variant of CreateTemporaryFile
121*635a8641SAndroid Build Coastguard Worker // that returns a FilePath and a File.
122*635a8641SAndroid Build Coastguard Worker if (!CreateTemporaryFile(&file_path_)) {
123*635a8641SAndroid Build Coastguard Worker // TODO(davidben): base::CreateTemporaryFile should preserve the error
124*635a8641SAndroid Build Coastguard Worker // code.
125*635a8641SAndroid Build Coastguard Worker error_ = File::FILE_ERROR_FAILED;
126*635a8641SAndroid Build Coastguard Worker return;
127*635a8641SAndroid Build Coastguard Worker }
128*635a8641SAndroid Build Coastguard Worker
129*635a8641SAndroid Build Coastguard Worker uint32_t file_flags = File::FLAG_WRITE | File::FLAG_TEMPORARY |
130*635a8641SAndroid Build Coastguard Worker File::FLAG_CREATE_ALWAYS | additional_file_flags;
131*635a8641SAndroid Build Coastguard Worker
132*635a8641SAndroid Build Coastguard Worker file_.Initialize(file_path_, file_flags);
133*635a8641SAndroid Build Coastguard Worker if (file_.IsValid()) {
134*635a8641SAndroid Build Coastguard Worker error_ = File::FILE_OK;
135*635a8641SAndroid Build Coastguard Worker } else {
136*635a8641SAndroid Build Coastguard Worker error_ = file_.error_details();
137*635a8641SAndroid Build Coastguard Worker DeleteFile(file_path_, false);
138*635a8641SAndroid Build Coastguard Worker file_path_.clear();
139*635a8641SAndroid Build Coastguard Worker }
140*635a8641SAndroid Build Coastguard Worker }
141*635a8641SAndroid Build Coastguard Worker
Reply(FileProxy::CreateTemporaryCallback callback)142*635a8641SAndroid Build Coastguard Worker void Reply(FileProxy::CreateTemporaryCallback callback) {
143*635a8641SAndroid Build Coastguard Worker DCHECK(!callback.is_null());
144*635a8641SAndroid Build Coastguard Worker PassFile();
145*635a8641SAndroid Build Coastguard Worker std::move(callback).Run(error_, file_path_);
146*635a8641SAndroid Build Coastguard Worker }
147*635a8641SAndroid Build Coastguard Worker
148*635a8641SAndroid Build Coastguard Worker private:
149*635a8641SAndroid Build Coastguard Worker FilePath file_path_;
150*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(CreateTemporaryHelper);
151*635a8641SAndroid Build Coastguard Worker };
152*635a8641SAndroid Build Coastguard Worker
153*635a8641SAndroid Build Coastguard Worker class GetInfoHelper : public FileHelper {
154*635a8641SAndroid Build Coastguard Worker public:
GetInfoHelper(FileProxy * proxy,File file)155*635a8641SAndroid Build Coastguard Worker GetInfoHelper(FileProxy* proxy, File file)
156*635a8641SAndroid Build Coastguard Worker : FileHelper(proxy, std::move(file)) {
157*635a8641SAndroid Build Coastguard Worker }
158*635a8641SAndroid Build Coastguard Worker
RunWork()159*635a8641SAndroid Build Coastguard Worker void RunWork() {
160*635a8641SAndroid Build Coastguard Worker if (file_.GetInfo(&file_info_))
161*635a8641SAndroid Build Coastguard Worker error_ = File::FILE_OK;
162*635a8641SAndroid Build Coastguard Worker }
163*635a8641SAndroid Build Coastguard Worker
Reply(FileProxy::GetFileInfoCallback callback)164*635a8641SAndroid Build Coastguard Worker void Reply(FileProxy::GetFileInfoCallback callback) {
165*635a8641SAndroid Build Coastguard Worker PassFile();
166*635a8641SAndroid Build Coastguard Worker DCHECK(!callback.is_null());
167*635a8641SAndroid Build Coastguard Worker std::move(callback).Run(error_, file_info_);
168*635a8641SAndroid Build Coastguard Worker }
169*635a8641SAndroid Build Coastguard Worker
170*635a8641SAndroid Build Coastguard Worker private:
171*635a8641SAndroid Build Coastguard Worker File::Info file_info_;
172*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(GetInfoHelper);
173*635a8641SAndroid Build Coastguard Worker };
174*635a8641SAndroid Build Coastguard Worker
175*635a8641SAndroid Build Coastguard Worker class ReadHelper : public FileHelper {
176*635a8641SAndroid Build Coastguard Worker public:
ReadHelper(FileProxy * proxy,File file,int bytes_to_read)177*635a8641SAndroid Build Coastguard Worker ReadHelper(FileProxy* proxy, File file, int bytes_to_read)
178*635a8641SAndroid Build Coastguard Worker : FileHelper(proxy, std::move(file)),
179*635a8641SAndroid Build Coastguard Worker buffer_(new char[bytes_to_read]),
180*635a8641SAndroid Build Coastguard Worker bytes_to_read_(bytes_to_read),
181*635a8641SAndroid Build Coastguard Worker bytes_read_(0) {
182*635a8641SAndroid Build Coastguard Worker }
183*635a8641SAndroid Build Coastguard Worker
RunWork(int64_t offset)184*635a8641SAndroid Build Coastguard Worker void RunWork(int64_t offset) {
185*635a8641SAndroid Build Coastguard Worker bytes_read_ = file_.Read(offset, buffer_.get(), bytes_to_read_);
186*635a8641SAndroid Build Coastguard Worker error_ = (bytes_read_ < 0) ? File::FILE_ERROR_FAILED : File::FILE_OK;
187*635a8641SAndroid Build Coastguard Worker }
188*635a8641SAndroid Build Coastguard Worker
Reply(FileProxy::ReadCallback callback)189*635a8641SAndroid Build Coastguard Worker void Reply(FileProxy::ReadCallback callback) {
190*635a8641SAndroid Build Coastguard Worker PassFile();
191*635a8641SAndroid Build Coastguard Worker DCHECK(!callback.is_null());
192*635a8641SAndroid Build Coastguard Worker std::move(callback).Run(error_, buffer_.get(), bytes_read_);
193*635a8641SAndroid Build Coastguard Worker }
194*635a8641SAndroid Build Coastguard Worker
195*635a8641SAndroid Build Coastguard Worker private:
196*635a8641SAndroid Build Coastguard Worker std::unique_ptr<char[]> buffer_;
197*635a8641SAndroid Build Coastguard Worker int bytes_to_read_;
198*635a8641SAndroid Build Coastguard Worker int bytes_read_;
199*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(ReadHelper);
200*635a8641SAndroid Build Coastguard Worker };
201*635a8641SAndroid Build Coastguard Worker
202*635a8641SAndroid Build Coastguard Worker class WriteHelper : public FileHelper {
203*635a8641SAndroid Build Coastguard Worker public:
WriteHelper(FileProxy * proxy,File file,const char * buffer,int bytes_to_write)204*635a8641SAndroid Build Coastguard Worker WriteHelper(FileProxy* proxy,
205*635a8641SAndroid Build Coastguard Worker File file,
206*635a8641SAndroid Build Coastguard Worker const char* buffer, int bytes_to_write)
207*635a8641SAndroid Build Coastguard Worker : FileHelper(proxy, std::move(file)),
208*635a8641SAndroid Build Coastguard Worker buffer_(new char[bytes_to_write]),
209*635a8641SAndroid Build Coastguard Worker bytes_to_write_(bytes_to_write),
210*635a8641SAndroid Build Coastguard Worker bytes_written_(0) {
211*635a8641SAndroid Build Coastguard Worker memcpy(buffer_.get(), buffer, bytes_to_write);
212*635a8641SAndroid Build Coastguard Worker }
213*635a8641SAndroid Build Coastguard Worker
RunWork(int64_t offset)214*635a8641SAndroid Build Coastguard Worker void RunWork(int64_t offset) {
215*635a8641SAndroid Build Coastguard Worker bytes_written_ = file_.Write(offset, buffer_.get(), bytes_to_write_);
216*635a8641SAndroid Build Coastguard Worker error_ = (bytes_written_ < 0) ? File::FILE_ERROR_FAILED : File::FILE_OK;
217*635a8641SAndroid Build Coastguard Worker }
218*635a8641SAndroid Build Coastguard Worker
Reply(FileProxy::WriteCallback callback)219*635a8641SAndroid Build Coastguard Worker void Reply(FileProxy::WriteCallback callback) {
220*635a8641SAndroid Build Coastguard Worker PassFile();
221*635a8641SAndroid Build Coastguard Worker if (!callback.is_null())
222*635a8641SAndroid Build Coastguard Worker std::move(callback).Run(error_, bytes_written_);
223*635a8641SAndroid Build Coastguard Worker }
224*635a8641SAndroid Build Coastguard Worker
225*635a8641SAndroid Build Coastguard Worker private:
226*635a8641SAndroid Build Coastguard Worker std::unique_ptr<char[]> buffer_;
227*635a8641SAndroid Build Coastguard Worker int bytes_to_write_;
228*635a8641SAndroid Build Coastguard Worker int bytes_written_;
229*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(WriteHelper);
230*635a8641SAndroid Build Coastguard Worker };
231*635a8641SAndroid Build Coastguard Worker
232*635a8641SAndroid Build Coastguard Worker } // namespace
233*635a8641SAndroid Build Coastguard Worker
FileProxy(TaskRunner * task_runner)234*635a8641SAndroid Build Coastguard Worker FileProxy::FileProxy(TaskRunner* task_runner) : task_runner_(task_runner) {
235*635a8641SAndroid Build Coastguard Worker }
236*635a8641SAndroid Build Coastguard Worker
~FileProxy()237*635a8641SAndroid Build Coastguard Worker FileProxy::~FileProxy() {
238*635a8641SAndroid Build Coastguard Worker if (file_.IsValid())
239*635a8641SAndroid Build Coastguard Worker task_runner_->PostTask(FROM_HERE, BindOnce(&FileDeleter, std::move(file_)));
240*635a8641SAndroid Build Coastguard Worker }
241*635a8641SAndroid Build Coastguard Worker
CreateOrOpen(const FilePath & file_path,uint32_t file_flags,StatusCallback callback)242*635a8641SAndroid Build Coastguard Worker bool FileProxy::CreateOrOpen(const FilePath& file_path,
243*635a8641SAndroid Build Coastguard Worker uint32_t file_flags,
244*635a8641SAndroid Build Coastguard Worker StatusCallback callback) {
245*635a8641SAndroid Build Coastguard Worker DCHECK(!file_.IsValid());
246*635a8641SAndroid Build Coastguard Worker CreateOrOpenHelper* helper = new CreateOrOpenHelper(this, File());
247*635a8641SAndroid Build Coastguard Worker return task_runner_->PostTaskAndReply(
248*635a8641SAndroid Build Coastguard Worker FROM_HERE,
249*635a8641SAndroid Build Coastguard Worker BindOnce(&CreateOrOpenHelper::RunWork, Unretained(helper), file_path,
250*635a8641SAndroid Build Coastguard Worker file_flags),
251*635a8641SAndroid Build Coastguard Worker BindOnce(&CreateOrOpenHelper::Reply, Owned(helper), std::move(callback)));
252*635a8641SAndroid Build Coastguard Worker }
253*635a8641SAndroid Build Coastguard Worker
CreateTemporary(uint32_t additional_file_flags,CreateTemporaryCallback callback)254*635a8641SAndroid Build Coastguard Worker bool FileProxy::CreateTemporary(uint32_t additional_file_flags,
255*635a8641SAndroid Build Coastguard Worker CreateTemporaryCallback callback) {
256*635a8641SAndroid Build Coastguard Worker DCHECK(!file_.IsValid());
257*635a8641SAndroid Build Coastguard Worker CreateTemporaryHelper* helper = new CreateTemporaryHelper(this, File());
258*635a8641SAndroid Build Coastguard Worker return task_runner_->PostTaskAndReply(
259*635a8641SAndroid Build Coastguard Worker FROM_HERE,
260*635a8641SAndroid Build Coastguard Worker BindOnce(&CreateTemporaryHelper::RunWork, Unretained(helper),
261*635a8641SAndroid Build Coastguard Worker additional_file_flags),
262*635a8641SAndroid Build Coastguard Worker BindOnce(&CreateTemporaryHelper::Reply, Owned(helper),
263*635a8641SAndroid Build Coastguard Worker std::move(callback)));
264*635a8641SAndroid Build Coastguard Worker }
265*635a8641SAndroid Build Coastguard Worker
IsValid() const266*635a8641SAndroid Build Coastguard Worker bool FileProxy::IsValid() const {
267*635a8641SAndroid Build Coastguard Worker return file_.IsValid();
268*635a8641SAndroid Build Coastguard Worker }
269*635a8641SAndroid Build Coastguard Worker
SetFile(File file)270*635a8641SAndroid Build Coastguard Worker void FileProxy::SetFile(File file) {
271*635a8641SAndroid Build Coastguard Worker DCHECK(!file_.IsValid());
272*635a8641SAndroid Build Coastguard Worker file_ = std::move(file);
273*635a8641SAndroid Build Coastguard Worker }
274*635a8641SAndroid Build Coastguard Worker
TakeFile()275*635a8641SAndroid Build Coastguard Worker File FileProxy::TakeFile() {
276*635a8641SAndroid Build Coastguard Worker return std::move(file_);
277*635a8641SAndroid Build Coastguard Worker }
278*635a8641SAndroid Build Coastguard Worker
DuplicateFile()279*635a8641SAndroid Build Coastguard Worker File FileProxy::DuplicateFile() {
280*635a8641SAndroid Build Coastguard Worker return file_.Duplicate();
281*635a8641SAndroid Build Coastguard Worker }
282*635a8641SAndroid Build Coastguard Worker
GetPlatformFile() const283*635a8641SAndroid Build Coastguard Worker PlatformFile FileProxy::GetPlatformFile() const {
284*635a8641SAndroid Build Coastguard Worker return file_.GetPlatformFile();
285*635a8641SAndroid Build Coastguard Worker }
286*635a8641SAndroid Build Coastguard Worker
Close(StatusCallback callback)287*635a8641SAndroid Build Coastguard Worker bool FileProxy::Close(StatusCallback callback) {
288*635a8641SAndroid Build Coastguard Worker DCHECK(file_.IsValid());
289*635a8641SAndroid Build Coastguard Worker GenericFileHelper* helper = new GenericFileHelper(this, std::move(file_));
290*635a8641SAndroid Build Coastguard Worker return task_runner_->PostTaskAndReply(
291*635a8641SAndroid Build Coastguard Worker FROM_HERE, BindOnce(&GenericFileHelper::Close, Unretained(helper)),
292*635a8641SAndroid Build Coastguard Worker BindOnce(&GenericFileHelper::Reply, Owned(helper), std::move(callback)));
293*635a8641SAndroid Build Coastguard Worker }
294*635a8641SAndroid Build Coastguard Worker
GetInfo(GetFileInfoCallback callback)295*635a8641SAndroid Build Coastguard Worker bool FileProxy::GetInfo(GetFileInfoCallback callback) {
296*635a8641SAndroid Build Coastguard Worker DCHECK(file_.IsValid());
297*635a8641SAndroid Build Coastguard Worker GetInfoHelper* helper = new GetInfoHelper(this, std::move(file_));
298*635a8641SAndroid Build Coastguard Worker return task_runner_->PostTaskAndReply(
299*635a8641SAndroid Build Coastguard Worker FROM_HERE, BindOnce(&GetInfoHelper::RunWork, Unretained(helper)),
300*635a8641SAndroid Build Coastguard Worker BindOnce(&GetInfoHelper::Reply, Owned(helper), std::move(callback)));
301*635a8641SAndroid Build Coastguard Worker }
302*635a8641SAndroid Build Coastguard Worker
Read(int64_t offset,int bytes_to_read,ReadCallback callback)303*635a8641SAndroid Build Coastguard Worker bool FileProxy::Read(int64_t offset, int bytes_to_read, ReadCallback callback) {
304*635a8641SAndroid Build Coastguard Worker DCHECK(file_.IsValid());
305*635a8641SAndroid Build Coastguard Worker if (bytes_to_read < 0)
306*635a8641SAndroid Build Coastguard Worker return false;
307*635a8641SAndroid Build Coastguard Worker
308*635a8641SAndroid Build Coastguard Worker ReadHelper* helper = new ReadHelper(this, std::move(file_), bytes_to_read);
309*635a8641SAndroid Build Coastguard Worker return task_runner_->PostTaskAndReply(
310*635a8641SAndroid Build Coastguard Worker FROM_HERE, BindOnce(&ReadHelper::RunWork, Unretained(helper), offset),
311*635a8641SAndroid Build Coastguard Worker BindOnce(&ReadHelper::Reply, Owned(helper), std::move(callback)));
312*635a8641SAndroid Build Coastguard Worker }
313*635a8641SAndroid Build Coastguard Worker
Write(int64_t offset,const char * buffer,int bytes_to_write,WriteCallback callback)314*635a8641SAndroid Build Coastguard Worker bool FileProxy::Write(int64_t offset,
315*635a8641SAndroid Build Coastguard Worker const char* buffer,
316*635a8641SAndroid Build Coastguard Worker int bytes_to_write,
317*635a8641SAndroid Build Coastguard Worker WriteCallback callback) {
318*635a8641SAndroid Build Coastguard Worker DCHECK(file_.IsValid());
319*635a8641SAndroid Build Coastguard Worker if (bytes_to_write <= 0 || buffer == nullptr)
320*635a8641SAndroid Build Coastguard Worker return false;
321*635a8641SAndroid Build Coastguard Worker
322*635a8641SAndroid Build Coastguard Worker WriteHelper* helper =
323*635a8641SAndroid Build Coastguard Worker new WriteHelper(this, std::move(file_), buffer, bytes_to_write);
324*635a8641SAndroid Build Coastguard Worker return task_runner_->PostTaskAndReply(
325*635a8641SAndroid Build Coastguard Worker FROM_HERE, BindOnce(&WriteHelper::RunWork, Unretained(helper), offset),
326*635a8641SAndroid Build Coastguard Worker BindOnce(&WriteHelper::Reply, Owned(helper), std::move(callback)));
327*635a8641SAndroid Build Coastguard Worker }
328*635a8641SAndroid Build Coastguard Worker
SetTimes(Time last_access_time,Time last_modified_time,StatusCallback callback)329*635a8641SAndroid Build Coastguard Worker bool FileProxy::SetTimes(Time last_access_time,
330*635a8641SAndroid Build Coastguard Worker Time last_modified_time,
331*635a8641SAndroid Build Coastguard Worker StatusCallback callback) {
332*635a8641SAndroid Build Coastguard Worker DCHECK(file_.IsValid());
333*635a8641SAndroid Build Coastguard Worker GenericFileHelper* helper = new GenericFileHelper(this, std::move(file_));
334*635a8641SAndroid Build Coastguard Worker return task_runner_->PostTaskAndReply(
335*635a8641SAndroid Build Coastguard Worker FROM_HERE,
336*635a8641SAndroid Build Coastguard Worker BindOnce(&GenericFileHelper::SetTimes, Unretained(helper),
337*635a8641SAndroid Build Coastguard Worker last_access_time, last_modified_time),
338*635a8641SAndroid Build Coastguard Worker BindOnce(&GenericFileHelper::Reply, Owned(helper), std::move(callback)));
339*635a8641SAndroid Build Coastguard Worker }
340*635a8641SAndroid Build Coastguard Worker
SetLength(int64_t length,StatusCallback callback)341*635a8641SAndroid Build Coastguard Worker bool FileProxy::SetLength(int64_t length, StatusCallback callback) {
342*635a8641SAndroid Build Coastguard Worker DCHECK(file_.IsValid());
343*635a8641SAndroid Build Coastguard Worker GenericFileHelper* helper = new GenericFileHelper(this, std::move(file_));
344*635a8641SAndroid Build Coastguard Worker return task_runner_->PostTaskAndReply(
345*635a8641SAndroid Build Coastguard Worker FROM_HERE,
346*635a8641SAndroid Build Coastguard Worker BindOnce(&GenericFileHelper::SetLength, Unretained(helper), length),
347*635a8641SAndroid Build Coastguard Worker BindOnce(&GenericFileHelper::Reply, Owned(helper), std::move(callback)));
348*635a8641SAndroid Build Coastguard Worker }
349*635a8641SAndroid Build Coastguard Worker
Flush(StatusCallback callback)350*635a8641SAndroid Build Coastguard Worker bool FileProxy::Flush(StatusCallback callback) {
351*635a8641SAndroid Build Coastguard Worker DCHECK(file_.IsValid());
352*635a8641SAndroid Build Coastguard Worker GenericFileHelper* helper = new GenericFileHelper(this, std::move(file_));
353*635a8641SAndroid Build Coastguard Worker return task_runner_->PostTaskAndReply(
354*635a8641SAndroid Build Coastguard Worker FROM_HERE, BindOnce(&GenericFileHelper::Flush, Unretained(helper)),
355*635a8641SAndroid Build Coastguard Worker BindOnce(&GenericFileHelper::Reply, Owned(helper), std::move(callback)));
356*635a8641SAndroid Build Coastguard Worker }
357*635a8641SAndroid Build Coastguard Worker
358*635a8641SAndroid Build Coastguard Worker } // namespace base
359