Chromium Code Reviews| 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/chromeos/gdata/gdata_file_system.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_file_system.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/message_loop_proxy.h" | 13 #include "base/message_loop_proxy.h" |
| 14 #include "base/platform_file.h" | 14 #include "base/platform_file.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/profiles/profile.h" | |
| 18 #include "chrome/browser/profiles/profile_dependency_manager.h" | |
| 19 #include "chrome/browser/chromeos/gdata/gdata.h" | 17 #include "chrome/browser/chromeos/gdata/gdata.h" |
| 20 #include "chrome/browser/chromeos/gdata/gdata_download_observer.h" | 18 #include "chrome/browser/chromeos/gdata/gdata_download_observer.h" |
| 21 #include "chrome/browser/chromeos/gdata/gdata_parser.h" | 19 #include "chrome/browser/chromeos/gdata/gdata_parser.h" |
| 20 #include "chrome/browser/download/download_service.h" | |
| 21 #include "chrome/browser/download/download_service_factory.h" | |
| 22 #include "chrome/browser/profiles/profile.h" | |
| 23 #include "chrome/browser/profiles/profile_dependency_manager.h" | |
| 22 #include "chrome/common/chrome_constants.h" | 24 #include "chrome/common/chrome_constants.h" |
| 23 #include "chrome/common/chrome_paths_internal.h" | 25 #include "chrome/common/chrome_paths_internal.h" |
| 24 #include "chrome/browser/download/download_service.h" | |
| 25 #include "chrome/browser/download/download_service_factory.h" | |
| 26 #include "chrome/browser/profiles/profile_dependency_manager.h" | |
| 27 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 28 #include "webkit/fileapi/file_system_file_util_proxy.h" | 27 #include "webkit/fileapi/file_system_file_util_proxy.h" |
| 29 #include "webkit/fileapi/file_system_types.h" | 28 #include "webkit/fileapi/file_system_types.h" |
| 30 #include "webkit/fileapi/file_system_util.h" | 29 #include "webkit/fileapi/file_system_util.h" |
| 31 | 30 |
| 32 using content::BrowserThread; | 31 using content::BrowserThread; |
| 33 | 32 |
| 34 namespace { | 33 namespace { |
| 35 | 34 |
| 36 const char kGDataRootDirectory[] = "gdata"; | 35 const char kGDataRootDirectory[] = "gdata"; |
| (...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 528 : created_directory_path(created_directory_path), | 527 : created_directory_path(created_directory_path), |
| 529 target_directory_path(target_directory_path), | 528 target_directory_path(target_directory_path), |
| 530 is_exclusive(is_exclusive), | 529 is_exclusive(is_exclusive), |
| 531 is_recursive(is_recursive), | 530 is_recursive(is_recursive), |
| 532 callback(callback) { | 531 callback(callback) { |
| 533 } | 532 } |
| 534 | 533 |
| 535 GDataFileSystem::CreateDirectoryParams::~CreateDirectoryParams() { | 534 GDataFileSystem::CreateDirectoryParams::~CreateDirectoryParams() { |
| 536 } | 535 } |
| 537 | 536 |
| 537 // GDataFileSystem::CopyMoveFileParams struct implementation. | |
| 538 | |
| 539 GDataFileSystem::CopyMoveFileParams::CopyMoveFileParams() | |
| 540 : is_directory(false), | |
| 541 is_root_directory(false), | |
| 542 is_hosted_document(false) { | |
| 543 } | |
| 544 | |
| 545 GDataFileSystem::CopyMoveFileParams::~CopyMoveFileParams() { | |
| 546 } | |
| 538 | 547 |
| 539 // GDataFileSystem class implementatsion. | 548 // GDataFileSystem class implementatsion. |
| 540 | 549 |
| 541 GDataFileSystem::GDataFileSystem(Profile* profile, | 550 GDataFileSystem::GDataFileSystem(Profile* profile, |
| 542 DocumentsServiceInterface* documents_service) | 551 DocumentsServiceInterface* documents_service) |
| 543 : profile_(profile), | 552 : profile_(profile), |
| 544 documents_service_(documents_service), | 553 documents_service_(documents_service), |
| 545 gdata_uploader_(new GDataUploader(ALLOW_THIS_IN_INITIALIZER_LIST(this))), | 554 gdata_uploader_(new GDataUploader(ALLOW_THIS_IN_INITIALIZER_LIST(this))), |
| 546 gdata_download_observer_(new GDataDownloadObserver()), | 555 gdata_download_observer_(new GDataDownloadObserver()), |
| 547 cache_initialized_(false), | 556 cache_initialized_(false), |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 601 void GDataFileSystem::RefreshDirectoryAndContinueSearch( | 610 void GDataFileSystem::RefreshDirectoryAndContinueSearch( |
| 602 const FindFileParams& params) { | 611 const FindFileParams& params) { |
| 603 scoped_ptr<base::ListValue> feed_list(new base::ListValue()); | 612 scoped_ptr<base::ListValue> feed_list(new base::ListValue()); |
| 604 // Kick off document feed fetching here if we don't have complete data | 613 // Kick off document feed fetching here if we don't have complete data |
| 605 // to finish this call. | 614 // to finish this call. |
| 606 // |feed_list| will contain the list of all collected feed updates that | 615 // |feed_list| will contain the list of all collected feed updates that |
| 607 // we will receive through calls of DocumentsService::GetDocuments(). | 616 // we will receive through calls of DocumentsService::GetDocuments(). |
| 608 ContinueDirectoryRefresh(params, feed_list.Pass()); | 617 ContinueDirectoryRefresh(params, feed_list.Pass()); |
| 609 } | 618 } |
| 610 | 619 |
| 620 bool GDataFileSystem::GetCopyMoveFileParams(const FilePath& path, | |
| 621 CopyMoveFileParams* params) { | |
| 622 DCHECK(params); | |
| 623 | |
| 624 scoped_refptr<ReadOnlyFindFileDelegate> delegate( | |
| 625 new ReadOnlyFindFileDelegate()); | |
| 626 | |
| 627 base::AutoLock lock(lock_); | |
| 628 | |
| 629 UnsafeFindFileByPath(path, delegate); | |
| 630 GDataFileBase* file = delegate->file(); | |
| 631 if (!file) | |
| 632 return false; | |
| 633 | |
| 634 params->resource_id = file->resource_id(); | |
| 635 params->self_url = file->self_url(); | |
| 636 params->content_url = file->content_url(); | |
| 637 params->is_directory = file->AsGDataDirectory(); | |
| 638 params->is_root_directory = file->AsGDataRootDirectory(); | |
| 639 | |
| 640 GDataFile* gdata_file = file->AsGDataFile(); | |
| 641 if (gdata_file) { | |
| 642 params->is_hosted_document = gdata_file->is_hosted_document(); | |
| 643 params->document_extension = gdata_file->document_extension(); | |
| 644 } | |
| 645 | |
| 646 return true; | |
| 647 } | |
| 648 | |
| 649 void GDataFileSystem::Copy(const FilePath& src_file_path, | |
| 650 const FilePath& dest_file_path, | |
| 651 const FileOperationCallback& callback) { | |
| 652 base::PlatformFileError error = base::PLATFORM_FILE_OK; | |
| 653 FilePath dest_parent_path = dest_file_path.DirName(); | |
| 654 CopyMoveFileParams src_file_params, dest_parent_params; | |
| 655 if (!GetCopyMoveFileParams(src_file_path, &src_file_params) || | |
| 656 !GetCopyMoveFileParams(dest_parent_path, &dest_parent_params)) { | |
| 657 error = base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 658 } else { | |
| 659 // TODO(benchan): Implement copy for regular files and directories. | |
| 660 // To copy a regular file, we need to first download the file and | |
| 661 // then upload it, which is not yet implemented. Also, in the interim, | |
| 662 // we handle recursive directory copy in the file manager. | |
| 663 if (!src_file_params.is_hosted_document) { | |
| 664 error = base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | |
| 665 } else if (!dest_parent_params.is_directory) { | |
| 666 error = base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; | |
| 667 } | |
| 668 } | |
| 669 | |
| 670 if (error != base::PLATFORM_FILE_OK) { | |
| 671 if (!callback.is_null()) | |
| 672 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, error)); | |
| 673 | |
| 674 return; | |
| 675 } | |
| 676 | |
| 677 FilePathUpdateCallback add_file_to_directory_callback = | |
| 678 base::Bind(&GDataFileSystem::AddFileToDirectory, | |
| 679 weak_ptr_factory_.GetWeakPtr(), | |
| 680 dest_parent_path, | |
| 681 callback); | |
| 682 | |
| 683 documents_service_->CopyDocument( | |
| 684 src_file_params.self_url, | |
| 685 // Drop the document extension, which should not be in the document title. | |
| 686 dest_file_path.BaseName().RemoveExtension().value(), | |
| 687 base::Bind(&GDataFileSystem::OnCopyDocumentCompleted, | |
| 688 weak_ptr_factory_.GetWeakPtr(), | |
| 689 add_file_to_directory_callback)); | |
| 690 } | |
| 691 | |
| 692 void GDataFileSystem::Rename(const FilePath& file_path, | |
| 693 const FilePath::StringType& new_name, | |
| 694 const FilePathUpdateCallback& callback) { | |
| 695 // It is a no-op if the file is renamed to the same name. | |
| 696 if (file_path.BaseName().value() == new_name) { | |
| 697 if (!callback.is_null()) { | |
| 698 MessageLoop::current()->PostTask( | |
| 699 FROM_HERE, base::Bind(callback, base::PLATFORM_FILE_OK, file_path)); | |
| 700 } | |
| 701 return; | |
| 702 } | |
| 703 | |
| 704 CopyMoveFileParams file_params; | |
| 705 if (!GetCopyMoveFileParams(file_path, &file_params)) { | |
| 706 if (!callback.is_null()) { | |
| 707 MessageLoop::current()->PostTask(FROM_HERE, | |
| 708 base::Bind(callback, base::PLATFORM_FILE_ERROR_NOT_FOUND, file_path)); | |
| 709 } | |
| 710 return; | |
| 711 } | |
| 712 | |
| 713 // Drop the .g<something> extension from |new_name| if the file being | |
| 714 // renamed is a hosted document and |new_name| has the same .g<something> | |
| 715 // extension as the file. | |
| 716 FilePath::StringType file_name = new_name; | |
| 717 if (file_params.is_hosted_document) { | |
| 718 FilePath new_file(file_name); | |
| 719 if (new_file.Extension() == file_params.document_extension) { | |
| 720 file_name = new_file.RemoveExtension().value(); | |
| 721 } | |
| 722 } | |
| 723 | |
| 724 documents_service_->RenameResource( | |
| 725 file_params.self_url, | |
| 726 file_name, | |
| 727 base::Bind(&GDataFileSystem::OnRenameResourceCompleted, | |
| 728 weak_ptr_factory_.GetWeakPtr(), | |
| 729 file_path, | |
| 730 file_name, | |
| 731 callback)); | |
| 732 } | |
| 733 | |
| 734 void GDataFileSystem::Move(const FilePath& src_file_path, | |
| 735 const FilePath& dest_file_path, | |
| 736 const FileOperationCallback& callback) { | |
| 737 base::PlatformFileError error = base::PLATFORM_FILE_OK; | |
| 738 FilePath dest_parent_path = dest_file_path.DirName(); | |
| 739 CopyMoveFileParams src_file_params, dest_parent_params; | |
| 740 if (!GetCopyMoveFileParams(src_file_path, &src_file_params) || | |
| 741 !GetCopyMoveFileParams(dest_parent_path, &dest_parent_params)) { | |
| 742 error = base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 743 } else { | |
| 744 if (!dest_parent_params.is_directory) | |
| 745 error = base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; | |
| 746 } | |
| 747 | |
| 748 if (error != base::PLATFORM_FILE_OK) { | |
| 749 if (!callback.is_null()) | |
| 750 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, error)); | |
| 751 | |
| 752 return; | |
| 753 } | |
| 754 | |
| 755 // If the file/directory is moved to the same directory, just rename it. | |
| 756 if (src_file_path.DirName() == dest_parent_path) { | |
| 757 FilePathUpdateCallback final_file_path_update_callback = | |
| 758 base::Bind(&GDataFileSystem::OnFilePathUpdated, | |
| 759 weak_ptr_factory_.GetWeakPtr(), | |
| 760 callback); | |
| 761 | |
| 762 Rename(src_file_path, dest_file_path.BaseName().value(), | |
| 763 final_file_path_update_callback); | |
| 764 return; | |
| 765 } | |
| 766 | |
| 767 // Otherwise, the move operation involves three steps: | |
| 768 // 1. Renames the file at |src_file_path| to basename(|dest_file_path|) | |
| 769 // within the same directory. The rename operation is a no-op if | |
| 770 // basename(|src_file_path|) equals to basename(|dest_file_path|). | |
| 771 // 2. Removes the file from its parent directory (the file is not deleted), | |
| 772 // which effectively moves the file to the root directory. | |
| 773 // 3. Adds the file to the parent directory of |dest_file_path|, which | |
| 774 // effectively moves the file from the root directory to the parent | |
| 775 // directory of |dest_file_path|. | |
|
satorux1
2012/03/13 01:12:17
Thank you for adding this!
| |
| 776 FilePathUpdateCallback add_file_to_directory_callback = | |
| 777 base::Bind(&GDataFileSystem::AddFileToDirectory, | |
| 778 weak_ptr_factory_.GetWeakPtr(), | |
| 779 dest_file_path.DirName(), | |
| 780 callback); | |
| 781 | |
| 782 FilePathUpdateCallback remove_file_from_directory_callback = | |
| 783 base::Bind(&GDataFileSystem::RemoveFileFromDirectory, | |
| 784 weak_ptr_factory_.GetWeakPtr(), | |
| 785 src_file_path.DirName(), | |
| 786 add_file_to_directory_callback); | |
| 787 | |
| 788 Rename(src_file_path, dest_file_path.BaseName().value(), | |
| 789 remove_file_from_directory_callback); | |
| 790 } | |
| 791 | |
| 792 void GDataFileSystem::AddFileToDirectory(const FilePath& dir_path, | |
| 793 const FileOperationCallback& callback, | |
| 794 base::PlatformFileError error, | |
| 795 const FilePath& file_path) { | |
| 796 CopyMoveFileParams file_params, dir_params; | |
| 797 if (error == base::PLATFORM_FILE_OK) { | |
| 798 if (!GetCopyMoveFileParams(file_path, &file_params) || | |
| 799 !GetCopyMoveFileParams(dir_path, &dir_params)) { | |
| 800 error = base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 801 } else { | |
| 802 if (!dir_params.is_directory) | |
| 803 error = base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; | |
| 804 } | |
| 805 } | |
| 806 | |
| 807 // Returns if there is an error or |dir_path| is the root directory. | |
| 808 if (error != base::PLATFORM_FILE_OK || dir_params.is_root_directory) { | |
| 809 if (!callback.is_null()) | |
| 810 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, error)); | |
| 811 | |
| 812 return; | |
| 813 } | |
| 814 | |
| 815 documents_service_->AddResourceToDirectory( | |
| 816 dir_params.content_url, | |
| 817 file_params.self_url, | |
| 818 base::Bind(&GDataFileSystem::OnAddFileToDirectoryCompleted, | |
| 819 weak_ptr_factory_.GetWeakPtr(), | |
| 820 callback, | |
| 821 file_path, | |
| 822 dir_path)); | |
| 823 } | |
| 824 | |
| 825 void GDataFileSystem::RemoveFileFromDirectory( | |
| 826 const FilePath& dir_path, | |
| 827 const FilePathUpdateCallback& callback, | |
| 828 base::PlatformFileError error, | |
| 829 const FilePath& file_path) { | |
| 830 CopyMoveFileParams file_params, dir_params; | |
| 831 if (error == base::PLATFORM_FILE_OK) { | |
| 832 if (!GetCopyMoveFileParams(file_path, &file_params) || | |
| 833 !GetCopyMoveFileParams(dir_path, &dir_params)) { | |
| 834 error = base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 835 } else { | |
| 836 if (!dir_params.is_directory) | |
| 837 error = base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; | |
| 838 } | |
| 839 } | |
| 840 | |
| 841 // Returns if there is an error or |dir_path| is the root directory. | |
| 842 if (error != base::PLATFORM_FILE_OK || dir_params.is_root_directory) { | |
| 843 if (!callback.is_null()) { | |
| 844 MessageLoop::current()->PostTask(FROM_HERE, | |
| 845 base::Bind(callback, error, file_path)); | |
| 846 } | |
| 847 return; | |
| 848 } | |
| 849 | |
| 850 documents_service_->RemoveResourceFromDirectory( | |
| 851 dir_params.content_url, | |
| 852 file_params.self_url, | |
| 853 file_params.resource_id, | |
| 854 base::Bind(&GDataFileSystem::OnRemoveFileFromDirectoryCompleted, | |
| 855 weak_ptr_factory_.GetWeakPtr(), | |
| 856 callback, | |
| 857 file_path, | |
| 858 dir_path)); | |
| 859 } | |
| 860 | |
| 611 void GDataFileSystem::Remove(const FilePath& file_path, | 861 void GDataFileSystem::Remove(const FilePath& file_path, |
| 612 bool is_recursive, | 862 bool is_recursive, |
| 613 const FileOperationCallback& callback) { | 863 const FileOperationCallback& callback) { |
| 614 base::AutoLock lock(lock_); | 864 base::AutoLock lock(lock_); |
| 615 GDataFileBase* file_info = GetGDataFileInfoFromPath(file_path); | 865 GDataFileBase* file_info = GetGDataFileInfoFromPath(file_path); |
| 616 if (!file_info) { | 866 if (!file_info) { |
| 617 if (!callback.is_null()) { | 867 if (!callback.is_null()) { |
| 618 MessageLoop::current()->PostTask( | 868 MessageLoop::current()->PostTask( |
| 619 FROM_HERE, | 869 FROM_HERE, |
| 620 base::Bind(callback, base::PLATFORM_FILE_ERROR_NOT_FOUND)); | 870 base::Bind(callback, base::PLATFORM_FILE_ERROR_NOT_FOUND)); |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 866 | 1116 |
| 867 { // Lock to use GetGDataFileInfoFromPath and returned pointer, but need to | 1117 { // Lock to use GetGDataFileInfoFromPath and returned pointer, but need to |
| 868 // release before GetFromCache. | 1118 // release before GetFromCache. |
| 869 base::AutoLock lock(lock_); | 1119 base::AutoLock lock(lock_); |
| 870 GDataFileBase* file_base = GetGDataFileInfoFromPath(gdata_file_path); | 1120 GDataFileBase* file_base = GetGDataFileInfoFromPath(gdata_file_path); |
| 871 | 1121 |
| 872 if (!file_base || !file_base->AsGDataFile()) | 1122 if (!file_base || !file_base->AsGDataFile()) |
| 873 return cache_file_path; | 1123 return cache_file_path; |
| 874 | 1124 |
| 875 GDataFile* file = file_base->AsGDataFile(); | 1125 GDataFile* file = file_base->AsGDataFile(); |
| 876 resource = file->resource(); | 1126 resource = file->resource_id(); |
| 877 md5 = file->file_md5(); | 1127 md5 = file->file_md5(); |
| 878 } | 1128 } |
| 879 | 1129 |
| 880 if (!GetFromCache(resource, md5, &cache_file_path)) | 1130 if (!GetFromCache(resource, md5, &cache_file_path)) |
| 881 cache_file_path.clear(); | 1131 cache_file_path.clear(); |
| 882 | 1132 |
| 883 return cache_file_path; | 1133 return cache_file_path; |
| 884 } | 1134 } |
| 885 | 1135 |
| 886 void GDataFileSystem::OnCreateDirectoryCompleted( | 1136 void GDataFileSystem::OnCreateDirectoryCompleted( |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 992 | 1242 |
| 993 // If this was the root feed, cache its content. | 1243 // If this was the root feed, cache its content. |
| 994 if (params.directory_path == FilePath(kGDataRootDirectory)) | 1244 if (params.directory_path == FilePath(kGDataRootDirectory)) |
| 995 SaveRootFeeds(feed_list.Pass()); | 1245 SaveRootFeeds(feed_list.Pass()); |
| 996 | 1246 |
| 997 // Continue file content search operation. | 1247 // Continue file content search operation. |
| 998 FindFileByPath(params.file_path, | 1248 FindFileByPath(params.file_path, |
| 999 params.delegate); | 1249 params.delegate); |
| 1000 } | 1250 } |
| 1001 | 1251 |
| 1252 void GDataFileSystem::OnFilePathUpdated(const FileOperationCallback& callback, | |
| 1253 base::PlatformFileError error, | |
| 1254 const FilePath& file_path) { | |
| 1255 if (!callback.is_null()) | |
| 1256 callback.Run(error); | |
| 1257 } | |
| 1258 | |
| 1259 void GDataFileSystem::OnRenameResourceCompleted( | |
| 1260 const FilePath& file_path, | |
| 1261 const FilePath::StringType& new_name, | |
| 1262 const FilePathUpdateCallback& callback, | |
| 1263 GDataErrorCode status, | |
| 1264 const GURL& document_url) { | |
| 1265 FilePath updated_file_path; | |
| 1266 base::PlatformFileError error = GDataToPlatformError(status); | |
| 1267 if (error == base::PLATFORM_FILE_OK) | |
| 1268 error = RenameFileOnFilesystem(file_path, new_name, &updated_file_path); | |
| 1269 | |
| 1270 if (!callback.is_null()) | |
| 1271 callback.Run(error, updated_file_path); | |
| 1272 } | |
| 1273 | |
| 1274 void GDataFileSystem::OnCopyDocumentCompleted( | |
| 1275 const FilePathUpdateCallback& callback, | |
| 1276 GDataErrorCode status, | |
| 1277 scoped_ptr<base::Value> data) { | |
| 1278 base::PlatformFileError error = GDataToPlatformError(status); | |
| 1279 if (error != base::PLATFORM_FILE_OK) { | |
| 1280 if (!callback.is_null()) | |
| 1281 callback.Run(error, FilePath()); | |
| 1282 | |
| 1283 return; | |
| 1284 } | |
| 1285 | |
| 1286 base::DictionaryValue* dict_value = NULL; | |
| 1287 base::Value* entry_value = NULL; | |
| 1288 if (data.get() && data->GetAsDictionary(&dict_value) && dict_value) | |
| 1289 dict_value->Get("entry", &entry_value); | |
| 1290 | |
| 1291 if (!entry_value) { | |
| 1292 if (!callback.is_null()) | |
| 1293 callback.Run(base::PLATFORM_FILE_ERROR_FAILED, FilePath()); | |
| 1294 | |
| 1295 return; | |
| 1296 } | |
| 1297 | |
| 1298 scoped_ptr<DocumentEntry> entry(DocumentEntry::CreateFrom(entry_value)); | |
| 1299 if (!entry.get()) { | |
| 1300 if (!callback.is_null()) | |
| 1301 callback.Run(base::PLATFORM_FILE_ERROR_FAILED, FilePath()); | |
| 1302 | |
| 1303 return; | |
| 1304 } | |
| 1305 | |
| 1306 FilePath file_path; | |
| 1307 { | |
| 1308 base::AutoLock lock(lock_); | |
| 1309 GDataFileBase* file = | |
| 1310 GDataFileBase::FromDocumentEntry(root_.get(), entry.get()); | |
| 1311 if (!file) { | |
| 1312 if (!callback.is_null()) | |
| 1313 callback.Run(base::PLATFORM_FILE_ERROR_FAILED, FilePath()); | |
| 1314 | |
| 1315 return; | |
| 1316 } | |
| 1317 root_->AddFile(file); | |
| 1318 file_path = file->GetFilePath(); | |
| 1319 } | |
| 1320 | |
| 1321 if (!callback.is_null()) | |
| 1322 callback.Run(error, file_path); | |
| 1323 } | |
| 1324 | |
| 1325 void GDataFileSystem::OnAddFileToDirectoryCompleted( | |
| 1326 const FileOperationCallback& callback, | |
| 1327 const FilePath& file_path, | |
| 1328 const FilePath& dir_path, | |
| 1329 GDataErrorCode status, | |
| 1330 const GURL& document_url) { | |
| 1331 base::PlatformFileError error = GDataToPlatformError(status); | |
| 1332 if (error == base::PLATFORM_FILE_OK) | |
| 1333 error = AddFileToDirectoryOnFilesystem(file_path, dir_path); | |
| 1334 | |
| 1335 if (!callback.is_null()) | |
| 1336 callback.Run(error); | |
| 1337 } | |
| 1338 | |
| 1339 void GDataFileSystem::OnRemoveFileFromDirectoryCompleted( | |
| 1340 const FilePathUpdateCallback& callback, | |
| 1341 const FilePath& file_path, | |
| 1342 const FilePath& dir_path, | |
| 1343 GDataErrorCode status, | |
| 1344 const GURL& document_url) { | |
| 1345 FilePath updated_file_path = file_path; | |
| 1346 base::PlatformFileError error = GDataToPlatformError(status); | |
| 1347 if (error == base::PLATFORM_FILE_OK) | |
| 1348 error = RemoveFileFromDirectoryOnFilesystem(file_path, dir_path, | |
| 1349 &updated_file_path); | |
| 1350 | |
| 1351 if (!callback.is_null()) | |
| 1352 callback.Run(error, updated_file_path); | |
| 1353 } | |
| 1354 | |
| 1002 void GDataFileSystem::SaveRootFeeds(scoped_ptr<base::ListValue> feed_vector) { | 1355 void GDataFileSystem::SaveRootFeeds(scoped_ptr<base::ListValue> feed_vector) { |
| 1003 BrowserThread::PostBlockingPoolTask(FROM_HERE, | 1356 BrowserThread::PostBlockingPoolTask(FROM_HERE, |
| 1004 base::Bind(&GDataFileSystem::SaveRootFeedsOnIOThreadPool, | 1357 base::Bind(&GDataFileSystem::SaveRootFeedsOnIOThreadPool, |
| 1005 cache_paths_[CACHE_TYPE_META], | 1358 cache_paths_[CACHE_TYPE_META], |
| 1006 base::Passed(&feed_vector))); | 1359 base::Passed(&feed_vector))); |
| 1007 } | 1360 } |
| 1008 | 1361 |
| 1009 // Static. | 1362 // Static. |
| 1010 void GDataFileSystem::SaveRootFeedsOnIOThreadPool( | 1363 void GDataFileSystem::SaveRootFeedsOnIOThreadPool( |
| 1011 const FilePath& meta_cache_path, | 1364 const FilePath& meta_cache_path, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1056 GDataErrorCode status, | 1409 GDataErrorCode status, |
| 1057 const GURL& content_url, | 1410 const GURL& content_url, |
| 1058 const FilePath& file_path) { | 1411 const FilePath& file_path) { |
| 1059 base::PlatformFileError error = GDataToPlatformError(status); | 1412 base::PlatformFileError error = GDataToPlatformError(status); |
| 1060 | 1413 |
| 1061 if (!callback.is_null()) { | 1414 if (!callback.is_null()) { |
| 1062 callback.Run(error, file_path); | 1415 callback.Run(error, file_path); |
| 1063 } | 1416 } |
| 1064 } | 1417 } |
| 1065 | 1418 |
| 1419 base::PlatformFileError GDataFileSystem::RenameFileOnFilesystem( | |
| 1420 const FilePath& file_path, | |
| 1421 const FilePath::StringType& new_name, | |
| 1422 FilePath* updated_file_path) { | |
| 1423 DCHECK(updated_file_path); | |
| 1424 | |
| 1425 base::AutoLock lock(lock_); | |
| 1426 | |
| 1427 scoped_refptr<ReadOnlyFindFileDelegate> find_file_delegate( | |
| 1428 new ReadOnlyFindFileDelegate()); | |
| 1429 UnsafeFindFileByPath(file_path, find_file_delegate); | |
| 1430 | |
| 1431 GDataFileBase* file = find_file_delegate->file(); | |
| 1432 if (!file) | |
| 1433 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 1434 | |
| 1435 DCHECK(file->parent()); | |
| 1436 file->set_original_file_name(new_name); | |
| 1437 // After changing the original file name, call TakeFile() to remove the | |
| 1438 // file from its parent directory and then add it back in order to go | |
| 1439 // through the file name de-duplication. | |
| 1440 if (!file->parent()->TakeFile(file)) | |
| 1441 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 1442 | |
| 1443 *updated_file_path = file->GetFilePath(); | |
| 1444 return base::PLATFORM_FILE_OK; | |
| 1445 } | |
| 1446 | |
| 1447 base::PlatformFileError GDataFileSystem::AddFileToDirectoryOnFilesystem( | |
| 1448 const FilePath& file_path, const FilePath& dir_path) { | |
| 1449 base::AutoLock lock(lock_); | |
| 1450 | |
| 1451 scoped_refptr<ReadOnlyFindFileDelegate> find_file_delegate( | |
| 1452 new ReadOnlyFindFileDelegate()); | |
| 1453 UnsafeFindFileByPath(file_path, find_file_delegate); | |
| 1454 | |
| 1455 GDataFileBase* file = find_file_delegate->file(); | |
| 1456 if (!file) | |
| 1457 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 1458 | |
| 1459 DCHECK_EQ(root_.get(), file->parent()); | |
| 1460 | |
| 1461 scoped_refptr<ReadOnlyFindFileDelegate> find_dir_delegate( | |
| 1462 new ReadOnlyFindFileDelegate()); | |
| 1463 UnsafeFindFileByPath(dir_path, find_dir_delegate); | |
| 1464 GDataFileBase* dir = find_dir_delegate->file(); | |
| 1465 if (!dir) | |
| 1466 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 1467 | |
| 1468 if (!dir->AsGDataDirectory()) | |
| 1469 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; | |
| 1470 | |
| 1471 if (!dir->AsGDataDirectory()->TakeFile(file)) | |
| 1472 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 1473 | |
| 1474 return base::PLATFORM_FILE_OK; | |
| 1475 } | |
| 1476 | |
| 1477 base::PlatformFileError GDataFileSystem::RemoveFileFromDirectoryOnFilesystem( | |
| 1478 const FilePath& file_path, const FilePath& dir_path, | |
| 1479 FilePath* updated_file_path) { | |
| 1480 DCHECK(updated_file_path); | |
| 1481 | |
| 1482 base::AutoLock lock(lock_); | |
| 1483 | |
| 1484 scoped_refptr<ReadOnlyFindFileDelegate> find_file_delegate( | |
| 1485 new ReadOnlyFindFileDelegate()); | |
| 1486 UnsafeFindFileByPath(file_path, find_file_delegate); | |
| 1487 | |
| 1488 GDataFileBase* file = find_file_delegate->file(); | |
| 1489 if (!file) | |
| 1490 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 1491 | |
| 1492 scoped_refptr<ReadOnlyFindFileDelegate> find_dir_delegate( | |
| 1493 new ReadOnlyFindFileDelegate()); | |
| 1494 UnsafeFindFileByPath(dir_path, find_dir_delegate); | |
| 1495 GDataFileBase* dir = find_dir_delegate->file(); | |
| 1496 if (!dir) | |
| 1497 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 1498 | |
| 1499 if (!dir->AsGDataDirectory()) | |
| 1500 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; | |
| 1501 | |
| 1502 DCHECK_EQ(dir->AsGDataDirectory(), file->parent()); | |
| 1503 | |
| 1504 if (!root_->TakeFile(file)) | |
| 1505 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 1506 | |
| 1507 *updated_file_path = file->GetFilePath(); | |
| 1508 return base::PLATFORM_FILE_OK; | |
| 1509 } | |
| 1510 | |
| 1066 base::PlatformFileError GDataFileSystem::RemoveFileFromFileSystem( | 1511 base::PlatformFileError GDataFileSystem::RemoveFileFromFileSystem( |
| 1067 const FilePath& file_path) { | 1512 const FilePath& file_path) { |
| 1068 // We need to lock here as well (despite FindFileByPath lock) since directory | 1513 // We need to lock here as well (despite FindFileByPath lock) since directory |
| 1069 // instance below is a 'live' object. | 1514 // instance below is a 'live' object. |
| 1070 base::AutoLock lock(lock_); | 1515 base::AutoLock lock(lock_); |
| 1071 | 1516 |
| 1072 // Find directory element within the cached file system snapshot. | 1517 // Find directory element within the cached file system snapshot. |
| 1073 scoped_refptr<ReadOnlyFindFileDelegate> update_delegate( | 1518 scoped_refptr<ReadOnlyFindFileDelegate> update_delegate( |
| 1074 new ReadOnlyFindFileDelegate()); | 1519 new ReadOnlyFindFileDelegate()); |
| 1075 UnsafeFindFileByPath(file_path, update_delegate); | 1520 UnsafeFindFileByPath(file_path, update_delegate); |
| 1076 | 1521 |
| 1077 GDataFileBase* file = update_delegate->file(); | 1522 GDataFileBase* file = update_delegate->file(); |
| 1078 | 1523 |
| 1079 if (!file) | 1524 if (!file) |
| 1080 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 1525 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 1081 | 1526 |
| 1082 // If it's a file (only files have resource), remove it from cache. | 1527 // If it's a file (only files have resource), remove it from cache. |
| 1083 if (file->AsGDataFile()) { | 1528 if (file->AsGDataFile()) { |
| 1084 RemoveFromCache(file->AsGDataFile()->resource(), | 1529 RemoveFromCache(file->AsGDataFile()->resource_id(), |
| 1085 base::Bind(&GDataFileSystem::OnRemovedFromCache, | 1530 base::Bind(&GDataFileSystem::OnRemovedFromCache, |
| 1086 weak_ptr_factory_.GetWeakPtr())); | 1531 weak_ptr_factory_.GetWeakPtr())); |
| 1087 } | 1532 } |
| 1088 | 1533 |
| 1089 // You can't remove root element. | 1534 // You can't remove root element. |
| 1090 if (!file->parent()) | 1535 if (!file->parent()) |
| 1091 return base::PLATFORM_FILE_ERROR_ACCESS_DENIED; | 1536 return base::PLATFORM_FILE_ERROR_ACCESS_DENIED; |
| 1092 | 1537 |
| 1093 if (!file->parent()->RemoveFile(file)) | 1538 if (!file->parent()->RemoveFile(file)) |
| 1094 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 1539 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| (...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1498 | 1943 |
| 1499 GDataFileSystemFactory::~GDataFileSystemFactory() { | 1944 GDataFileSystemFactory::~GDataFileSystemFactory() { |
| 1500 } | 1945 } |
| 1501 | 1946 |
| 1502 ProfileKeyedService* GDataFileSystemFactory::BuildServiceInstanceFor( | 1947 ProfileKeyedService* GDataFileSystemFactory::BuildServiceInstanceFor( |
| 1503 Profile* profile) const { | 1948 Profile* profile) const { |
| 1504 return new GDataFileSystem(profile, new DocumentsService); | 1949 return new GDataFileSystem(profile, new DocumentsService); |
| 1505 } | 1950 } |
| 1506 | 1951 |
| 1507 } // namespace gdata | 1952 } // namespace gdata |
| OLD | NEW |