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/provided_file_system_info.h"
15 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
16 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_observer.h"
17 #include "chrome/browser/chromeos/file_system_provider/request_manager.h"
18 #include "storage/browser/fileapi/async_file_util.h"
19 #include "storage/browser/fileapi/watcher_manager.h"
32 namespace extensions {
34 } // namespace extensions
37 namespace file_system_provider {
39 class NotificationManagerInterface;
41 // Automatically calls the |update_callback| after all of the callbacks created
42 // with |CreateCallback| are called.
44 // It's used to update tags of watchers once a notification about a change is
45 // handled. It is to make sure that the change notification is fully handled
46 // before remembering the new tag.
48 // It is necessary to update the tag after all observers handle it fully, so
49 // in case of shutdown or a crash we get the notifications again.
50 class AutoUpdater : public base::RefCounted<AutoUpdater> {
52 explicit AutoUpdater(const base::Closure& update_callback);
54 // Creates a new callback which needs to be called before the update callback
56 base::Closure CreateCallback();
59 friend class base::RefCounted<AutoUpdater>;
61 // Called once the callback created with |CreateCallback| is executed. Once
62 // all of such callbacks are called, then the update callback is invoked.
63 void OnPendingCallback();
65 virtual ~AutoUpdater();
67 base::Closure update_callback_;
68 int created_callbacks_;
69 int pending_callbacks_;
72 // Provided file system implementation. Forwards requests between providers and
74 class ProvidedFileSystem : public ProvidedFileSystemInterface {
76 ProvidedFileSystem(Profile* profile,
77 const ProvidedFileSystemInfo& file_system_info);
78 virtual ~ProvidedFileSystem();
80 // Sets a custom event router. Used in unit tests to mock out the real
82 void SetEventRouterForTesting(extensions::EventRouter* event_router);
84 // Sets a custom notification manager. It will recreate the request manager,
85 // so is must be called just after creating ProvideFileSystem instance.
86 // Used by unit tests.
87 void SetNotificationManagerForTesting(
88 scoped_ptr<NotificationManagerInterface> notification_manager);
90 // ProvidedFileSystemInterface overrides.
91 virtual AbortCallback RequestUnmount(
92 const storage::AsyncFileUtil::StatusCallback& callback) override;
93 virtual AbortCallback GetMetadata(
94 const base::FilePath& entry_path,
95 MetadataFieldMask fields,
96 const GetMetadataCallback& callback) override;
97 virtual AbortCallback ReadDirectory(
98 const base::FilePath& directory_path,
99 const storage::AsyncFileUtil::ReadDirectoryCallback& callback) override;
100 virtual AbortCallback OpenFile(const base::FilePath& file_path,
102 const OpenFileCallback& callback) override;
103 virtual AbortCallback CloseFile(
105 const storage::AsyncFileUtil::StatusCallback& callback) override;
106 virtual AbortCallback ReadFile(
108 net::IOBuffer* buffer,
111 const ReadChunkReceivedCallback& callback) override;
112 virtual AbortCallback CreateDirectory(
113 const base::FilePath& directory_path,
115 const storage::AsyncFileUtil::StatusCallback& callback) override;
116 virtual AbortCallback DeleteEntry(
117 const base::FilePath& entry_path,
119 const storage::AsyncFileUtil::StatusCallback& callback) override;
120 virtual AbortCallback CreateFile(
121 const base::FilePath& file_path,
122 const storage::AsyncFileUtil::StatusCallback& callback) override;
123 virtual AbortCallback CopyEntry(
124 const base::FilePath& source_path,
125 const base::FilePath& target_path,
126 const storage::AsyncFileUtil::StatusCallback& callback) override;
127 virtual AbortCallback MoveEntry(
128 const base::FilePath& source_path,
129 const base::FilePath& target_path,
130 const storage::AsyncFileUtil::StatusCallback& callback) override;
131 virtual AbortCallback Truncate(
132 const base::FilePath& file_path,
134 const storage::AsyncFileUtil::StatusCallback& callback) override;
135 virtual AbortCallback WriteFile(
137 net::IOBuffer* buffer,
140 const storage::AsyncFileUtil::StatusCallback& callback) override;
141 virtual AbortCallback AddWatcher(
143 const base::FilePath& entry_path,
146 const storage::AsyncFileUtil::StatusCallback& callback,
147 const storage::WatcherManager::NotificationCallback&
148 notification_callback) override;
149 virtual void RemoveWatcher(
151 const base::FilePath& entry_path,
153 const storage::AsyncFileUtil::StatusCallback& callback) override;
154 virtual const ProvidedFileSystemInfo& GetFileSystemInfo() const override;
155 virtual RequestManager* GetRequestManager() override;
156 virtual Watchers* GetWatchers() override;
157 virtual void AddObserver(ProvidedFileSystemObserver* observer) override;
158 virtual void RemoveObserver(ProvidedFileSystemObserver* observer) override;
159 virtual bool Notify(const base::FilePath& entry_path,
161 storage::WatcherManager::ChangeType change_type,
162 scoped_ptr<ProvidedFileSystemObserver::Changes> changes,
163 const std::string& tag) override;
164 virtual base::WeakPtr<ProvidedFileSystemInterface> GetWeakPtr() override;
167 // Aborts an operation executed with a request id equal to
168 // |operation_request_id|. The request is removed immediately on the C++ side
169 // despite being handled by the providing extension or not.
170 void Abort(int operation_request_id,
171 const storage::AsyncFileUtil::StatusCallback& callback);
173 // Called when adding a watcher is completed with either success or en error.
174 void OnAddWatcherCompleted(
175 const base::FilePath& entry_path,
177 const Subscriber& subscriber,
178 const storage::AsyncFileUtil::StatusCallback& callback,
179 base::File::Error result);
181 // Called when all observers finished handling the change notification. It
182 // updates the tag from |last_tag| to |tag| for the entry at |entry_path|.
183 void OnNotifyCompleted(
184 const base::FilePath& entry_path,
186 storage::WatcherManager::ChangeType change_type,
187 scoped_ptr<ProvidedFileSystemObserver::Changes> changes,
188 const std::string& last_tag,
189 const std::string& tag);
191 Profile* profile_; // Not owned.
192 extensions::EventRouter* event_router_; // Not owned. May be NULL.
193 ProvidedFileSystemInfo file_system_info_;
194 scoped_ptr<NotificationManagerInterface> notification_manager_;
195 scoped_ptr<RequestManager> request_manager_;
197 ObserverList<ProvidedFileSystemObserver> observers_;
199 base::WeakPtrFactory<ProvidedFileSystem> weak_ptr_factory_;
200 DISALLOW_COPY_AND_ASSIGN(ProvidedFileSystem);
203 } // namespace file_system_provider
204 } // namespace chromeos
206 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_PROVIDED_FILE_SYSTEM_H_