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/extensions/api/file_system/file_system_api.h" | 5 #include "chrome/browser/extensions/api/file_system/file_system_api.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 #include "webkit/fileapi/external_mount_points.h" | 32 #include "webkit/fileapi/external_mount_points.h" |
33 #include "webkit/fileapi/file_system_types.h" | 33 #include "webkit/fileapi/file_system_types.h" |
34 #include "webkit/fileapi/file_system_util.h" | 34 #include "webkit/fileapi/file_system_util.h" |
35 #include "webkit/fileapi/isolated_context.h" | 35 #include "webkit/fileapi/isolated_context.h" |
36 | 36 |
37 #if defined(OS_MACOSX) | 37 #if defined(OS_MACOSX) |
38 #include <CoreFoundation/CoreFoundation.h> | 38 #include <CoreFoundation/CoreFoundation.h> |
39 #include "base/mac/foundation_util.h" | 39 #include "base/mac/foundation_util.h" |
40 #endif | 40 #endif |
41 | 41 |
| 42 #if defined(OS_CHROMEOS) |
| 43 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" |
| 44 #endif |
| 45 |
42 using fileapi::IsolatedContext; | 46 using fileapi::IsolatedContext; |
43 | 47 |
44 const char kInvalidParameters[] = "Invalid parameters"; | 48 const char kInvalidParameters[] = "Invalid parameters"; |
45 const char kSecurityError[] = "Security error"; | 49 const char kSecurityError[] = "Security error"; |
46 const char kInvalidCallingPage[] = "Invalid calling page. This function can't " | 50 const char kInvalidCallingPage[] = "Invalid calling page. This function can't " |
47 "be called from a background page."; | 51 "be called from a background page."; |
48 const char kUserCancelled[] = "User cancelled"; | 52 const char kUserCancelled[] = "User cancelled"; |
49 const char kWritableFileError[] = "Invalid file for writing"; | 53 const char kWritableFileError[] = "Invalid file for writing"; |
50 const char kRequiresFileSystemWriteError[] = | 54 const char kRequiresFileSystemWriteError[] = |
51 "Operation requires fileSystem.write permission"; | 55 "Operation requires fileSystem.write permission"; |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 base::PLATFORM_FILE_WRITE; | 189 base::PLATFORM_FILE_WRITE; |
186 base::PlatformFile file = base::CreatePlatformFile(path, creation_flags, | 190 base::PlatformFile file = base::CreatePlatformFile(path, creation_flags, |
187 NULL, &error); | 191 NULL, &error); |
188 // Close the file so we don't keep a lock open. | 192 // Close the file so we don't keep a lock open. |
189 if (file != base::kInvalidPlatformFileValue) | 193 if (file != base::kInvalidPlatformFileValue) |
190 base::ClosePlatformFile(file); | 194 base::ClosePlatformFile(file); |
191 return error == base::PLATFORM_FILE_OK || | 195 return error == base::PLATFORM_FILE_OK || |
192 error == base::PLATFORM_FILE_ERROR_EXISTS; | 196 error == base::PLATFORM_FILE_ERROR_EXISTS; |
193 } | 197 } |
194 | 198 |
| 199 void CheckLocalWritableFile(const base::FilePath& path, |
| 200 const base::Closure& on_success, |
| 201 const base::Closure& on_failure) { |
| 202 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 203 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 204 DoCheckWritableFile(path) ? on_success : on_failure); |
| 205 } |
| 206 |
| 207 #if defined(OS_CHROMEOS) |
| 208 void CheckRemoteWritableFile(const base::Closure& on_success, |
| 209 const base::Closure& on_failure, |
| 210 drive::DriveFileError error, |
| 211 const base::FilePath& path) { |
| 212 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 213 error == drive::DRIVE_FILE_OK ? on_success : on_failure); |
| 214 } |
| 215 #endif |
| 216 |
195 // Expand the mime-types and extensions provided in an AcceptOption, returning | 217 // Expand the mime-types and extensions provided in an AcceptOption, returning |
196 // them within the passed extension vector. Returns false if no valid types | 218 // them within the passed extension vector. Returns false if no valid types |
197 // were found. | 219 // were found. |
198 bool GetFileTypesFromAcceptOption( | 220 bool GetFileTypesFromAcceptOption( |
199 const file_system::AcceptOption& accept_option, | 221 const file_system::AcceptOption& accept_option, |
200 std::vector<base::FilePath::StringType>* extensions, | 222 std::vector<base::FilePath::StringType>* extensions, |
201 string16* description) { | 223 string16* description) { |
202 std::set<base::FilePath::StringType> extension_set; | 224 std::set<base::FilePath::StringType> extension_set; |
203 int description_id = 0; | 225 int description_id = 0; |
204 | 226 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 | 299 |
278 bool FileSystemEntryFunction::HasFileSystemWritePermission() { | 300 bool FileSystemEntryFunction::HasFileSystemWritePermission() { |
279 const extensions::Extension* extension = GetExtension(); | 301 const extensions::Extension* extension = GetExtension(); |
280 if (!extension) | 302 if (!extension) |
281 return false; | 303 return false; |
282 | 304 |
283 return extension->HasAPIPermission(APIPermission::kFileSystemWrite); | 305 return extension->HasAPIPermission(APIPermission::kFileSystemWrite); |
284 } | 306 } |
285 | 307 |
286 void FileSystemEntryFunction::CheckWritableFile(const base::FilePath& path) { | 308 void FileSystemEntryFunction::CheckWritableFile(const base::FilePath& path) { |
287 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 309 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
288 if (DoCheckWritableFile(path)) { | 310 base::Closure on_success = |
289 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 311 base::Bind(&FileSystemEntryFunction::RegisterFileSystemAndSendResponse, |
290 base::Bind(&FileSystemEntryFunction::RegisterFileSystemAndSendResponse, | 312 this, path, WRITABLE); |
291 this, path, WRITABLE)); | 313 base::Closure on_failure = |
| 314 base::Bind(&FileSystemEntryFunction::HandleWritableFileError, this); |
| 315 |
| 316 #if defined(OS_CHROMEOS) |
| 317 if (drive::util::IsUnderDriveMountPoint(path)) { |
| 318 drive::util::PrepareWritableFileAndRun(profile_, path, |
| 319 base::Bind(&CheckRemoteWritableFile, on_success, on_failure)); |
292 return; | 320 return; |
293 } | 321 } |
294 | 322 #endif |
295 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 323 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, |
296 base::Bind(&FileSystemEntryFunction::HandleWritableFileError, this)); | 324 base::Bind(&CheckLocalWritableFile, path, on_success, on_failure)); |
297 } | 325 } |
298 | 326 |
299 void FileSystemEntryFunction::RegisterFileSystemAndSendResponse( | 327 void FileSystemEntryFunction::RegisterFileSystemAndSendResponse( |
300 const base::FilePath& path, EntryType entry_type) { | 328 const base::FilePath& path, EntryType entry_type) { |
301 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 329 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
302 | 330 |
303 fileapi::IsolatedContext* isolated_context = | 331 fileapi::IsolatedContext* isolated_context = |
304 fileapi::IsolatedContext::GetInstance(); | 332 fileapi::IsolatedContext::GetInstance(); |
305 DCHECK(isolated_context); | 333 DCHECK(isolated_context); |
306 | 334 |
(...skipping 27 matching lines...) Expand all Loading... |
334 if (!HasFileSystemWritePermission()) { | 362 if (!HasFileSystemWritePermission()) { |
335 error_ = kRequiresFileSystemWriteError; | 363 error_ = kRequiresFileSystemWriteError; |
336 return false; | 364 return false; |
337 } | 365 } |
338 | 366 |
339 base::FilePath path; | 367 base::FilePath path; |
340 if (!GetFilePathOfFileEntry(filesystem_name, filesystem_path, | 368 if (!GetFilePathOfFileEntry(filesystem_name, filesystem_path, |
341 render_view_host_, &path, &error_)) | 369 render_view_host_, &path, &error_)) |
342 return false; | 370 return false; |
343 | 371 |
344 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, | 372 CheckWritableFile(path); |
345 base::Bind(&FileSystemGetWritableEntryFunction::CheckWritableFile, | |
346 this, path)); | |
347 return true; | 373 return true; |
348 } | 374 } |
349 | 375 |
350 bool FileSystemIsWritableEntryFunction::RunImpl() { | 376 bool FileSystemIsWritableEntryFunction::RunImpl() { |
351 std::string filesystem_name; | 377 std::string filesystem_name; |
352 std::string filesystem_path; | 378 std::string filesystem_path; |
353 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); | 379 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); |
354 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); | 380 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); |
355 | 381 |
356 std::string filesystem_id; | 382 std::string filesystem_id; |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 // For testing on Chrome OS, where to deal with remote and local paths | 530 // For testing on Chrome OS, where to deal with remote and local paths |
505 // smoothly, all accessed paths need to be registered in the list of | 531 // smoothly, all accessed paths need to be registered in the list of |
506 // external mount points. | 532 // external mount points. |
507 fileapi::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( | 533 fileapi::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( |
508 name, fileapi::kFileSystemTypeNativeLocal, path); | 534 name, fileapi::kFileSystemTypeNativeLocal, path); |
509 } | 535 } |
510 | 536 |
511 void FileSystemChooseEntryFunction::FileSelected(const base::FilePath& path, | 537 void FileSystemChooseEntryFunction::FileSelected(const base::FilePath& path, |
512 EntryType entry_type) { | 538 EntryType entry_type) { |
513 if (entry_type == WRITABLE) { | 539 if (entry_type == WRITABLE) { |
514 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, | 540 CheckWritableFile(path); |
515 base::Bind(&FileSystemChooseEntryFunction::CheckWritableFile, | |
516 this, path)); | |
517 return; | 541 return; |
518 } | 542 } |
519 | 543 |
520 // Don't need to check the file, it's for reading. | 544 // Don't need to check the file, it's for reading. |
521 RegisterFileSystemAndSendResponse(path, READ_ONLY); | 545 RegisterFileSystemAndSendResponse(path, READ_ONLY); |
522 } | 546 } |
523 | 547 |
524 void FileSystemChooseEntryFunction::FileSelectionCanceled() { | 548 void FileSystemChooseEntryFunction::FileSelectionCanceled() { |
525 error_ = kUserCancelled; | 549 error_ = kUserCancelled; |
526 SendResponse(false); | 550 SendResponse(false); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
616 | 640 |
617 BuildFileTypeInfo(&file_type_info, suggested_extension, | 641 BuildFileTypeInfo(&file_type_info, suggested_extension, |
618 options->accepts.get(), options->accepts_all_types.get()); | 642 options->accepts.get(), options->accepts_all_types.get()); |
619 } | 643 } |
620 | 644 |
621 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { | 645 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { |
622 error_ = kRequiresFileSystemWriteError; | 646 error_ = kRequiresFileSystemWriteError; |
623 return false; | 647 return false; |
624 } | 648 } |
625 | 649 |
626 if (entry_type != WRITABLE) | 650 file_type_info.support_drive = true; |
627 file_type_info.support_drive = true; | |
628 | |
629 return ShowPicker(suggested_name, file_type_info, picker_type, entry_type); | 651 return ShowPicker(suggested_name, file_type_info, picker_type, entry_type); |
630 } | 652 } |
631 | 653 |
632 } // namespace extensions | 654 } // namespace extensions |
OLD | NEW |