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_PROVIDED_FILE_SYSTEM_H_
6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_PROVIDED_FILE_SYSTEM_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "chrome/browser/chromeos/file_system_provider/abort_callback.h"
15 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info.h"
16 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
17 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_observer.h"
18 #include "chrome/browser/chromeos/file_system_provider/queue.h"
19 #include "chrome/browser/chromeos/file_system_provider/request_manager.h"
20 #include "storage/browser/fileapi/async_file_util.h"
21 #include "storage/browser/fileapi/watcher_manager.h"
34 namespace extensions
{
36 } // namespace extensions
39 namespace file_system_provider
{
41 class NotificationManagerInterface
;
43 // Automatically calls the |update_callback| after all of the callbacks created
44 // with |CreateCallback| are called.
46 // It's used to update tags of watchers once a notification about a change is
47 // handled. It is to make sure that the change notification is fully handled
48 // before remembering the new tag.
50 // It is necessary to update the tag after all observers handle it fully, so
51 // in case of shutdown or a crash we get the notifications again.
52 class AutoUpdater
: public base::RefCounted
<AutoUpdater
> {
54 explicit AutoUpdater(const base::Closure
& update_callback
);
56 // Creates a new callback which needs to be called before the update callback
58 base::Closure
CreateCallback();
61 friend class base::RefCounted
<AutoUpdater
>;
63 // Called once the callback created with |CreateCallback| is executed. Once
64 // all of such callbacks are called, then the update callback is invoked.
65 void OnPendingCallback();
67 virtual ~AutoUpdater();
69 base::Closure update_callback_
;
70 int created_callbacks_
;
71 int pending_callbacks_
;
74 // Provided file system implementation. Forwards requests between providers and
76 class ProvidedFileSystem
: public ProvidedFileSystemInterface
{
78 ProvidedFileSystem(Profile
* profile
,
79 const ProvidedFileSystemInfo
& file_system_info
);
80 ~ProvidedFileSystem() override
;
82 // Sets a custom event router. Used in unit tests to mock out the real
84 void SetEventRouterForTesting(extensions::EventRouter
* event_router
);
86 // Sets a custom notification manager. It will recreate the request manager,
87 // so is must be called just after creating ProvideFileSystem instance.
88 // Used by unit tests.
89 void SetNotificationManagerForTesting(
90 scoped_ptr
<NotificationManagerInterface
> notification_manager
);
92 // ProvidedFileSystemInterface overrides.
93 AbortCallback
RequestUnmount(
94 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
95 AbortCallback
GetMetadata(const base::FilePath
& entry_path
,
96 MetadataFieldMask fields
,
97 const GetMetadataCallback
& callback
) override
;
98 AbortCallback
GetActions(const base::FilePath
& entry_path
,
99 const GetActionsCallback
& callback
) override
;
100 AbortCallback
ExecuteAction(
101 const base::FilePath
& entry_path
,
102 const std::string
& action_id
,
103 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
104 AbortCallback
ReadDirectory(
105 const base::FilePath
& directory_path
,
106 const storage::AsyncFileUtil::ReadDirectoryCallback
& callback
) override
;
107 AbortCallback
OpenFile(const base::FilePath
& file_path
,
109 const OpenFileCallback
& callback
) override
;
110 AbortCallback
CloseFile(
112 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
113 AbortCallback
ReadFile(int file_handle
,
114 net::IOBuffer
* buffer
,
117 const ReadChunkReceivedCallback
& callback
) override
;
118 AbortCallback
CreateDirectory(
119 const base::FilePath
& directory_path
,
121 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
122 AbortCallback
DeleteEntry(
123 const base::FilePath
& entry_path
,
125 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
126 AbortCallback
CreateFile(
127 const base::FilePath
& file_path
,
128 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
129 AbortCallback
CopyEntry(
130 const base::FilePath
& source_path
,
131 const base::FilePath
& target_path
,
132 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
133 AbortCallback
MoveEntry(
134 const base::FilePath
& source_path
,
135 const base::FilePath
& target_path
,
136 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
137 AbortCallback
Truncate(
138 const base::FilePath
& file_path
,
140 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
141 AbortCallback
WriteFile(
143 net::IOBuffer
* buffer
,
146 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
147 AbortCallback
AddWatcher(
149 const base::FilePath
& entry_path
,
152 const storage::AsyncFileUtil::StatusCallback
& callback
,
153 const storage::WatcherManager::NotificationCallback
&
154 notification_callback
) override
;
157 const base::FilePath
& entry_path
,
159 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
160 const ProvidedFileSystemInfo
& GetFileSystemInfo() const override
;
161 RequestManager
* GetRequestManager() override
;
162 Watchers
* GetWatchers() override
;
163 const OpenedFiles
& GetOpenedFiles() const override
;
164 void AddObserver(ProvidedFileSystemObserver
* observer
) override
;
165 void RemoveObserver(ProvidedFileSystemObserver
* observer
) override
;
166 void Notify(const base::FilePath
& entry_path
,
168 storage::WatcherManager::ChangeType change_type
,
169 scoped_ptr
<ProvidedFileSystemObserver::Changes
> changes
,
170 const std::string
& tag
,
171 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
173 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
174 base::WeakPtr
<ProvidedFileSystemInterface
> GetWeakPtr() override
;
177 // Wrapper for arguments for AddWatcherInQueue, as it's too many of them to
178 // be used by base::Bind.
179 struct AddWatcherInQueueArgs
;
181 // Wrapper for arguments for NotifyInQueue, as it's too many of them to be
182 // used by base::Bind.
183 struct NotifyInQueueArgs
;
185 // Aborts an operation executed with a request id equal to
186 // |operation_request_id|. The request is removed immediately on the C++ side
187 // despite being handled by the providing extension or not.
188 void Abort(int operation_request_id
);
190 // Called when aborting is completed with either a success or an error.
191 void OnAbortCompleted(int operation_request_id
, base::File::Error result
);
193 // Adds a watcher within |watcher_queue_|.
194 AbortCallback
AddWatcherInQueue(const AddWatcherInQueueArgs
& args
);
196 // Removes a watcher within |watcher_queue_|.
197 AbortCallback
RemoveWatcherInQueue(
200 const base::FilePath
& entry_path
,
202 const storage::AsyncFileUtil::StatusCallback
& callback
);
204 // Notifies about a notifier even within |watcher_queue_|.
205 AbortCallback
NotifyInQueue(scoped_ptr
<NotifyInQueueArgs
> args
);
207 // Called when adding a watcher is completed with either success or en error.
208 void OnAddWatcherInQueueCompleted(
210 const base::FilePath
& entry_path
,
212 const Subscriber
& subscriber
,
213 const storage::AsyncFileUtil::StatusCallback
& callback
,
214 base::File::Error result
);
216 // Called when adding a watcher is completed with either a success or an
218 void OnRemoveWatcherInQueueCompleted(
221 const WatcherKey
& key
,
222 const storage::AsyncFileUtil::StatusCallback
& callback
,
223 bool extension_response
,
224 base::File::Error result
);
226 // Called when all observers finished handling the change notification. It
227 // updates the tag to |tag| for the entry at |entry_path|.
228 void OnNotifyInQueueCompleted(scoped_ptr
<NotifyInQueueArgs
> args
,
229 const base::File::Error result
);
231 // Called when opening a file is completed with either a success or an error.
232 void OnOpenFileCompleted(const base::FilePath
& file_path
,
234 const OpenFileCallback
& callback
,
236 base::File::Error result
);
238 // Called when closing a file is completed with either a success or an error.
239 void OnCloseFileCompleted(
241 const storage::AsyncFileUtil::StatusCallback
& callback
,
242 base::File::Error result
);
244 Profile
* profile_
; // Not owned.
245 extensions::EventRouter
* event_router_
; // Not owned. May be NULL.
246 ProvidedFileSystemInfo file_system_info_
;
247 scoped_ptr
<NotificationManagerInterface
> notification_manager_
;
248 scoped_ptr
<RequestManager
> request_manager_
;
250 Queue watcher_queue_
;
251 OpenedFiles opened_files_
;
252 base::ObserverList
<ProvidedFileSystemObserver
> observers_
;
254 base::WeakPtrFactory
<ProvidedFileSystem
> weak_ptr_factory_
;
255 DISALLOW_COPY_AND_ASSIGN(ProvidedFileSystem
);
258 } // namespace file_system_provider
259 } // namespace chromeos
261 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_PROVIDED_FILE_SYSTEM_H_