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
ReadDirectory(
99 const base::FilePath
& directory_path
,
100 const storage::AsyncFileUtil::ReadDirectoryCallback
& callback
) override
;
101 AbortCallback
OpenFile(const base::FilePath
& file_path
,
103 const OpenFileCallback
& callback
) override
;
104 AbortCallback
CloseFile(
106 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
107 AbortCallback
ReadFile(int file_handle
,
108 net::IOBuffer
* buffer
,
111 const ReadChunkReceivedCallback
& callback
) override
;
112 AbortCallback
CreateDirectory(
113 const base::FilePath
& directory_path
,
115 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
116 AbortCallback
DeleteEntry(
117 const base::FilePath
& entry_path
,
119 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
120 AbortCallback
CreateFile(
121 const base::FilePath
& file_path
,
122 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
123 AbortCallback
CopyEntry(
124 const base::FilePath
& source_path
,
125 const base::FilePath
& target_path
,
126 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
127 AbortCallback
MoveEntry(
128 const base::FilePath
& source_path
,
129 const base::FilePath
& target_path
,
130 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
131 AbortCallback
Truncate(
132 const base::FilePath
& file_path
,
134 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
135 AbortCallback
WriteFile(
137 net::IOBuffer
* buffer
,
140 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
141 AbortCallback
AddWatcher(
143 const base::FilePath
& entry_path
,
146 const storage::AsyncFileUtil::StatusCallback
& callback
,
147 const storage::WatcherManager::NotificationCallback
&
148 notification_callback
) override
;
151 const base::FilePath
& entry_path
,
153 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
154 const ProvidedFileSystemInfo
& GetFileSystemInfo() const override
;
155 RequestManager
* GetRequestManager() override
;
156 Watchers
* GetWatchers() override
;
157 const OpenedFiles
& GetOpenedFiles() const override
;
158 void AddObserver(ProvidedFileSystemObserver
* observer
) override
;
159 void RemoveObserver(ProvidedFileSystemObserver
* observer
) override
;
160 void Notify(const base::FilePath
& entry_path
,
162 storage::WatcherManager::ChangeType change_type
,
163 scoped_ptr
<ProvidedFileSystemObserver::Changes
> changes
,
164 const std::string
& tag
,
165 const storage::AsyncFileUtil::StatusCallback
& callback
) override
;
166 base::WeakPtr
<ProvidedFileSystemInterface
> GetWeakPtr() override
;
169 // Wrapper for arguments for AddWatcherInQueue, as it's too many of them to
170 // be used by base::Bind.
171 struct AddWatcherInQueueArgs
;
173 // Wrapper for arguments for NotifyInQueue, as it's too many of them to be
174 // used by base::Bind.
175 struct NotifyInQueueArgs
;
177 // Aborts an operation executed with a request id equal to
178 // |operation_request_id|. The request is removed immediately on the C++ side
179 // despite being handled by the providing extension or not.
180 void Abort(int operation_request_id
);
182 // Adds a watcher within |watcher_queue_|.
183 AbortCallback
AddWatcherInQueue(const AddWatcherInQueueArgs
& args
);
185 // Removes a watcher within |watcher_queue_|.
186 AbortCallback
RemoveWatcherInQueue(
189 const base::FilePath
& entry_path
,
191 const storage::AsyncFileUtil::StatusCallback
& callback
);
193 // Notifies about a notifier even within |watcher_queue_|.
194 AbortCallback
NotifyInQueue(scoped_ptr
<NotifyInQueueArgs
> args
);
196 // Called when adding a watcher is completed with either success or en error.
197 void OnAddWatcherInQueueCompleted(
199 const base::FilePath
& entry_path
,
201 const Subscriber
& subscriber
,
202 const storage::AsyncFileUtil::StatusCallback
& callback
,
203 base::File::Error result
);
205 // Called when adding a watcher is completed with either a success or an
207 void OnRemoveWatcherInQueueCompleted(
210 const WatcherKey
& key
,
211 const storage::AsyncFileUtil::StatusCallback
& callback
,
212 bool extension_response
,
213 base::File::Error result
);
215 // Called when all observers finished handling the change notification. It
216 // updates the tag to |tag| for the entry at |entry_path|.
217 void OnNotifyInQueueCompleted(scoped_ptr
<NotifyInQueueArgs
> args
,
218 const base::File::Error result
);
220 // Called when opening a file is completed with either a success or an error.
221 void OnOpenFileCompleted(const base::FilePath
& file_path
,
223 const OpenFileCallback
& callback
,
225 base::File::Error result
);
227 // Called when closing a file is completed with either a success or an error.
228 void OnCloseFileCompleted(
230 const storage::AsyncFileUtil::StatusCallback
& callback
,
231 base::File::Error result
);
233 Profile
* profile_
; // Not owned.
234 extensions::EventRouter
* event_router_
; // Not owned. May be NULL.
235 ProvidedFileSystemInfo file_system_info_
;
236 scoped_ptr
<NotificationManagerInterface
> notification_manager_
;
237 scoped_ptr
<RequestManager
> request_manager_
;
239 Queue watcher_queue_
;
240 OpenedFiles opened_files_
;
241 ObserverList
<ProvidedFileSystemObserver
> observers_
;
243 base::WeakPtrFactory
<ProvidedFileSystem
> weak_ptr_factory_
;
244 DISALLOW_COPY_AND_ASSIGN(ProvidedFileSystem
);
247 } // namespace file_system_provider
248 } // namespace chromeos
250 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_PROVIDED_FILE_SYSTEM_H_