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.
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_
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"
36 namespace file_system_provider {
40 // Path of a sample fake file, which is added to the fake file system by
42 extern const char kFakeFilePath[];
44 // Represents a file or a directory on a fake file system.
47 FakeEntry(scoped_ptr<EntryMetadata> metadata, const std::string& contents);
50 scoped_ptr<EntryMetadata> metadata;
54 DISALLOW_COPY_AND_ASSIGN(FakeEntry);
57 // Fake provided file system implementation. Does not communicate with target
58 // extensions. Used for unit tests.
59 class FakeProvidedFileSystem : public ProvidedFileSystemInterface {
61 explicit FakeProvidedFileSystem(
62 const ProvidedFileSystemInfo& file_system_info);
63 virtual ~FakeProvidedFileSystem();
65 // Adds a fake entry to the fake file system.
66 void AddEntry(const base::FilePath& entry_path,
68 const std::string& name,
70 base::Time modification_time,
71 std::string mime_type,
72 std::string contents);
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;
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)
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,
92 const OpenFileCallback& callback) override;
93 virtual AbortCallback CloseFile(
95 const storage::AsyncFileUtil::StatusCallback& callback) override;
96 virtual AbortCallback ReadFile(
98 net::IOBuffer* buffer,
101 const ReadChunkReceivedCallback& callback) override;
102 virtual AbortCallback CreateDirectory(
103 const base::FilePath& directory_path,
105 const storage::AsyncFileUtil::StatusCallback& callback) override;
106 virtual AbortCallback DeleteEntry(
107 const base::FilePath& entry_path,
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,
124 const storage::AsyncFileUtil::StatusCallback& callback) override;
125 virtual AbortCallback WriteFile(
127 net::IOBuffer* buffer,
130 const storage::AsyncFileUtil::StatusCallback& callback) override;
131 virtual AbortCallback AddWatcher(
133 const base::FilePath& entry_path,
136 const storage::AsyncFileUtil::StatusCallback& callback,
137 const storage::WatcherManager::NotificationCallback&
138 notification_callback) override;
139 virtual void RemoveWatcher(
141 const base::FilePath& entry_path,
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,
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;
156 // Factory callback, to be used in Service::SetFileSystemFactory(). The
157 // |event_router| argument can be NULL.
158 static ProvidedFileSystemInterface* Create(
160 const ProvidedFileSystemInfo& file_system_info);
163 typedef std::map<base::FilePath, linked_ptr<FakeEntry> > Entries;
164 typedef std::map<int, base::FilePath> OpenedFilesMap;
166 // Utility function for posting a task which can be aborted by calling the
167 // returned callback.
168 AbortCallback PostAbortableTask(const base::Closure& callback);
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);
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
178 void AbortMany(const std::vector<int>& task_ids,
179 const storage::AsyncFileUtil::StatusCallback& callback);
181 ProvidedFileSystemInfo file_system_info_;
183 OpenedFilesMap opened_files_;
184 int last_file_handle_;
185 base::CancelableTaskTracker tracker_;
186 ObserverList<ProvidedFileSystemObserver> observers_;
189 base::WeakPtrFactory<FakeProvidedFileSystem> weak_ptr_factory_;
190 DISALLOW_COPY_AND_ASSIGN(FakeProvidedFileSystem);
193 } // namespace file_system_provider
194 } // namespace chromeos
196 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FAKE_PROVIDED_FILE_SYSTEM_H_