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