Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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 #ifndef WEBKIT_CHROMEOS_FILEAPI_FILE_SYSTEM_FILE_UTIL_MEMORY_H_ | |
| 6 #define WEBKIT_CHROMEOS_FILEAPI_FILE_SYSTEM_FILE_UTIL_MEMORY_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/callback.h" | |
| 13 #include "base/time.h" | |
| 14 #include "webkit/chromeos/fileapi/file_util_async.h" | |
| 15 | |
| 16 namespace fileapi { | |
| 17 | |
| 18 // The in-memory file system. Primarily for the purpose of testing | |
| 19 // FileUtilAsync. | |
| 20 class MemoryFileUtil : public FileUtilAsync { | |
| 21 public: | |
| 22 struct FileEntry { | |
| 23 FileEntry(); | |
| 24 ~FileEntry(); | |
| 25 | |
| 26 bool is_directory; | |
| 27 std::string contents; | |
| 28 base::Time last_modified; | |
| 29 }; | |
| 30 | |
| 31 MemoryFileUtil(const FilePath& root_path); | |
| 32 | |
| 33 // FileUtilAsync overrides. | |
| 34 virtual void Open(const FilePath& file_path, | |
| 35 int file_flags, // PlatformFileFlags | |
| 36 const OpenCallback& callback) OVERRIDE; | |
| 37 virtual void GetFileInfo(const FilePath& file_path, | |
| 38 const GetFileInfoCallback& callback) OVERRIDE; | |
| 39 virtual void Create(const FilePath& file_path, | |
| 40 const StatusCallback& callback) OVERRIDE; | |
| 41 virtual void Truncate(const FilePath& file_path, | |
| 42 int64 length, | |
| 43 const StatusCallback& callback) OVERRIDE; | |
| 44 // This FS ignores last_access_time. | |
| 45 virtual void Touch(const FilePath& file_path, | |
| 46 const base::Time& last_access_time, | |
| 47 const base::Time& last_modified_time, | |
| 48 const StatusCallback& callback) OVERRIDE; | |
| 49 virtual void Remove(const FilePath& file_path, | |
| 50 RemoveMode mode, | |
| 51 const StatusCallback& callback) OVERRIDE; | |
| 52 virtual void CreateDirectory(const FilePath& dir_path, | |
| 53 const StatusCallback& callback) OVERRIDE; | |
| 54 virtual void ReadDirectory(const FilePath& dir_path, | |
| 55 const ReadDirectoryCallback& callback) OVERRIDE; | |
| 56 | |
| 57 private: | |
| 58 friend class MemoryFileUtilTest; | |
| 59 | |
| 60 typedef std::map<FilePath, FileEntry>::iterator FileIterator; | |
| 61 typedef std::map<FilePath, FileEntry>::const_iterator ConstFileIterator; | |
| 62 | |
| 63 // Returns true if the given |file_path| is present in the file system. | |
| 64 bool FileExists(const FilePath& file_path) { | |
|
kinuko
2011/12/14 11:19:13
could this method be const?
satorux1
2011/12/15 04:38:05
Done.
| |
| 65 return files_.find(file_path) != files_.end(); | |
| 66 } | |
| 67 | |
| 68 // Returns true if the given |file_path| is present and a directory. | |
| 69 bool IsDirectory(const FilePath& file_path) { | |
|
kinuko
2011/12/14 11:19:13
const?
satorux1
2011/12/15 04:38:05
Done.
| |
| 70 ConstFileIterator it = files_.find(file_path); | |
| 71 return it != files_.end() && it->second.is_directory; | |
| 72 } | |
| 73 | |
| 74 // Callback function used to implement GetFileInfo(). | |
| 75 void DoGetFileInfo(const FilePath& file_path, | |
| 76 const GetFileInfoCallback& callback); | |
| 77 | |
| 78 // Callback function used to implement Create(). | |
| 79 void DoCreate(const FilePath& file_path, | |
| 80 bool is_directory, | |
| 81 const StatusCallback& callback); | |
| 82 | |
| 83 // Callback function used to implement Truncate(). | |
| 84 void DoTruncate(const FilePath& file_path, | |
| 85 int64 length, | |
| 86 const StatusCallback& callback); | |
| 87 | |
| 88 // Callback function used to implement Touch(). | |
| 89 void DoTouch(const FilePath& file_path, | |
| 90 const base::Time& last_modified_time, | |
| 91 const StatusCallback& callback); | |
| 92 | |
| 93 // Callback function used to implement Remove(). | |
| 94 void DoRemoveSingleFile(const FilePath& file_path, | |
| 95 const StatusCallback& callback); | |
| 96 | |
| 97 // Callback function used to implement Remove(). | |
| 98 void DoRemoveRecursive(const FilePath& file_path, | |
| 99 const StatusCallback& callback); | |
| 100 | |
| 101 // Will start enumerating with file path |from|. If |from| path is | |
| 102 // empty, will start from the beginning. | |
| 103 void DoReadDirectory(const FilePath& dir_path, | |
| 104 const FilePath& from, | |
| 105 const ReadDirectoryCallback& callback); | |
| 106 | |
| 107 // Opens a file of the given |file_path| with |flags|. A file is | |
| 108 // guaranteed to be present at |file_path|. | |
| 109 void OpenVerifiedFile(const FilePath& file_path, | |
| 110 int flags, | |
| 111 const OpenCallback& callback); | |
| 112 | |
| 113 // Callback function used to implement Open(). | |
| 114 void DidGetFileInfoForOpen(const FilePath& file_path, | |
| 115 int flags, | |
| 116 const OpenCallback& callback, | |
| 117 PlatformFileError get_info_result, | |
| 118 const base::PlatformFileInfo& file_info); | |
| 119 | |
| 120 // Callback function used to implement Open(). | |
| 121 void OpenTruncatedFileOrCreate(const FilePath& file_path, | |
| 122 int flags, | |
| 123 const OpenCallback& callback, | |
| 124 PlatformFileError result); | |
| 125 | |
| 126 // Callback function used to implement Open(). | |
| 127 void DidCreateOrTruncateForOpen(const FilePath& file_path, | |
| 128 int flags, | |
| 129 int64 size, | |
| 130 const OpenCallback& callback, | |
| 131 PlatformFileError result); | |
| 132 | |
| 133 // Sets the read directory size buffer. | |
| 134 // Mainly for the purpose of testing. | |
| 135 void set_read_directory_buffer_size(size_t size) { | |
| 136 read_directory_buffer_size_ = size; | |
| 137 } | |
| 138 | |
| 139 // The files in the file system. | |
| 140 std::map<FilePath, FileEntry> files_; | |
| 141 size_t read_directory_buffer_size_; | |
| 142 std::vector<DirectoryEntry> read_directory_buffer_; | |
| 143 | |
| 144 DISALLOW_COPY_AND_ASSIGN(MemoryFileUtil); | |
| 145 }; | |
| 146 | |
| 147 } // fileapi | |
| 148 | |
| 149 #endif // WEBKIT_CHROMEOS_FILEAPI_FILE_SYSTEM_FILE_UTIL_ASYNC_H_ | |
| OLD | NEW |