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