OLD | NEW |
| (Empty) |
1 // Copyright (c) 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 "webkit/fileapi/async_file_util_adapter.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/sequenced_task_runner.h" | |
9 #include "base/task_runner_util.h" | |
10 #include "webkit/blob/shareable_file_reference.h" | |
11 #include "webkit/browser/fileapi/file_system_file_util.h" | |
12 #include "webkit/fileapi/file_system_context.h" | |
13 #include "webkit/fileapi/file_system_operation_context.h" | |
14 #include "webkit/fileapi/file_system_url.h" | |
15 #include "webkit/fileapi/file_system_util.h" | |
16 | |
17 using base::Bind; | |
18 using base::Callback; | |
19 using base::Owned; | |
20 using base::PlatformFileError; | |
21 using base::Unretained; | |
22 using webkit_blob::ShareableFileReference; | |
23 | |
24 namespace fileapi { | |
25 | |
26 namespace { | |
27 | |
28 class EnsureFileExistsHelper { | |
29 public: | |
30 EnsureFileExistsHelper() : error_(base::PLATFORM_FILE_OK), created_(false) {} | |
31 | |
32 void RunWork(FileSystemFileUtil* file_util, | |
33 FileSystemOperationContext* context, | |
34 const FileSystemURL& url) { | |
35 error_ = file_util->EnsureFileExists(context, url, &created_); | |
36 } | |
37 | |
38 void Reply(const AsyncFileUtil::EnsureFileExistsCallback& callback) { | |
39 if (!callback.is_null()) | |
40 callback.Run(error_, created_); | |
41 } | |
42 | |
43 private: | |
44 base::PlatformFileError error_; | |
45 bool created_; | |
46 DISALLOW_COPY_AND_ASSIGN(EnsureFileExistsHelper); | |
47 }; | |
48 | |
49 class GetFileInfoHelper { | |
50 public: | |
51 GetFileInfoHelper() | |
52 : error_(base::PLATFORM_FILE_OK) {} | |
53 | |
54 void GetFileInfo(FileSystemFileUtil* file_util, | |
55 FileSystemOperationContext* context, | |
56 const FileSystemURL& url) { | |
57 error_ = file_util->GetFileInfo(context, url, &file_info_, &platform_path_); | |
58 } | |
59 | |
60 void CreateSnapshotFile(FileSystemFileUtil* file_util, | |
61 FileSystemOperationContext* context, | |
62 const FileSystemURL& url) { | |
63 scoped_file_ = file_util->CreateSnapshotFile( | |
64 context, url, &error_, &file_info_, &platform_path_); | |
65 } | |
66 | |
67 void ReplyFileInfo(const AsyncFileUtil::GetFileInfoCallback& callback) { | |
68 if (!callback.is_null()) | |
69 callback.Run(error_, file_info_, platform_path_); | |
70 } | |
71 | |
72 void ReplySnapshotFile( | |
73 const AsyncFileUtil::CreateSnapshotFileCallback& callback) { | |
74 if (!callback.is_null()) | |
75 callback.Run(error_, file_info_, platform_path_, | |
76 ShareableFileReference::GetOrCreate(scoped_file_.Pass())); | |
77 } | |
78 | |
79 private: | |
80 base::PlatformFileError error_; | |
81 base::PlatformFileInfo file_info_; | |
82 base::FilePath platform_path_; | |
83 webkit_blob::ScopedFile scoped_file_; | |
84 DISALLOW_COPY_AND_ASSIGN(GetFileInfoHelper); | |
85 }; | |
86 | |
87 class ReadDirectoryHelper { | |
88 public: | |
89 ReadDirectoryHelper() : error_(base::PLATFORM_FILE_OK) {} | |
90 | |
91 void RunWork(FileSystemFileUtil* file_util, | |
92 FileSystemOperationContext* context, | |
93 const FileSystemURL& url) { | |
94 base::PlatformFileInfo file_info; | |
95 base::FilePath platform_path; | |
96 PlatformFileError error = file_util->GetFileInfo( | |
97 context, url, &file_info, &platform_path); | |
98 if (error != base::PLATFORM_FILE_OK) { | |
99 error_ = error; | |
100 return; | |
101 } | |
102 if (!file_info.is_directory) { | |
103 error_ = base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; | |
104 return; | |
105 } | |
106 | |
107 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> file_enum( | |
108 file_util->CreateFileEnumerator(context, url)); | |
109 | |
110 base::FilePath current; | |
111 while (!(current = file_enum->Next()).empty()) { | |
112 DirectoryEntry entry; | |
113 entry.is_directory = file_enum->IsDirectory(); | |
114 entry.name = VirtualPath::BaseName(current).value(); | |
115 entry.size = file_enum->Size(); | |
116 entry.last_modified_time = file_enum->LastModifiedTime(); | |
117 entries_.push_back(entry); | |
118 } | |
119 error_ = base::PLATFORM_FILE_OK; | |
120 } | |
121 | |
122 void Reply(const AsyncFileUtil::ReadDirectoryCallback& callback) { | |
123 if (!callback.is_null()) | |
124 callback.Run(error_, entries_, false /* has_more */); | |
125 } | |
126 | |
127 private: | |
128 base::PlatformFileError error_; | |
129 std::vector<DirectoryEntry> entries_; | |
130 DISALLOW_COPY_AND_ASSIGN(ReadDirectoryHelper); | |
131 }; | |
132 | |
133 } // namespace | |
134 | |
135 AsyncFileUtilAdapter::AsyncFileUtilAdapter( | |
136 FileSystemFileUtil* sync_file_util) | |
137 : sync_file_util_(sync_file_util) { | |
138 DCHECK(sync_file_util_.get()); | |
139 } | |
140 | |
141 AsyncFileUtilAdapter::~AsyncFileUtilAdapter() { | |
142 } | |
143 | |
144 bool AsyncFileUtilAdapter::CreateOrOpen( | |
145 FileSystemOperationContext* context, | |
146 const FileSystemURL& url, | |
147 int file_flags, | |
148 const CreateOrOpenCallback& callback) { | |
149 return base::FileUtilProxy::RelayCreateOrOpen( | |
150 context->task_runner(), | |
151 Bind(&FileSystemFileUtil::CreateOrOpen, Unretained(sync_file_util_.get()), | |
152 context, url, file_flags), | |
153 Bind(&FileSystemFileUtil::Close, Unretained(sync_file_util_.get()), | |
154 context), | |
155 callback); | |
156 } | |
157 | |
158 bool AsyncFileUtilAdapter::EnsureFileExists( | |
159 FileSystemOperationContext* context, | |
160 const FileSystemURL& url, | |
161 const EnsureFileExistsCallback& callback) { | |
162 EnsureFileExistsHelper* helper = new EnsureFileExistsHelper; | |
163 return context->task_runner()->PostTaskAndReply( | |
164 FROM_HERE, | |
165 Bind(&EnsureFileExistsHelper::RunWork, Unretained(helper), | |
166 sync_file_util_.get(), context, url), | |
167 Bind(&EnsureFileExistsHelper::Reply, Owned(helper), callback)); | |
168 } | |
169 | |
170 bool AsyncFileUtilAdapter::CreateDirectory( | |
171 FileSystemOperationContext* context, | |
172 const FileSystemURL& url, | |
173 bool exclusive, | |
174 bool recursive, | |
175 const StatusCallback& callback) { | |
176 return base::PostTaskAndReplyWithResult( | |
177 context->task_runner(), FROM_HERE, | |
178 Bind(&FileSystemFileUtil::CreateDirectory, | |
179 Unretained(sync_file_util_.get()), | |
180 context, url, exclusive, recursive), | |
181 callback); | |
182 } | |
183 | |
184 bool AsyncFileUtilAdapter::GetFileInfo( | |
185 FileSystemOperationContext* context, | |
186 const FileSystemURL& url, | |
187 const GetFileInfoCallback& callback) { | |
188 GetFileInfoHelper* helper = new GetFileInfoHelper; | |
189 return context->task_runner()->PostTaskAndReply( | |
190 FROM_HERE, | |
191 Bind(&GetFileInfoHelper::GetFileInfo, Unretained(helper), | |
192 sync_file_util_.get(), context, url), | |
193 Bind(&GetFileInfoHelper::ReplyFileInfo, Owned(helper), callback)); | |
194 } | |
195 | |
196 bool AsyncFileUtilAdapter::ReadDirectory( | |
197 FileSystemOperationContext* context, | |
198 const FileSystemURL& url, | |
199 const ReadDirectoryCallback& callback) { | |
200 ReadDirectoryHelper* helper = new ReadDirectoryHelper; | |
201 return context->task_runner()->PostTaskAndReply( | |
202 FROM_HERE, | |
203 Bind(&ReadDirectoryHelper::RunWork, Unretained(helper), | |
204 sync_file_util_.get(), context, url), | |
205 Bind(&ReadDirectoryHelper::Reply, Owned(helper), callback)); | |
206 } | |
207 | |
208 bool AsyncFileUtilAdapter::Touch( | |
209 FileSystemOperationContext* context, | |
210 const FileSystemURL& url, | |
211 const base::Time& last_access_time, | |
212 const base::Time& last_modified_time, | |
213 const StatusCallback& callback) { | |
214 return base::PostTaskAndReplyWithResult( | |
215 context->task_runner(), FROM_HERE, | |
216 Bind(&FileSystemFileUtil::Touch, Unretained(sync_file_util_.get()), | |
217 context, url, last_access_time, last_modified_time), | |
218 callback); | |
219 } | |
220 | |
221 bool AsyncFileUtilAdapter::Truncate( | |
222 FileSystemOperationContext* context, | |
223 const FileSystemURL& url, | |
224 int64 length, | |
225 const StatusCallback& callback) { | |
226 return base::PostTaskAndReplyWithResult( | |
227 context->task_runner(), FROM_HERE, | |
228 Bind(&FileSystemFileUtil::Truncate, Unretained(sync_file_util_.get()), | |
229 context, url, length), | |
230 callback); | |
231 } | |
232 | |
233 bool AsyncFileUtilAdapter::CopyFileLocal( | |
234 FileSystemOperationContext* context, | |
235 const FileSystemURL& src_url, | |
236 const FileSystemURL& dest_url, | |
237 const StatusCallback& callback) { | |
238 return base::PostTaskAndReplyWithResult( | |
239 context->task_runner(), FROM_HERE, | |
240 Bind(&FileSystemFileUtil::CopyOrMoveFile, | |
241 Unretained(sync_file_util_.get()), | |
242 context, src_url, dest_url, true /* copy */), | |
243 callback); | |
244 } | |
245 | |
246 bool AsyncFileUtilAdapter::MoveFileLocal( | |
247 FileSystemOperationContext* context, | |
248 const FileSystemURL& src_url, | |
249 const FileSystemURL& dest_url, | |
250 const StatusCallback& callback) { | |
251 return base::PostTaskAndReplyWithResult( | |
252 context->task_runner(), FROM_HERE, | |
253 Bind(&FileSystemFileUtil::CopyOrMoveFile, | |
254 Unretained(sync_file_util_.get()), | |
255 context, src_url, dest_url, false /* copy */), | |
256 callback); | |
257 } | |
258 | |
259 bool AsyncFileUtilAdapter::CopyInForeignFile( | |
260 FileSystemOperationContext* context, | |
261 const base::FilePath& src_file_path, | |
262 const FileSystemURL& dest_url, | |
263 const StatusCallback& callback) { | |
264 return base::PostTaskAndReplyWithResult( | |
265 context->task_runner(), FROM_HERE, | |
266 Bind(&FileSystemFileUtil::CopyInForeignFile, | |
267 Unretained(sync_file_util_.get()), | |
268 context, src_file_path, dest_url), | |
269 callback); | |
270 } | |
271 | |
272 bool AsyncFileUtilAdapter::DeleteFile( | |
273 FileSystemOperationContext* context, | |
274 const FileSystemURL& url, | |
275 const StatusCallback& callback) { | |
276 return base::PostTaskAndReplyWithResult( | |
277 context->task_runner(), FROM_HERE, | |
278 Bind(&FileSystemFileUtil::DeleteFile, | |
279 Unretained(sync_file_util_.get()), context, url), | |
280 callback); | |
281 } | |
282 | |
283 bool AsyncFileUtilAdapter::DeleteDirectory( | |
284 FileSystemOperationContext* context, | |
285 const FileSystemURL& url, | |
286 const StatusCallback& callback) { | |
287 return base::PostTaskAndReplyWithResult( | |
288 context->task_runner(), FROM_HERE, | |
289 Bind(&FileSystemFileUtil::DeleteDirectory, | |
290 Unretained(sync_file_util_.get()), | |
291 context, url), | |
292 callback); | |
293 } | |
294 | |
295 bool AsyncFileUtilAdapter::CreateSnapshotFile( | |
296 FileSystemOperationContext* context, | |
297 const FileSystemURL& url, | |
298 const CreateSnapshotFileCallback& callback) { | |
299 GetFileInfoHelper* helper = new GetFileInfoHelper; | |
300 return context->task_runner()->PostTaskAndReply( | |
301 FROM_HERE, | |
302 Bind(&GetFileInfoHelper::CreateSnapshotFile, Unretained(helper), | |
303 sync_file_util_.get(), context, url), | |
304 Bind(&GetFileInfoHelper::ReplySnapshotFile, Owned(helper), callback)); | |
305 } | |
306 | |
307 } // namespace fileapi | |
OLD | NEW |