OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/media_galleries/fileapi/native_media_file_util.h" | 5 #include "chrome/browser/media_galleries/fileapi/native_media_file_util.h" |
6 | 6 |
7 #include "base/bind.h" | |
7 #include "base/file_util.h" | 8 #include "base/file_util.h" |
8 #include "base/string_util.h" | 9 #include "base/string_util.h" |
9 #include "chrome/browser/media_galleries/fileapi/filtering_file_enumerator.h" | 10 #include "base/task_runner_util.h" |
10 #include "chrome/browser/media_galleries/fileapi/media_file_system_mount_point_p rovider.h" | 11 #include "chrome/browser/media_galleries/fileapi/media_file_system_mount_point_p rovider.h" |
11 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h" | 12 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h" |
12 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
13 #include "net/base/mime_sniffer.h" | 14 #include "net/base/mime_sniffer.h" |
14 #include "webkit/browser/fileapi/file_system_context.h" | 15 #include "webkit/browser/fileapi/file_system_context.h" |
15 #include "webkit/browser/fileapi/file_system_operation_context.h" | 16 #include "webkit/browser/fileapi/file_system_operation_context.h" |
16 #include "webkit/browser/fileapi/file_system_task_runners.h" | 17 #include "webkit/browser/fileapi/file_system_task_runners.h" |
17 #include "webkit/browser/fileapi/native_file_util.h" | 18 #include "webkit/browser/fileapi/native_file_util.h" |
19 #include "webkit/common/blob/shareable_file_reference.h" | |
18 | 20 |
21 using base::Bind; | |
22 using base::Owned; | |
19 using base::PlatformFile; | 23 using base::PlatformFile; |
20 using base::PlatformFileError; | 24 using base::PlatformFileError; |
21 using base::PlatformFileInfo; | 25 using base::PlatformFileInfo; |
26 using base::Unretained; | |
22 using fileapi::FileSystemOperationContext; | 27 using fileapi::FileSystemOperationContext; |
23 using fileapi::FileSystemURL; | 28 using fileapi::FileSystemURL; |
24 using fileapi::NativeFileUtil; | 29 using fileapi::NativeFileUtil; |
25 | 30 |
26 namespace chrome { | 31 namespace chrome { |
27 | 32 |
28 namespace { | 33 namespace { |
29 | 34 |
30 // Modelled after ScopedFILEClose. | 35 // Modelled after ScopedFILEClose. |
31 struct ScopedPlatformFileClose { | 36 struct ScopedPlatformFileClose { |
32 void operator()(base::PlatformFile* file) { | 37 void operator()(base::PlatformFile* file) { |
33 if (file && *file != base::kInvalidPlatformFileValue) | 38 if (file && *file != base::kInvalidPlatformFileValue) |
34 base::ClosePlatformFile(*file); | 39 base::ClosePlatformFile(*file); |
35 } | 40 } |
36 }; | 41 }; |
37 | 42 |
38 typedef scoped_ptr<base::PlatformFile, ScopedPlatformFileClose> | 43 typedef scoped_ptr<base::PlatformFile, ScopedPlatformFileClose> |
39 ScopedPlatformFile; | 44 ScopedPlatformFile; |
40 | 45 |
46 // Used to skip the hidden folders and files. Returns true if the file specified | |
47 // by |path| should be skipped. | |
48 bool ShouldSkip(const base::FilePath& path) { | |
49 const base::FilePath::StringType base_name = path.BaseName().value(); | |
50 if (base_name.empty()) | |
51 return false; | |
52 | |
53 // Dot files (aka hidden files) | |
54 if (base_name[0] == '.') | |
55 return true; | |
56 | |
57 // Mac OS X file. | |
58 if (base_name == FILE_PATH_LITERAL("__MACOSX")) | |
59 return true; | |
60 | |
61 #if defined(OS_WIN) | |
62 DWORD file_attributes = ::GetFileAttributes(path.value().c_str()); | |
63 if ((file_attributes != INVALID_FILE_ATTRIBUTES) && | |
64 ((file_attributes & FILE_ATTRIBUTE_HIDDEN) != 0)) | |
65 return true; | |
66 #else | |
67 // Windows always creates a recycle bin folder in the attached device to store | |
68 // all the deleted contents. On non-windows operating systems, there is no way | |
69 // to get the hidden attribute of windows recycle bin folders that are present | |
70 // on the attached device. Therefore, compare the file path name to the | |
71 // recycle bin name and exclude those folders. For more details, please refer | |
72 // to http://support.microsoft.com/kb/171694. | |
73 const char win_98_recycle_bin_name[] = "RECYCLED"; | |
74 const char win_xp_recycle_bin_name[] = "RECYCLER"; | |
75 const char win_vista_recycle_bin_name[] = "$Recycle.bin"; | |
76 if ((base::strncasecmp(base_name.c_str(), | |
77 win_98_recycle_bin_name, | |
78 strlen(win_98_recycle_bin_name)) == 0) || | |
79 (base::strncasecmp(base_name.c_str(), | |
80 win_xp_recycle_bin_name, | |
81 strlen(win_xp_recycle_bin_name)) == 0) || | |
82 (base::strncasecmp(base_name.c_str(), | |
83 win_vista_recycle_bin_name, | |
84 strlen(win_vista_recycle_bin_name)) == 0)) | |
85 return true; | |
86 #endif | |
87 return false; | |
88 } | |
89 | |
41 // Returns true if the current thread is capable of doing IO. | 90 // Returns true if the current thread is capable of doing IO. |
42 bool IsOnTaskRunnerThread(fileapi::FileSystemOperationContext* context) { | 91 bool IsOnTaskRunnerThread(fileapi::FileSystemOperationContext* context) { |
43 return context->file_system_context()->task_runners()-> | 92 return context->file_system_context()->task_runners()-> |
44 media_task_runner()->RunsTasksOnCurrentThread(); | 93 media_task_runner()->RunsTasksOnCurrentThread(); |
45 } | 94 } |
46 | 95 |
47 MediaPathFilter* GetMediaPathFilter(FileSystemOperationContext* context) { | 96 MediaPathFilter* GetMediaPathFilter(FileSystemOperationContext* context) { |
48 return context->GetUserValue<MediaPathFilter*>( | 97 return context->GetUserValue<MediaPathFilter*>( |
49 MediaFileSystemMountPointProvider::kMediaPathFilterKey); | 98 MediaFileSystemMountPointProvider::kMediaPathFilterKey); |
50 } | 99 } |
51 | 100 |
101 void FinishGetFileInfo( | |
102 const NativeMediaFileUtil::GetFileInfoCallback& callback, | |
103 PlatformFileInfo* file_info, | |
104 base::FilePath* platform_path, | |
105 const PlatformFileError& error) { | |
106 callback.Run(error, *file_info, *platform_path); | |
107 } | |
108 | |
109 void FinishReadDirectory( | |
110 const NativeMediaFileUtil::ReadDirectoryCallback& callback, | |
111 NativeMediaFileUtil::EntryList* file_list, | |
112 const base::PlatformFileError& error) { | |
113 callback.Run(error, *file_list, false /* has_more */); | |
114 } | |
115 | |
116 void FinishCreateSnapshotFile( | |
117 const NativeMediaFileUtil::CreateSnapshotFileCallback& callback, | |
118 PlatformFileInfo* file_info, | |
119 base::FilePath* platform_path, | |
120 scoped_refptr<webkit_blob::ShareableFileReference>* file_ref, | |
121 PlatformFileError error) { | |
122 callback.Run(error, *file_info, *platform_path, *file_ref); | |
123 } | |
124 | |
125 void FinishStatusCallback( | |
vandebo (ex-Chrome)
2013/05/31 23:22:17
This isn't needed.
tommycli
2013/06/01 01:40:54
See below.
| |
126 const NativeMediaFileUtil::StatusCallback& callback, | |
127 PlatformFileError error) { | |
128 callback.Run(error); | |
129 } | |
130 | |
52 } // namespace | 131 } // namespace |
53 | 132 |
54 NativeMediaFileUtil::NativeMediaFileUtil() { | 133 NativeMediaFileUtil::NativeMediaFileUtil() { |
55 } | 134 } |
56 | 135 |
57 PlatformFileError NativeMediaFileUtil::CreateOrOpen( | 136 bool NativeMediaFileUtil::CreateOrOpen( |
58 FileSystemOperationContext* context, | 137 FileSystemOperationContext* context, |
59 const FileSystemURL& url, | 138 const FileSystemURL& url, |
60 int file_flags, | 139 int file_flags, |
61 PlatformFile* file_handle, | 140 const CreateOrOpenCallback& callback) { |
62 bool* created) { | |
63 // Only called by NaCl, which should not have access to media file systems. | 141 // Only called by NaCl, which should not have access to media file systems. |
64 return base::PLATFORM_FILE_ERROR_SECURITY; | 142 base::PlatformFile invalid_file(base::kInvalidPlatformFileValue); |
65 } | 143 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY, |
66 | 144 base::PassPlatformFile(&invalid_file), |
67 PlatformFileError NativeMediaFileUtil::EnsureFileExists( | 145 false); |
68 FileSystemOperationContext* context, | 146 return true; |
69 const FileSystemURL& url, bool* created) { | 147 } |
148 | |
149 bool NativeMediaFileUtil::EnsureFileExists( | |
150 FileSystemOperationContext* context, | |
151 const FileSystemURL& url, | |
152 const EnsureFileExistsCallback& callback) { | |
153 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY, false); | |
154 return true; | |
155 } | |
156 | |
157 bool NativeMediaFileUtil::CreateDirectory( | |
158 FileSystemOperationContext* context, | |
159 const FileSystemURL& url, | |
160 bool exclusive, | |
161 bool recursive, | |
162 const StatusCallback& callback) { | |
163 return base::PostTaskAndReplyWithResult( | |
164 context->task_runner(), | |
165 FROM_HERE, | |
166 Bind(&NativeMediaFileUtil::CreateDirectorySync, Unretained(this), | |
167 context, url, exclusive, recursive), | |
168 Bind(&FinishStatusCallback, callback)); | |
vandebo (ex-Chrome)
2013/05/31 23:22:17
Just pass callback as the reply callback argument.
tommycli
2013/06/01 01:40:54
I tried to keep all the Sync methods purely synchr
| |
169 } | |
170 | |
171 bool NativeMediaFileUtil::GetFileInfo( | |
172 FileSystemOperationContext* context, | |
173 const FileSystemURL& url, | |
174 const GetFileInfoCallback& callback) { | |
175 PlatformFileInfo* file_info = new PlatformFileInfo(); | |
176 base::FilePath* platform_path = new base::FilePath(); | |
177 return base::PostTaskAndReplyWithResult( | |
vandebo (ex-Chrome)
2013/05/31 23:22:17
This will be cleaner if you just pass the callback
tommycli
2013/06/01 01:40:54
Done.
| |
178 context->task_runner(), | |
179 FROM_HERE, | |
180 Bind(&NativeMediaFileUtil::GetFileInfoSync, Unretained(this), | |
181 context, url, file_info, platform_path), | |
182 Bind(&FinishGetFileInfo, callback, Owned(file_info), | |
183 Owned(platform_path))); | |
184 } | |
185 | |
186 bool NativeMediaFileUtil::ReadDirectory( | |
187 FileSystemOperationContext* context, | |
188 const FileSystemURL& url, | |
189 const ReadDirectoryCallback& callback) { | |
190 EntryList* entry_list = new EntryList(); | |
191 return base::PostTaskAndReplyWithResult( | |
192 context->task_runner(), | |
193 FROM_HERE, | |
194 Bind(&NativeMediaFileUtil::ReadDirectorySync, Unretained(this), | |
195 context, url, entry_list), | |
196 Bind(&FinishReadDirectory, callback, Owned(entry_list))); | |
197 } | |
198 | |
199 bool NativeMediaFileUtil::Touch( | |
200 FileSystemOperationContext* context, | |
201 const FileSystemURL& url, | |
202 const base::Time& last_access_time, | |
203 const base::Time& last_modified_time, | |
204 const StatusCallback& callback) { | |
205 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); | |
206 return true; | |
207 } | |
208 | |
209 bool NativeMediaFileUtil::Truncate( | |
210 FileSystemOperationContext* context, | |
211 const FileSystemURL& url, | |
212 int64 length, | |
213 const StatusCallback& callback) { | |
214 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); | |
215 return true; | |
216 } | |
217 | |
218 bool NativeMediaFileUtil::CopyFileLocal( | |
219 FileSystemOperationContext* context, | |
220 const FileSystemURL& src_url, | |
221 const FileSystemURL& dest_url, | |
222 const StatusCallback& callback) { | |
223 return base::PostTaskAndReplyWithResult( | |
224 context->task_runner(), | |
225 FROM_HERE, | |
226 Bind(&NativeMediaFileUtil::CopyOrMoveFileSync, Unretained(this), | |
227 context, src_url, dest_url, true /* copy */), | |
228 Bind(&FinishStatusCallback, callback)); | |
229 } | |
230 | |
231 bool NativeMediaFileUtil::MoveFileLocal( | |
232 FileSystemOperationContext* context, | |
233 const FileSystemURL& src_url, | |
234 const FileSystemURL& dest_url, | |
235 const StatusCallback& callback) { | |
236 return base::PostTaskAndReplyWithResult( | |
237 context->task_runner(), | |
238 FROM_HERE, | |
239 Bind(&NativeMediaFileUtil::CopyOrMoveFileSync, Unretained(this), | |
240 context, src_url, dest_url, false /* copy */), | |
241 Bind(&FinishStatusCallback, callback)); | |
242 } | |
243 | |
244 bool NativeMediaFileUtil::CopyInForeignFile( | |
245 FileSystemOperationContext* context, | |
246 const base::FilePath& src_file_path, | |
247 const FileSystemURL& dest_url, | |
248 const StatusCallback& callback) { | |
249 return base::PostTaskAndReplyWithResult( | |
250 context->task_runner(), | |
251 FROM_HERE, | |
252 Bind(&NativeMediaFileUtil::CopyInForeignFileSync, Unretained(this), | |
253 context, src_file_path, dest_url), | |
254 Bind(&FinishStatusCallback, callback)); | |
255 } | |
256 | |
257 bool NativeMediaFileUtil::DeleteFile( | |
258 FileSystemOperationContext* context, | |
259 const FileSystemURL& url, | |
260 const StatusCallback& callback) { | |
261 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); | |
262 return true; | |
263 } | |
264 | |
265 bool NativeMediaFileUtil::DeleteDirectory( | |
266 FileSystemOperationContext* context, | |
267 const FileSystemURL& url, | |
268 const StatusCallback& callback) { | |
269 return base::PostTaskAndReplyWithResult( | |
270 context->task_runner(), | |
271 FROM_HERE, | |
272 Bind(&NativeMediaFileUtil::DeleteDirectorySync, Unretained(this), | |
273 context, url), | |
274 Bind(&FinishStatusCallback, callback)); | |
275 } | |
276 | |
277 bool NativeMediaFileUtil::CreateSnapshotFile( | |
278 FileSystemOperationContext* context, | |
279 const FileSystemURL& url, | |
280 const CreateSnapshotFileCallback& callback) { | |
281 PlatformFileInfo* file_info = new PlatformFileInfo(); | |
282 base::FilePath* platform_path = new base::FilePath(); | |
283 scoped_refptr<webkit_blob::ShareableFileReference>* file_ref = | |
284 new scoped_refptr<webkit_blob::ShareableFileReference>(); | |
285 return base::PostTaskAndReplyWithResult( | |
286 context->task_runner(), | |
287 FROM_HERE, | |
288 Bind(&NativeMediaFileUtil::CreateSnapshotFileSync, Unretained(this), | |
289 context, url, file_info, platform_path, file_ref), | |
290 Bind(&FinishCreateSnapshotFile, callback, Owned(file_info), | |
291 Owned(platform_path), Owned(file_ref))); | |
292 } | |
293 | |
294 PlatformFileError NativeMediaFileUtil::CreateDirectorySync( | |
295 FileSystemOperationContext* context, | |
296 const FileSystemURL& url, | |
297 bool exclusive, | |
298 bool recursive) { | |
70 base::FilePath file_path; | 299 base::FilePath file_path; |
71 PlatformFileError error = GetFilteredLocalFilePath(context, url, &file_path); | 300 PlatformFileError error = GetLocalFilePath(context, url, &file_path); |
72 if (error != base::PLATFORM_FILE_OK) | 301 if (error != base::PLATFORM_FILE_OK) |
73 return error; | 302 return error; |
74 return NativeFileUtil::EnsureFileExists(file_path, created); | 303 return NativeFileUtil::CreateDirectory(file_path, exclusive, recursive); |
75 } | 304 } |
76 | 305 |
77 scoped_ptr<fileapi::FileSystemFileUtil::AbstractFileEnumerator> | 306 PlatformFileError NativeMediaFileUtil::GetLocalFilePath( |
78 NativeMediaFileUtil::CreateFileEnumerator( | 307 FileSystemOperationContext* context, |
79 FileSystemOperationContext* context, | 308 const FileSystemURL& url, |
80 const FileSystemURL& root_url) { | 309 base::FilePath* local_file_path) { |
81 DCHECK(context); | 310 DCHECK(local_file_path); |
82 return make_scoped_ptr(new FilteringFileEnumerator( | 311 DCHECK(url.is_valid()); |
83 IsolatedFileUtil::CreateFileEnumerator(context, root_url), | 312 if (url.path().empty()) { |
84 GetMediaPathFilter(context))) | 313 // Root direcory case, which should not be accessed. |
85 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>(); | 314 return base::PLATFORM_FILE_ERROR_ACCESS_DENIED; |
86 } | 315 } |
87 | 316 *local_file_path = url.path(); |
88 PlatformFileError NativeMediaFileUtil::Touch( | 317 return base::PLATFORM_FILE_OK; |
89 FileSystemOperationContext* context, | 318 } |
90 const FileSystemURL& url, | 319 |
91 const base::Time& last_access_time, | 320 PlatformFileError NativeMediaFileUtil::ReadDirectorySync( |
92 const base::Time& last_modified_time) { | 321 FileSystemOperationContext* context, |
93 base::FilePath file_path; | 322 const FileSystemURL& url, |
94 PlatformFileError error = GetFilteredLocalFilePathForExistingFileOrDirectory( | 323 EntryList* file_list) { |
95 context, | 324 DCHECK(file_list); |
96 url, | 325 DCHECK(file_list->empty()); |
97 // Touch fails for non-existent paths and filtered paths. | 326 base::PlatformFileInfo file_info; |
98 base::PLATFORM_FILE_ERROR_FAILED, | 327 base::FilePath platform_path; |
99 &file_path); | 328 PlatformFileError error = GetFileInfoSync(context, url, &file_info, |
329 &platform_path); | |
330 | |
331 if (error == base::PLATFORM_FILE_OK && !file_info.is_directory) | |
332 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; | |
333 | |
100 if (error != base::PLATFORM_FILE_OK) | 334 if (error != base::PLATFORM_FILE_OK) |
101 return error; | 335 return error; |
102 return NativeFileUtil::Touch(file_path, last_access_time, last_modified_time); | 336 |
103 } | 337 file_util::FileEnumerator file_enum( |
104 | 338 platform_path, |
105 PlatformFileError NativeMediaFileUtil::Truncate( | 339 false /* recursive */, |
106 FileSystemOperationContext* context, | 340 file_util::FileEnumerator::FILES | |
107 const FileSystemURL& url, | 341 file_util::FileEnumerator::DIRECTORIES); |
108 int64 length) { | 342 file_util::FileEnumerator::FindInfo file_util_info; |
109 base::FilePath file_path; | 343 #if defined(OS_WIN) |
110 PlatformFileError error = GetFilteredLocalFilePathForExistingFileOrDirectory( | 344 memset(&file_util_info, 0, sizeof(file_util_info)); |
111 context, | 345 #endif // defined(OS_WIN) |
112 url, | 346 |
113 // Cannot truncate paths that do not exist, or are filtered. | 347 for (base::FilePath platform_path = file_enum.Next(); |
114 base::PLATFORM_FILE_ERROR_NOT_FOUND, | 348 !platform_path.empty(); |
115 &file_path); | 349 platform_path = file_enum.Next()) { |
116 if (error != base::PLATFORM_FILE_OK) | 350 // Skip symlinks. |
117 return error; | 351 if (file_util::IsLink(platform_path)) |
118 return NativeFileUtil::Truncate(file_path, length); | 352 continue; |
119 } | 353 |
120 | 354 file_enum.GetFindInfo(&file_util_info); |
121 PlatformFileError NativeMediaFileUtil::CopyOrMoveFile( | 355 |
356 // NativeMediaFileUtil skip criteria. | |
357 if (ShouldSkip(platform_path)) | |
358 continue; | |
359 if (!file_util::FileEnumerator::IsDirectory(file_util_info) && | |
360 !GetMediaPathFilter(context)->Match(platform_path)) | |
361 continue; | |
362 | |
363 fileapi::DirectoryEntry entry; | |
364 entry.is_directory = file_util::FileEnumerator::IsDirectory(file_util_info); | |
365 entry.name = platform_path.BaseName().value(); | |
366 entry.size = file_util::FileEnumerator::GetFilesize(file_util_info); | |
367 entry.last_modified_time = | |
368 file_util::FileEnumerator::GetLastModifiedTime(file_util_info); | |
369 | |
370 file_list->push_back(entry); | |
371 } | |
372 | |
373 return base::PLATFORM_FILE_OK; | |
374 } | |
375 | |
376 PlatformFileError NativeMediaFileUtil::CopyOrMoveFileSync( | |
122 FileSystemOperationContext* context, | 377 FileSystemOperationContext* context, |
123 const FileSystemURL& src_url, | 378 const FileSystemURL& src_url, |
124 const FileSystemURL& dest_url, | 379 const FileSystemURL& dest_url, |
125 bool copy) { | 380 bool copy) { |
126 base::FilePath src_file_path; | 381 base::FilePath src_file_path; |
127 PlatformFileError error = | 382 PlatformFileError error = |
128 GetFilteredLocalFilePathForExistingFileOrDirectory( | 383 GetFilteredLocalFilePathForExistingFileOrDirectory( |
129 context, src_url, | 384 context, src_url, |
130 base::PLATFORM_FILE_ERROR_NOT_FOUND, | 385 base::PLATFORM_FILE_ERROR_NOT_FOUND, |
131 &src_file_path); | 386 &src_file_path); |
(...skipping 12 matching lines...) Expand all Loading... | |
144 error != base::PLATFORM_FILE_ERROR_NOT_FOUND) | 399 error != base::PLATFORM_FILE_ERROR_NOT_FOUND) |
145 return error; | 400 return error; |
146 if (error == base::PLATFORM_FILE_OK && file_info.is_directory) | 401 if (error == base::PLATFORM_FILE_OK && file_info.is_directory) |
147 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 402 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
148 if (!GetMediaPathFilter(context)->Match(dest_file_path)) | 403 if (!GetMediaPathFilter(context)->Match(dest_file_path)) |
149 return base::PLATFORM_FILE_ERROR_SECURITY; | 404 return base::PLATFORM_FILE_ERROR_SECURITY; |
150 | 405 |
151 return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, copy); | 406 return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, copy); |
152 } | 407 } |
153 | 408 |
154 PlatformFileError NativeMediaFileUtil::CopyInForeignFile( | 409 PlatformFileError NativeMediaFileUtil::CopyInForeignFileSync( |
155 FileSystemOperationContext* context, | 410 FileSystemOperationContext* context, |
156 const base::FilePath& src_file_path, | 411 const base::FilePath& src_file_path, |
157 const FileSystemURL& dest_url) { | 412 const FileSystemURL& dest_url) { |
158 if (src_file_path.empty()) | 413 if (src_file_path.empty()) |
159 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 414 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
160 | 415 |
161 base::FilePath dest_file_path; | 416 base::FilePath dest_file_path; |
162 PlatformFileError error = | 417 PlatformFileError error = |
163 GetFilteredLocalFilePath(context, dest_url, &dest_file_path); | 418 GetFilteredLocalFilePath(context, dest_url, &dest_file_path); |
164 if (error != base::PLATFORM_FILE_OK) | 419 if (error != base::PLATFORM_FILE_OK) |
165 return error; | 420 return error; |
166 return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, true); | 421 return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, true); |
167 } | 422 } |
168 | 423 |
169 PlatformFileError NativeMediaFileUtil::DeleteFile( | 424 PlatformFileError NativeMediaFileUtil::GetFilteredLocalFilePath( |
170 FileSystemOperationContext* context, | 425 FileSystemOperationContext* context, |
171 const FileSystemURL& url) { | 426 const FileSystemURL& file_system_url, |
427 base::FilePath* local_file_path) { | |
172 base::FilePath file_path; | 428 base::FilePath file_path; |
173 PlatformFileError error = GetLocalFilePath(context, url, &file_path); | 429 PlatformFileError error = |
430 GetLocalFilePath(context, file_system_url, &file_path); | |
174 if (error != base::PLATFORM_FILE_OK) | 431 if (error != base::PLATFORM_FILE_OK) |
175 return error; | 432 return error; |
176 PlatformFileInfo file_info; | |
177 error = NativeFileUtil::GetFileInfo(file_path, &file_info); | |
178 if (error != base::PLATFORM_FILE_OK) | |
179 return error; | |
180 if (file_info.is_directory) | |
181 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; | |
182 if (!GetMediaPathFilter(context)->Match(file_path)) | 433 if (!GetMediaPathFilter(context)->Match(file_path)) |
183 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 434 return base::PLATFORM_FILE_ERROR_SECURITY; |
184 return NativeFileUtil::DeleteFile(file_path); | 435 |
436 *local_file_path = file_path; | |
437 return base::PLATFORM_FILE_OK; | |
185 } | 438 } |
186 | 439 |
187 PlatformFileError NativeMediaFileUtil::GetFileInfo( | 440 PlatformFileError NativeMediaFileUtil::GetFileInfoSync( |
188 FileSystemOperationContext* context, | 441 FileSystemOperationContext* context, |
189 const FileSystemURL& url, | 442 const FileSystemURL& url, |
190 PlatformFileInfo* file_info, | 443 PlatformFileInfo* file_info, |
191 base::FilePath* platform_path) { | 444 base::FilePath* platform_path) { |
192 DCHECK(context); | 445 DCHECK(context); |
193 DCHECK(GetMediaPathFilter(context)); | |
194 DCHECK(file_info); | 446 DCHECK(file_info); |
195 DCHECK(platform_path); | 447 DCHECK(platform_path); |
448 DCHECK(GetMediaPathFilter(context)); | |
196 | 449 |
197 base::PlatformFileError error = | 450 base::FilePath file_path; |
198 IsolatedFileUtil::GetFileInfo(context, url, file_info, platform_path); | 451 PlatformFileError error = GetLocalFilePath(context, url, &file_path); |
452 if (error != base::PLATFORM_FILE_OK) | |
453 return error; | |
454 if (file_util::IsLink(file_path)) | |
455 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
456 error = NativeFileUtil::GetFileInfo(file_path, file_info); | |
199 if (error != base::PLATFORM_FILE_OK) | 457 if (error != base::PLATFORM_FILE_OK) |
200 return error; | 458 return error; |
201 | 459 |
460 *platform_path = file_path; | |
vandebo (ex-Chrome)
2013/05/31 23:22:17
if (platform_path) and don't pass platform_path if
tommycli
2013/06/01 01:40:54
Done.
| |
202 if (file_info->is_directory || | 461 if (file_info->is_directory || |
203 GetMediaPathFilter(context)->Match(*platform_path)) { | 462 GetMediaPathFilter(context)->Match(*platform_path)) { |
204 return base::PLATFORM_FILE_OK; | 463 return base::PLATFORM_FILE_OK; |
205 } | 464 } |
206 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 465 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
207 } | 466 } |
208 | 467 |
209 PlatformFileError NativeMediaFileUtil::GetFilteredLocalFilePath( | |
210 FileSystemOperationContext* context, | |
211 const FileSystemURL& file_system_url, | |
212 base::FilePath* local_file_path) { | |
213 base::FilePath file_path; | |
214 PlatformFileError error = | |
215 IsolatedFileUtil::GetLocalFilePath(context, file_system_url, &file_path); | |
216 if (error != base::PLATFORM_FILE_OK) | |
217 return error; | |
218 if (!GetMediaPathFilter(context)->Match(file_path)) | |
219 return base::PLATFORM_FILE_ERROR_SECURITY; | |
220 | |
221 *local_file_path = file_path; | |
222 return base::PLATFORM_FILE_OK; | |
223 } | |
224 | |
225 PlatformFileError | 468 PlatformFileError |
226 NativeMediaFileUtil::GetFilteredLocalFilePathForExistingFileOrDirectory( | 469 NativeMediaFileUtil::GetFilteredLocalFilePathForExistingFileOrDirectory( |
227 FileSystemOperationContext* context, | 470 FileSystemOperationContext* context, |
228 const FileSystemURL& file_system_url, | 471 const FileSystemURL& file_system_url, |
229 PlatformFileError failure_error, | 472 PlatformFileError failure_error, |
230 base::FilePath* local_file_path) { | 473 base::FilePath* local_file_path) { |
231 base::FilePath file_path; | 474 base::FilePath file_path; |
232 PlatformFileError error = | 475 PlatformFileError error = |
233 GetLocalFilePath(context, file_system_url, &file_path); | 476 GetLocalFilePath(context, file_system_url, &file_path); |
234 if (error != base::PLATFORM_FILE_OK) | 477 if (error != base::PLATFORM_FILE_OK) |
235 return error; | 478 return error; |
236 | 479 |
237 if (!file_util::PathExists(file_path)) | 480 if (!file_util::PathExists(file_path)) |
238 return failure_error; | 481 return failure_error; |
239 PlatformFileInfo file_info; | 482 PlatformFileInfo file_info; |
240 if (!file_util::GetFileInfo(file_path, &file_info)) | 483 if (!file_util::GetFileInfo(file_path, &file_info)) |
241 return base::PLATFORM_FILE_ERROR_FAILED; | 484 return base::PLATFORM_FILE_ERROR_FAILED; |
242 | 485 |
243 if (!file_info.is_directory && | 486 if (!file_info.is_directory && |
244 !GetMediaPathFilter(context)->Match(file_path)) { | 487 !GetMediaPathFilter(context)->Match(file_path)) { |
245 return failure_error; | 488 return failure_error; |
246 } | 489 } |
247 | 490 |
248 *local_file_path = file_path; | 491 *local_file_path = file_path; |
249 return base::PLATFORM_FILE_OK; | 492 return base::PLATFORM_FILE_OK; |
250 } | 493 } |
251 | 494 |
252 webkit_blob::ScopedFile NativeMediaFileUtil::CreateSnapshotFile( | 495 PlatformFileError NativeMediaFileUtil::DeleteDirectorySync( |
253 fileapi::FileSystemOperationContext* context, | 496 FileSystemOperationContext* context, |
497 const FileSystemURL& url) { | |
498 base::FilePath file_path; | |
499 PlatformFileError error = GetLocalFilePath(context, url, &file_path); | |
500 if (error != base::PLATFORM_FILE_OK) | |
501 return error; | |
502 return NativeFileUtil::DeleteDirectory(file_path); | |
503 } | |
504 | |
505 PlatformFileError NativeMediaFileUtil::CreateSnapshotFileSync( | |
506 FileSystemOperationContext* context, | |
254 const fileapi::FileSystemURL& url, | 507 const fileapi::FileSystemURL& url, |
255 base::PlatformFileError* error, | 508 PlatformFileInfo* file_info, |
256 base::PlatformFileInfo* file_info, | 509 base::FilePath* platform_path, |
257 base::FilePath* platform_path) { | 510 scoped_refptr<webkit_blob::ShareableFileReference>* file_ref) { |
258 DCHECK(IsOnTaskRunnerThread(context)); | 511 PlatformFileError error = |
259 webkit_blob::ScopedFile file; | 512 GetFileInfoSync(context, url, file_info, platform_path); |
260 file = IsolatedFileUtil::CreateSnapshotFile( | 513 if (error == base::PLATFORM_FILE_OK && file_info->is_directory) |
261 context, url, error, file_info, platform_path); | 514 error = base::PLATFORM_FILE_ERROR_NOT_A_FILE; |
262 if (*error != base::PLATFORM_FILE_OK) | 515 if (error == base::PLATFORM_FILE_OK) |
263 return file.Pass(); | 516 error = NativeMediaFileUtil::IsMediaFile(*platform_path); |
264 *error = IsMediaFile(*platform_path); | 517 |
265 if (*error == base::PLATFORM_FILE_OK) | 518 // We're just returning the local file information. |
266 return file.Pass(); | 519 *file_ref = scoped_refptr<webkit_blob::ShareableFileReference>(); |
267 return webkit_blob::ScopedFile(); | 520 |
521 return error; | |
268 } | 522 } |
269 | 523 |
270 // static | 524 // static |
271 base::PlatformFileError NativeMediaFileUtil::IsMediaFile( | 525 PlatformFileError NativeMediaFileUtil::IsMediaFile( |
272 const base::FilePath& path) { | 526 const base::FilePath& path) { |
273 base::PlatformFile file_handle; | 527 base::PlatformFile file_handle; |
274 const int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; | 528 const int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; |
275 base::PlatformFileError error = | 529 PlatformFileError error = |
276 NativeFileUtil::CreateOrOpen(path, flags, &file_handle, NULL); | 530 NativeFileUtil::CreateOrOpen(path, flags, &file_handle, NULL); |
277 if (error != base::PLATFORM_FILE_OK) | 531 if (error != base::PLATFORM_FILE_OK) |
278 return error; | 532 return error; |
279 | 533 |
280 ScopedPlatformFile scoped_platform_file(&file_handle); | 534 ScopedPlatformFile scoped_platform_file(&file_handle); |
281 char buffer[net::kMaxBytesToSniff]; | 535 char buffer[net::kMaxBytesToSniff]; |
282 | 536 |
283 // Read as much as net::SniffMimeTypeFromLocalData() will bother looking at. | 537 // Read as much as net::SniffMimeTypeFromLocalData() will bother looking at. |
284 int64 len = | 538 int64 len = |
285 base::ReadPlatformFile(file_handle, 0, buffer, net::kMaxBytesToSniff); | 539 base::ReadPlatformFile(file_handle, 0, buffer, net::kMaxBytesToSniff); |
286 if (len < 0) | 540 if (len < 0) |
287 return base::PLATFORM_FILE_ERROR_FAILED; | 541 return base::PLATFORM_FILE_ERROR_FAILED; |
288 if (len == 0) | 542 if (len == 0) |
289 return base::PLATFORM_FILE_ERROR_SECURITY; | 543 return base::PLATFORM_FILE_ERROR_SECURITY; |
290 | 544 |
291 std::string mime_type; | 545 std::string mime_type; |
292 if (!net::SniffMimeTypeFromLocalData(buffer, len, &mime_type)) | 546 if (!net::SniffMimeTypeFromLocalData(buffer, len, &mime_type)) |
293 return base::PLATFORM_FILE_ERROR_SECURITY; | 547 return base::PLATFORM_FILE_ERROR_SECURITY; |
294 | 548 |
295 if (StartsWithASCII(mime_type, "image/", true) || | 549 if (StartsWithASCII(mime_type, "image/", true) || |
296 StartsWithASCII(mime_type, "audio/", true) || | 550 StartsWithASCII(mime_type, "audio/", true) || |
297 StartsWithASCII(mime_type, "video/", true) || | 551 StartsWithASCII(mime_type, "video/", true) || |
298 mime_type == "application/x-shockwave-flash") { | 552 mime_type == "application/x-shockwave-flash") { |
299 return base::PLATFORM_FILE_OK; | 553 return base::PLATFORM_FILE_OK; |
300 } | 554 } |
301 return base::PLATFORM_FILE_ERROR_SECURITY; | 555 return base::PLATFORM_FILE_ERROR_SECURITY; |
302 } | 556 } |
303 | 557 |
304 } // namespace chrome | 558 } // namespace chrome |
OLD | NEW |