Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / storage / browser / fileapi / sandbox_file_system_backend_delegate.h
1 // Copyright 2013 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 STORAGE_BROWSER_FILEAPI_SANDBOX_FILE_SYSTEM_BACKEND_DELEGATE_H_
6 #define STORAGE_BROWSER_FILEAPI_SANDBOX_FILE_SYSTEM_BACKEND_DELEGATE_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <utility>
12
13 #include "base/files/file_path.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/threading/thread_checker.h"
19 #include "base/time/time.h"
20 #include "storage/browser/fileapi/file_system_backend.h"
21 #include "storage/browser/fileapi/file_system_options.h"
22 #include "storage/browser/fileapi/file_system_quota_util.h"
23 #include "storage/browser/fileapi/task_runner_bound_observer_list.h"
24 #include "storage/browser/storage_browser_export.h"
25
26 namespace base {
27 class SequencedTaskRunner;
28 }
29
30 namespace content {
31 class SandboxFileSystemBackendDelegateTest;
32 class SandboxFileSystemTestHelper;
33 }
34
35 namespace storage {
36 class QuotaManagerProxy;
37 class SpecialStoragePolicy;
38 }
39
40 namespace storage {
41 class FileStreamReader;
42 }
43
44 namespace storage {
45
46 class AsyncFileUtil;
47 class FileStreamWriter;
48 class FileSystemFileUtil;
49 class FileSystemOperationContext;
50 class FileSystemURL;
51 class FileSystemUsageCache;
52 class ObfuscatedFileUtil;
53 class QuotaReservationManager;
54 class SandboxFileSystemBackend;
55 class SandboxQuotaObserver;
56
57 // Delegate implementation of the some methods in Sandbox/SyncFileSystemBackend.
58 // An instance of this class is created and owned by FileSystemContext.
59 class STORAGE_EXPORT SandboxFileSystemBackendDelegate
60     : public FileSystemQuotaUtil {
61  public:
62   typedef FileSystemBackend::OpenFileSystemCallback OpenFileSystemCallback;
63
64   // The FileSystem directory name.
65   static const base::FilePath::CharType kFileSystemDirectory[];
66
67   // Origin enumerator interface.
68   // An instance of this interface is assumed to be called on the file thread.
69   class OriginEnumerator {
70    public:
71     virtual ~OriginEnumerator() {}
72
73     // Returns the next origin.  Returns empty if there are no more origins.
74     virtual GURL Next() = 0;
75
76     // Returns the current origin's information.
77     virtual bool HasFileSystemType(FileSystemType type) const = 0;
78   };
79
80   // Returns the type directory name in sandbox directory for given |type|.
81   static std::string GetTypeString(FileSystemType type);
82
83   SandboxFileSystemBackendDelegate(
84       storage::QuotaManagerProxy* quota_manager_proxy,
85       base::SequencedTaskRunner* file_task_runner,
86       const base::FilePath& profile_path,
87       storage::SpecialStoragePolicy* special_storage_policy,
88       const FileSystemOptions& file_system_options);
89
90   ~SandboxFileSystemBackendDelegate() override;
91
92   // Returns an origin enumerator of sandbox filesystem.
93   // This method can only be called on the file thread.
94   OriginEnumerator* CreateOriginEnumerator();
95
96   // Gets a base directory path of the sandboxed filesystem that is
97   // specified by |origin_url| and |type|.
98   // (The path is similar to the origin's root path but doesn't contain
99   // the 'unique' part.)
100   // Returns an empty path if the given type is invalid.
101   // This method can only be called on the file thread.
102   base::FilePath GetBaseDirectoryForOriginAndType(
103       const GURL& origin_url,
104       FileSystemType type,
105       bool create);
106
107   // FileSystemBackend helpers.
108   void OpenFileSystem(
109       const GURL& origin_url,
110       FileSystemType type,
111       OpenFileSystemMode mode,
112       const OpenFileSystemCallback& callback,
113       const GURL& root_url);
114   scoped_ptr<FileSystemOperationContext> CreateFileSystemOperationContext(
115       const FileSystemURL& url,
116       FileSystemContext* context,
117       base::File::Error* error_code) const;
118   scoped_ptr<storage::FileStreamReader> CreateFileStreamReader(
119       const FileSystemURL& url,
120       int64 offset,
121       const base::Time& expected_modification_time,
122       FileSystemContext* context) const;
123   scoped_ptr<FileStreamWriter> CreateFileStreamWriter(
124       const FileSystemURL& url,
125       int64 offset,
126       FileSystemContext* context,
127       FileSystemType type) const;
128
129   // FileSystemQuotaUtil overrides.
130   base::File::Error DeleteOriginDataOnFileTaskRunner(
131       FileSystemContext* context,
132       storage::QuotaManagerProxy* proxy,
133       const GURL& origin_url,
134       FileSystemType type) override;
135   void GetOriginsForTypeOnFileTaskRunner(FileSystemType type,
136                                          std::set<GURL>* origins) override;
137   void GetOriginsForHostOnFileTaskRunner(FileSystemType type,
138                                          const std::string& host,
139                                          std::set<GURL>* origins) override;
140   int64 GetOriginUsageOnFileTaskRunner(FileSystemContext* context,
141                                        const GURL& origin_url,
142                                        FileSystemType type) override;
143   scoped_refptr<QuotaReservation> CreateQuotaReservationOnFileTaskRunner(
144       const GURL& origin_url,
145       FileSystemType type) override;
146
147   // Adds an observer for the secified |type| of a file system, bound to
148   // |task_runner|.
149   virtual void AddFileUpdateObserver(FileSystemType type,
150                                      FileUpdateObserver* observer,
151                                      base::SequencedTaskRunner* task_runner);
152   virtual void AddFileChangeObserver(FileSystemType type,
153                                      FileChangeObserver* observer,
154                                      base::SequencedTaskRunner* task_runner);
155   virtual void AddFileAccessObserver(FileSystemType type,
156                                      FileAccessObserver* observer,
157                                      base::SequencedTaskRunner* task_runner);
158
159   // Returns observer lists for the specified |type| of a file system.
160   virtual const UpdateObserverList* GetUpdateObservers(
161       FileSystemType type) const;
162   virtual const ChangeObserverList* GetChangeObservers(
163       FileSystemType type) const;
164   virtual const AccessObserverList* GetAccessObservers(
165       FileSystemType type) const;
166
167   // Registers quota observer for file updates on filesystem of |type|.
168   void RegisterQuotaUpdateObserver(FileSystemType type);
169
170   void InvalidateUsageCache(const GURL& origin_url,
171                             FileSystemType type);
172   void StickyInvalidateUsageCache(const GURL& origin_url,
173                                   FileSystemType type);
174
175   void CollectOpenFileSystemMetrics(base::File::Error error_code);
176
177   base::SequencedTaskRunner* file_task_runner() {
178     return file_task_runner_.get();
179   }
180
181   AsyncFileUtil* file_util() { return sandbox_file_util_.get(); }
182   FileSystemUsageCache* usage_cache() { return file_system_usage_cache_.get(); }
183   SandboxQuotaObserver* quota_observer() { return quota_observer_.get(); }
184
185   storage::SpecialStoragePolicy* special_storage_policy() {
186     return special_storage_policy_.get();
187   }
188
189   const FileSystemOptions& file_system_options() const {
190     return file_system_options_;
191   }
192
193   FileSystemFileUtil* sync_file_util();
194
195  private:
196   friend class QuotaBackendImpl;
197   friend class SandboxQuotaObserver;
198   friend class content::SandboxFileSystemBackendDelegateTest;
199   friend class content::SandboxFileSystemTestHelper;
200
201   // Performs API-specific validity checks on the given path |url|.
202   // Returns true if access to |url| is valid in this filesystem.
203   bool IsAccessValid(const FileSystemURL& url) const;
204
205   // Returns true if the given |url|'s scheme is allowed to access
206   // filesystem.
207   bool IsAllowedScheme(const GURL& url) const;
208
209   // Returns a path to the usage cache file.
210   base::FilePath GetUsageCachePathForOriginAndType(
211       const GURL& origin_url,
212       FileSystemType type);
213
214   // Returns a path to the usage cache file (static version).
215   static base::FilePath GetUsageCachePathForOriginAndType(
216       ObfuscatedFileUtil* sandbox_file_util,
217       const GURL& origin_url,
218       FileSystemType type,
219       base::File::Error* error_out);
220
221   int64 RecalculateUsage(FileSystemContext* context,
222                          const GURL& origin,
223                          FileSystemType type);
224
225   ObfuscatedFileUtil* obfuscated_file_util();
226
227   scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
228
229   scoped_ptr<AsyncFileUtil> sandbox_file_util_;
230   scoped_ptr<FileSystemUsageCache> file_system_usage_cache_;
231   scoped_ptr<SandboxQuotaObserver> quota_observer_;
232   scoped_ptr<QuotaReservationManager> quota_reservation_manager_;
233
234   scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy_;
235
236   FileSystemOptions file_system_options_;
237
238   bool is_filesystem_opened_;
239   base::ThreadChecker io_thread_checker_;
240
241   // Accessed only on the file thread.
242   std::set<GURL> visited_origins_;
243
244   std::set<std::pair<GURL, FileSystemType> > sticky_dirty_origins_;
245
246   std::map<FileSystemType, UpdateObserverList> update_observers_;
247   std::map<FileSystemType, ChangeObserverList> change_observers_;
248   std::map<FileSystemType, AccessObserverList> access_observers_;
249
250   base::Time next_release_time_for_open_filesystem_stat_;
251
252   base::WeakPtrFactory<SandboxFileSystemBackendDelegate> weak_factory_;
253
254   DISALLOW_COPY_AND_ASSIGN(SandboxFileSystemBackendDelegate);
255 };
256
257 }  // namespace storage
258
259 #endif  // STORAGE_BROWSER_FILEAPI_SANDBOX_FILE_SYSTEM_BACKEND_DELEGATE_H_