- add sources.
[platform/framework/web/crosswalk.git] / src / content / public / test / test_file_system_backend.cc
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 #include "content/public/test/test_file_system_backend.h"
6
7 #include <set>
8 #include <string>
9 #include <vector>
10
11 #include "base/file_util.h"
12 #include "base/sequenced_task_runner.h"
13 #include "webkit/browser/blob/file_stream_reader.h"
14 #include "webkit/browser/fileapi/copy_or_move_file_validator.h"
15 #include "webkit/browser/fileapi/file_observers.h"
16 #include "webkit/browser/fileapi/file_system_operation.h"
17 #include "webkit/browser/fileapi/file_system_operation_context.h"
18 #include "webkit/browser/fileapi/file_system_quota_util.h"
19 #include "webkit/browser/fileapi/local_file_util.h"
20 #include "webkit/browser/fileapi/native_file_util.h"
21 #include "webkit/browser/fileapi/sandbox_file_stream_writer.h"
22 #include "webkit/browser/quota/quota_manager.h"
23 #include "webkit/common/fileapi/file_system_util.h"
24
25 namespace fileapi {
26
27 namespace {
28
29 class TestFileUtil : public LocalFileUtil {
30  public:
31   explicit TestFileUtil(const base::FilePath& base_path)
32       : base_path_(base_path) {}
33   virtual ~TestFileUtil() {}
34
35   // LocalFileUtil overrides.
36   virtual base::PlatformFileError GetLocalFilePath(
37       FileSystemOperationContext* context,
38       const FileSystemURL& file_system_url,
39       base::FilePath* local_file_path) OVERRIDE {
40     *local_file_path = base_path_.Append(file_system_url.path());
41     return base::PLATFORM_FILE_OK;
42   }
43
44  private:
45   base::FilePath base_path_;
46 };
47
48 }  // namespace
49
50 // This only supports single origin.
51 class TestFileSystemBackend::QuotaUtil
52     : public FileSystemQuotaUtil,
53       public FileUpdateObserver {
54  public:
55   QuotaUtil(base::SequencedTaskRunner* task_runner)
56       : usage_(0),
57         task_runner_(task_runner) {
58     update_observers_ = update_observers_.AddObserver(this, task_runner_.get());
59   }
60   virtual ~QuotaUtil() {}
61
62   // FileSystemQuotaUtil overrides.
63   virtual base::PlatformFileError DeleteOriginDataOnFileThread(
64       FileSystemContext* context,
65       quota::QuotaManagerProxy* proxy,
66       const GURL& origin_url,
67       FileSystemType type) OVERRIDE {
68     NOTREACHED();
69     return base::PLATFORM_FILE_OK;
70   }
71
72   virtual void GetOriginsForTypeOnFileThread(
73       FileSystemType type,
74       std::set<GURL>* origins) OVERRIDE {
75     NOTREACHED();
76   }
77
78   virtual void GetOriginsForHostOnFileThread(
79       FileSystemType type,
80       const std::string& host,
81       std::set<GURL>* origins) OVERRIDE {
82     NOTREACHED();
83   }
84
85   virtual int64 GetOriginUsageOnFileThread(
86       FileSystemContext* context,
87       const GURL& origin_url,
88       FileSystemType type) OVERRIDE {
89     return usage_;
90   }
91
92   virtual void AddFileUpdateObserver(
93       FileSystemType type,
94       FileUpdateObserver* observer,
95       base::SequencedTaskRunner* task_runner) OVERRIDE {
96     NOTIMPLEMENTED();
97   }
98
99   virtual void AddFileChangeObserver(
100       FileSystemType type,
101       FileChangeObserver* observer,
102       base::SequencedTaskRunner* task_runner) OVERRIDE {
103     change_observers_ = change_observers_.AddObserver(observer, task_runner);
104   }
105
106   virtual void AddFileAccessObserver(
107       FileSystemType type,
108       FileAccessObserver* observer,
109       base::SequencedTaskRunner* task_runner) OVERRIDE {
110     NOTIMPLEMENTED();
111   }
112
113   virtual const UpdateObserverList* GetUpdateObservers(
114       FileSystemType type) const OVERRIDE {
115     return &update_observers_;
116   }
117
118   virtual const ChangeObserverList* GetChangeObservers(
119       FileSystemType type) const OVERRIDE {
120     return &change_observers_;
121   }
122
123   virtual const AccessObserverList* GetAccessObservers(
124       FileSystemType type) const OVERRIDE {
125     NOTIMPLEMENTED();
126     return NULL;
127   }
128
129   // FileUpdateObserver overrides.
130   virtual void OnStartUpdate(const FileSystemURL& url) OVERRIDE {}
131   virtual void OnUpdate(const FileSystemURL& url, int64 delta) OVERRIDE {
132     usage_ += delta;
133   }
134   virtual void OnEndUpdate(const FileSystemURL& url) OVERRIDE {}
135
136   base::SequencedTaskRunner* task_runner() { return task_runner_.get(); }
137
138  private:
139   int64 usage_;
140
141   scoped_refptr<base::SequencedTaskRunner> task_runner_;
142
143   UpdateObserverList update_observers_;
144   ChangeObserverList change_observers_;
145 };
146
147 TestFileSystemBackend::TestFileSystemBackend(
148     base::SequencedTaskRunner* task_runner,
149     const base::FilePath& base_path)
150     : base_path_(base_path),
151       file_util_(new AsyncFileUtilAdapter(new TestFileUtil(base_path))),
152       quota_util_(new QuotaUtil(task_runner)),
153       require_copy_or_move_validator_(false) {
154 }
155
156 TestFileSystemBackend::~TestFileSystemBackend() {
157 }
158
159 bool TestFileSystemBackend::CanHandleType(FileSystemType type) const {
160   return (type == kFileSystemTypeTest);
161 }
162
163 void TestFileSystemBackend::Initialize(FileSystemContext* context) {
164 }
165
166 void TestFileSystemBackend::OpenFileSystem(
167     const GURL& origin_url,
168     FileSystemType type,
169     OpenFileSystemMode mode,
170     const OpenFileSystemCallback& callback) {
171   callback.Run(GetFileSystemRootURI(origin_url, type),
172                GetFileSystemName(origin_url, type),
173                base::PLATFORM_FILE_OK);
174 }
175
176 AsyncFileUtil* TestFileSystemBackend::GetAsyncFileUtil(FileSystemType type) {
177   return file_util_.get();
178 }
179
180 CopyOrMoveFileValidatorFactory*
181 TestFileSystemBackend::GetCopyOrMoveFileValidatorFactory(
182     FileSystemType type, base::PlatformFileError* error_code) {
183   DCHECK(error_code);
184   *error_code = base::PLATFORM_FILE_OK;
185   if (require_copy_or_move_validator_) {
186     if (!copy_or_move_file_validator_factory_)
187       *error_code = base::PLATFORM_FILE_ERROR_SECURITY;
188     return copy_or_move_file_validator_factory_.get();
189   }
190   return NULL;
191 }
192
193 void TestFileSystemBackend::InitializeCopyOrMoveFileValidatorFactory(
194     scoped_ptr<CopyOrMoveFileValidatorFactory> factory) {
195   if (!copy_or_move_file_validator_factory_)
196     copy_or_move_file_validator_factory_ = factory.Pass();
197 }
198
199 FileSystemOperation* TestFileSystemBackend::CreateFileSystemOperation(
200     const FileSystemURL& url,
201     FileSystemContext* context,
202     base::PlatformFileError* error_code) const {
203   scoped_ptr<FileSystemOperationContext> operation_context(
204       new FileSystemOperationContext(context));
205   operation_context->set_update_observers(*GetUpdateObservers(url.type()));
206   operation_context->set_change_observers(
207       *quota_util_->GetChangeObservers(url.type()));
208   return FileSystemOperation::Create(url, context, operation_context.Pass());
209 }
210
211 scoped_ptr<webkit_blob::FileStreamReader>
212 TestFileSystemBackend::CreateFileStreamReader(
213     const FileSystemURL& url,
214     int64 offset,
215     const base::Time& expected_modification_time,
216     FileSystemContext* context) const {
217   return scoped_ptr<webkit_blob::FileStreamReader>(
218       webkit_blob::FileStreamReader::CreateForFileSystemFile(
219           context, url, offset, expected_modification_time));
220 }
221
222 scoped_ptr<fileapi::FileStreamWriter>
223 TestFileSystemBackend::CreateFileStreamWriter(
224     const FileSystemURL& url,
225     int64 offset,
226     FileSystemContext* context) const {
227   return scoped_ptr<fileapi::FileStreamWriter>(
228       new SandboxFileStreamWriter(context, url, offset,
229                                   *GetUpdateObservers(url.type())));
230 }
231
232 FileSystemQuotaUtil* TestFileSystemBackend::GetQuotaUtil() {
233   return quota_util_.get();
234 }
235
236 const UpdateObserverList* TestFileSystemBackend::GetUpdateObservers(
237     FileSystemType type) const {
238   return quota_util_->GetUpdateObservers(type);
239 }
240
241 void TestFileSystemBackend::AddFileChangeObserver(
242     FileChangeObserver* observer) {
243   quota_util_->AddFileChangeObserver(
244       kFileSystemTypeTest, observer, quota_util_->task_runner());
245 }
246
247 }  // namespace fileapi