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