summaryrefslogtreecommitdiffstats
path: root/webkit/chromeos/fileapi/memory_file_util.h
blob: 357b934dac6596ed99cb1c8998e0538de8a8d185 (plain)
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
// 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.

#ifndef WEBKIT_CHROMEOS_FILEAPI_FILE_SYSTEM_FILE_UTIL_MEMORY_H_
#define WEBKIT_CHROMEOS_FILEAPI_FILE_SYSTEM_FILE_UTIL_MEMORY_H_

#include <map>
#include <string>
#include <vector>

#include "base/callback.h"
#include "base/time.h"
#include "webkit/chromeos/fileapi/file_util_async.h"

namespace fileapi {

// The in-memory file system. Primarily for the purpose of testing
// FileUtilAsync.
class MemoryFileUtil : public FileUtilAsync {
 public:
  struct FileEntry {
    FileEntry();
    ~FileEntry();

    bool is_directory;
    std::string contents;
    base::Time last_modified;
  };

  MemoryFileUtil(const FilePath& root_path);
  virtual ~MemoryFileUtil();

  // FileUtilAsync overrides.
  virtual void Open(const FilePath& file_path,
                    int file_flags,  // PlatformFileFlags
                    const OpenCallback& callback) OVERRIDE;
  virtual void GetFileInfo(const FilePath& file_path,
                           const GetFileInfoCallback& callback) OVERRIDE;
  virtual void Create(const FilePath& file_path,
                      const StatusCallback& callback) OVERRIDE;
  virtual void Truncate(const FilePath& file_path,
                        int64 length,
                        const StatusCallback& callback) OVERRIDE;
  // This FS ignores last_access_time.
  virtual void Touch(const FilePath& file_path,
                     const base::Time& last_access_time,
                     const base::Time& last_modified_time,
                     const StatusCallback& callback) OVERRIDE;
  virtual void Remove(const FilePath& file_path,
                      bool recursive,
                      const StatusCallback& callback) OVERRIDE;
  virtual void CreateDirectory(const FilePath& dir_path,
                               const StatusCallback& callback) OVERRIDE;
  virtual void ReadDirectory(const FilePath& dir_path,
                             const ReadDirectoryCallback& callback) OVERRIDE;

 private:
  friend class MemoryFileUtilTest;

  typedef std::map<FilePath, FileEntry>::iterator FileIterator;
  typedef std::map<FilePath, FileEntry>::const_iterator ConstFileIterator;

  // Returns true if the given |file_path| is present in the file system.
  bool FileExists(const FilePath& file_path) const {
    return files_.find(file_path) != files_.end();
  }

  // Returns true if the given |file_path| is present and a directory.
  bool IsDirectory(const FilePath& file_path) const {
    ConstFileIterator it = files_.find(file_path);
    return it != files_.end() && it->second.is_directory;
  }

  // Callback function used to implement GetFileInfo().
  void DoGetFileInfo(const FilePath& file_path,
                     const GetFileInfoCallback& callback);

  // Callback function used to implement Create().
  void DoCreate(const FilePath& file_path,
                bool is_directory,
                const StatusCallback& callback);

  // Callback function used to implement Truncate().
  void DoTruncate(const FilePath& file_path,
                  int64 length,
                  const StatusCallback& callback);

  // Callback function used to implement Touch().
  void DoTouch(const FilePath& file_path,
               const base::Time& last_modified_time,
               const StatusCallback& callback);

  // Callback function used to implement Remove().
  void DoRemoveSingleFile(const FilePath& file_path,
                          const StatusCallback& callback);

  // Callback function used to implement Remove().
  void DoRemoveRecursive(const FilePath& file_path,
                         const StatusCallback& callback);

  // Will start enumerating with file path |from|. If |from| path is
  // empty, will start from the beginning.
  void DoReadDirectory(const FilePath& dir_path,
                       const FilePath& from,
                       const ReadDirectoryCallback& callback);

  // Opens a file of the given |file_path| with |flags|. A file is
  // guaranteed to be present at |file_path|.
  void OpenVerifiedFile(const FilePath& file_path,
                        int flags,
                        const OpenCallback& callback);

  // Callback function used to implement Open().
  void DidGetFileInfoForOpen(const FilePath& file_path,
                             int flags,
                             const OpenCallback& callback,
                             PlatformFileError get_info_result,
                             const base::PlatformFileInfo& file_info);

  // Callback function used to implement Open().
  void OpenTruncatedFileOrCreate(const FilePath& file_path,
                                 int flags,
                                 const OpenCallback& callback,
                                 PlatformFileError result);

  // Callback function used to implement Open().
  void DidCreateOrTruncateForOpen(const FilePath& file_path,
                                  int flags,
                                  int64 size,
                                  const OpenCallback& callback,
                                  PlatformFileError result);

  // Sets the read directory size buffer.
  // Mainly for the purpose of testing.
  void set_read_directory_buffer_size(size_t size) {
    read_directory_buffer_size_ = size;
  }

  // The files in the file system.
  std::map<FilePath, FileEntry> files_;
  size_t read_directory_buffer_size_;
  std::vector<DirectoryEntry> read_directory_buffer_;

  DISALLOW_COPY_AND_ASSIGN(MemoryFileUtil);
};

}  // fileapi

#endif  // WEBKIT_CHROMEOS_FILEAPI_FILE_SYSTEM_FILE_UTIL_ASYNC_H_