OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "webkit/fileapi/test_mount_point_provider.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/fileapi/copy_or_move_file_validator.h" | |
14 #include "webkit/browser/fileapi/file_observers.h" | |
15 #include "webkit/browser/fileapi/file_system_quota_util.h" | |
16 #include "webkit/browser/fileapi/local_file_system_operation.h" | |
17 #include "webkit/browser/fileapi/local_file_util.h" | |
18 #include "webkit/browser/fileapi/native_file_util.h" | |
19 #include "webkit/browser/fileapi/sandbox_file_stream_writer.h" | |
20 #include "webkit/fileapi/file_system_file_stream_reader.h" | |
21 #include "webkit/fileapi/file_system_operation_context.h" | |
22 #include "webkit/fileapi/file_system_util.h" | |
23 #include "webkit/quota/quota_manager.h" | |
24 | |
25 namespace fileapi { | |
26 | |
27 // This only supports single origin. | |
28 class TestMountPointProvider::QuotaUtil | |
29 : public FileSystemQuotaUtil, | |
30 public FileUpdateObserver { | |
31 public: | |
32 QuotaUtil() : usage_(0) {} | |
33 virtual ~QuotaUtil() {} | |
34 | |
35 // FileSystemQuotaUtil overrides. | |
36 virtual void GetOriginsForTypeOnFileThread( | |
37 FileSystemType type, | |
38 std::set<GURL>* origins) OVERRIDE { | |
39 NOTREACHED(); | |
40 } | |
41 virtual void GetOriginsForHostOnFileThread( | |
42 FileSystemType type, | |
43 const std::string& host, | |
44 std::set<GURL>* origins) OVERRIDE { | |
45 NOTREACHED(); | |
46 } | |
47 virtual int64 GetOriginUsageOnFileThread( | |
48 FileSystemContext* context, | |
49 const GURL& origin_url, | |
50 FileSystemType type) OVERRIDE { | |
51 return usage_; | |
52 } | |
53 virtual void InvalidateUsageCache(const GURL& origin_url, | |
54 FileSystemType type) OVERRIDE { | |
55 // Do nothing. | |
56 } | |
57 virtual void StickyInvalidateUsageCache( | |
58 const GURL& origin, | |
59 FileSystemType type) OVERRIDE { | |
60 // Do nothing. | |
61 } | |
62 | |
63 // FileUpdateObserver overrides. | |
64 virtual void OnStartUpdate(const FileSystemURL& url) OVERRIDE {} | |
65 virtual void OnUpdate(const FileSystemURL& url, int64 delta) OVERRIDE { | |
66 usage_ += delta; | |
67 } | |
68 virtual void OnEndUpdate(const FileSystemURL& url) OVERRIDE {} | |
69 | |
70 private: | |
71 int64 usage_; | |
72 }; | |
73 | |
74 TestMountPointProvider::TestMountPointProvider( | |
75 base::SequencedTaskRunner* task_runner, | |
76 const base::FilePath& base_path) | |
77 : base_path_(base_path), | |
78 task_runner_(task_runner), | |
79 local_file_util_(new AsyncFileUtilAdapter(new LocalFileUtil())), | |
80 quota_util_(new QuotaUtil), | |
81 require_copy_or_move_validator_(false) { | |
82 UpdateObserverList::Source source; | |
83 source.AddObserver(quota_util_.get(), task_runner_); | |
84 observers_ = UpdateObserverList(source); | |
85 } | |
86 | |
87 TestMountPointProvider::~TestMountPointProvider() { | |
88 } | |
89 | |
90 bool TestMountPointProvider::CanHandleType(FileSystemType type) const { | |
91 return (type == kFileSystemTypeTest); | |
92 } | |
93 | |
94 void TestMountPointProvider::ValidateFileSystemRoot( | |
95 const GURL& origin_url, | |
96 FileSystemType type, | |
97 bool create, | |
98 const ValidateFileSystemCallback& callback) { | |
99 // This won't be called unless we add test code that opens a test | |
100 // filesystem by OpenFileSystem. | |
101 NOTREACHED(); | |
102 } | |
103 | |
104 base::FilePath TestMountPointProvider::GetFileSystemRootPathOnFileThread( | |
105 const FileSystemURL& url, | |
106 bool create) { | |
107 DCHECK_EQ(kFileSystemTypeTest, url.type()); | |
108 bool success = true; | |
109 if (create) | |
110 success = file_util::CreateDirectory(base_path_); | |
111 else | |
112 success = file_util::DirectoryExists(base_path_); | |
113 return success ? base_path_ : base::FilePath(); | |
114 } | |
115 | |
116 FileSystemFileUtil* TestMountPointProvider::GetFileUtil(FileSystemType type) { | |
117 DCHECK(local_file_util_.get()); | |
118 return local_file_util_->sync_file_util(); | |
119 } | |
120 | |
121 AsyncFileUtil* TestMountPointProvider::GetAsyncFileUtil(FileSystemType type) { | |
122 return local_file_util_.get(); | |
123 } | |
124 | |
125 CopyOrMoveFileValidatorFactory* | |
126 TestMountPointProvider::GetCopyOrMoveFileValidatorFactory( | |
127 FileSystemType type, base::PlatformFileError* error_code) { | |
128 DCHECK(error_code); | |
129 *error_code = base::PLATFORM_FILE_OK; | |
130 if (require_copy_or_move_validator_) { | |
131 if (!copy_or_move_file_validator_factory_) | |
132 *error_code = base::PLATFORM_FILE_ERROR_SECURITY; | |
133 return copy_or_move_file_validator_factory_.get(); | |
134 } | |
135 return NULL; | |
136 } | |
137 | |
138 void TestMountPointProvider::InitializeCopyOrMoveFileValidatorFactory( | |
139 FileSystemType type, scoped_ptr<CopyOrMoveFileValidatorFactory> factory) { | |
140 if (!require_copy_or_move_validator_) { | |
141 DCHECK(!factory); | |
142 return; | |
143 } | |
144 if (!copy_or_move_file_validator_factory_) | |
145 copy_or_move_file_validator_factory_ = factory.Pass(); | |
146 } | |
147 | |
148 FilePermissionPolicy TestMountPointProvider::GetPermissionPolicy( | |
149 const FileSystemURL& url, int permissions) const { | |
150 return FILE_PERMISSION_ALWAYS_DENY; | |
151 } | |
152 | |
153 FileSystemOperation* TestMountPointProvider::CreateFileSystemOperation( | |
154 const FileSystemURL& url, | |
155 FileSystemContext* context, | |
156 base::PlatformFileError* error_code) const { | |
157 scoped_ptr<FileSystemOperationContext> operation_context( | |
158 new FileSystemOperationContext(context)); | |
159 operation_context->set_update_observers(observers_); | |
160 return new LocalFileSystemOperation(context, operation_context.Pass()); | |
161 } | |
162 | |
163 scoped_ptr<webkit_blob::FileStreamReader> | |
164 TestMountPointProvider::CreateFileStreamReader( | |
165 const FileSystemURL& url, | |
166 int64 offset, | |
167 const base::Time& expected_modification_time, | |
168 FileSystemContext* context) const { | |
169 return scoped_ptr<webkit_blob::FileStreamReader>( | |
170 new FileSystemFileStreamReader( | |
171 context, url, offset, expected_modification_time)); | |
172 } | |
173 | |
174 scoped_ptr<fileapi::FileStreamWriter> | |
175 TestMountPointProvider::CreateFileStreamWriter( | |
176 const FileSystemURL& url, | |
177 int64 offset, | |
178 FileSystemContext* context) const { | |
179 return scoped_ptr<fileapi::FileStreamWriter>( | |
180 new SandboxFileStreamWriter(context, url, offset, observers_)); | |
181 } | |
182 | |
183 FileSystemQuotaUtil* TestMountPointProvider::GetQuotaUtil() { | |
184 return quota_util_.get(); | |
185 } | |
186 | |
187 void TestMountPointProvider::DeleteFileSystem( | |
188 const GURL& origin_url, | |
189 FileSystemType type, | |
190 FileSystemContext* context, | |
191 const DeleteFileSystemCallback& callback) { | |
192 // This won't be called unless we add test code that opens a test | |
193 // filesystem by OpenFileSystem. | |
194 NOTREACHED(); | |
195 callback.Run(base::PLATFORM_FILE_ERROR_INVALID_OPERATION); | |
196 } | |
197 | |
198 const UpdateObserverList* TestMountPointProvider::GetUpdateObservers( | |
199 FileSystemType type) const { | |
200 return &observers_; | |
201 } | |
202 | |
203 } // namespace fileapi | |
OLD | NEW |