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