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
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
,
100 const OpenFileCallback
& callback
) override
;
101 AbortCallback
CloseFile(
103 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
104 AbortCallback
ReadFile(int file_handle
,
105 net::IOBuffer
* buffer
,
108 const ReadChunkReceivedCallback
& callback
) override
;
109 AbortCallback
CreateDirectory(
110 const base::FilePath
& directory_path
,
112 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
113 AbortCallback
DeleteEntry(
114 const base::FilePath
& entry_path
,
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
,
131 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
132 AbortCallback
WriteFile(
134 net::IOBuffer
* buffer
,
137 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
138 AbortCallback
AddWatcher(
140 const base::FilePath
& entry_path
,
143 const storage::AsyncFileUtil::StatusCallback
& callback
,
144 const storage::WatcherManager::NotificationCallback
&
145 notification_callback
) override
;
148 const base::FilePath
& entry_path
,
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
,
159 storage::WatcherManager::ChangeType change_type
,
160 scoped_ptr
<ProvidedFileSystemObserver::Changes
> changes
,
161 const std::string
& tag
,
162 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
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(
171 const ProvidedFileSystemInfo
& file_system_info
);
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
187 void AbortMany(const std::vector
<int>& task_ids
);
189 ProvidedFileSystemInfo file_system_info_
;
191 OpenedFiles opened_files_
;
192 int last_file_handle_
;
193 base::CancelableTaskTracker tracker_
;
194 base::ObserverList
<ProvidedFileSystemObserver
> observers_
;
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_