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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
// Copyright (c) 2011 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 "base/file_util_proxy.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/file_util.h"
#include "base/message_loop_proxy.h"
namespace base {
namespace {
// Helper templates to call file_util or base::PlatformFile methods
// and reply with the returned value.
//
// Typically when you have these methods:
// R DoWorkAndReturn();
// void Callback(R& result);
//
// You can pass the result of DoWorkAndReturn to the Callback by:
//
// R* result = new R;
// message_loop_proxy->PostTaskAndReply(
// from_here,
// ReturnAsParam<R>(Bind(&DoWorkAndReturn), result),
// RelayHelper(Bind(&Callback), Owned(result)));
//
// Or just use PostTaskAndReplyWithStatus helper template (see the code below).
template <typename R1, typename R2>
struct ReturnValueTranslator {
static R2 Value(const R1& value);
};
template <typename R>
struct ReturnValueTranslator<R, R> {
static R Value(const R& value) { return value; }
};
template <>
struct ReturnValueTranslator<bool, PlatformFileError> {
static PlatformFileError Value(const bool& value) {
if (value)
return PLATFORM_FILE_OK;
return PLATFORM_FILE_ERROR_FAILED;
}
};
template <typename R1, typename R2>
void ReturnAsParamAdapter(const Callback<R1(void)>& func, R2* result) {
if (!func.is_null())
*result = ReturnValueTranslator<R1, R2>::Value(func.Run());
}
template <typename R1, typename R2>
Closure ReturnAsParam(const Callback<R1(void)>& func, R2* result) {
DCHECK(result);
return Bind(&ReturnAsParamAdapter<R1, R2>, func, result);
}
template <typename R, typename A1>
void ReturnAsParamAdapter1(const Callback<R(A1)>& func, A1 a1, R* result) {
if (!func.is_null())
*result = func.Run(a1);
}
template <typename R, typename A1>
Closure ReturnAsParam(const Callback<R(A1)>& func, A1 a1, R* result) {
DCHECK(result);
return Bind(&ReturnAsParamAdapter1<R, A1>, func, a1, result);
}
template <typename R>
void ReplyAdapter(const Callback<void(R)>& callback, R* result) {
DCHECK(result);
if (!callback.is_null())
callback.Run(*result);
}
template <typename R, typename OWNED>
Closure ReplyHelper(const Callback<void(R)>& callback, OWNED result) {
return Bind(&ReplyAdapter<R>, callback, result);
}
// Putting everything together.
template <typename R1, typename R2>
bool PostTaskAndReplyWithStatus(
const scoped_refptr<MessageLoopProxy>& message_loop_proxy,
const tracked_objects::Location& from_here,
const Callback<R1(void)>& file_util_work,
const Callback<void(R2)>& callback,
R2* result) {
return message_loop_proxy->PostTaskAndReply(
from_here,
ReturnAsParam<R1>(file_util_work, result),
ReplyHelper(callback, Owned(result)));
}
// Helper classes or routines for individual methods.
class CreateOrOpenHelper {
public:
CreateOrOpenHelper(MessageLoopProxy* message_loop_proxy,
const FileUtilProxy::CloseTask& close_task)
: message_loop_proxy_(message_loop_proxy),
close_task_(close_task),
file_handle_(kInvalidPlatformFileValue),
created_(false),
error_(PLATFORM_FILE_OK) {}
~CreateOrOpenHelper() {
if (file_handle_ != kInvalidPlatformFileValue) {
message_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(base::IgnoreResult(close_task_), file_handle_));
}
}
void RunWork(const FileUtilProxy::CreateOrOpenTask& task) {
error_ = task.Run(&file_handle_, &created_);
}
void Reply(const FileUtilProxy::CreateOrOpenCallback& callback) {
DCHECK(!callback.is_null());
callback.Run(error_, PassPlatformFile(&file_handle_), created_);
}
private:
scoped_refptr<MessageLoopProxy> message_loop_proxy_;
FileUtilProxy::CloseTask close_task_;
PlatformFile file_handle_;
bool created_;
PlatformFileError error_;
DISALLOW_COPY_AND_ASSIGN(CreateOrOpenHelper);
};
class CreateTemporaryHelper {
public:
CreateTemporaryHelper(MessageLoopProxy* message_loop_proxy)
: message_loop_proxy_(message_loop_proxy),
file_handle_(kInvalidPlatformFileValue),
error_(PLATFORM_FILE_OK) {}
~CreateTemporaryHelper() {
if (file_handle_ != kInvalidPlatformFileValue) {
FileUtilProxy::Close(message_loop_proxy_, file_handle_,
FileUtilProxy::StatusCallback());
}
}
void RunWork(int additional_file_flags) {
// TODO(darin): file_util should have a variant of CreateTemporaryFile
// that returns a FilePath and a PlatformFile.
file_util::CreateTemporaryFile(&file_path_);
int file_flags =
PLATFORM_FILE_WRITE |
PLATFORM_FILE_TEMPORARY |
PLATFORM_FILE_CREATE_ALWAYS |
additional_file_flags;
error_ = PLATFORM_FILE_OK;
file_handle_ = CreatePlatformFile(file_path_, file_flags, NULL, &error_);
}
void Reply(const FileUtilProxy::CreateTemporaryCallback& callback) {
DCHECK(!callback.is_null());
callback.Run(error_, PassPlatformFile(&file_handle_), file_path_);
}
private:
scoped_refptr<MessageLoopProxy> message_loop_proxy_;
PlatformFile file_handle_;
FilePath file_path_;
PlatformFileError error_;
DISALLOW_COPY_AND_ASSIGN(CreateTemporaryHelper);
};
class GetFileInfoHelper {
public:
GetFileInfoHelper()
: error_(PLATFORM_FILE_OK) {}
void RunWorkForFilePath(const FilePath& file_path) {
if (!file_util::PathExists(file_path)) {
error_ = PLATFORM_FILE_ERROR_NOT_FOUND;
return;
}
if (!file_util::GetFileInfo(file_path, &file_info_))
error_ = PLATFORM_FILE_ERROR_FAILED;
}
void RunWorkForPlatformFile(PlatformFile file) {
if (!GetPlatformFileInfo(file, &file_info_))
error_ = PLATFORM_FILE_ERROR_FAILED;
}
void Reply(const FileUtilProxy::GetFileInfoCallback& callback) {
if (!callback.is_null()) {
callback.Run(error_, file_info_);
}
}
private:
PlatformFileError error_;
PlatformFileInfo file_info_;
DISALLOW_COPY_AND_ASSIGN(GetFileInfoHelper);
};
class ReadHelper {
public:
ReadHelper(int bytes_to_read)
: buffer_(new char[bytes_to_read]),
bytes_to_read_(bytes_to_read),
bytes_read_(0) {}
void RunWork(PlatformFile file, int64 offset) {
bytes_read_ = ReadPlatformFile(file, offset, buffer_.get(), bytes_to_read_);
}
void Reply(const FileUtilProxy::ReadCallback& callback) {
if (!callback.is_null()) {
PlatformFileError error =
(bytes_read_ < 0) ? PLATFORM_FILE_ERROR_FAILED : PLATFORM_FILE_OK;
callback.Run(error, buffer_.get(), bytes_read_);
}
}
private:
scoped_array<char> buffer_;
int bytes_to_read_;
int bytes_read_;
DISALLOW_COPY_AND_ASSIGN(ReadHelper);
};
class WriteHelper {
public:
WriteHelper(const char* buffer, int bytes_to_write)
: buffer_(new char[bytes_to_write]),
bytes_to_write_(bytes_to_write),
bytes_written_(0) {
memcpy(buffer_.get(), buffer, bytes_to_write);
}
void RunWork(PlatformFile file, int64 offset) {
bytes_written_ = WritePlatformFile(file, offset, buffer_.get(),
bytes_to_write_);
}
void Reply(const FileUtilProxy::WriteCallback& callback) {
if (!callback.is_null()) {
PlatformFileError error =
(bytes_written_ < 0) ? PLATFORM_FILE_ERROR_FAILED : PLATFORM_FILE_OK;
callback.Run(error, bytes_written_);
}
}
private:
scoped_array<char> buffer_;
int bytes_to_write_;
int bytes_written_;
DISALLOW_COPY_AND_ASSIGN(WriteHelper);
};
PlatformFileError CreateOrOpenAdapter(
const FilePath& file_path, int file_flags,
PlatformFile* file_handle, bool* created) {
DCHECK(file_handle);
DCHECK(created);
if (!file_util::DirectoryExists(file_path.DirName())) {
// If its parent does not exist, should return NOT_FOUND error.
return PLATFORM_FILE_ERROR_NOT_FOUND;
}
PlatformFileError error = PLATFORM_FILE_OK;
*file_handle = CreatePlatformFile(file_path, file_flags, created, &error);
return error;
}
PlatformFileError CloseAdapter(PlatformFile file_handle) {
if (!ClosePlatformFile(file_handle)) {
return PLATFORM_FILE_ERROR_FAILED;
}
return PLATFORM_FILE_OK;
}
PlatformFileError DeleteAdapter(const FilePath& file_path, bool recursive) {
if (!file_util::PathExists(file_path)) {
return PLATFORM_FILE_ERROR_NOT_FOUND;
}
if (!file_util::Delete(file_path, recursive)) {
if (!recursive && !file_util::IsDirectoryEmpty(file_path)) {
return PLATFORM_FILE_ERROR_NOT_EMPTY;
}
return PLATFORM_FILE_ERROR_FAILED;
}
return PLATFORM_FILE_OK;
}
} // namespace
// static
bool FileUtilProxy::CreateOrOpen(
scoped_refptr<MessageLoopProxy> message_loop_proxy,
const FilePath& file_path, int file_flags,
const CreateOrOpenCallback& callback) {
return RelayCreateOrOpen(
message_loop_proxy,
base::Bind(&CreateOrOpenAdapter, file_path, file_flags),
base::Bind(&CloseAdapter),
callback);
}
// static
bool FileUtilProxy::CreateTemporary(
scoped_refptr<MessageLoopProxy> message_loop_proxy,
int additional_file_flags,
const CreateTemporaryCallback& callback) {
CreateTemporaryHelper* helper = new CreateTemporaryHelper(message_loop_proxy);
return message_loop_proxy->PostTaskAndReply(
FROM_HERE,
Bind(&CreateTemporaryHelper::RunWork, Unretained(helper),
additional_file_flags),
Bind(&CreateTemporaryHelper::Reply, Owned(helper), callback));
}
// static
bool FileUtilProxy::Close(
scoped_refptr<MessageLoopProxy> message_loop_proxy,
base::PlatformFile file_handle,
const StatusCallback& callback) {
return RelayClose(
message_loop_proxy,
base::Bind(&CloseAdapter),
file_handle, callback);
}
// Retrieves the information about a file. It is invalid to pass NULL for the
// callback.
bool FileUtilProxy::GetFileInfo(
scoped_refptr<MessageLoopProxy> message_loop_proxy,
const FilePath& file_path,
const GetFileInfoCallback& callback) {
GetFileInfoHelper* helper = new GetFileInfoHelper;
return message_loop_proxy->PostTaskAndReply(
FROM_HERE,
Bind(&GetFileInfoHelper::RunWorkForFilePath,
Unretained(helper), file_path),
Bind(&GetFileInfoHelper::Reply, Owned(helper), callback));
}
// static
bool FileUtilProxy::GetFileInfoFromPlatformFile(
scoped_refptr<MessageLoopProxy> message_loop_proxy,
PlatformFile file,
const GetFileInfoCallback& callback) {
GetFileInfoHelper* helper = new GetFileInfoHelper;
return message_loop_proxy->PostTaskAndReply(
FROM_HERE,
Bind(&GetFileInfoHelper::RunWorkForPlatformFile,
Unretained(helper), file),
Bind(&GetFileInfoHelper::Reply, Owned(helper), callback));
}
// static
bool FileUtilProxy::Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy,
const FilePath& file_path,
bool recursive,
const StatusCallback& callback) {
return RelayFileTask(
message_loop_proxy, FROM_HERE,
Bind(&DeleteAdapter, file_path, recursive),
callback);
}
// static
bool FileUtilProxy::RecursiveDelete(
scoped_refptr<MessageLoopProxy> message_loop_proxy,
const FilePath& file_path,
const StatusCallback& callback) {
return RelayFileTask(
message_loop_proxy, FROM_HERE,
Bind(&DeleteAdapter, file_path, true /* recursive */),
callback);
}
// static
bool FileUtilProxy::Read(
scoped_refptr<MessageLoopProxy> message_loop_proxy,
PlatformFile file,
int64 offset,
int bytes_to_read,
const ReadCallback& callback) {
if (bytes_to_read < 0) {
return false;
}
ReadHelper* helper = new ReadHelper(bytes_to_read);
return message_loop_proxy->PostTaskAndReply(
FROM_HERE,
Bind(&ReadHelper::RunWork, Unretained(helper), file, offset),
Bind(&ReadHelper::Reply, Owned(helper), callback));
}
// static
bool FileUtilProxy::Write(
scoped_refptr<MessageLoopProxy> message_loop_proxy,
PlatformFile file,
int64 offset,
const char* buffer,
int bytes_to_write,
const WriteCallback& callback) {
if (bytes_to_write <= 0 || buffer == NULL) {
return false;
}
WriteHelper* helper = new WriteHelper(buffer, bytes_to_write);
return message_loop_proxy->PostTaskAndReply(
FROM_HERE,
Bind(&WriteHelper::RunWork, Unretained(helper), file, offset),
Bind(&WriteHelper::Reply, Owned(helper), callback));
}
// static
bool FileUtilProxy::Touch(
scoped_refptr<MessageLoopProxy> message_loop_proxy,
PlatformFile file,
const Time& last_access_time,
const Time& last_modified_time,
const StatusCallback& callback) {
return PostTaskAndReplyWithStatus<bool>(
message_loop_proxy, FROM_HERE,
Bind(&TouchPlatformFile, file,
last_access_time, last_modified_time), callback,
new PlatformFileError);
}
// static
bool FileUtilProxy::Touch(
scoped_refptr<MessageLoopProxy> message_loop_proxy,
const FilePath& file_path,
const Time& last_access_time,
const Time& last_modified_time,
const StatusCallback& callback) {
return PostTaskAndReplyWithStatus<bool>(
message_loop_proxy, FROM_HERE,
Bind(&file_util::TouchFile, file_path,
last_access_time, last_modified_time),
callback,
new PlatformFileError);
}
// static
bool FileUtilProxy::Truncate(
scoped_refptr<MessageLoopProxy> message_loop_proxy,
PlatformFile file,
int64 length,
const StatusCallback& callback) {
return PostTaskAndReplyWithStatus<bool>(
message_loop_proxy, FROM_HERE,
Bind(&TruncatePlatformFile, file, length), callback,
new PlatformFileError);
}
// static
bool FileUtilProxy::Flush(
scoped_refptr<MessageLoopProxy> message_loop_proxy,
PlatformFile file,
const StatusCallback& callback) {
return PostTaskAndReplyWithStatus<bool>(
message_loop_proxy, FROM_HERE,
Bind(&FlushPlatformFile, file), callback,
new PlatformFileError);
}
// static
bool FileUtilProxy::RelayFileTask(
scoped_refptr<MessageLoopProxy> message_loop_proxy,
const tracked_objects::Location& from_here,
const FileTask& file_task,
const StatusCallback& callback) {
PlatformFileError* result = new PlatformFileError;
return message_loop_proxy->PostTaskAndReply(
from_here,
ReturnAsParam(file_task, result),
ReplyHelper(callback, Owned(result)));
}
// static
bool FileUtilProxy::RelayCreateOrOpen(
scoped_refptr<MessageLoopProxy> message_loop_proxy,
const CreateOrOpenTask& open_task,
const CloseTask& close_task,
const CreateOrOpenCallback& callback) {
CreateOrOpenHelper* helper = new CreateOrOpenHelper(
message_loop_proxy, close_task);
return message_loop_proxy->PostTaskAndReply(
FROM_HERE,
Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), open_task),
Bind(&CreateOrOpenHelper::Reply, Owned(helper), callback));
}
// static
bool FileUtilProxy::RelayClose(
scoped_refptr<MessageLoopProxy> message_loop_proxy,
const CloseTask& close_task,
PlatformFile file_handle,
const StatusCallback& callback) {
PlatformFileError* result = new PlatformFileError;
return message_loop_proxy->PostTaskAndReply(
FROM_HERE,
ReturnAsParam(close_task, file_handle, result),
ReplyHelper(callback, Owned(result)));
}
} // namespace base
|