Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(251)

Side by Side Diff: base/file_util_proxy.cc

Issue 10095028: Convert MessageLoopProxy to TaskRunner in FileUtilProxy (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/file_util_proxy.h ('k') | base/file_util_proxy_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_util_proxy.h" 5 #include "base/file_util_proxy.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/message_loop_proxy.h" 10 #include "base/task_runner.h"
11 #include "base/task_runner_util.h" 11 #include "base/task_runner_util.h"
12 12
13 namespace base { 13 namespace base {
14 14
15 namespace { 15 namespace {
16 16
17 void CallWithTranslatedParameter(const FileUtilProxy::StatusCallback& callback, 17 void CallWithTranslatedParameter(const FileUtilProxy::StatusCallback& callback,
18 bool value) { 18 bool value) {
19 DCHECK(!callback.is_null()); 19 DCHECK(!callback.is_null());
20 callback.Run(value ? PLATFORM_FILE_OK : PLATFORM_FILE_ERROR_FAILED); 20 callback.Run(value ? PLATFORM_FILE_OK : PLATFORM_FILE_ERROR_FAILED);
21 } 21 }
22 22
23 // Helper classes or routines for individual methods. 23 // Helper classes or routines for individual methods.
24 class CreateOrOpenHelper { 24 class CreateOrOpenHelper {
25 public: 25 public:
26 CreateOrOpenHelper(MessageLoopProxy* message_loop_proxy, 26 CreateOrOpenHelper(TaskRunner* task_runner,
27 const FileUtilProxy::CloseTask& close_task) 27 const FileUtilProxy::CloseTask& close_task)
28 : message_loop_proxy_(message_loop_proxy), 28 : task_runner_(task_runner),
29 close_task_(close_task), 29 close_task_(close_task),
30 file_handle_(kInvalidPlatformFileValue), 30 file_handle_(kInvalidPlatformFileValue),
31 created_(false), 31 created_(false),
32 error_(PLATFORM_FILE_OK) {} 32 error_(PLATFORM_FILE_OK) {}
33 33
34 ~CreateOrOpenHelper() { 34 ~CreateOrOpenHelper() {
35 if (file_handle_ != kInvalidPlatformFileValue) { 35 if (file_handle_ != kInvalidPlatformFileValue) {
36 message_loop_proxy_->PostTask( 36 task_runner_->PostTask(
37 FROM_HERE, 37 FROM_HERE,
38 base::Bind(base::IgnoreResult(close_task_), file_handle_)); 38 base::Bind(base::IgnoreResult(close_task_), file_handle_));
39 } 39 }
40 } 40 }
41 41
42 void RunWork(const FileUtilProxy::CreateOrOpenTask& task) { 42 void RunWork(const FileUtilProxy::CreateOrOpenTask& task) {
43 error_ = task.Run(&file_handle_, &created_); 43 error_ = task.Run(&file_handle_, &created_);
44 } 44 }
45 45
46 void Reply(const FileUtilProxy::CreateOrOpenCallback& callback) { 46 void Reply(const FileUtilProxy::CreateOrOpenCallback& callback) {
47 DCHECK(!callback.is_null()); 47 DCHECK(!callback.is_null());
48 callback.Run(error_, PassPlatformFile(&file_handle_), created_); 48 callback.Run(error_, PassPlatformFile(&file_handle_), created_);
49 } 49 }
50 50
51 private: 51 private:
52 scoped_refptr<MessageLoopProxy> message_loop_proxy_; 52 scoped_refptr<TaskRunner> task_runner_;
53 FileUtilProxy::CloseTask close_task_; 53 FileUtilProxy::CloseTask close_task_;
54 PlatformFile file_handle_; 54 PlatformFile file_handle_;
55 bool created_; 55 bool created_;
56 PlatformFileError error_; 56 PlatformFileError error_;
57 DISALLOW_COPY_AND_ASSIGN(CreateOrOpenHelper); 57 DISALLOW_COPY_AND_ASSIGN(CreateOrOpenHelper);
58 }; 58 };
59 59
60 class CreateTemporaryHelper { 60 class CreateTemporaryHelper {
61 public: 61 public:
62 CreateTemporaryHelper(MessageLoopProxy* message_loop_proxy) 62 CreateTemporaryHelper(TaskRunner* task_runner)
63 : message_loop_proxy_(message_loop_proxy), 63 : task_runner_(task_runner),
64 file_handle_(kInvalidPlatformFileValue), 64 file_handle_(kInvalidPlatformFileValue),
65 error_(PLATFORM_FILE_OK) {} 65 error_(PLATFORM_FILE_OK) {}
66 66
67 ~CreateTemporaryHelper() { 67 ~CreateTemporaryHelper() {
68 if (file_handle_ != kInvalidPlatformFileValue) { 68 if (file_handle_ != kInvalidPlatformFileValue) {
69 FileUtilProxy::Close(message_loop_proxy_, file_handle_, 69 FileUtilProxy::Close(task_runner_, file_handle_,
70 FileUtilProxy::StatusCallback()); 70 FileUtilProxy::StatusCallback());
71 } 71 }
72 } 72 }
73 73
74 void RunWork(int additional_file_flags) { 74 void RunWork(int additional_file_flags) {
75 // TODO(darin): file_util should have a variant of CreateTemporaryFile 75 // TODO(darin): file_util should have a variant of CreateTemporaryFile
76 // that returns a FilePath and a PlatformFile. 76 // that returns a FilePath and a PlatformFile.
77 file_util::CreateTemporaryFile(&file_path_); 77 file_util::CreateTemporaryFile(&file_path_);
78 78
79 int file_flags = 79 int file_flags =
80 PLATFORM_FILE_WRITE | 80 PLATFORM_FILE_WRITE |
81 PLATFORM_FILE_TEMPORARY | 81 PLATFORM_FILE_TEMPORARY |
82 PLATFORM_FILE_CREATE_ALWAYS | 82 PLATFORM_FILE_CREATE_ALWAYS |
83 additional_file_flags; 83 additional_file_flags;
84 84
85 error_ = PLATFORM_FILE_OK; 85 error_ = PLATFORM_FILE_OK;
86 file_handle_ = CreatePlatformFile(file_path_, file_flags, NULL, &error_); 86 file_handle_ = CreatePlatformFile(file_path_, file_flags, NULL, &error_);
87 } 87 }
88 88
89 void Reply(const FileUtilProxy::CreateTemporaryCallback& callback) { 89 void Reply(const FileUtilProxy::CreateTemporaryCallback& callback) {
90 DCHECK(!callback.is_null()); 90 DCHECK(!callback.is_null());
91 callback.Run(error_, PassPlatformFile(&file_handle_), file_path_); 91 callback.Run(error_, PassPlatformFile(&file_handle_), file_path_);
92 } 92 }
93 93
94 private: 94 private:
95 scoped_refptr<MessageLoopProxy> message_loop_proxy_; 95 scoped_refptr<TaskRunner> task_runner_;
96 PlatformFile file_handle_; 96 PlatformFile file_handle_;
97 FilePath file_path_; 97 FilePath file_path_;
98 PlatformFileError error_; 98 PlatformFileError error_;
99 DISALLOW_COPY_AND_ASSIGN(CreateTemporaryHelper); 99 DISALLOW_COPY_AND_ASSIGN(CreateTemporaryHelper);
100 }; 100 };
101 101
102 class GetFileInfoHelper { 102 class GetFileInfoHelper {
103 public: 103 public:
104 GetFileInfoHelper() 104 GetFileInfoHelper()
105 : error_(PLATFORM_FILE_OK) {} 105 : error_(PLATFORM_FILE_OK) {}
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 } 217 }
218 return PLATFORM_FILE_ERROR_FAILED; 218 return PLATFORM_FILE_ERROR_FAILED;
219 } 219 }
220 return PLATFORM_FILE_OK; 220 return PLATFORM_FILE_OK;
221 } 221 }
222 222
223 } // namespace 223 } // namespace
224 224
225 // static 225 // static
226 bool FileUtilProxy::CreateOrOpen( 226 bool FileUtilProxy::CreateOrOpen(
227 scoped_refptr<MessageLoopProxy> message_loop_proxy, 227 TaskRunner* task_runner,
228 const FilePath& file_path, int file_flags, 228 const FilePath& file_path, int file_flags,
229 const CreateOrOpenCallback& callback) { 229 const CreateOrOpenCallback& callback) {
230 return RelayCreateOrOpen( 230 return RelayCreateOrOpen(
231 message_loop_proxy, 231 task_runner,
232 base::Bind(&CreateOrOpenAdapter, file_path, file_flags), 232 base::Bind(&CreateOrOpenAdapter, file_path, file_flags),
233 base::Bind(&CloseAdapter), 233 base::Bind(&CloseAdapter),
234 callback); 234 callback);
235 } 235 }
236 236
237 // static 237 // static
238 bool FileUtilProxy::CreateTemporary( 238 bool FileUtilProxy::CreateTemporary(
239 scoped_refptr<MessageLoopProxy> message_loop_proxy, 239 TaskRunner* task_runner,
240 int additional_file_flags, 240 int additional_file_flags,
241 const CreateTemporaryCallback& callback) { 241 const CreateTemporaryCallback& callback) {
242 CreateTemporaryHelper* helper = new CreateTemporaryHelper(message_loop_proxy); 242 CreateTemporaryHelper* helper = new CreateTemporaryHelper(task_runner);
243 return message_loop_proxy->PostTaskAndReply( 243 return task_runner->PostTaskAndReply(
244 FROM_HERE, 244 FROM_HERE,
245 Bind(&CreateTemporaryHelper::RunWork, Unretained(helper), 245 Bind(&CreateTemporaryHelper::RunWork, Unretained(helper),
246 additional_file_flags), 246 additional_file_flags),
247 Bind(&CreateTemporaryHelper::Reply, Owned(helper), callback)); 247 Bind(&CreateTemporaryHelper::Reply, Owned(helper), callback));
248 } 248 }
249 249
250 // static 250 // static
251 bool FileUtilProxy::Close( 251 bool FileUtilProxy::Close(
252 scoped_refptr<MessageLoopProxy> message_loop_proxy, 252 TaskRunner* task_runner,
253 base::PlatformFile file_handle, 253 base::PlatformFile file_handle,
254 const StatusCallback& callback) { 254 const StatusCallback& callback) {
255 return RelayClose( 255 return RelayClose(
256 message_loop_proxy, 256 task_runner,
257 base::Bind(&CloseAdapter), 257 base::Bind(&CloseAdapter),
258 file_handle, callback); 258 file_handle, callback);
259 } 259 }
260 260
261 // Retrieves the information about a file. It is invalid to pass NULL for the 261 // Retrieves the information about a file. It is invalid to pass NULL for the
262 // callback. 262 // callback.
263 bool FileUtilProxy::GetFileInfo( 263 bool FileUtilProxy::GetFileInfo(
264 scoped_refptr<MessageLoopProxy> message_loop_proxy, 264 TaskRunner* task_runner,
265 const FilePath& file_path, 265 const FilePath& file_path,
266 const GetFileInfoCallback& callback) { 266 const GetFileInfoCallback& callback) {
267 GetFileInfoHelper* helper = new GetFileInfoHelper; 267 GetFileInfoHelper* helper = new GetFileInfoHelper;
268 return message_loop_proxy->PostTaskAndReply( 268 return task_runner->PostTaskAndReply(
269 FROM_HERE, 269 FROM_HERE,
270 Bind(&GetFileInfoHelper::RunWorkForFilePath, 270 Bind(&GetFileInfoHelper::RunWorkForFilePath,
271 Unretained(helper), file_path), 271 Unretained(helper), file_path),
272 Bind(&GetFileInfoHelper::Reply, Owned(helper), callback)); 272 Bind(&GetFileInfoHelper::Reply, Owned(helper), callback));
273 } 273 }
274 274
275 // static 275 // static
276 bool FileUtilProxy::GetFileInfoFromPlatformFile( 276 bool FileUtilProxy::GetFileInfoFromPlatformFile(
277 scoped_refptr<MessageLoopProxy> message_loop_proxy, 277 TaskRunner* task_runner,
278 PlatformFile file, 278 PlatformFile file,
279 const GetFileInfoCallback& callback) { 279 const GetFileInfoCallback& callback) {
280 GetFileInfoHelper* helper = new GetFileInfoHelper; 280 GetFileInfoHelper* helper = new GetFileInfoHelper;
281 return message_loop_proxy->PostTaskAndReply( 281 return task_runner->PostTaskAndReply(
282 FROM_HERE, 282 FROM_HERE,
283 Bind(&GetFileInfoHelper::RunWorkForPlatformFile, 283 Bind(&GetFileInfoHelper::RunWorkForPlatformFile,
284 Unretained(helper), file), 284 Unretained(helper), file),
285 Bind(&GetFileInfoHelper::Reply, Owned(helper), callback)); 285 Bind(&GetFileInfoHelper::Reply, Owned(helper), callback));
286 } 286 }
287 287
288 // static 288 // static
289 bool FileUtilProxy::Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy, 289 bool FileUtilProxy::Delete(TaskRunner* task_runner,
290 const FilePath& file_path, 290 const FilePath& file_path,
291 bool recursive, 291 bool recursive,
292 const StatusCallback& callback) { 292 const StatusCallback& callback) {
293 return RelayFileTask( 293 return RelayFileTask(
294 message_loop_proxy, FROM_HERE, 294 task_runner, FROM_HERE,
295 Bind(&DeleteAdapter, file_path, recursive), 295 Bind(&DeleteAdapter, file_path, recursive),
296 callback); 296 callback);
297 } 297 }
298 298
299 // static 299 // static
300 bool FileUtilProxy::RecursiveDelete( 300 bool FileUtilProxy::RecursiveDelete(
301 scoped_refptr<MessageLoopProxy> message_loop_proxy, 301 TaskRunner* task_runner,
302 const FilePath& file_path, 302 const FilePath& file_path,
303 const StatusCallback& callback) { 303 const StatusCallback& callback) {
304 return RelayFileTask( 304 return RelayFileTask(
305 message_loop_proxy, FROM_HERE, 305 task_runner, FROM_HERE,
306 Bind(&DeleteAdapter, file_path, true /* recursive */), 306 Bind(&DeleteAdapter, file_path, true /* recursive */),
307 callback); 307 callback);
308 } 308 }
309 309
310 // static 310 // static
311 bool FileUtilProxy::Read( 311 bool FileUtilProxy::Read(
312 scoped_refptr<MessageLoopProxy> message_loop_proxy, 312 TaskRunner* task_runner,
313 PlatformFile file, 313 PlatformFile file,
314 int64 offset, 314 int64 offset,
315 int bytes_to_read, 315 int bytes_to_read,
316 const ReadCallback& callback) { 316 const ReadCallback& callback) {
317 if (bytes_to_read < 0) { 317 if (bytes_to_read < 0) {
318 return false; 318 return false;
319 } 319 }
320 ReadHelper* helper = new ReadHelper(bytes_to_read); 320 ReadHelper* helper = new ReadHelper(bytes_to_read);
321 return message_loop_proxy->PostTaskAndReply( 321 return task_runner->PostTaskAndReply(
322 FROM_HERE, 322 FROM_HERE,
323 Bind(&ReadHelper::RunWork, Unretained(helper), file, offset), 323 Bind(&ReadHelper::RunWork, Unretained(helper), file, offset),
324 Bind(&ReadHelper::Reply, Owned(helper), callback)); 324 Bind(&ReadHelper::Reply, Owned(helper), callback));
325 } 325 }
326 326
327 // static 327 // static
328 bool FileUtilProxy::Write( 328 bool FileUtilProxy::Write(
329 scoped_refptr<MessageLoopProxy> message_loop_proxy, 329 TaskRunner* task_runner,
330 PlatformFile file, 330 PlatformFile file,
331 int64 offset, 331 int64 offset,
332 const char* buffer, 332 const char* buffer,
333 int bytes_to_write, 333 int bytes_to_write,
334 const WriteCallback& callback) { 334 const WriteCallback& callback) {
335 if (bytes_to_write <= 0 || buffer == NULL) { 335 if (bytes_to_write <= 0 || buffer == NULL) {
336 return false; 336 return false;
337 } 337 }
338 WriteHelper* helper = new WriteHelper(buffer, bytes_to_write); 338 WriteHelper* helper = new WriteHelper(buffer, bytes_to_write);
339 return message_loop_proxy->PostTaskAndReply( 339 return task_runner->PostTaskAndReply(
340 FROM_HERE, 340 FROM_HERE,
341 Bind(&WriteHelper::RunWork, Unretained(helper), file, offset), 341 Bind(&WriteHelper::RunWork, Unretained(helper), file, offset),
342 Bind(&WriteHelper::Reply, Owned(helper), callback)); 342 Bind(&WriteHelper::Reply, Owned(helper), callback));
343 } 343 }
344 344
345 // static 345 // static
346 bool FileUtilProxy::Touch( 346 bool FileUtilProxy::Touch(
347 scoped_refptr<MessageLoopProxy> message_loop_proxy, 347 TaskRunner* task_runner,
348 PlatformFile file, 348 PlatformFile file,
349 const Time& last_access_time, 349 const Time& last_access_time,
350 const Time& last_modified_time, 350 const Time& last_modified_time,
351 const StatusCallback& callback) { 351 const StatusCallback& callback) {
352 return base::PostTaskAndReplyWithResult( 352 return base::PostTaskAndReplyWithResult(
353 message_loop_proxy, 353 task_runner,
354 FROM_HERE, 354 FROM_HERE,
355 Bind(&TouchPlatformFile, file, 355 Bind(&TouchPlatformFile, file,
356 last_access_time, last_modified_time), 356 last_access_time, last_modified_time),
357 Bind(&CallWithTranslatedParameter, callback)); 357 Bind(&CallWithTranslatedParameter, callback));
358 } 358 }
359 359
360 // static 360 // static
361 bool FileUtilProxy::Touch( 361 bool FileUtilProxy::Touch(
362 scoped_refptr<MessageLoopProxy> message_loop_proxy, 362 TaskRunner* task_runner,
363 const FilePath& file_path, 363 const FilePath& file_path,
364 const Time& last_access_time, 364 const Time& last_access_time,
365 const Time& last_modified_time, 365 const Time& last_modified_time,
366 const StatusCallback& callback) { 366 const StatusCallback& callback) {
367 return base::PostTaskAndReplyWithResult( 367 return base::PostTaskAndReplyWithResult(
368 message_loop_proxy, 368 task_runner,
369 FROM_HERE, 369 FROM_HERE,
370 Bind(&file_util::TouchFile, file_path, 370 Bind(&file_util::TouchFile, file_path,
371 last_access_time, last_modified_time), 371 last_access_time, last_modified_time),
372 Bind(&CallWithTranslatedParameter, callback)); 372 Bind(&CallWithTranslatedParameter, callback));
373 } 373 }
374 374
375 // static 375 // static
376 bool FileUtilProxy::Truncate( 376 bool FileUtilProxy::Truncate(
377 scoped_refptr<MessageLoopProxy> message_loop_proxy, 377 TaskRunner* task_runner,
378 PlatformFile file, 378 PlatformFile file,
379 int64 length, 379 int64 length,
380 const StatusCallback& callback) { 380 const StatusCallback& callback) {
381 return base::PostTaskAndReplyWithResult( 381 return base::PostTaskAndReplyWithResult(
382 message_loop_proxy, 382 task_runner,
383 FROM_HERE, 383 FROM_HERE,
384 Bind(&TruncatePlatformFile, file, length), 384 Bind(&TruncatePlatformFile, file, length),
385 Bind(&CallWithTranslatedParameter, callback)); 385 Bind(&CallWithTranslatedParameter, callback));
386 } 386 }
387 387
388 // static 388 // static
389 bool FileUtilProxy::Flush( 389 bool FileUtilProxy::Flush(
390 scoped_refptr<MessageLoopProxy> message_loop_proxy, 390 TaskRunner* task_runner,
391 PlatformFile file, 391 PlatformFile file,
392 const StatusCallback& callback) { 392 const StatusCallback& callback) {
393 return base::PostTaskAndReplyWithResult( 393 return base::PostTaskAndReplyWithResult(
394 message_loop_proxy, 394 task_runner,
395 FROM_HERE, 395 FROM_HERE,
396 Bind(&FlushPlatformFile, file), 396 Bind(&FlushPlatformFile, file),
397 Bind(&CallWithTranslatedParameter, callback)); 397 Bind(&CallWithTranslatedParameter, callback));
398 } 398 }
399 399
400 // static 400 // static
401 bool FileUtilProxy::RelayFileTask( 401 bool FileUtilProxy::RelayFileTask(
402 scoped_refptr<MessageLoopProxy> message_loop_proxy, 402 TaskRunner* task_runner,
403 const tracked_objects::Location& from_here, 403 const tracked_objects::Location& from_here,
404 const FileTask& file_task, 404 const FileTask& file_task,
405 const StatusCallback& callback) { 405 const StatusCallback& callback) {
406 return base::PostTaskAndReplyWithResult( 406 return base::PostTaskAndReplyWithResult(
407 message_loop_proxy, from_here, file_task, callback); 407 task_runner, from_here, file_task, callback);
408 } 408 }
409 409
410 // static 410 // static
411 bool FileUtilProxy::RelayCreateOrOpen( 411 bool FileUtilProxy::RelayCreateOrOpen(
412 scoped_refptr<MessageLoopProxy> message_loop_proxy, 412 TaskRunner* task_runner,
413 const CreateOrOpenTask& open_task, 413 const CreateOrOpenTask& open_task,
414 const CloseTask& close_task, 414 const CloseTask& close_task,
415 const CreateOrOpenCallback& callback) { 415 const CreateOrOpenCallback& callback) {
416 CreateOrOpenHelper* helper = new CreateOrOpenHelper( 416 CreateOrOpenHelper* helper = new CreateOrOpenHelper(
417 message_loop_proxy, close_task); 417 task_runner, close_task);
418 return message_loop_proxy->PostTaskAndReply( 418 return task_runner->PostTaskAndReply(
419 FROM_HERE, 419 FROM_HERE,
420 Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), open_task), 420 Bind(&CreateOrOpenHelper::RunWork, Unretained(helper), open_task),
421 Bind(&CreateOrOpenHelper::Reply, Owned(helper), callback)); 421 Bind(&CreateOrOpenHelper::Reply, Owned(helper), callback));
422 } 422 }
423 423
424 // static 424 // static
425 bool FileUtilProxy::RelayClose( 425 bool FileUtilProxy::RelayClose(
426 scoped_refptr<MessageLoopProxy> message_loop_proxy, 426 TaskRunner* task_runner,
427 const CloseTask& close_task, 427 const CloseTask& close_task,
428 PlatformFile file_handle, 428 PlatformFile file_handle,
429 const StatusCallback& callback) { 429 const StatusCallback& callback) {
430 return base::PostTaskAndReplyWithResult( 430 return base::PostTaskAndReplyWithResult(
431 message_loop_proxy, FROM_HERE, Bind(close_task, file_handle), callback); 431 task_runner, FROM_HERE, Bind(close_task, file_handle), callback);
432 } 432 }
433 433
434 } // namespace base 434 } // namespace base
OLDNEW
« no previous file with comments | « base/file_util_proxy.h ('k') | base/file_util_proxy_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698