Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / browser / chromeos / file_system_provider / fake_provided_file_system.h
blobb4e949dba4f108e364d4fbe2fb6420493006317a
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 GetActions(const base::FilePath& entry_path,
90 const GetActionsCallback& callback) override;
91 AbortCallback ExecuteAction(
92 const base::FilePath& entry_path,
93 const std::string& action_id,
94 const storage::AsyncFileUtil::StatusCallback& callback) override;
95 AbortCallback ReadDirectory(
96 const base::FilePath& directory_path,
97 const storage::AsyncFileUtil::ReadDirectoryCallback& callback) override;
98 AbortCallback OpenFile(const base::FilePath& file_path,
99 OpenFileMode mode,
100 const OpenFileCallback& callback) override;
101 AbortCallback CloseFile(
102 int file_handle,
103 const storage::AsyncFileUtil::StatusCallback& callback) override;
104 AbortCallback ReadFile(int file_handle,
105 net::IOBuffer* buffer,
106 int64 offset,
107 int length,
108 const ReadChunkReceivedCallback& callback) override;
109 AbortCallback CreateDirectory(
110 const base::FilePath& directory_path,
111 bool recursive,
112 const storage::AsyncFileUtil::StatusCallback& callback) override;
113 AbortCallback DeleteEntry(
114 const base::FilePath& entry_path,
115 bool recursive,
116 const storage::AsyncFileUtil::StatusCallback& callback) override;
117 AbortCallback CreateFile(
118 const base::FilePath& file_path,
119 const storage::AsyncFileUtil::StatusCallback& callback) override;
120 AbortCallback CopyEntry(
121 const base::FilePath& source_path,
122 const base::FilePath& target_path,
123 const storage::AsyncFileUtil::StatusCallback& callback) override;
124 AbortCallback MoveEntry(
125 const base::FilePath& source_path,
126 const base::FilePath& target_path,
127 const storage::AsyncFileUtil::StatusCallback& callback) override;
128 AbortCallback Truncate(
129 const base::FilePath& file_path,
130 int64 length,
131 const storage::AsyncFileUtil::StatusCallback& callback) override;
132 AbortCallback WriteFile(
133 int file_handle,
134 net::IOBuffer* buffer,
135 int64 offset,
136 int length,
137 const storage::AsyncFileUtil::StatusCallback& callback) override;
138 AbortCallback AddWatcher(
139 const GURL& origin,
140 const base::FilePath& entry_path,
141 bool recursive,
142 bool persistent,
143 const storage::AsyncFileUtil::StatusCallback& callback,
144 const storage::WatcherManager::NotificationCallback&
145 notification_callback) override;
146 void RemoveWatcher(
147 const GURL& origin,
148 const base::FilePath& entry_path,
149 bool recursive,
150 const storage::AsyncFileUtil::StatusCallback& callback) override;
151 const ProvidedFileSystemInfo& GetFileSystemInfo() const override;
152 RequestManager* GetRequestManager() override;
153 Watchers* GetWatchers() override;
154 const OpenedFiles& GetOpenedFiles() const override;
155 void AddObserver(ProvidedFileSystemObserver* observer) override;
156 void RemoveObserver(ProvidedFileSystemObserver* observer) override;
157 void Notify(const base::FilePath& entry_path,
158 bool recursive,
159 storage::WatcherManager::ChangeType change_type,
160 scoped_ptr<ProvidedFileSystemObserver::Changes> changes,
161 const std::string& tag,
162 const storage::AsyncFileUtil::StatusCallback& callback) override;
163 void Configure(
164 const storage::AsyncFileUtil::StatusCallback& callback) override;
165 base::WeakPtr<ProvidedFileSystemInterface> GetWeakPtr() override;
167 // Factory callback, to be used in Service::SetFileSystemFactory(). The
168 // |event_router| argument can be NULL.
169 static ProvidedFileSystemInterface* Create(
170 Profile* profile,
171 const ProvidedFileSystemInfo& file_system_info);
173 private:
174 typedef std::map<base::FilePath, linked_ptr<FakeEntry>> Entries;
176 // Utility function for posting a task which can be aborted by calling the
177 // returned callback.
178 AbortCallback PostAbortableTask(const base::Closure& callback);
180 // Aborts a request. |task_id| refers to a posted callback returning a
181 // response for the operation, which will be cancelled, hence not called.
182 void Abort(int task_id);
184 // Aborts a request. |task_ids| refers to a vector of posted callbacks
185 // returning a response for the operation, which will be cancelled, hence not
186 // called.
187 void AbortMany(const std::vector<int>& task_ids);
189 ProvidedFileSystemInfo file_system_info_;
190 Entries entries_;
191 OpenedFiles opened_files_;
192 int last_file_handle_;
193 base::CancelableTaskTracker tracker_;
194 base::ObserverList<ProvidedFileSystemObserver> observers_;
195 Watchers watchers_;
197 base::WeakPtrFactory<FakeProvidedFileSystem> weak_ptr_factory_;
198 DISALLOW_COPY_AND_ASSIGN(FakeProvidedFileSystem);
201 } // namespace file_system_provider
202 } // namespace chromeos
204 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FAKE_PROVIDED_FILE_SYSTEM_H_