Upstream version 10.39.225.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 #include <vector>
11
12 #include "base/callback.h"
13 #include "base/memory/linked_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/task/cancelable_task_tracker.h"
16 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info.h"
17 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
18
19 class Profile;
20
21 namespace base {
22 class Time;
23 }  // namespace base
24
25 namespace net {
26 class IOBuffer;
27 }  // namespace net
28
29 namespace chromeos {
30 namespace file_system_provider {
31
32 class RequestManager;
33
34 // Path of a sample fake file, which is added to the fake file system by
35 // default.
36 extern const char kFakeFilePath[];
37
38 // Represents a file or a directory on a fake file system.
39 struct FakeEntry {
40   FakeEntry();
41   FakeEntry(scoped_ptr<EntryMetadata> metadata, const std::string& contents);
42   ~FakeEntry();
43
44   scoped_ptr<EntryMetadata> metadata;
45   std::string contents;
46
47  private:
48   DISALLOW_COPY_AND_ASSIGN(FakeEntry);
49 };
50
51 // Fake provided file system implementation. Does not communicate with target
52 // extensions. Used for unit tests.
53 class FakeProvidedFileSystem : public ProvidedFileSystemInterface {
54  public:
55   explicit FakeProvidedFileSystem(
56       const ProvidedFileSystemInfo& file_system_info);
57   virtual ~FakeProvidedFileSystem();
58
59   // Adds a fake entry to the fake file system.
60   void AddEntry(const base::FilePath& entry_path,
61                 bool is_directory,
62                 const std::string& name,
63                 int64 size,
64                 base::Time modification_time,
65                 std::string mime_type,
66                 std::string contents);
67
68   // Fetches a pointer to a fake entry registered in the fake file system. If
69   // not found, then returns NULL. The returned pointes is owned by
70   // FakeProvidedFileSystem.
71   const FakeEntry* GetEntry(const base::FilePath& entry_path) const;
72
73   // ProvidedFileSystemInterface overrides.
74   virtual AbortCallback RequestUnmount(
75       const storage::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
76   virtual AbortCallback GetMetadata(
77       const base::FilePath& entry_path,
78       ProvidedFileSystemInterface::MetadataFieldMask fields,
79       const ProvidedFileSystemInterface::GetMetadataCallback& callback)
80       OVERRIDE;
81   virtual AbortCallback ReadDirectory(
82       const base::FilePath& directory_path,
83       const storage::AsyncFileUtil::ReadDirectoryCallback& callback) OVERRIDE;
84   virtual AbortCallback OpenFile(const base::FilePath& file_path,
85                                  OpenFileMode mode,
86                                  const OpenFileCallback& callback) OVERRIDE;
87   virtual AbortCallback CloseFile(
88       int file_handle,
89       const storage::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
90   virtual AbortCallback ReadFile(
91       int file_handle,
92       net::IOBuffer* buffer,
93       int64 offset,
94       int length,
95       const ReadChunkReceivedCallback& callback) OVERRIDE;
96   virtual AbortCallback CreateDirectory(
97       const base::FilePath& directory_path,
98       bool recursive,
99       const storage::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
100   virtual AbortCallback DeleteEntry(
101       const base::FilePath& entry_path,
102       bool recursive,
103       const storage::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
104   virtual AbortCallback CreateFile(
105       const base::FilePath& file_path,
106       const storage::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
107   virtual AbortCallback CopyEntry(
108       const base::FilePath& source_path,
109       const base::FilePath& target_path,
110       const storage::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
111   virtual AbortCallback MoveEntry(
112       const base::FilePath& source_path,
113       const base::FilePath& target_path,
114       const storage::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
115   virtual AbortCallback Truncate(
116       const base::FilePath& file_path,
117       int64 length,
118       const storage::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
119   virtual AbortCallback WriteFile(
120       int file_handle,
121       net::IOBuffer* buffer,
122       int64 offset,
123       int length,
124       const storage::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
125   virtual const ProvidedFileSystemInfo& GetFileSystemInfo() const OVERRIDE;
126   virtual RequestManager* GetRequestManager() OVERRIDE;
127   virtual base::WeakPtr<ProvidedFileSystemInterface> GetWeakPtr() OVERRIDE;
128
129   // Factory callback, to be used in Service::SetFileSystemFactory(). The
130   // |event_router| argument can be NULL.
131   static ProvidedFileSystemInterface* Create(
132       Profile* profile,
133       const ProvidedFileSystemInfo& file_system_info);
134
135  private:
136   typedef std::map<base::FilePath, linked_ptr<FakeEntry> > Entries;
137   typedef std::map<int, base::FilePath> OpenedFilesMap;
138
139   // Utility function for posting a task which can be aborted by calling the
140   // returned callback.
141   AbortCallback PostAbortableTask(const base::Closure& callback);
142
143   // Aborts a request. |task_id| refers to a posted callback returning a
144   // response for the operation, which will be cancelled, hence not called.
145   void Abort(int task_id,
146              const storage::AsyncFileUtil::StatusCallback& callback);
147
148   // Aborts a request. |task_ids| refers to a vector of posted callbacks
149   // returning a response for the operation, which will be cancelled, hence not
150   // called.
151   void AbortMany(const std::vector<int>& task_ids,
152                  const storage::AsyncFileUtil::StatusCallback& callback);
153
154   ProvidedFileSystemInfo file_system_info_;
155   Entries entries_;
156   OpenedFilesMap opened_files_;
157   int last_file_handle_;
158   base::CancelableTaskTracker tracker_;
159
160   base::WeakPtrFactory<FakeProvidedFileSystem> weak_ptr_factory_;
161   DISALLOW_COPY_AND_ASSIGN(FakeProvidedFileSystem);
162 };
163
164 }  // namespace file_system_provider
165 }  // namespace chromeos
166
167 #endif  // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FAKE_PROVIDED_FILE_SYSTEM_H_