Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_system_provider / provided_file_system.h
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.
4
5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_PROVIDED_FILE_SYSTEM_H_
6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_PROVIDED_FILE_SYSTEM_H_
7
8 #include <string>
9
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"
20 #include "url/gurl.h"
21
22 class Profile;
23
24 namespace net {
25 class IOBuffer;
26 }  // namespace net
27
28 namespace base {
29 class FilePath;
30 }  // namespace base
31
32 namespace extensions {
33 class EventRouter;
34 }  // namespace extensions
35
36 namespace chromeos {
37 namespace file_system_provider {
38
39 class NotificationManagerInterface;
40
41 // Automatically calls the |update_callback| after all of the callbacks created
42 // with |CreateCallback| are called.
43 //
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.
47 //
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> {
51  public:
52   explicit AutoUpdater(const base::Closure& update_callback);
53
54   // Creates a new callback which needs to be called before the update callback
55   // is called.
56   base::Closure CreateCallback();
57
58  private:
59   friend class base::RefCounted<AutoUpdater>;
60
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();
64
65   virtual ~AutoUpdater();
66
67   base::Closure update_callback_;
68   int created_callbacks_;
69   int pending_callbacks_;
70 };
71
72 // Provided file system implementation. Forwards requests between providers and
73 // clients.
74 class ProvidedFileSystem : public ProvidedFileSystemInterface {
75  public:
76   ProvidedFileSystem(Profile* profile,
77                      const ProvidedFileSystemInfo& file_system_info);
78   virtual ~ProvidedFileSystem();
79
80   // Sets a custom event router. Used in unit tests to mock out the real
81   // extension.
82   void SetEventRouterForTesting(extensions::EventRouter* event_router);
83
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);
89
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,
101                                  OpenFileMode mode,
102                                  const OpenFileCallback& callback) override;
103   virtual AbortCallback CloseFile(
104       int file_handle,
105       const storage::AsyncFileUtil::StatusCallback& callback) override;
106   virtual AbortCallback ReadFile(
107       int file_handle,
108       net::IOBuffer* buffer,
109       int64 offset,
110       int length,
111       const ReadChunkReceivedCallback& callback) override;
112   virtual AbortCallback CreateDirectory(
113       const base::FilePath& directory_path,
114       bool recursive,
115       const storage::AsyncFileUtil::StatusCallback& callback) override;
116   virtual AbortCallback DeleteEntry(
117       const base::FilePath& entry_path,
118       bool recursive,
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,
133       int64 length,
134       const storage::AsyncFileUtil::StatusCallback& callback) override;
135   virtual AbortCallback WriteFile(
136       int file_handle,
137       net::IOBuffer* buffer,
138       int64 offset,
139       int length,
140       const storage::AsyncFileUtil::StatusCallback& callback) override;
141   virtual AbortCallback AddWatcher(
142       const GURL& origin,
143       const base::FilePath& entry_path,
144       bool recursive,
145       bool persistent,
146       const storage::AsyncFileUtil::StatusCallback& callback,
147       const storage::WatcherManager::NotificationCallback&
148           notification_callback) override;
149   virtual void RemoveWatcher(
150       const GURL& origin,
151       const base::FilePath& entry_path,
152       bool recursive,
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,
160                       bool recursive,
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;
165
166  private:
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);
172
173   // Called when adding a watcher is completed with either success or en error.
174   void OnAddWatcherCompleted(
175       const base::FilePath& entry_path,
176       bool recursive,
177       const Subscriber& subscriber,
178       const storage::AsyncFileUtil::StatusCallback& callback,
179       base::File::Error result);
180
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,
185       bool recursive,
186       storage::WatcherManager::ChangeType change_type,
187       scoped_ptr<ProvidedFileSystemObserver::Changes> changes,
188       const std::string& last_tag,
189       const std::string& tag);
190
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_;
196   Watchers watchers_;
197   ObserverList<ProvidedFileSystemObserver> observers_;
198
199   base::WeakPtrFactory<ProvidedFileSystem> weak_ptr_factory_;
200   DISALLOW_COPY_AND_ASSIGN(ProvidedFileSystem);
201 };
202
203 }  // namespace file_system_provider
204 }  // namespace chromeos
205
206 #endif  // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_PROVIDED_FILE_SYSTEM_H_