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

Side by Side Diff: chrome/browser/extensions/api/developer_private/developer_private_api.cc

Issue 16402016: Copy folder recursively for syncfsToLocalfs export. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: 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
« no previous file with comments | « chrome/browser/extensions/api/developer_private/developer_private_api.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/extensions/api/developer_private/developer_private_api. h" 5 #include "chrome/browser/extensions/api/developer_private/developer_private_api. h"
6 6
7 #include "apps/app_load_service.h" 7 #include "apps/app_load_service.h"
8 #include "apps/app_restore_service.h" 8 #include "apps/app_restore_service.h"
9 #include "apps/saved_files_service.h" 9 #include "apps/saved_files_service.h"
10 #include "base/base64.h" 10 #include "base/base64.h"
(...skipping 818 matching lines...) Expand 10 before | Expand all | Expand 10 after
829 if (!file_util::Delete(project_path, true/*recursive*/)) { 829 if (!file_util::Delete(project_path, true/*recursive*/)) {
830 SetError("Error in copying files from sync filesystem."); 830 SetError("Error in copying files from sync filesystem.");
831 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, 831 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
832 base::Bind(&DeveloperPrivateExportSyncfsFolderToLocalfsFunction:: 832 base::Bind(&DeveloperPrivateExportSyncfsFolderToLocalfsFunction::
833 SendResponse, 833 SendResponse,
834 this, 834 this,
835 false)); 835 false));
836 return; 836 return;
837 } 837 }
838 838
839 pendingCopyOperationsCount_ = 1;
840
839 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, 841 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE,
840 base::Bind(&DeveloperPrivateExportSyncfsFolderToLocalfsFunction:: 842 base::Bind(&DeveloperPrivateExportSyncfsFolderToLocalfsFunction::
841 ReadSyncFileSystemDirectory, 843 ReadSyncFileSystemDirectory,
842 this, project_path)); 844 this, project_path, project_path.BaseName()));
843 } 845 }
844 846
845 void DeveloperPrivateExportSyncfsFolderToLocalfsFunction:: 847 void DeveloperPrivateExportSyncfsFolderToLocalfsFunction::
846 ReadSyncFileSystemDirectory(const base::FilePath& project_path) { 848 ReadSyncFileSystemDirectory(const base::FilePath& project_path,
849 const base::FilePath& destination_path) {
847 std::string origin_url( 850 std::string origin_url(
848 Extension::GetBaseURLFromExtensionId(extension_id()).spec()); 851 Extension::GetBaseURLFromExtensionId(extension_id()).spec());
849 fileapi::FileSystemURL url(sync_file_system::CreateSyncableFileSystemURL( 852 fileapi::FileSystemURL url(sync_file_system::CreateSyncableFileSystemURL(
850 GURL(origin_url), 853 GURL(origin_url),
851 project_path.BaseName())); 854 destination_path));
852 855
853 context_->operation_runner()->ReadDirectory( 856 context_->operation_runner()->ReadDirectory(
854 url, base::Bind(&DeveloperPrivateExportSyncfsFolderToLocalfsFunction:: 857 url, base::Bind(&DeveloperPrivateExportSyncfsFolderToLocalfsFunction::
855 ReadSyncFileSystemDirectoryCb, this, project_path)); 858 ReadSyncFileSystemDirectoryCb,
859 this, project_path, destination_path));
856 } 860 }
857 861
858 void DeveloperPrivateExportSyncfsFolderToLocalfsFunction:: 862 void DeveloperPrivateExportSyncfsFolderToLocalfsFunction::
859 ReadSyncFileSystemDirectoryCb( 863 ReadSyncFileSystemDirectoryCb(
860 const base::FilePath& project_path, 864 const base::FilePath& project_path,
865 const base::FilePath& destination_path,
861 base::PlatformFileError status, 866 base::PlatformFileError status,
862 const fileapi::FileSystemOperation::FileEntryList& file_list, 867 const fileapi::FileSystemOperation::FileEntryList& file_list,
863 bool has_more) { 868 bool has_more) {
864 869
865 if (status != base::PLATFORM_FILE_OK) { 870 if (status != base::PLATFORM_FILE_OK) {
866 DLOG(ERROR) << "Error in copying files from sync filesystem."; 871 DLOG(ERROR) << "Error in copying files from sync filesystem.";
867 return; 872 return;
868 } 873 }
869 874
870 // Create an empty project folder if there are no files. 875 // We add 1 to the pending copy operations for both files and directories. We
871 if (!file_list.size()) { 876 // release the directory copy operation once all the files under the directory
872 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, 877 // are added for copying. We do that to ensure that pendingCopyOperationsCount
873 base::Bind(&DeveloperPrivateExportSyncfsFolderToLocalfsFunction:: 878 // does not become zero before all copy operations are finished.
874 CreateFolderAndSendResponse, 879 // In case the directory happens to be executing the last copy operation it
875 this, 880 // will call SendResponse to send the response to the API. The pending copy
876 project_path)); 881 // operations of files are released by the CopyFile function
miket_OOO 2013/06/25 16:44:23 period at end of sentence.
877 return; 882 pendingCopyOperationsCount_ += file_list.size();
878 }
879
880 pendingCallbacksCount_ = file_list.size();
881 883
882 for (size_t i = 0; i < file_list.size(); ++i) { 884 for (size_t i = 0; i < file_list.size(); ++i) {
885 if (file_list[i].is_directory) {
886 ReadSyncFileSystemDirectory(project_path.Append(file_list[i].name),
887 destination_path.Append(file_list[i].name));
888 continue;
889 }
890
883 std::string origin_url( 891 std::string origin_url(
884 Extension::GetBaseURLFromExtensionId(extension_id()).spec()); 892 Extension::GetBaseURLFromExtensionId(extension_id()).spec());
885 fileapi::FileSystemURL url(sync_file_system::CreateSyncableFileSystemURL( 893 fileapi::FileSystemURL url(sync_file_system::CreateSyncableFileSystemURL(
886 GURL(origin_url), 894 GURL(origin_url),
887 project_path.BaseName().Append(file_list[i].name))); 895 destination_path.Append(file_list[i].name)));
888 base::FilePath target_path = project_path; 896 base::FilePath target_path = project_path;
889 target_path = target_path.Append(file_list[i].name); 897 target_path = target_path.Append(file_list[i].name);
890 898
891 context_->operation_runner()->CreateSnapshotFile( 899 context_->operation_runner()->CreateSnapshotFile(
892 url, 900 url,
893 base::Bind( 901 base::Bind(
894 &DeveloperPrivateExportSyncfsFolderToLocalfsFunction:: 902 &DeveloperPrivateExportSyncfsFolderToLocalfsFunction::
895 SnapshotFileCallback, 903 SnapshotFileCallback,
896 this, 904 this,
897 target_path)); 905 target_path));
906
907 }
908
909 // Directory copy operation released here.
910 pendingCopyOperationsCount_--;
911
912 if (!pendingCopyOperationsCount_) {
913 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
914 base::Bind(&DeveloperPrivateExportSyncfsFolderToLocalfsFunction::
915 SendResponse,
916 this,
917 success_));
898 } 918 }
899 } 919 }
900 920
901 void DeveloperPrivateExportSyncfsFolderToLocalfsFunction::
902 CreateFolderAndSendResponse(const base::FilePath& project_path) {
903 if (!(success_ = file_util::CreateDirectory(project_path))) {
904 SetError("Error in copying files from sync filesystem.");
905 }
906 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
907 base::Bind(&DeveloperPrivateExportSyncfsFolderToLocalfsFunction::
908 SendResponse,
909 this,
910 success_));
911 }
912
913 void DeveloperPrivateExportSyncfsFolderToLocalfsFunction::SnapshotFileCallback( 921 void DeveloperPrivateExportSyncfsFolderToLocalfsFunction::SnapshotFileCallback(
914 const base::FilePath& target_path, 922 const base::FilePath& target_path,
915 base::PlatformFileError result, 923 base::PlatformFileError result,
916 const base::PlatformFileInfo& file_info, 924 const base::PlatformFileInfo& file_info,
917 const base::FilePath& src_path, 925 const base::FilePath& src_path,
918 const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref) { 926 const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref) {
919 if (result != base::PLATFORM_FILE_OK) { 927 if (result != base::PLATFORM_FILE_OK) {
920 SetError("Error in copying files from sync filesystem."); 928 SetError("Error in copying files from sync filesystem.");
921 success_ = false; 929 success_ = false;
922 return; 930 return;
(...skipping 10 matching lines...) Expand all
933 const base::FilePath& src_path, 941 const base::FilePath& src_path,
934 const base::FilePath& target_path) { 942 const base::FilePath& target_path) {
935 if (!file_util::CreateDirectory(target_path.DirName())) { 943 if (!file_util::CreateDirectory(target_path.DirName())) {
936 SetError("Error in copying files from sync filesystem."); 944 SetError("Error in copying files from sync filesystem.");
937 success_ = false; 945 success_ = false;
938 } 946 }
939 947
940 if (success_) 948 if (success_)
941 file_util::CopyFile(src_path, target_path); 949 file_util::CopyFile(src_path, target_path);
942 950
943 CHECK(pendingCallbacksCount_ > 0); 951 CHECK(pendingCopyOperationsCount_ > 0);
944 pendingCallbacksCount_--; 952 pendingCopyOperationsCount_--;
945 953
946 if (!pendingCallbacksCount_) { 954 if (!pendingCopyOperationsCount_) {
947 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, 955 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
948 base::Bind(&DeveloperPrivateExportSyncfsFolderToLocalfsFunction:: 956 base::Bind(&DeveloperPrivateExportSyncfsFolderToLocalfsFunction::
949 SendResponse, 957 SendResponse,
950 this, 958 this,
951 success_)); 959 success_));
952 } 960 }
953 } 961 }
954 962
955 DeveloperPrivateExportSyncfsFolderToLocalfsFunction:: 963 DeveloperPrivateExportSyncfsFolderToLocalfsFunction::
956 DeveloperPrivateExportSyncfsFolderToLocalfsFunction() 964 DeveloperPrivateExportSyncfsFolderToLocalfsFunction()
957 : pendingCallbacksCount_(0), success_(true) {} 965 : pendingCopyOperationsCount_(0), success_(true) {}
958 966
959 DeveloperPrivateExportSyncfsFolderToLocalfsFunction:: 967 DeveloperPrivateExportSyncfsFolderToLocalfsFunction::
960 ~DeveloperPrivateExportSyncfsFolderToLocalfsFunction() {} 968 ~DeveloperPrivateExportSyncfsFolderToLocalfsFunction() {}
961 969
962 bool DeveloperPrivateLoadProjectFunction::RunImpl() { 970 bool DeveloperPrivateLoadProjectFunction::RunImpl() {
963 // TODO(grv) : add unit tests. 971 // TODO(grv) : add unit tests.
964 base::FilePath::StringType project_name; 972 base::FilePath::StringType project_name;
965 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &project_name)); 973 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &project_name));
966 if (!ValidateFolderName(project_name)) { 974 if (!ValidateFolderName(project_name)) {
967 DLOG(INFO) << "Invalid project_name : [" << project_name << "]"; 975 DLOG(INFO) << "Invalid project_name : [" << project_name << "]";
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
1146 1154
1147 #undef SET_STRING 1155 #undef SET_STRING
1148 return true; 1156 return true;
1149 } 1157 }
1150 1158
1151 DeveloperPrivateGetStringsFunction::~DeveloperPrivateGetStringsFunction() {} 1159 DeveloperPrivateGetStringsFunction::~DeveloperPrivateGetStringsFunction() {}
1152 1160
1153 } // namespace api 1161 } // namespace api
1154 1162
1155 } // namespace extensions 1163 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/developer_private/developer_private_api.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698