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