1 // Copyright (c) 2012 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 "webkit/fileapi/test_mount_point_provider.h"
11 #include "base/file_util.h"
12 #include "base/sequenced_task_runner.h"
13 #include "webkit/fileapi/copy_or_move_file_validator.h"
14 #include "webkit/fileapi/file_observers.h"
15 #include "webkit/fileapi/file_system_file_stream_reader.h"
16 #include "webkit/fileapi/file_system_operation_context.h"
17 #include "webkit/fileapi/file_system_quota_util.h"
18 #include "webkit/fileapi/file_system_util.h"
19 #include "webkit/fileapi/local_file_system_operation.h"
20 #include "webkit/fileapi/local_file_util.h"
21 #include "webkit/fileapi/native_file_util.h"
22 #include "webkit/fileapi/sandbox_file_stream_writer.h"
23 #include "webkit/quota/quota_manager.h"
27 // This only supports single origin.
28 class TestMountPointProvider::QuotaUtil
29 : public FileSystemQuotaUtil
,
30 public FileUpdateObserver
{
32 QuotaUtil() : usage_(0) {}
33 virtual ~QuotaUtil() {}
35 // FileSystemQuotaUtil overrides.
36 virtual void GetOriginsForTypeOnFileThread(
38 std::set
<GURL
>* origins
) OVERRIDE
{
41 virtual void GetOriginsForHostOnFileThread(
43 const std::string
& host
,
44 std::set
<GURL
>* origins
) OVERRIDE
{
47 virtual int64
GetOriginUsageOnFileThread(
48 FileSystemContext
* context
,
49 const GURL
& origin_url
,
50 FileSystemType type
) OVERRIDE
{
53 virtual void InvalidateUsageCache(const GURL
& origin_url
,
54 FileSystemType type
) OVERRIDE
{
58 // FileUpdateObserver overrides.
59 virtual void OnStartUpdate(const FileSystemURL
& url
) OVERRIDE
{}
60 virtual void OnUpdate(const FileSystemURL
& url
, int64 delta
) OVERRIDE
{
63 virtual void OnEndUpdate(const FileSystemURL
& url
) OVERRIDE
{}
69 TestMountPointProvider::TestMountPointProvider(
70 base::SequencedTaskRunner
* task_runner
,
71 const base::FilePath
& base_path
)
72 : base_path_(base_path
),
73 task_runner_(task_runner
),
74 local_file_util_(new AsyncFileUtilAdapter(new LocalFileUtil())),
75 quota_util_(new QuotaUtil
) {
76 UpdateObserverList::Source source
;
77 source
.AddObserver(quota_util_
.get(), task_runner_
);
78 observers_
= UpdateObserverList(source
);
81 TestMountPointProvider::~TestMountPointProvider() {
84 void TestMountPointProvider::ValidateFileSystemRoot(
85 const GURL
& origin_url
,
88 const ValidateFileSystemCallback
& callback
) {
89 // This won't be called unless we add test code that opens a test
90 // filesystem by OpenFileSystem.
94 base::FilePath
TestMountPointProvider::GetFileSystemRootPathOnFileThread(
95 const FileSystemURL
& url
,
97 DCHECK_EQ(kFileSystemTypeTest
, url
.type());
100 success
= file_util::CreateDirectory(base_path_
);
102 success
= file_util::DirectoryExists(base_path_
);
103 return success
? base_path_
: base::FilePath();
106 FileSystemFileUtil
* TestMountPointProvider::GetFileUtil(FileSystemType type
) {
107 DCHECK(local_file_util_
.get());
108 return local_file_util_
->sync_file_util();
111 AsyncFileUtil
* TestMountPointProvider::GetAsyncFileUtil(FileSystemType type
) {
112 return local_file_util_
.get();
115 CopyOrMoveFileValidatorFactory
*
116 TestMountPointProvider::GetCopyOrMoveFileValidatorFactory(
117 FileSystemType type
, base::PlatformFileError
* error_code
) {
119 *error_code
= base::PLATFORM_FILE_OK
;
123 void TestMountPointProvider::InitializeCopyOrMoveFileValidatorFactory(
124 FileSystemType type
, scoped_ptr
<CopyOrMoveFileValidatorFactory
> factory
) {
128 FilePermissionPolicy
TestMountPointProvider::GetPermissionPolicy(
129 const FileSystemURL
& url
, int permissions
) const {
130 return FILE_PERMISSION_ALWAYS_DENY
;
133 FileSystemOperation
* TestMountPointProvider::CreateFileSystemOperation(
134 const FileSystemURL
& url
,
135 FileSystemContext
* context
,
136 base::PlatformFileError
* error_code
) const {
137 scoped_ptr
<FileSystemOperationContext
> operation_context(
138 new FileSystemOperationContext(context
));
139 operation_context
->set_update_observers(observers_
);
140 return new LocalFileSystemOperation(context
, operation_context
.Pass());
143 scoped_ptr
<webkit_blob::FileStreamReader
>
144 TestMountPointProvider::CreateFileStreamReader(
145 const FileSystemURL
& url
,
147 const base::Time
& expected_modification_time
,
148 FileSystemContext
* context
) const {
149 return scoped_ptr
<webkit_blob::FileStreamReader
>(
150 new FileSystemFileStreamReader(
151 context
, url
, offset
, expected_modification_time
));
154 scoped_ptr
<fileapi::FileStreamWriter
>
155 TestMountPointProvider::CreateFileStreamWriter(
156 const FileSystemURL
& url
,
158 FileSystemContext
* context
) const {
159 return scoped_ptr
<fileapi::FileStreamWriter
>(
160 new SandboxFileStreamWriter(context
, url
, offset
, observers_
));
163 FileSystemQuotaUtil
* TestMountPointProvider::GetQuotaUtil() {
164 return quota_util_
.get();
167 void TestMountPointProvider::DeleteFileSystem(
168 const GURL
& origin_url
,
170 FileSystemContext
* context
,
171 const DeleteFileSystemCallback
& callback
) {
172 // This won't be called unless we add test code that opens a test
173 // filesystem by OpenFileSystem.
175 callback
.Run(base::PLATFORM_FILE_ERROR_INVALID_OPERATION
);
178 const UpdateObserverList
* TestMountPointProvider::GetUpdateObservers(
179 FileSystemType type
) const {
183 } // namespace fileapi