Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / webkit / chromeos / fileapi / memory_file_util.h
blobadbdce6b8cc490e4260d912b36474087907570d8
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
3 // found in the LICENSE file.
5 #ifndef WEBKIT_CHROMEOS_FILEAPI_FILE_SYSTEM_FILE_UTIL_MEMORY_H_
6 #define WEBKIT_CHROMEOS_FILEAPI_FILE_SYSTEM_FILE_UTIL_MEMORY_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/callback.h"
13 #include "base/time.h"
14 #include "webkit/chromeos/fileapi/file_util_async.h"
16 namespace fileapi {
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();
26 bool is_directory;
27 std::string contents;
28 base::Time last_modified;
31 MemoryFileUtil(const base::FilePath& root_path);
32 virtual ~MemoryFileUtil();
34 // FileUtilAsync overrides.
35 virtual void Open(const base::FilePath& file_path,
36 int file_flags, // PlatformFileFlags
37 const OpenCallback& callback) OVERRIDE;
38 virtual void GetFileInfo(const base::FilePath& file_path,
39 const GetFileInfoCallback& callback) OVERRIDE;
40 virtual void Create(const base::FilePath& file_path,
41 const StatusCallback& callback) OVERRIDE;
42 virtual void Truncate(const base::FilePath& file_path,
43 int64 length,
44 const StatusCallback& callback) OVERRIDE;
45 // This FS ignores last_access_time.
46 virtual void Touch(const base::FilePath& file_path,
47 const base::Time& last_access_time,
48 const base::Time& last_modified_time,
49 const StatusCallback& callback) OVERRIDE;
50 virtual void Remove(const base::FilePath& file_path,
51 bool recursive,
52 const StatusCallback& callback) OVERRIDE;
53 virtual void CreateDirectory(const base::FilePath& dir_path,
54 const StatusCallback& callback) OVERRIDE;
55 virtual void ReadDirectory(const base::FilePath& dir_path,
56 const ReadDirectoryCallback& callback) OVERRIDE;
58 private:
59 friend class MemoryFileUtilTest;
61 typedef std::map<base::FilePath, FileEntry>::iterator FileIterator;
62 typedef std::map<base::FilePath, FileEntry>::const_iterator ConstFileIterator;
64 // Returns true if the given |file_path| is present in the file system.
65 bool FileExists(const base::FilePath& file_path) const {
66 return files_.find(file_path) != files_.end();
69 // Returns true if the given |file_path| is present and a directory.
70 bool IsDirectory(const base::FilePath& file_path) const {
71 ConstFileIterator it = files_.find(file_path);
72 return it != files_.end() && it->second.is_directory;
75 // Callback function used to implement GetFileInfo().
76 void DoGetFileInfo(const base::FilePath& file_path,
77 const GetFileInfoCallback& callback);
79 // Callback function used to implement Create().
80 void DoCreate(const base::FilePath& file_path,
81 bool is_directory,
82 const StatusCallback& callback);
84 // Callback function used to implement Truncate().
85 void DoTruncate(const base::FilePath& file_path,
86 int64 length,
87 const StatusCallback& callback);
89 // Callback function used to implement Touch().
90 void DoTouch(const base::FilePath& file_path,
91 const base::Time& last_modified_time,
92 const StatusCallback& callback);
94 // Callback function used to implement Remove().
95 void DoRemoveSingleFile(const base::FilePath& file_path,
96 const StatusCallback& callback);
98 // Callback function used to implement Remove().
99 void DoRemoveRecursive(const base::FilePath& file_path,
100 const StatusCallback& callback);
102 // Will start enumerating with file path |from|. If |from| path is
103 // empty, will start from the beginning.
104 void DoReadDirectory(const base::FilePath& dir_path,
105 const base::FilePath& from,
106 const ReadDirectoryCallback& callback);
108 // Opens a file of the given |file_path| with |flags|. A file is
109 // guaranteed to be present at |file_path|.
110 void OpenVerifiedFile(const base::FilePath& file_path,
111 int flags,
112 const OpenCallback& callback);
114 // Callback function used to implement Open().
115 void DidGetFileInfoForOpen(const base::FilePath& file_path,
116 int flags,
117 const OpenCallback& callback,
118 PlatformFileError get_info_result,
119 const base::PlatformFileInfo& file_info);
121 // Callback function used to implement Open().
122 void OpenTruncatedFileOrCreate(const base::FilePath& file_path,
123 int flags,
124 const OpenCallback& callback,
125 PlatformFileError result);
127 // Callback function used to implement Open().
128 void DidCreateOrTruncateForOpen(const base::FilePath& file_path,
129 int flags,
130 int64 size,
131 const OpenCallback& callback,
132 PlatformFileError result);
134 // Sets the read directory size buffer.
135 // Mainly for the purpose of testing.
136 void set_read_directory_buffer_size(size_t size) {
137 read_directory_buffer_size_ = size;
140 // The files in the file system.
141 std::map<base::FilePath, FileEntry> files_;
142 size_t read_directory_buffer_size_;
143 std::vector<DirectoryEntry> read_directory_buffer_;
145 DISALLOW_COPY_AND_ASSIGN(MemoryFileUtil);
148 } // namespace fileapi
150 #endif // WEBKIT_CHROMEOS_FILEAPI_FILE_SYSTEM_FILE_UTIL_ASYNC_H_