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_PLUGIN_PRIVATE_FILE_SYSTEM_BACKEND_H_
6#define STORAGE_BROWSER_FILEAPI_PLUGIN_PRIVATE_FILE_SYSTEM_BACKEND_H_
7
8#include <set>
9#include <string>
10
11#include "base/memory/ref_counted.h"
12#include "base/memory/weak_ptr.h"
13#include "storage/browser/fileapi/file_system_backend.h"
14#include "storage/browser/fileapi/file_system_options.h"
15#include "storage/browser/fileapi/file_system_quota_util.h"
16#include "storage/browser/fileapi/task_runner_bound_observer_list.h"
17
18namespace base {
19class SequencedTaskRunner;
20}
21
22namespace content {
23class PluginPrivateFileSystemBackendTest;
24}
25
26namespace storage {
27class SpecialStoragePolicy;
28}
29
30namespace storage {
31
32class ObfuscatedFileUtil;
33class WatcherManager;
34
35class STORAGE_EXPORT PluginPrivateFileSystemBackend
36    : public FileSystemBackend,
37      public FileSystemQuotaUtil {
38 public:
39  class FileSystemIDToPluginMap;
40  typedef base::Callback<void(base::File::Error result)> StatusCallback;
41
42  PluginPrivateFileSystemBackend(
43      base::SequencedTaskRunner* file_task_runner,
44      const base::FilePath& profile_path,
45      storage::SpecialStoragePolicy* special_storage_policy,
46      const FileSystemOptions& file_system_options);
47  virtual ~PluginPrivateFileSystemBackend();
48
49  // This must be used to open 'private' filesystem instead of regular
50  // OpenFileSystem.
51  // |plugin_id| must be an identifier string for per-plugin
52  // isolation, e.g. name, MIME type etc.
53  // NOTE: |plugin_id| must be sanitized ASCII string that doesn't
54  // include *any* dangerous character like '/'.
55  void OpenPrivateFileSystem(
56      const GURL& origin_url,
57      FileSystemType type,
58      const std::string& filesystem_id,
59      const std::string& plugin_id,
60      OpenFileSystemMode mode,
61      const StatusCallback& callback);
62
63  // FileSystemBackend overrides.
64  virtual bool CanHandleType(FileSystemType type) const OVERRIDE;
65  virtual void Initialize(FileSystemContext* context) OVERRIDE;
66  virtual void ResolveURL(const FileSystemURL& url,
67                          OpenFileSystemMode mode,
68                          const OpenFileSystemCallback& callback) OVERRIDE;
69  virtual AsyncFileUtil* GetAsyncFileUtil(FileSystemType type) OVERRIDE;
70  virtual WatcherManager* GetWatcherManager(FileSystemType type) OVERRIDE;
71  virtual CopyOrMoveFileValidatorFactory* GetCopyOrMoveFileValidatorFactory(
72      FileSystemType type,
73      base::File::Error* error_code) OVERRIDE;
74  virtual FileSystemOperation* CreateFileSystemOperation(
75      const FileSystemURL& url,
76      FileSystemContext* context,
77      base::File::Error* error_code) const OVERRIDE;
78  virtual bool SupportsStreaming(const FileSystemURL& url) const OVERRIDE;
79  virtual bool HasInplaceCopyImplementation(
80      storage::FileSystemType type) const OVERRIDE;
81  virtual scoped_ptr<storage::FileStreamReader> CreateFileStreamReader(
82      const FileSystemURL& url,
83      int64 offset,
84      int64 max_bytes_to_read,
85      const base::Time& expected_modification_time,
86      FileSystemContext* context) const OVERRIDE;
87  virtual scoped_ptr<FileStreamWriter> CreateFileStreamWriter(
88      const FileSystemURL& url,
89      int64 offset,
90      FileSystemContext* context) const OVERRIDE;
91  virtual FileSystemQuotaUtil* GetQuotaUtil() OVERRIDE;
92  virtual const UpdateObserverList* GetUpdateObservers(
93      FileSystemType type) const OVERRIDE;
94  virtual const ChangeObserverList* GetChangeObservers(
95      FileSystemType type) const OVERRIDE;
96  virtual const AccessObserverList* GetAccessObservers(
97      FileSystemType type) const OVERRIDE;
98
99  // FileSystemQuotaUtil overrides.
100  virtual base::File::Error DeleteOriginDataOnFileTaskRunner(
101      FileSystemContext* context,
102      storage::QuotaManagerProxy* proxy,
103      const GURL& origin_url,
104      FileSystemType type) OVERRIDE;
105  virtual void GetOriginsForTypeOnFileTaskRunner(
106      FileSystemType type,
107      std::set<GURL>* origins) OVERRIDE;
108  virtual void GetOriginsForHostOnFileTaskRunner(
109      FileSystemType type,
110      const std::string& host,
111      std::set<GURL>* origins) OVERRIDE;
112  virtual int64 GetOriginUsageOnFileTaskRunner(
113      FileSystemContext* context,
114      const GURL& origin_url,
115      FileSystemType type) OVERRIDE;
116  virtual scoped_refptr<QuotaReservation>
117      CreateQuotaReservationOnFileTaskRunner(
118          const GURL& origin_url,
119          FileSystemType type) OVERRIDE;
120
121 private:
122  friend class content::PluginPrivateFileSystemBackendTest;
123
124  ObfuscatedFileUtil* obfuscated_file_util();
125  const base::FilePath& base_path() const { return base_path_; }
126
127  scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
128  const FileSystemOptions file_system_options_;
129  const base::FilePath base_path_;
130  scoped_ptr<AsyncFileUtil> file_util_;
131  FileSystemIDToPluginMap* plugin_map_;  // Owned by file_util_.
132  base::WeakPtrFactory<PluginPrivateFileSystemBackend> weak_factory_;
133
134  DISALLOW_COPY_AND_ASSIGN(PluginPrivateFileSystemBackend);
135};
136
137}  // namespace storage
138
139#endif  // STORAGE_BROWSER_FILEAPI_PLUGIN_PRIVATE_FILE_SYSTEM_BACKEND_H_
140