| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2009 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. | |
| 4 // | |
| 5 // FileSystemAccessor provides functions so consumers can do file access | |
| 6 // asynchronously. It would PostTask to FILE thread to access file info, then on | |
| 7 // completion PostTask back to the caller thread and pass the result back. | |
| 8 // Here is an example on how to use it to get file size: | |
| 9 // 1. Define a callback function so FileSystemAccessor could run it after the | |
| 10 // task has been completed: | |
| 11 // void Foo::GetFileSizeCallback(int64 result, void* param) { | |
| 12 // } | |
| 13 // 2. Call static function FileSystemAccessor::RequestFileSize, provide file | |
| 14 // path, param (any object you want to pass back to the callback function) | |
| 15 // and callback: | |
| 16 // FileSystemAccessor::RequestFileSize( | |
| 17 // path, | |
| 18 // param, | |
| 19 // NewCallback(this, &Foo::GetFileSizeCallback)); | |
| 20 // 3. FileSystemAceessor would PostTask to FILE thread to get file size, then | |
| 21 // on completion it would PostTask back to the current thread and run | |
| 22 // Foo::GetFileSizeCallback. | |
| 23 // | |
| 24 | |
| 25 #ifndef CHROME_BROWSER_RENDERER_HOST_FILE_SYSTEM_ACCESSOR_H_ | |
| 26 #define CHROME_BROWSER_RENDERER_HOST_FILE_SYSTEM_ACCESSOR_H_ | |
| 27 | |
| 28 #include "base/scoped_ptr.h" | |
| 29 #include "base/ref_counted.h" | |
| 30 #include "base/task.h" | |
| 31 | |
| 32 class FilePath; | |
| 33 class MessageLoop; | |
| 34 | |
| 35 class FileSystemAccessor | |
| 36 : public base::RefCountedThreadSafe<FileSystemAccessor> { | |
| 37 public: | |
| 38 typedef Callback2<int64, void*>::Type FileSizeCallback; | |
| 39 | |
| 40 virtual ~FileSystemAccessor(); | |
| 41 | |
| 42 // Request to get file size. | |
| 43 // | |
| 44 // param is an object that is owned by the caller and needs to pass back to | |
| 45 // the caller by FileSystemAccessor through the callback function. | |
| 46 // It can be set to NULL if no object needs to pass back. | |
| 47 // | |
| 48 // FileSizeCallback function is defined as: | |
| 49 // void f(int64 result, void* param); | |
| 50 // Variable result has the file size. If the file does not exist or there is | |
| 51 // error accessing the file, result is set to -1. If the given path is a | |
| 52 // directory, result is set to 0. | |
| 53 static void RequestFileSize(const FilePath& path, | |
| 54 void* param, | |
| 55 FileSizeCallback* callback); | |
| 56 | |
| 57 private: | |
| 58 FileSystemAccessor(void* param, FileSizeCallback* callback); | |
| 59 | |
| 60 // Get file size on the worker thread and pass result back to the caller | |
| 61 // thread. | |
| 62 void GetFileSize(const FilePath& path); | |
| 63 | |
| 64 // Getting file size completed, callback to reply message. | |
| 65 void GetFileSizeCompleted(int64 result); | |
| 66 | |
| 67 MessageLoop* caller_loop_; | |
| 68 void* param_; | |
| 69 scoped_ptr<FileSizeCallback> callback_; | |
| 70 | |
| 71 DISALLOW_COPY_AND_ASSIGN(FileSystemAccessor); | |
| 72 }; | |
| 73 | |
| 74 #endif // CHROME_BROWSER_RENDERER_HOST_FILE_SYSTEM_ACCESSOR_H_ | |
| OLD | NEW |