Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_system_provider / fake_provided_file_system.h
1 // Copyright 2014 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 CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FAKE_PROVIDED_FILE_SYSTEM_H_
6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FAKE_PROVIDED_FILE_SYSTEM_H_
7
8 #include <map>
9 #include <string>
10
11 #include "base/memory/weak_ptr.h"
12 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info.h"
13 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
14
15 class Profile;
16
17 namespace base {
18 class Time;
19 }  // namespace base
20
21 namespace net {
22 class IOBuffer;
23 }  // namespace net
24
25 namespace chromeos {
26 namespace file_system_provider {
27
28 class RequestManager;
29
30 // Path of a sample fake file, which is added to the fake file system by
31 // default.
32 extern const char kFakeFilePath[];
33
34 // Represents a file or a directory on a fake file system.
35 struct FakeEntry {
36   FakeEntry() {}
37
38   FakeEntry(const EntryMetadata& metadata, const std::string& contents)
39       : metadata(metadata), contents(contents) {}
40
41   virtual ~FakeEntry() {}
42
43   EntryMetadata metadata;
44   std::string contents;
45 };
46
47 // Fake provided file system implementation. Does not communicate with target
48 // extensions. Used for unit tests.
49 class FakeProvidedFileSystem : public ProvidedFileSystemInterface {
50  public:
51   explicit FakeProvidedFileSystem(
52       const ProvidedFileSystemInfo& file_system_info);
53   virtual ~FakeProvidedFileSystem();
54
55   // Adds a fake entry to the fake file system.
56   void AddEntry(const base::FilePath& entry_path,
57                 bool is_directory,
58                 const std::string& name,
59                 int64 size,
60                 base::Time modification_time,
61                 std::string mime_type,
62                 std::string contents);
63
64   // Fetches a pointer to a fake entry registered in the fake file system. If
65   // found, then the result is written to |fake_entry| and true is returned.
66   // Otherwise, false is returned. |fake_entry| must not be NULL.
67   bool GetEntry(const base::FilePath& entry_path, FakeEntry* fake_entry) const;
68
69   // ProvidedFileSystemInterface overrides.
70   virtual void RequestUnmount(
71       const fileapi::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
72   virtual void GetMetadata(
73       const base::FilePath& entry_path,
74       const ProvidedFileSystemInterface::GetMetadataCallback& callback)
75       OVERRIDE;
76   virtual void ReadDirectory(
77       const base::FilePath& directory_path,
78       const fileapi::AsyncFileUtil::ReadDirectoryCallback& callback) OVERRIDE;
79   virtual void OpenFile(const base::FilePath& file_path,
80                         OpenFileMode mode,
81                         const OpenFileCallback& callback) OVERRIDE;
82   virtual void CloseFile(
83       int file_handle,
84       const fileapi::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
85   virtual void ReadFile(int file_handle,
86                         net::IOBuffer* buffer,
87                         int64 offset,
88                         int length,
89                         const ReadChunkReceivedCallback& callback) OVERRIDE;
90   virtual void CreateDirectory(
91       const base::FilePath& directory_path,
92       bool exclusive,
93       bool recursive,
94       const fileapi::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
95   virtual void DeleteEntry(
96       const base::FilePath& entry_path,
97       bool recursive,
98       const fileapi::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
99   virtual void CreateFile(
100       const base::FilePath& file_path,
101       const fileapi::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
102   virtual void CopyEntry(
103       const base::FilePath& source_path,
104       const base::FilePath& target_path,
105       const fileapi::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
106   virtual void MoveEntry(
107       const base::FilePath& source_path,
108       const base::FilePath& target_path,
109       const fileapi::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
110   virtual void Truncate(
111       const base::FilePath& file_path,
112       int64 length,
113       const fileapi::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
114   virtual void WriteFile(
115       int file_handle,
116       net::IOBuffer* buffer,
117       int64 offset,
118       int length,
119       const fileapi::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
120   virtual const ProvidedFileSystemInfo& GetFileSystemInfo() const OVERRIDE;
121   virtual RequestManager* GetRequestManager() OVERRIDE;
122   virtual base::WeakPtr<ProvidedFileSystemInterface> GetWeakPtr() OVERRIDE;
123
124   // Factory callback, to be used in Service::SetFileSystemFactory(). The
125   // |event_router| argument can be NULL.
126   static ProvidedFileSystemInterface* Create(
127       Profile* profile,
128       const ProvidedFileSystemInfo& file_system_info);
129
130  private:
131   typedef std::map<base::FilePath, FakeEntry> Entries;
132   typedef std::map<int, base::FilePath> OpenedFilesMap;
133
134   ProvidedFileSystemInfo file_system_info_;
135   Entries entries_;
136   OpenedFilesMap opened_files_;
137   int last_file_handle_;
138
139   base::WeakPtrFactory<ProvidedFileSystemInterface> weak_ptr_factory_;
140   DISALLOW_COPY_AND_ASSIGN(FakeProvidedFileSystem);
141 };
142
143 }  // namespace file_system_provider
144 }  // namespace chromeos
145
146 #endif  // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FAKE_PROVIDED_FILE_SYSTEM_H_