Upstream version 10.39.225.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   virtual ~SandboxFileSystemBackendDelegate();
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   virtual base::File::Error DeleteOriginDataOnFileTaskRunner(
131       FileSystemContext* context,
132       storage::QuotaManagerProxy* proxy,
133       const GURL& origin_url,
134       FileSystemType type) OVERRIDE;
135   virtual void GetOriginsForTypeOnFileTaskRunner(
136       FileSystemType type,
137       std::set<GURL>* origins) OVERRIDE;
138   virtual void GetOriginsForHostOnFileTaskRunner(
139       FileSystemType type,
140       const std::string& host,
141       std::set<GURL>* origins) OVERRIDE;
142   virtual int64 GetOriginUsageOnFileTaskRunner(
143       FileSystemContext* context,
144       const GURL& origin_url,
145       FileSystemType type) OVERRIDE;
146   virtual scoped_refptr<QuotaReservation>
147       CreateQuotaReservationOnFileTaskRunner(
148           const GURL& origin_url,
149           FileSystemType type) OVERRIDE;
150
151   // Adds an observer for the secified |type| of a file system, bound to
152   // |task_runner|.
153   virtual void AddFileUpdateObserver(FileSystemType type,
154                                      FileUpdateObserver* observer,
155                                      base::SequencedTaskRunner* task_runner);
156   virtual void AddFileChangeObserver(FileSystemType type,
157                                      FileChangeObserver* observer,
158                                      base::SequencedTaskRunner* task_runner);
159   virtual void AddFileAccessObserver(FileSystemType type,
160                                      FileAccessObserver* observer,
161                                      base::SequencedTaskRunner* task_runner);
162
163   // Returns observer lists for the specified |type| of a file system.
164   virtual const UpdateObserverList* GetUpdateObservers(
165       FileSystemType type) const;
166   virtual const ChangeObserverList* GetChangeObservers(
167       FileSystemType type) const;
168   virtual const AccessObserverList* GetAccessObservers(
169       FileSystemType type) const;
170
171   // Registers quota observer for file updates on filesystem of |type|.
172   void RegisterQuotaUpdateObserver(FileSystemType type);
173
174   void InvalidateUsageCache(const GURL& origin_url,
175                             FileSystemType type);
176   void StickyInvalidateUsageCache(const GURL& origin_url,
177                                   FileSystemType type);
178
179   void CollectOpenFileSystemMetrics(base::File::Error error_code);
180
181   base::SequencedTaskRunner* file_task_runner() {
182     return file_task_runner_.get();
183   }
184
185   AsyncFileUtil* file_util() { return sandbox_file_util_.get(); }
186   FileSystemUsageCache* usage_cache() { return file_system_usage_cache_.get(); }
187   SandboxQuotaObserver* quota_observer() { return quota_observer_.get(); }
188
189   storage::SpecialStoragePolicy* special_storage_policy() {
190     return special_storage_policy_.get();
191   }
192
193   const FileSystemOptions& file_system_options() const {
194     return file_system_options_;
195   }
196
197   FileSystemFileUtil* sync_file_util();
198
199  private:
200   friend class QuotaBackendImpl;
201   friend class SandboxQuotaObserver;
202   friend class content::SandboxFileSystemBackendDelegateTest;
203   friend class content::SandboxFileSystemTestHelper;
204
205   // Performs API-specific validity checks on the given path |url|.
206   // Returns true if access to |url| is valid in this filesystem.
207   bool IsAccessValid(const FileSystemURL& url) const;
208
209   // Returns true if the given |url|'s scheme is allowed to access
210   // filesystem.
211   bool IsAllowedScheme(const GURL& url) const;
212
213   // Returns a path to the usage cache file.
214   base::FilePath GetUsageCachePathForOriginAndType(
215       const GURL& origin_url,
216       FileSystemType type);
217
218   // Returns a path to the usage cache file (static version).
219   static base::FilePath GetUsageCachePathForOriginAndType(
220       ObfuscatedFileUtil* sandbox_file_util,
221       const GURL& origin_url,
222       FileSystemType type,
223       base::File::Error* error_out);
224
225   int64 RecalculateUsage(FileSystemContext* context,
226                          const GURL& origin,
227                          FileSystemType type);
228
229   ObfuscatedFileUtil* obfuscated_file_util();
230
231   scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
232
233   scoped_ptr<AsyncFileUtil> sandbox_file_util_;
234   scoped_ptr<FileSystemUsageCache> file_system_usage_cache_;
235   scoped_ptr<SandboxQuotaObserver> quota_observer_;
236   scoped_ptr<QuotaReservationManager> quota_reservation_manager_;
237
238   scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy_;
239
240   FileSystemOptions file_system_options_;
241
242   bool is_filesystem_opened_;
243   base::ThreadChecker io_thread_checker_;
244
245   // Accessed only on the file thread.
246   std::set<GURL> visited_origins_;
247
248   std::set<std::pair<GURL, FileSystemType> > sticky_dirty_origins_;
249
250   std::map<FileSystemType, UpdateObserverList> update_observers_;
251   std::map<FileSystemType, ChangeObserverList> change_observers_;
252   std::map<FileSystemType, AccessObserverList> access_observers_;
253
254   base::Time next_release_time_for_open_filesystem_stat_;
255
256   base::WeakPtrFactory<SandboxFileSystemBackendDelegate> weak_factory_;
257
258   DISALLOW_COPY_AND_ASSIGN(SandboxFileSystemBackendDelegate);
259 };
260
261 }  // namespace storage
262
263 #endif  // STORAGE_BROWSER_FILEAPI_SANDBOX_FILE_SYSTEM_BACKEND_DELEGATE_H_