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/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"
38 namespace file_system_provider
{
42 // Path of a sample fake file, which is added to the fake file system by
44 extern const base::FilePath::CharType kFakeFilePath
[];
46 // Represents a file or a directory on a fake file system.
49 FakeEntry(scoped_ptr
<EntryMetadata
> metadata
, const std::string
& contents
);
52 scoped_ptr
<EntryMetadata
> metadata
;
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
{
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
,
70 const std::string
& name
,
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
)
89 AbortCallback
ReadDirectory(
90 const base::FilePath
& directory_path
,
91 const storage::AsyncFileUtil::ReadDirectoryCallback
& callback
) override
;
92 AbortCallback
OpenFile(const base::FilePath
& file_path
,
94 const OpenFileCallback
& callback
) override
;
95 AbortCallback
CloseFile(
97 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
98 AbortCallback
ReadFile(int file_handle
,
99 net::IOBuffer
* buffer
,
102 const ReadChunkReceivedCallback
& callback
) override
;
103 AbortCallback
CreateDirectory(
104 const base::FilePath
& directory_path
,
106 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
107 AbortCallback
DeleteEntry(
108 const base::FilePath
& entry_path
,
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
,
125 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
126 AbortCallback
WriteFile(
128 net::IOBuffer
* buffer
,
131 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
132 AbortCallback
AddWatcher(
134 const base::FilePath
& entry_path
,
137 const storage::AsyncFileUtil::StatusCallback
& callback
,
138 const storage::WatcherManager::NotificationCallback
&
139 notification_callback
) override
;
142 const base::FilePath
& entry_path
,
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
,
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(
163 const ProvidedFileSystemInfo
& file_system_info
);
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
179 void AbortMany(const std::vector
<int>& task_ids
);
181 ProvidedFileSystemInfo file_system_info_
;
183 OpenedFiles 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_