Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(441)

Side by Side Diff: chrome/browser/media_galleries/fileapi/native_media_file_util.cc

Issue 16304002: Make NativeMediaFileUtil an AsyncFileUtil (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Address my comments Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY,
66 107 base::PassPlatformFile(&invalid_file),
67 PlatformFileError NativeMediaFileUtil::EnsureFileExists( 108 false);
68 FileSystemOperationContext* context, 109 return true;
69 const FileSystemURL& url, bool* created) { 110 }
111
112 bool NativeMediaFileUtil::EnsureFileExists(
113 fileapi::FileSystemOperationContext* context,
114 const fileapi::FileSystemURL& url,
115 const EnsureFileExistsCallback& callback) {
116 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
117 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY, false);
Lei Zhang 2013/06/03 10:05:46 You need to check if the callback is null. if (!c
vandebo (ex-Chrome) 2013/06/03 19:15:26 Done.
118 return true;
119 }
120
121 bool NativeMediaFileUtil::CreateDirectory(
122 fileapi::FileSystemOperationContext* context,
123 const fileapi::FileSystemURL& url,
124 bool exclusive,
125 bool recursive,
126 const StatusCallback& callback) {
127 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
128 return context->task_runner()->PostTask(
129 FROM_HERE,
130 base::Bind(&NativeMediaFileUtil::CreateDirectoryOnTaskRunnerThread,
131 weak_factory_.GetWeakPtr(), context, url, exclusive,
132 recursive, callback));
133 }
134
135 bool NativeMediaFileUtil::GetFileInfo(
136 fileapi::FileSystemOperationContext* context,
137 const fileapi::FileSystemURL& url,
138 const GetFileInfoCallback& callback) {
139 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
140 return context->task_runner()->PostTask(
141 FROM_HERE,
142 base::Bind(&NativeMediaFileUtil::GetFileInfoOnTaskRunnerThread,
143 weak_factory_.GetWeakPtr(), context, url, callback));
144 }
145
146 bool NativeMediaFileUtil::ReadDirectory(
147 fileapi::FileSystemOperationContext* context,
148 const fileapi::FileSystemURL& url,
149 const ReadDirectoryCallback& callback) {
150 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
151 return context->task_runner()->PostTask(
152 FROM_HERE,
153 base::Bind(&NativeMediaFileUtil::ReadDirectoryOnTaskRunnerThread,
154 weak_factory_.GetWeakPtr(), context, url, callback));
155 }
156
157 bool NativeMediaFileUtil::Touch(
158 fileapi::FileSystemOperationContext* context,
159 const fileapi::FileSystemURL& url,
160 const base::Time& last_access_time,
161 const base::Time& last_modified_time,
162 const StatusCallback& callback) {
163 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
164 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY);
165 return true;
166 }
167
168 bool NativeMediaFileUtil::Truncate(
169 fileapi::FileSystemOperationContext* context,
170 const fileapi::FileSystemURL& url,
171 int64 length,
172 const StatusCallback& callback) {
173 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
174 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY);
175 return true;
176 }
177
178 bool NativeMediaFileUtil::CopyFileLocal(
179 fileapi::FileSystemOperationContext* context,
180 const fileapi::FileSystemURL& src_url,
181 const fileapi::FileSystemURL& dest_url,
182 const StatusCallback& callback) {
183 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
184 return context->task_runner()->PostTask(
185 FROM_HERE,
186 base::Bind(&NativeMediaFileUtil::CopyOrMoveFileLocalOnTaskRunnerThread,
187 weak_factory_.GetWeakPtr(), context, src_url, dest_url,
188 true /* copy */, callback));
189 }
190
191 bool NativeMediaFileUtil::MoveFileLocal(
192 fileapi::FileSystemOperationContext* context,
193 const fileapi::FileSystemURL& src_url,
194 const fileapi::FileSystemURL& dest_url,
195 const StatusCallback& callback) {
196 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
197 return context->task_runner()->PostTask(
198 FROM_HERE,
199 base::Bind(&NativeMediaFileUtil::CopyOrMoveFileLocalOnTaskRunnerThread,
200 weak_factory_.GetWeakPtr(), context, src_url, dest_url,
201 false /* copy */, callback));
202 }
203
204 bool NativeMediaFileUtil::CopyInForeignFile(
205 fileapi::FileSystemOperationContext* context,
206 const base::FilePath& src_file_path,
207 const fileapi::FileSystemURL& dest_url,
208 const StatusCallback& callback) {
209 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
210 return context->task_runner()->PostTask(
211 FROM_HERE,
212 base::Bind(&NativeMediaFileUtil::CopyInForeignFileOnTaskRunnerThread,
213 weak_factory_.GetWeakPtr(), context, src_file_path, dest_url,
214 callback));
215 }
216
217 bool NativeMediaFileUtil::DeleteFile(
Lei Zhang 2013/06/03 10:05:46 Seems weird that we can delete directories, but no
vandebo (ex-Chrome) 2013/06/03 19:15:26 I assume this if for Move - we need to recursively
218 fileapi::FileSystemOperationContext* context,
219 const fileapi::FileSystemURL& url,
220 const StatusCallback& callback) {
221 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
222 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY);
223 return true;
224 }
225
226 bool NativeMediaFileUtil::DeleteDirectory(
227 fileapi::FileSystemOperationContext* context,
228 const fileapi::FileSystemURL& url,
229 const StatusCallback& callback) {
230 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
231 return context->task_runner()->PostTask(
232 FROM_HERE,
233 base::Bind(&NativeMediaFileUtil::DeleteDirectoryOnTaskRunnerThread,
234 weak_factory_.GetWeakPtr(), context, url, callback));
235 }
236
237 bool NativeMediaFileUtil::CreateSnapshotFile(
238 fileapi::FileSystemOperationContext* context,
239 const fileapi::FileSystemURL& url,
240 const CreateSnapshotFileCallback& callback) {
241 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
242 return context->task_runner()->PostTask(
243 FROM_HERE,
244 base::Bind(&NativeMediaFileUtil::CreateSnapshotFileOnTaskRunnerThread,
245 weak_factory_.GetWeakPtr(), context, url, callback));
246 }
247
248 base::PlatformFileError NativeMediaFileUtil::CreateDirectorySync(
249 fileapi::FileSystemOperationContext* context,
250 const fileapi::FileSystemURL& url,
251 bool exclusive,
252 bool recursive) {
70 base::FilePath file_path; 253 base::FilePath file_path;
71 PlatformFileError error = GetFilteredLocalFilePath(context, url, &file_path); 254 base::PlatformFileError error = GetLocalFilePath(context, url, &file_path);
72 if (error != base::PLATFORM_FILE_OK) 255 if (error != base::PLATFORM_FILE_OK)
73 return error; 256 return error;
74 return NativeFileUtil::EnsureFileExists(file_path, created); 257 return fileapi::NativeFileUtil::CreateDirectory(file_path, exclusive,
75 } 258 recursive);
76 259 }
77 scoped_ptr<fileapi::FileSystemFileUtil::AbstractFileEnumerator> 260
78 NativeMediaFileUtil::CreateFileEnumerator( 261 base::PlatformFileError NativeMediaFileUtil::GetLocalFilePath(
Lei Zhang 2013/06/03 10:05:46 This is identical to IsolatedFileUtil::GetLocalFil
vandebo (ex-Chrome) 2013/06/03 19:15:26 It's not static and is protected, so there isn't a
79 FileSystemOperationContext* context, 262 fileapi::FileSystemOperationContext* context,
80 const FileSystemURL& root_url) { 263 const fileapi::FileSystemURL& url,
81 DCHECK(context); 264 base::FilePath* local_file_path) {
82 return make_scoped_ptr(new FilteringFileEnumerator( 265 DCHECK(local_file_path);
83 IsolatedFileUtil::CreateFileEnumerator(context, root_url), 266 DCHECK(url.is_valid());
84 GetMediaPathFilter(context))) 267 if (url.path().empty()) {
85 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>(); 268 // Root direcory case, which should not be accessed.
86 } 269 return base::PLATFORM_FILE_ERROR_ACCESS_DENIED;
87 270 }
88 PlatformFileError NativeMediaFileUtil::Touch( 271 *local_file_path = url.path();
89 FileSystemOperationContext* context, 272 return base::PLATFORM_FILE_OK;
90 const FileSystemURL& url, 273 }
91 const base::Time& last_access_time, 274
92 const base::Time& last_modified_time) { 275 base::PlatformFileError NativeMediaFileUtil::ReadDirectorySync(
93 base::FilePath file_path; 276 fileapi::FileSystemOperationContext* context,
94 PlatformFileError error = GetFilteredLocalFilePathForExistingFileOrDirectory( 277 const fileapi::FileSystemURL& url,
95 context, 278 EntryList* file_list) {
96 url, 279 DCHECK(IsOnTaskRunnerThread(context));
97 // Touch fails for non-existent paths and filtered paths. 280 DCHECK(file_list);
98 base::PLATFORM_FILE_ERROR_FAILED, 281 DCHECK(file_list->empty());
99 &file_path); 282 base::PlatformFileInfo file_info;
283 base::FilePath platform_path;
284 base::PlatformFileError error = GetFileInfoSync(context, url, &file_info,
285 &platform_path);
286
287 if (error == base::PLATFORM_FILE_OK && !file_info.is_directory)
Lei Zhang 2013/06/03 10:05:46 do this after check on line 290
vandebo (ex-Chrome) 2013/06/03 19:15:26 Done.
288 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY;
289
100 if (error != base::PLATFORM_FILE_OK) 290 if (error != base::PLATFORM_FILE_OK)
101 return error; 291 return error;
102 return NativeFileUtil::Touch(file_path, last_access_time, last_modified_time); 292
103 } 293 file_util::FileEnumerator file_enum(
104 294 platform_path,
105 PlatformFileError NativeMediaFileUtil::Truncate( 295 false /* recursive */,
106 FileSystemOperationContext* context, 296 file_util::FileEnumerator::FILES |
107 const FileSystemURL& url, 297 file_util::FileEnumerator::DIRECTORIES);
108 int64 length) { 298 file_util::FileEnumerator::FindInfo file_util_info;
109 base::FilePath file_path; 299 #if defined(OS_WIN)
110 PlatformFileError error = GetFilteredLocalFilePathForExistingFileOrDirectory( 300 memset(&file_util_info, 0, sizeof(file_util_info));
111 context, 301 #endif // defined(OS_WIN)
112 url, 302
113 // Cannot truncate paths that do not exist, or are filtered. 303 for (base::FilePath platform_path = file_enum.Next();
114 base::PLATFORM_FILE_ERROR_NOT_FOUND, 304 !platform_path.empty();
115 &file_path); 305 platform_path = file_enum.Next()) {
116 if (error != base::PLATFORM_FILE_OK) 306 // Skip symlinks.
117 return error; 307 if (file_util::IsLink(platform_path))
118 return NativeFileUtil::Truncate(file_path, length); 308 continue;
119 } 309
120 310 file_enum.GetFindInfo(&file_util_info);
121 PlatformFileError NativeMediaFileUtil::CopyOrMoveFile( 311
122 FileSystemOperationContext* context, 312 // NativeMediaFileUtil skip criteria.
123 const FileSystemURL& src_url, 313 if (ShouldSkip(platform_path))
124 const FileSystemURL& dest_url, 314 continue;
315 if (!file_util::FileEnumerator::IsDirectory(file_util_info) &&
316 !GetMediaPathFilter(context)->Match(platform_path))
317 continue;
318
319 fileapi::DirectoryEntry entry;
320 entry.is_directory = file_util::FileEnumerator::IsDirectory(file_util_info);
321 entry.name = platform_path.BaseName().value();
322 entry.size = file_util::FileEnumerator::GetFilesize(file_util_info);
323 entry.last_modified_time =
324 file_util::FileEnumerator::GetLastModifiedTime(file_util_info);
325
326 file_list->push_back(entry);
327 }
328
329 return base::PLATFORM_FILE_OK;
330 }
331
332 base::PlatformFileError NativeMediaFileUtil::CopyOrMoveFileSync(
333 fileapi::FileSystemOperationContext* context,
334 const fileapi::FileSystemURL& src_url,
335 const fileapi::FileSystemURL& dest_url,
125 bool copy) { 336 bool copy) {
337 DCHECK(IsOnTaskRunnerThread(context));
126 base::FilePath src_file_path; 338 base::FilePath src_file_path;
127 PlatformFileError error = 339 base::PlatformFileError error =
128 GetFilteredLocalFilePathForExistingFileOrDirectory( 340 GetFilteredLocalFilePathForExistingFileOrDirectory(
129 context, src_url, 341 context, src_url,
130 base::PLATFORM_FILE_ERROR_NOT_FOUND, 342 base::PLATFORM_FILE_ERROR_NOT_FOUND,
131 &src_file_path); 343 &src_file_path);
132 if (error != base::PLATFORM_FILE_OK) 344 if (error != base::PLATFORM_FILE_OK)
133 return error; 345 return error;
134 if (NativeFileUtil::DirectoryExists(src_file_path)) 346 if (fileapi::NativeFileUtil::DirectoryExists(src_file_path))
135 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; 347 return base::PLATFORM_FILE_ERROR_NOT_A_FILE;
136 348
137 base::FilePath dest_file_path; 349 base::FilePath dest_file_path;
138 error = GetLocalFilePath(context, dest_url, &dest_file_path); 350 error = GetLocalFilePath(context, dest_url, &dest_file_path);
139 if (error != base::PLATFORM_FILE_OK) 351 if (error != base::PLATFORM_FILE_OK)
140 return error; 352 return error;
141 PlatformFileInfo file_info; 353 base::PlatformFileInfo file_info;
142 error = NativeFileUtil::GetFileInfo(dest_file_path, &file_info); 354 error = fileapi::NativeFileUtil::GetFileInfo(dest_file_path, &file_info);
143 if (error != base::PLATFORM_FILE_OK && 355 if (error != base::PLATFORM_FILE_OK &&
144 error != base::PLATFORM_FILE_ERROR_NOT_FOUND) 356 error != base::PLATFORM_FILE_ERROR_NOT_FOUND)
145 return error; 357 return error;
146 if (error == base::PLATFORM_FILE_OK && file_info.is_directory) 358 if (error == base::PLATFORM_FILE_OK && file_info.is_directory)
147 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; 359 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION;
148 if (!GetMediaPathFilter(context)->Match(dest_file_path)) 360 if (!GetMediaPathFilter(context)->Match(dest_file_path))
149 return base::PLATFORM_FILE_ERROR_SECURITY; 361 return base::PLATFORM_FILE_ERROR_SECURITY;
150 362
151 return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, copy); 363 return fileapi::NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path,
364 copy);
152 } 365 }
153 366
154 PlatformFileError NativeMediaFileUtil::CopyInForeignFile( 367 base::PlatformFileError NativeMediaFileUtil::CopyInForeignFileSync(
155 FileSystemOperationContext* context, 368 fileapi::FileSystemOperationContext* context,
156 const base::FilePath& src_file_path, 369 const base::FilePath& src_file_path,
157 const FileSystemURL& dest_url) { 370 const fileapi::FileSystemURL& dest_url) {
371 DCHECK(IsOnTaskRunnerThread(context));
158 if (src_file_path.empty()) 372 if (src_file_path.empty())
159 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; 373 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION;
160 374
161 base::FilePath dest_file_path; 375 base::FilePath dest_file_path;
162 PlatformFileError error = 376 base::PlatformFileError error =
163 GetFilteredLocalFilePath(context, dest_url, &dest_file_path); 377 GetFilteredLocalFilePath(context, dest_url, &dest_file_path);
164 if (error != base::PLATFORM_FILE_OK) 378 if (error != base::PLATFORM_FILE_OK)
165 return error; 379 return error;
166 return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, true); 380 return fileapi::NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path,
381 true);
167 } 382 }
168 383
169 PlatformFileError NativeMediaFileUtil::DeleteFile( 384 base::PlatformFileError NativeMediaFileUtil::GetFilteredLocalFilePath(
170 FileSystemOperationContext* context, 385 fileapi::FileSystemOperationContext* context,
171 const FileSystemURL& url) { 386 const fileapi::FileSystemURL& file_system_url,
387 base::FilePath* local_file_path) {
388 DCHECK(IsOnTaskRunnerThread(context));
172 base::FilePath file_path; 389 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 = 390 base::PlatformFileError error =
198 IsolatedFileUtil::GetFileInfo(context, url, file_info, platform_path); 391 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) 392 if (error != base::PLATFORM_FILE_OK)
217 return error; 393 return error;
218 if (!GetMediaPathFilter(context)->Match(file_path)) 394 if (!GetMediaPathFilter(context)->Match(file_path))
219 return base::PLATFORM_FILE_ERROR_SECURITY; 395 return base::PLATFORM_FILE_ERROR_SECURITY;
220 396
221 *local_file_path = file_path; 397 *local_file_path = file_path;
222 return base::PLATFORM_FILE_OK; 398 return base::PLATFORM_FILE_OK;
223 } 399 }
224 400
225 PlatformFileError 401 base::PlatformFileError NativeMediaFileUtil::GetFileInfoSync(
402 fileapi::FileSystemOperationContext* context,
403 const fileapi::FileSystemURL& url,
404 base::PlatformFileInfo* file_info,
405 base::FilePath* platform_path) {
406 DCHECK(IsOnTaskRunnerThread(context));
407 DCHECK(context);
408 DCHECK(file_info);
409 DCHECK(GetMediaPathFilter(context));
410
411 base::FilePath file_path;
412 base::PlatformFileError error = GetLocalFilePath(context, url, &file_path);
413 if (error != base::PLATFORM_FILE_OK)
414 return error;
415 if (file_util::IsLink(file_path))
416 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
417 error = fileapi::NativeFileUtil::GetFileInfo(file_path, file_info);
418 if (error != base::PLATFORM_FILE_OK)
419 return error;
420
421 if (platform_path)
422 *platform_path = file_path;
423 if (file_info->is_directory ||
424 GetMediaPathFilter(context)->Match(file_path)) {
425 return base::PLATFORM_FILE_OK;
426 }
427 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
428 }
429
430 base::PlatformFileError
226 NativeMediaFileUtil::GetFilteredLocalFilePathForExistingFileOrDirectory( 431 NativeMediaFileUtil::GetFilteredLocalFilePathForExistingFileOrDirectory(
227 FileSystemOperationContext* context, 432 fileapi::FileSystemOperationContext* context,
228 const FileSystemURL& file_system_url, 433 const fileapi::FileSystemURL& file_system_url,
229 PlatformFileError failure_error, 434 base::PlatformFileError failure_error,
230 base::FilePath* local_file_path) { 435 base::FilePath* local_file_path) {
436 DCHECK(IsOnTaskRunnerThread(context));
231 base::FilePath file_path; 437 base::FilePath file_path;
232 PlatformFileError error = 438 base::PlatformFileError error =
233 GetLocalFilePath(context, file_system_url, &file_path); 439 GetLocalFilePath(context, file_system_url, &file_path);
234 if (error != base::PLATFORM_FILE_OK) 440 if (error != base::PLATFORM_FILE_OK)
235 return error; 441 return error;
236 442
237 if (!file_util::PathExists(file_path)) 443 if (!file_util::PathExists(file_path))
238 return failure_error; 444 return failure_error;
239 PlatformFileInfo file_info; 445 base::PlatformFileInfo file_info;
240 if (!file_util::GetFileInfo(file_path, &file_info)) 446 if (!file_util::GetFileInfo(file_path, &file_info))
241 return base::PLATFORM_FILE_ERROR_FAILED; 447 return base::PLATFORM_FILE_ERROR_FAILED;
242 448
243 if (!file_info.is_directory && 449 if (!file_info.is_directory &&
244 !GetMediaPathFilter(context)->Match(file_path)) { 450 !GetMediaPathFilter(context)->Match(file_path)) {
245 return failure_error; 451 return failure_error;
246 } 452 }
247 453
248 *local_file_path = file_path; 454 *local_file_path = file_path;
249 return base::PLATFORM_FILE_OK; 455 return base::PLATFORM_FILE_OK;
250 } 456 }
251 457
252 webkit_blob::ScopedFile NativeMediaFileUtil::CreateSnapshotFile( 458 base::PlatformFileError NativeMediaFileUtil::DeleteDirectorySync(
459 fileapi::FileSystemOperationContext* context,
460 const fileapi::FileSystemURL& url) {
461 DCHECK(IsOnTaskRunnerThread(context));
462 base::FilePath file_path;
463 base::PlatformFileError error = GetLocalFilePath(context, url, &file_path);
464 if (error != base::PLATFORM_FILE_OK)
465 return error;
466 return fileapi::NativeFileUtil::DeleteDirectory(file_path);
467 }
468
469 base::PlatformFileError NativeMediaFileUtil::CreateSnapshotFileSync(
253 fileapi::FileSystemOperationContext* context, 470 fileapi::FileSystemOperationContext* context,
254 const fileapi::FileSystemURL& url, 471 const fileapi::FileSystemURL& url,
255 base::PlatformFileError* error,
256 base::PlatformFileInfo* file_info, 472 base::PlatformFileInfo* file_info,
257 base::FilePath* platform_path) { 473 base::FilePath* platform_path,
474 scoped_refptr<webkit_blob::ShareableFileReference>* file_ref) {
258 DCHECK(IsOnTaskRunnerThread(context)); 475 DCHECK(IsOnTaskRunnerThread(context));
259 webkit_blob::ScopedFile file; 476 base::PlatformFileError error =
260 file = IsolatedFileUtil::CreateSnapshotFile( 477 GetFileInfoSync(context, url, file_info, platform_path);
261 context, url, error, file_info, platform_path); 478 if (error == base::PLATFORM_FILE_OK && file_info->is_directory)
262 if (*error != base::PLATFORM_FILE_OK) 479 error = base::PLATFORM_FILE_ERROR_NOT_A_FILE;
263 return file.Pass(); 480 if (error == base::PLATFORM_FILE_OK)
264 *error = IsMediaFile(*platform_path); 481 error = NativeMediaFileUtil::IsMediaFile(*platform_path);
265 if (*error == base::PLATFORM_FILE_OK) 482
266 return file.Pass(); 483 // We're just returning the local file information.
267 return webkit_blob::ScopedFile(); 484 *file_ref = scoped_refptr<webkit_blob::ShareableFileReference>();
485
486 return error;
268 } 487 }
269 488
270 // static 489 // static
271 base::PlatformFileError NativeMediaFileUtil::IsMediaFile( 490 base::PlatformFileError NativeMediaFileUtil::IsMediaFile(
272 const base::FilePath& path) { 491 const base::FilePath& path) {
273 base::PlatformFile file_handle; 492 base::PlatformFile file_handle;
274 const int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; 493 const int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ;
275 base::PlatformFileError error = 494 base::PlatformFileError error =
276 NativeFileUtil::CreateOrOpen(path, flags, &file_handle, NULL); 495 fileapi::NativeFileUtil::CreateOrOpen(path, flags, &file_handle, NULL);
277 if (error != base::PLATFORM_FILE_OK) 496 if (error != base::PLATFORM_FILE_OK)
278 return error; 497 return error;
279 498
280 ScopedPlatformFile scoped_platform_file(&file_handle); 499 ScopedPlatformFile scoped_platform_file(&file_handle);
281 char buffer[net::kMaxBytesToSniff]; 500 char buffer[net::kMaxBytesToSniff];
282 501
283 // Read as much as net::SniffMimeTypeFromLocalData() will bother looking at. 502 // Read as much as net::SniffMimeTypeFromLocalData() will bother looking at.
284 int64 len = 503 int64 len =
285 base::ReadPlatformFile(file_handle, 0, buffer, net::kMaxBytesToSniff); 504 base::ReadPlatformFile(file_handle, 0, buffer, net::kMaxBytesToSniff);
286 if (len < 0) 505 if (len < 0)
287 return base::PLATFORM_FILE_ERROR_FAILED; 506 return base::PLATFORM_FILE_ERROR_FAILED;
288 if (len == 0) 507 if (len == 0)
289 return base::PLATFORM_FILE_ERROR_SECURITY; 508 return base::PLATFORM_FILE_ERROR_SECURITY;
290 509
291 std::string mime_type; 510 std::string mime_type;
292 if (!net::SniffMimeTypeFromLocalData(buffer, len, &mime_type)) 511 if (!net::SniffMimeTypeFromLocalData(buffer, len, &mime_type))
293 return base::PLATFORM_FILE_ERROR_SECURITY; 512 return base::PLATFORM_FILE_ERROR_SECURITY;
294 513
295 if (StartsWithASCII(mime_type, "image/", true) || 514 if (StartsWithASCII(mime_type, "image/", true) ||
296 StartsWithASCII(mime_type, "audio/", true) || 515 StartsWithASCII(mime_type, "audio/", true) ||
297 StartsWithASCII(mime_type, "video/", true) || 516 StartsWithASCII(mime_type, "video/", true) ||
298 mime_type == "application/x-shockwave-flash") { 517 mime_type == "application/x-shockwave-flash") {
299 return base::PLATFORM_FILE_OK; 518 return base::PLATFORM_FILE_OK;
300 } 519 }
301 return base::PLATFORM_FILE_ERROR_SECURITY; 520 return base::PLATFORM_FILE_ERROR_SECURITY;
302 } 521 }
303 522
523 void NativeMediaFileUtil::CreateDirectoryOnTaskRunnerThread(
524 fileapi::FileSystemOperationContext* context,
525 const fileapi::FileSystemURL& url,
526 bool exclusive,
527 bool recursive,
528 const StatusCallback& callback) {
529 DCHECK(IsOnTaskRunnerThread(context));
530 base::PlatformFileError error =
531 CreateDirectorySync(context, url, exclusive, recursive);
532 content::BrowserThread::PostTask(
533 content::BrowserThread::IO,
534 FROM_HERE,
535 base::Bind(callback, error));
536 }
537
538 void NativeMediaFileUtil::GetFileInfoOnTaskRunnerThread(
539 fileapi::FileSystemOperationContext* context,
540 const fileapi::FileSystemURL& url,
541 const GetFileInfoCallback& callback) {
542 DCHECK(IsOnTaskRunnerThread(context));
543 base::PlatformFileInfo file_info;
544 base::FilePath platform_path;
545 base::PlatformFileError error =
546 GetFileInfoSync(context, url, &file_info, &platform_path);
547 content::BrowserThread::PostTask(
548 content::BrowserThread::IO,
549 FROM_HERE,
550 base::Bind(callback, error, file_info, platform_path));
551 }
552
553 void NativeMediaFileUtil::ReadDirectoryOnTaskRunnerThread(
554 fileapi::FileSystemOperationContext* context,
555 const fileapi::FileSystemURL& url,
556 const ReadDirectoryCallback& callback) {
557 DCHECK(IsOnTaskRunnerThread(context));
558 EntryList entry_list;
559 base::PlatformFileError error =
560 ReadDirectorySync(context, url, &entry_list);
561 content::BrowserThread::PostTask(
562 content::BrowserThread::IO,
563 FROM_HERE,
564 base::Bind(callback, error, entry_list, false /* has_more */));
565 }
566
567 void NativeMediaFileUtil::CopyOrMoveFileLocalOnTaskRunnerThread(
568 fileapi::FileSystemOperationContext* context,
569 const fileapi::FileSystemURL& src_url,
570 const fileapi::FileSystemURL& dest_url,
571 bool copy,
572 const StatusCallback& callback) {
573 DCHECK(IsOnTaskRunnerThread(context));
574 base::PlatformFileError error =
575 CopyOrMoveFileSync(context, src_url, dest_url, copy);
576 content::BrowserThread::PostTask(
577 content::BrowserThread::IO,
578 FROM_HERE,
579 base::Bind(callback, error));
580 }
581
582 void NativeMediaFileUtil::CopyInForeignFileOnTaskRunnerThread(
583 fileapi::FileSystemOperationContext* context,
584 const base::FilePath& src_file_path,
585 const fileapi::FileSystemURL& dest_url,
586 const StatusCallback& callback) {
587 DCHECK(IsOnTaskRunnerThread(context));
588 base::PlatformFileError error =
589 CopyInForeignFileSync(context, src_file_path, dest_url);
590 content::BrowserThread::PostTask(
591 content::BrowserThread::IO,
592 FROM_HERE,
593 base::Bind(callback, error));
594 }
595
596 void NativeMediaFileUtil::DeleteDirectoryOnTaskRunnerThread(
597 fileapi::FileSystemOperationContext* context,
598 const fileapi::FileSystemURL& url,
599 const StatusCallback& callback) {
600 DCHECK(IsOnTaskRunnerThread(context));
601 base::PlatformFileError error = DeleteDirectorySync(context, url);
602 content::BrowserThread::PostTask(
603 content::BrowserThread::IO,
604 FROM_HERE,
605 base::Bind(callback, error));
606 }
607
608 void NativeMediaFileUtil::CreateSnapshotFileOnTaskRunnerThread(
609 fileapi::FileSystemOperationContext* context,
610 const fileapi::FileSystemURL& url,
611 const CreateSnapshotFileCallback& callback) {
612 DCHECK(IsOnTaskRunnerThread(context));
613 base::PlatformFileInfo file_info;
614 base::FilePath platform_path;
615 scoped_refptr<webkit_blob::ShareableFileReference> file_ref;
616 base::PlatformFileError error =
617 CreateSnapshotFileSync(context, url, &file_info, &platform_path,
618 &file_ref);
619 content::BrowserThread::PostTask(
620 content::BrowserThread::IO,
621 FROM_HERE,
622 base::Bind(callback, error, file_info, platform_path, file_ref));
623 }
624
304 } // namespace chrome 625 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698