Make sure webrtc::VideoSourceInterface is released on the main render thread.
[chromium-blink-merge.git] / content / public / test / test_file_system_backend.cc
blob8c37a9e525597de50b138525da269c82a50ff50b
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.
5 #include "content/public/test/test_file_system_backend.h"
7 #include <set>
8 #include <string>
9 #include <vector>
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/quota/quota_reservation.h"
22 #include "webkit/browser/fileapi/sandbox_file_stream_writer.h"
23 #include "webkit/browser/quota/quota_manager.h"
24 #include "webkit/common/fileapi/file_system_util.h"
26 using fileapi::FileSystemContext;
27 using fileapi::FileSystemOperation;
28 using fileapi::FileSystemOperationContext;
29 using fileapi::FileSystemType;
30 using fileapi::FileSystemURL;
32 namespace content {
34 namespace {
36 class TestFileUtil : public fileapi::LocalFileUtil {
37 public:
38 explicit TestFileUtil(const base::FilePath& base_path)
39 : base_path_(base_path) {}
40 virtual ~TestFileUtil() {}
42 // LocalFileUtil overrides.
43 virtual base::File::Error GetLocalFilePath(
44 FileSystemOperationContext* context,
45 const FileSystemURL& file_system_url,
46 base::FilePath* local_file_path) OVERRIDE {
47 *local_file_path = base_path_.Append(file_system_url.path());
48 return base::File::FILE_OK;
51 private:
52 base::FilePath base_path_;
55 } // namespace
57 // This only supports single origin.
58 class TestFileSystemBackend::QuotaUtil
59 : public fileapi::FileSystemQuotaUtil,
60 public fileapi::FileUpdateObserver {
61 public:
62 explicit QuotaUtil(base::SequencedTaskRunner* task_runner)
63 : usage_(0),
64 task_runner_(task_runner) {
65 update_observers_ = update_observers_.AddObserver(this, task_runner_.get());
67 virtual ~QuotaUtil() {}
69 // FileSystemQuotaUtil overrides.
70 virtual base::File::Error DeleteOriginDataOnFileTaskRunner(
71 FileSystemContext* context,
72 quota::QuotaManagerProxy* proxy,
73 const GURL& origin_url,
74 FileSystemType type) OVERRIDE {
75 NOTREACHED();
76 return base::File::FILE_OK;
79 virtual scoped_refptr<fileapi::QuotaReservation>
80 CreateQuotaReservationOnFileTaskRunner(
81 const GURL& origin_url,
82 FileSystemType type) OVERRIDE {
83 NOTREACHED();
84 return scoped_refptr<fileapi::QuotaReservation>();
87 virtual void GetOriginsForTypeOnFileTaskRunner(
88 FileSystemType type,
89 std::set<GURL>* origins) OVERRIDE {
90 NOTREACHED();
93 virtual void GetOriginsForHostOnFileTaskRunner(
94 FileSystemType type,
95 const std::string& host,
96 std::set<GURL>* origins) OVERRIDE {
97 NOTREACHED();
100 virtual int64 GetOriginUsageOnFileTaskRunner(
101 FileSystemContext* context,
102 const GURL& origin_url,
103 FileSystemType type) OVERRIDE {
104 return usage_;
107 virtual void AddFileUpdateObserver(
108 FileSystemType type,
109 FileUpdateObserver* observer,
110 base::SequencedTaskRunner* task_runner) OVERRIDE {
111 NOTIMPLEMENTED();
114 virtual void AddFileChangeObserver(
115 FileSystemType type,
116 fileapi::FileChangeObserver* observer,
117 base::SequencedTaskRunner* task_runner) OVERRIDE {
118 change_observers_ = change_observers_.AddObserver(observer, task_runner);
121 virtual void AddFileAccessObserver(
122 FileSystemType type,
123 fileapi::FileAccessObserver* observer,
124 base::SequencedTaskRunner* task_runner) OVERRIDE {
125 NOTIMPLEMENTED();
128 virtual const fileapi::UpdateObserverList* GetUpdateObservers(
129 FileSystemType type) const OVERRIDE {
130 return &update_observers_;
133 virtual const fileapi::ChangeObserverList* GetChangeObservers(
134 FileSystemType type) const OVERRIDE {
135 return &change_observers_;
138 virtual const fileapi::AccessObserverList* GetAccessObservers(
139 FileSystemType type) const OVERRIDE {
140 return NULL;
143 // FileUpdateObserver overrides.
144 virtual void OnStartUpdate(const FileSystemURL& url) OVERRIDE {}
145 virtual void OnUpdate(const FileSystemURL& url, int64 delta) OVERRIDE {
146 usage_ += delta;
148 virtual void OnEndUpdate(const FileSystemURL& url) OVERRIDE {}
150 base::SequencedTaskRunner* task_runner() { return task_runner_.get(); }
152 private:
153 int64 usage_;
155 scoped_refptr<base::SequencedTaskRunner> task_runner_;
157 fileapi::UpdateObserverList update_observers_;
158 fileapi::ChangeObserverList change_observers_;
161 TestFileSystemBackend::TestFileSystemBackend(
162 base::SequencedTaskRunner* task_runner,
163 const base::FilePath& base_path)
164 : base_path_(base_path),
165 file_util_(
166 new fileapi::AsyncFileUtilAdapter(new TestFileUtil(base_path))),
167 quota_util_(new QuotaUtil(task_runner)),
168 require_copy_or_move_validator_(false) {
171 TestFileSystemBackend::~TestFileSystemBackend() {
174 bool TestFileSystemBackend::CanHandleType(FileSystemType type) const {
175 return (type == fileapi::kFileSystemTypeTest);
178 void TestFileSystemBackend::Initialize(FileSystemContext* context) {
181 void TestFileSystemBackend::ResolveURL(const FileSystemURL& url,
182 fileapi::OpenFileSystemMode mode,
183 const OpenFileSystemCallback& callback) {
184 callback.Run(GetFileSystemRootURI(url.origin(), url.type()),
185 GetFileSystemName(url.origin(), url.type()),
186 base::File::FILE_OK);
189 fileapi::AsyncFileUtil* TestFileSystemBackend::GetAsyncFileUtil(
190 FileSystemType type) {
191 return file_util_.get();
194 fileapi::CopyOrMoveFileValidatorFactory*
195 TestFileSystemBackend::GetCopyOrMoveFileValidatorFactory(
196 FileSystemType type, base::File::Error* error_code) {
197 DCHECK(error_code);
198 *error_code = base::File::FILE_OK;
199 if (require_copy_or_move_validator_) {
200 if (!copy_or_move_file_validator_factory_)
201 *error_code = base::File::FILE_ERROR_SECURITY;
202 return copy_or_move_file_validator_factory_.get();
204 return NULL;
207 void TestFileSystemBackend::InitializeCopyOrMoveFileValidatorFactory(
208 scoped_ptr<fileapi::CopyOrMoveFileValidatorFactory> factory) {
209 if (!copy_or_move_file_validator_factory_)
210 copy_or_move_file_validator_factory_ = factory.Pass();
213 FileSystemOperation* TestFileSystemBackend::CreateFileSystemOperation(
214 const FileSystemURL& url,
215 FileSystemContext* context,
216 base::File::Error* error_code) const {
217 scoped_ptr<FileSystemOperationContext> operation_context(
218 new FileSystemOperationContext(context));
219 operation_context->set_update_observers(*GetUpdateObservers(url.type()));
220 operation_context->set_change_observers(
221 *quota_util_->GetChangeObservers(url.type()));
222 return FileSystemOperation::Create(url, context, operation_context.Pass());
225 bool TestFileSystemBackend::SupportsStreaming(
226 const fileapi::FileSystemURL& url) const {
227 return false;
230 scoped_ptr<webkit_blob::FileStreamReader>
231 TestFileSystemBackend::CreateFileStreamReader(
232 const FileSystemURL& url,
233 int64 offset,
234 const base::Time& expected_modification_time,
235 FileSystemContext* context) const {
236 return scoped_ptr<webkit_blob::FileStreamReader>(
237 webkit_blob::FileStreamReader::CreateForFileSystemFile(
238 context, url, offset, expected_modification_time));
241 scoped_ptr<fileapi::FileStreamWriter>
242 TestFileSystemBackend::CreateFileStreamWriter(
243 const FileSystemURL& url,
244 int64 offset,
245 FileSystemContext* context) const {
246 return scoped_ptr<fileapi::FileStreamWriter>(
247 new fileapi::SandboxFileStreamWriter(context, url, offset,
248 *GetUpdateObservers(url.type())));
251 fileapi::FileSystemQuotaUtil* TestFileSystemBackend::GetQuotaUtil() {
252 return quota_util_.get();
255 const fileapi::UpdateObserverList* TestFileSystemBackend::GetUpdateObservers(
256 FileSystemType type) const {
257 return quota_util_->GetUpdateObservers(type);
260 void TestFileSystemBackend::AddFileChangeObserver(
261 fileapi::FileChangeObserver* observer) {
262 quota_util_->AddFileChangeObserver(
263 fileapi::kFileSystemTypeTest, observer, quota_util_->task_runner());
266 } // namespace content