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

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

Issue 14232018: Add API to load project from localfs into syncfs. (Closed) Base URL: http://git.chromium.org/chromium/src.git@load_api
Patch Set: . Created 7 years, 8 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
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 "base/base64.h" 7 #include "base/base64.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
(...skipping 30 matching lines...) Expand all
41 #include "extensions/browser/view_type_utils.h" 41 #include "extensions/browser/view_type_utils.h"
42 #include "extensions/common/constants.h" 42 #include "extensions/common/constants.h"
43 #include "extensions/common/extension_resource.h" 43 #include "extensions/common/extension_resource.h"
44 #include "grit/chromium_strings.h" 44 #include "grit/chromium_strings.h"
45 #include "grit/generated_resources.h" 45 #include "grit/generated_resources.h"
46 #include "net/base/net_util.h" 46 #include "net/base/net_util.h"
47 #include "ui/base/l10n/l10n_util.h" 47 #include "ui/base/l10n/l10n_util.h"
48 #include "webkit/blob/shareable_file_reference.h" 48 #include "webkit/blob/shareable_file_reference.h"
49 #include "webkit/fileapi/file_system_context.h" 49 #include "webkit/fileapi/file_system_context.h"
50 #include "webkit/fileapi/file_system_operation.h" 50 #include "webkit/fileapi/file_system_operation.h"
51 #include "webkit/fileapi/local_file_system_operation.h"
51 #include "webkit/fileapi/syncable/syncable_file_system_util.h" 52 #include "webkit/fileapi/syncable/syncable_file_system_util.h"
52 53
53 using content::RenderViewHost; 54 using content::RenderViewHost;
54 55
55 namespace extensions { 56 namespace extensions {
56 57
57 namespace { 58 namespace {
58 59
60 char kUnpackedAppsFolder[] = "apps_target";
61
59 ExtensionUpdater* GetExtensionUpdater(Profile* profile) { 62 ExtensionUpdater* GetExtensionUpdater(Profile* profile) {
60 return profile->GetExtensionService()->updater(); 63 return profile->GetExtensionService()->updater();
61 } 64 }
62 65
63 GURL ToDataURL(const base::FilePath& path) { 66 GURL ToDataURL(const base::FilePath& path) {
64 std::string contents; 67 std::string contents;
65 if (!file_util::ReadFileToString(path, &contents)) 68 if (!file_util::ReadFileToString(path, &contents))
66 return GURL(); 69 return GURL();
67 70
68 std::string contents_base64; 71 std::string contents_base64;
69 if (!base::Base64Encode(contents, &contents_base64)) 72 if (!base::Base64Encode(contents, &contents_base64))
70 return GURL(); 73 return GURL();
71 74
72 const char kDataURLPrefix[] = "data:image;base64,"; 75 const char kDataURLPrefix[] = "data:image;base64,";
73 return GURL(kDataURLPrefix + contents_base64); 76 return GURL(kDataURLPrefix + contents_base64);
74 } 77 }
75 78
79 std::vector<base::FilePath> FolderLs(const base::FilePath path) {
miket_OOO 2013/04/18 03:22:28 This isn't a great name for this function. Please
kinuko 2013/04/18 08:15:12 ListFolder maybe?
Gaurav 2013/04/18 23:08:46 Done.
Gaurav 2013/04/18 23:08:46 Done.
80 file_util::FileEnumerator files(path, false,
81 file_util::FileEnumerator::DIRECTORIES
82 | file_util::FileEnumerator::FILES);
miket_OOO 2013/04/18 03:22:28 Is this right? You're asking for both directories
Gaurav 2013/04/18 23:08:46 Thought paths represent both files and folders ?
83 std::vector<base::FilePath> paths;
84
85 for (base::FilePath current_path = files.Next(); !current_path.empty();
86 current_path = files.Next()) {
87 paths.push_back(current_path);
88 }
89 return paths;
90 }
91
76 } // namespace 92 } // namespace
77 93
78 namespace AllowFileAccess = api::developer_private::AllowFileAccess; 94 namespace AllowFileAccess = api::developer_private::AllowFileAccess;
79 namespace AllowIncognito = api::developer_private::AllowIncognito; 95 namespace AllowIncognito = api::developer_private::AllowIncognito;
80 namespace ChoosePath = api::developer_private::ChoosePath; 96 namespace ChoosePath = api::developer_private::ChoosePath;
81 namespace Enable = api::developer_private::Enable; 97 namespace Enable = api::developer_private::Enable;
82 namespace GetItemsInfo = api::developer_private::GetItemsInfo; 98 namespace GetItemsInfo = api::developer_private::GetItemsInfo;
83 namespace Inspect = api::developer_private::Inspect; 99 namespace Inspect = api::developer_private::Inspect;
84 namespace PackDirectory = api::developer_private::PackDirectory; 100 namespace PackDirectory = api::developer_private::PackDirectory;
85 namespace Reload = api::developer_private::Reload; 101 namespace Reload = api::developer_private::Reload;
(...skipping 723 matching lines...) Expand 10 before | Expand all | Expand 10 after
809 825
810 for (size_t i = 0; i < file_list.size(); ++i) { 826 for (size_t i = 0; i < file_list.size(); ++i) {
811 std::string origin_url( 827 std::string origin_url(
812 Extension::GetBaseURLFromExtensionId(extension_id()).spec()); 828 Extension::GetBaseURLFromExtensionId(extension_id()).spec());
813 fileapi::FileSystemURL url(sync_file_system::CreateSyncableFileSystemURL( 829 fileapi::FileSystemURL url(sync_file_system::CreateSyncableFileSystemURL(
814 GURL(origin_url), 830 GURL(origin_url),
815 sync_file_system::DriveFileSyncService::kServiceName, 831 sync_file_system::DriveFileSyncService::kServiceName,
816 base::FilePath(file_list[i].name))); 832 base::FilePath(file_list[i].name)));
817 base::FilePath target_path(profile()->GetPath()); 833 base::FilePath target_path(profile()->GetPath());
818 target_path = 834 target_path =
819 target_path.Append(FILE_PATH_LITERAL("apps_target")); 835 target_path.Append(FILE_PATH_LITERAL(kUnpackedAppsFolder));
820 target_path = target_path.Append(project_name); 836 target_path = target_path.Append(project_name);
821 target_path = target_path.Append(file_list[i].name); 837 target_path = target_path.Append(file_list[i].name);
822 838
823 base::PlatformFileError error_code; 839 base::PlatformFileError error_code;
824 fileapi::FileSystemOperation* op = 840 fileapi::FileSystemOperation* op =
825 context_->CreateFileSystemOperation(url, &error_code); 841 context_->CreateFileSystemOperation(url, &error_code);
826 DCHECK(op); 842 DCHECK(op);
827 843
828 if (error_code != base::PLATFORM_FILE_OK) { 844 if (error_code != base::PLATFORM_FILE_OK) {
829 DLOG(ERROR) << "Error in copying files from sync filesystem."; 845 DLOG(ERROR) << "Error in copying files from sync filesystem.";
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
870 file_util::CopyFile(src_path, target_path); 886 file_util::CopyFile(src_path, target_path);
871 } 887 }
872 888
873 DeveloperPrivateExportSyncfsFolderToLocalfsFunction:: 889 DeveloperPrivateExportSyncfsFolderToLocalfsFunction::
874 DeveloperPrivateExportSyncfsFolderToLocalfsFunction() 890 DeveloperPrivateExportSyncfsFolderToLocalfsFunction()
875 {} 891 {}
876 892
877 DeveloperPrivateExportSyncfsFolderToLocalfsFunction:: 893 DeveloperPrivateExportSyncfsFolderToLocalfsFunction::
878 ~DeveloperPrivateExportSyncfsFolderToLocalfsFunction() {} 894 ~DeveloperPrivateExportSyncfsFolderToLocalfsFunction() {}
879 895
896 bool DeveloperPrivateLoadProjectToSyncfsFunction::RunImpl() {
897 base::FilePath::StringType project_name;
898 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &project_name));
899
900 context_ = content::BrowserContext::GetStoragePartition(profile(),
901 render_view_host()->GetSiteInstance())->GetFileSystemContext();
902 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE,
903 base::Bind(&DeveloperPrivateLoadProjectToSyncfsFunction::
904 CopyFolder,
905 this, project_name));
906 return true;
907 }
908
909 void DeveloperPrivateLoadProjectToSyncfsFunction::CopyFolder(
910 const base::FilePath::StringType& project_name) {
911 base::FilePath path(profile()->GetPath());
912 path = path.Append(FILE_PATH_LITERAL(kUnpackedAppsFolder));
913 path = path.Append(FILE_PATH_LITERAL(project_name));
miket_OOO 2013/04/18 03:22:28 Again, dangerous -- assume this name is maliciousl
Gaurav 2013/04/18 23:08:46 Ensuring that the project_name contains only alpha
914
915 std::vector<base::FilePath> paths = FolderLs(path);
916 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE,
917 base::Bind(&DeveloperPrivateLoadProjectToSyncfsFunction::
918 CopyFiles,
919 this, paths));
920 }
921
922 void DeveloperPrivateLoadProjectToSyncfsFunction::CopyFiles(
923 const std::vector<base::FilePath>& paths) {
924 std::string origin_url(
925 Extension::GetBaseURLFromExtensionId(extension_id()).spec());
926 fileapi::FileSystemURL url(sync_file_system::CreateSyncableFileSystemURL(
927 GURL(origin_url),
928 sync_file_system::DriveFileSyncService::kServiceName,
929 base::FilePath()));
930
931 for (size_t i = 0; i < paths.size(); ++i) {
932 base::PlatformFileError error_code;
933 fileapi::FileSystemOperation* op
934 = context_->CreateFileSystemOperation(url, &error_code);
935 DCHECK(op);
936
937 std::string origin_url(
938 Extension::GetBaseURLFromExtensionId(extension_id()).spec());
939 fileapi::FileSystemURL
940 dest_url(sync_file_system::CreateSyncableFileSystemURL(
941 GURL(origin_url),
942 sync_file_system::DriveFileSyncService::kServiceName,
943 base::FilePath(paths[i].BaseName().MaybeAsASCII())));
944
945 op->AsLocalFileSystemOperation()->CopyInForeignFile(paths[i], dest_url,
946 base::Bind(&DeveloperPrivateLoadProjectToSyncfsFunction::
947 CopyFilesCallback,
948 this));
miket_OOO 2013/04/18 03:22:28 It looks like this will immediately queue up N ope
Gaurav 2013/04/18 23:08:46 Discussed with Antony. Looks to be an optimal appr
949 }
950 }
951
952 void DeveloperPrivateLoadProjectToSyncfsFunction::CopyFilesCallback(
953 const base::PlatformFileError result) {
954 if (result != base::PLATFORM_FILE_OK) {
955 DLOG(ERROR) << "Error in copying files to sync filesystem.";
956 return;
kinuko 2013/04/18 08:15:12 If the # of file was 1 and we got error here we se
Gaurav 2013/04/18 23:08:46 Fixed. Send true response if all callbacks are suc
957 }
958
959 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
960 base::Bind(&DeveloperPrivateLoadProjectToSyncfsFunction::SendResponse,
961 this,
962 true));
miket_OOO 2013/04/18 03:22:28 Wait a minute -- won't this send the response afte
kinuko 2013/04/18 08:15:12 Looks like it does, we should count up the number
Gaurav 2013/04/18 23:08:46 Done.
Gaurav 2013/04/18 23:08:46 Done.
963 }
964
965 DeveloperPrivateLoadProjectToSyncfsFunction::
966 DeveloperPrivateLoadProjectToSyncfsFunction() {}
967
968 DeveloperPrivateLoadProjectToSyncfsFunction::
969 ~DeveloperPrivateLoadProjectToSyncfsFunction() {}
970
971 bool DeveloperPrivateGetProjectsInfoFunction::RunImpl() {
972 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE,
973 base::Bind(&DeveloperPrivateGetProjectsInfoFunction::ReadFolder,
974 this));
975
976 // Released by ReadFolder
977 AddRef();
978 return true;
979 }
980
981 void DeveloperPrivateGetProjectsInfoFunction::ReadFolder() {
982 base::FilePath path(profile()->GetPath());
983 path = path.Append(FILE_PATH_LITERAL(kUnpackedAppsFolder));
984
985 std::vector<base::FilePath> paths = FolderLs(path);
986 ProjectInfoList info_list;
987 for (size_t i = 0; i < paths.size(); ++i) {
988 scoped_ptr<developer::ProjectInfo> info(new developer::ProjectInfo());
989 info->name = paths[i].BaseName().MaybeAsASCII();
990 info_list.push_back(
991 make_linked_ptr<developer::ProjectInfo>(info.release()));
992 }
993
994 results_ = developer::GetProjectsInfo::Results::Create(info_list);
995 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
996 base::Bind(&DeveloperPrivateGetProjectsInfoFunction::SendResponse,
997 this,
998 true));
999 Release();
1000 }
1001
1002 DeveloperPrivateGetProjectsInfoFunction::
1003 DeveloperPrivateGetProjectsInfoFunction() {}
1004
1005 DeveloperPrivateGetProjectsInfoFunction::
1006 ~DeveloperPrivateGetProjectsInfoFunction() {}
1007
1008 bool DeveloperPrivateLoadProjectFunction::RunImpl() {
1009 std::string project_name;
1010 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &project_name));
1011 base::FilePath path(profile()->GetPath());
1012 path = path.Append(FILE_PATH_LITERAL(kUnpackedAppsFolder));
1013 path = path.Append(project_name);
miket_OOO 2013/04/18 03:22:28 Scary
Gaurav 2013/04/18 23:08:46 As above.
1014 ExtensionService* service = profile()->GetExtensionService();
1015 UnpackedInstaller::Create(service)->Load(path);
1016 SendResponse(true);
1017 return true;
1018 }
1019
1020 DeveloperPrivateLoadProjectFunction::DeveloperPrivateLoadProjectFunction() {}
1021
1022 DeveloperPrivateLoadProjectFunction::~DeveloperPrivateLoadProjectFunction() {}
1023
880 bool DeveloperPrivateChoosePathFunction::RunImpl() { 1024 bool DeveloperPrivateChoosePathFunction::RunImpl() {
881 1025
882 scoped_ptr<developer::ChoosePath::Params> params( 1026 scoped_ptr<developer::ChoosePath::Params> params(
883 developer::ChoosePath::Params::Create(*args_)); 1027 developer::ChoosePath::Params::Create(*args_));
884 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); 1028 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
885 1029
886 ui::SelectFileDialog::Type type = ui::SelectFileDialog::SELECT_FOLDER; 1030 ui::SelectFileDialog::Type type = ui::SelectFileDialog::SELECT_FOLDER;
887 ui::SelectFileDialog::FileTypeInfo info; 1031 ui::SelectFileDialog::FileTypeInfo info;
888 if (params->select_type == developer::SELECT_TYPE_FILE) { 1032 if (params->select_type == developer::SELECT_TYPE_FILE) {
889 type = ui::SelectFileDialog::SELECT_OPEN_FILE; 1033 type = ui::SelectFileDialog::SELECT_OPEN_FILE;
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
1009 1153
1010 #undef SET_STRING 1154 #undef SET_STRING
1011 return true; 1155 return true;
1012 } 1156 }
1013 1157
1014 DeveloperPrivateGetStringsFunction::~DeveloperPrivateGetStringsFunction() {} 1158 DeveloperPrivateGetStringsFunction::~DeveloperPrivateGetStringsFunction() {}
1015 1159
1016 } // namespace api 1160 } // namespace api
1017 1161
1018 } // namespace extensions 1162 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698