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/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/saved_files_service.h" | 8 #include "apps/saved_files_service.h" |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 | 89 |
90 namespace { | 90 namespace { |
91 | 91 |
92 const char kNoSuchExtensionError[] = "No such extension."; | 92 const char kNoSuchExtensionError[] = "No such extension."; |
93 const char kSupervisedUserError[] = | 93 const char kSupervisedUserError[] = |
94 "Supervised users cannot modify extension settings."; | 94 "Supervised users cannot modify extension settings."; |
95 const char kCannotModifyPolicyExtensionError[] = | 95 const char kCannotModifyPolicyExtensionError[] = |
96 "Cannot modify the extension by policy."; | 96 "Cannot modify the extension by policy."; |
97 const char kRequiresUserGestureError[] = | 97 const char kRequiresUserGestureError[] = |
98 "This action requires a user gesture."; | 98 "This action requires a user gesture."; |
| 99 const char kCouldNotShowSelectFileDialogError[] = |
| 100 "Could not show a file chooser."; |
| 101 const char kFileSelectionCanceled[] = |
| 102 "File selection was canceled."; |
99 | 103 |
100 const char kUnpackedAppsFolder[] = "apps_target"; | 104 const char kUnpackedAppsFolder[] = "apps_target"; |
101 | 105 |
102 ExtensionService* GetExtensionService(content::BrowserContext* context) { | 106 ExtensionService* GetExtensionService(content::BrowserContext* context) { |
103 return ExtensionSystem::Get(context)->extension_service(); | 107 return ExtensionSystem::Get(context)->extension_service(); |
104 } | 108 } |
105 | 109 |
106 ExtensionUpdater* GetExtensionUpdater(Profile* profile) { | 110 ExtensionUpdater* GetExtensionUpdater(Profile* profile) { |
107 return GetExtensionService(profile)->updater(); | 111 return GetExtensionService(profile)->updater(); |
108 } | 112 } |
(...skipping 751 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
860 return false; | 864 return false; |
861 } | 865 } |
862 | 866 |
863 DevToolsWindow::OpenDevToolsWindow( | 867 DevToolsWindow::OpenDevToolsWindow( |
864 content::WebContents::FromRenderViewHost(host)); | 868 content::WebContents::FromRenderViewHost(host)); |
865 return true; | 869 return true; |
866 } | 870 } |
867 | 871 |
868 DeveloperPrivateInspectFunction::~DeveloperPrivateInspectFunction() {} | 872 DeveloperPrivateInspectFunction::~DeveloperPrivateInspectFunction() {} |
869 | 873 |
870 bool DeveloperPrivateLoadUnpackedFunction::RunAsync() { | 874 ExtensionFunction::ResponseAction DeveloperPrivateLoadUnpackedFunction::Run() { |
871 base::string16 select_title = | 875 if (!ShowPicker( |
872 l10n_util::GetStringUTF16(IDS_EXTENSION_LOAD_FROM_DIRECTORY); | 876 ui::SelectFileDialog::SELECT_FOLDER, |
| 877 l10n_util::GetStringUTF16(IDS_EXTENSION_LOAD_FROM_DIRECTORY), |
| 878 ui::SelectFileDialog::FileTypeInfo(), |
| 879 0 /* file_type_index */)) { |
| 880 return RespondNow(Error(kCouldNotShowSelectFileDialogError)); |
| 881 } |
873 | 882 |
874 // Balanced in FileSelected / FileSelectionCanceled. | 883 AddRef(); // Balanced in FileSelected / FileSelectionCanceled. |
875 AddRef(); | 884 return RespondLater(); |
876 bool result = ShowPicker( | |
877 ui::SelectFileDialog::SELECT_FOLDER, | |
878 DeveloperPrivateAPI::Get(GetProfile())->GetLastUnpackedDirectory(), | |
879 select_title, | |
880 ui::SelectFileDialog::FileTypeInfo(), | |
881 0); | |
882 return result; | |
883 } | 885 } |
884 | 886 |
885 void DeveloperPrivateLoadUnpackedFunction::FileSelected( | 887 void DeveloperPrivateLoadUnpackedFunction::FileSelected( |
886 const base::FilePath& path) { | 888 const base::FilePath& path) { |
887 ExtensionService* service = GetExtensionService(GetProfile()); | 889 UnpackedInstaller::Create(GetExtensionService(browser_context()))->Load(path); |
888 UnpackedInstaller::Create(service)->Load(path); | 890 DeveloperPrivateAPI::Get(browser_context())->SetLastUnpackedDirectory(path); |
889 DeveloperPrivateAPI::Get(GetProfile())->SetLastUnpackedDirectory(path); | 891 // TODO(devlin): Shouldn't we wait until the extension is loaded? |
890 SendResponse(true); | 892 Respond(NoArguments()); |
891 Release(); | 893 Release(); // Balanced in Run(). |
892 } | 894 } |
893 | 895 |
894 void DeveloperPrivateLoadUnpackedFunction::FileSelectionCanceled() { | 896 void DeveloperPrivateLoadUnpackedFunction::FileSelectionCanceled() { |
895 SendResponse(false); | 897 // This isn't really an error, but we should keep it like this for |
896 Release(); | 898 // backward compatability. |
| 899 Respond(Error(kFileSelectionCanceled)); |
| 900 Release(); // Balanced in Run(). |
897 } | 901 } |
898 | 902 |
899 bool DeveloperPrivateChooseEntryFunction::ShowPicker( | 903 bool DeveloperPrivateChooseEntryFunction::ShowPicker( |
900 ui::SelectFileDialog::Type picker_type, | 904 ui::SelectFileDialog::Type picker_type, |
901 const base::FilePath& last_directory, | |
902 const base::string16& select_title, | 905 const base::string16& select_title, |
903 const ui::SelectFileDialog::FileTypeInfo& info, | 906 const ui::SelectFileDialog::FileTypeInfo& info, |
904 int file_type_index) { | 907 int file_type_index) { |
905 AppWindowRegistry* registry = AppWindowRegistry::Get(GetProfile()); | 908 content::WebContents* web_contents = GetSenderWebContents(); |
906 DCHECK(registry); | 909 if (!web_contents) |
907 AppWindow* app_window = | |
908 registry->GetAppWindowForRenderViewHost(render_view_host()); | |
909 if (!app_window) { | |
910 return false; | 910 return false; |
911 } | |
912 | 911 |
913 // The entry picker will hold a reference to this function instance, | 912 // The entry picker will hold a reference to this function instance, |
914 // and subsequent sending of the function response) until the user has | 913 // and subsequent sending of the function response) until the user has |
915 // selected a file or cancelled the picker. At that point, the picker will | 914 // selected a file or cancelled the picker. At that point, the picker will |
916 // delete itself. | 915 // delete itself. |
917 new EntryPicker(this, | 916 new EntryPicker(this, |
918 app_window->web_contents(), | 917 web_contents, |
919 picker_type, | 918 picker_type, |
920 last_directory, | 919 DeveloperPrivateAPI::Get(browser_context())-> |
| 920 GetLastUnpackedDirectory(), |
921 select_title, | 921 select_title, |
922 info, | 922 info, |
923 file_type_index); | 923 file_type_index); |
924 return true; | 924 return true; |
925 } | 925 } |
926 | 926 |
927 bool DeveloperPrivateChooseEntryFunction::RunAsync() { | |
928 return false; | |
929 } | |
930 | |
931 DeveloperPrivateChooseEntryFunction::~DeveloperPrivateChooseEntryFunction() {} | 927 DeveloperPrivateChooseEntryFunction::~DeveloperPrivateChooseEntryFunction() {} |
932 | 928 |
933 void DeveloperPrivatePackDirectoryFunction::OnPackSuccess( | 929 void DeveloperPrivatePackDirectoryFunction::OnPackSuccess( |
934 const base::FilePath& crx_file, | 930 const base::FilePath& crx_file, |
935 const base::FilePath& pem_file) { | 931 const base::FilePath& pem_file) { |
936 developer::PackDirectoryResponse response; | 932 developer::PackDirectoryResponse response; |
937 response.message = base::UTF16ToUTF8( | 933 response.message = base::UTF16ToUTF8( |
938 PackExtensionJob::StandardSuccessMessage(crx_file, pem_file)); | 934 PackExtensionJob::StandardSuccessMessage(crx_file, pem_file)); |
939 response.status = developer::PACK_STATUS_SUCCESS; | 935 response.status = developer::PACK_STATUS_SUCCESS; |
940 results_ = developer::PackDirectory::Results::Create(response); | 936 Respond(OneArgument(response.ToValue().release())); |
941 SendResponse(true); | 937 Release(); // Balanced in Run(). |
942 Release(); | |
943 } | 938 } |
944 | 939 |
945 void DeveloperPrivatePackDirectoryFunction::OnPackFailure( | 940 void DeveloperPrivatePackDirectoryFunction::OnPackFailure( |
946 const std::string& error, | 941 const std::string& error, |
947 ExtensionCreator::ErrorType error_type) { | 942 ExtensionCreator::ErrorType error_type) { |
948 developer::PackDirectoryResponse response; | 943 developer::PackDirectoryResponse response; |
949 response.message = error; | 944 response.message = error; |
950 if (error_type == ExtensionCreator::kCRXExists) { | 945 if (error_type == ExtensionCreator::kCRXExists) { |
951 response.item_path = item_path_str_; | 946 response.item_path = item_path_str_; |
952 response.pem_path = key_path_str_; | 947 response.pem_path = key_path_str_; |
953 response.override_flags = ExtensionCreator::kOverwriteCRX; | 948 response.override_flags = ExtensionCreator::kOverwriteCRX; |
954 response.status = developer::PACK_STATUS_WARNING; | 949 response.status = developer::PACK_STATUS_WARNING; |
955 } else { | 950 } else { |
956 response.status = developer::PACK_STATUS_ERROR; | 951 response.status = developer::PACK_STATUS_ERROR; |
957 } | 952 } |
958 results_ = developer::PackDirectory::Results::Create(response); | 953 Respond(OneArgument(response.ToValue().release())); |
959 SendResponse(true); | 954 Release(); // Balanced in Run(). |
960 Release(); | |
961 } | 955 } |
962 | 956 |
963 bool DeveloperPrivatePackDirectoryFunction::RunAsync() { | 957 ExtensionFunction::ResponseAction DeveloperPrivatePackDirectoryFunction::Run() { |
964 scoped_ptr<PackDirectory::Params> params( | 958 scoped_ptr<PackDirectory::Params> params( |
965 PackDirectory::Params::Create(*args_)); | 959 PackDirectory::Params::Create(*args_)); |
966 EXTENSION_FUNCTION_VALIDATE(params.get()); | 960 EXTENSION_FUNCTION_VALIDATE(params); |
967 | 961 |
968 int flags = params->flags ? *params->flags : 0; | 962 int flags = params->flags ? *params->flags : 0; |
969 item_path_str_ = params->path; | 963 item_path_str_ = params->path; |
970 if (params->private_key_path) | 964 if (params->private_key_path) |
971 key_path_str_ = *params->private_key_path; | 965 key_path_str_ = *params->private_key_path; |
972 | 966 |
973 base::FilePath root_directory = | 967 base::FilePath root_directory = |
974 base::FilePath::FromUTF8Unsafe(item_path_str_); | 968 base::FilePath::FromUTF8Unsafe(item_path_str_); |
975 | |
976 base::FilePath key_file = base::FilePath::FromUTF8Unsafe(key_path_str_); | 969 base::FilePath key_file = base::FilePath::FromUTF8Unsafe(key_path_str_); |
977 | 970 |
978 developer::PackDirectoryResponse response; | 971 developer::PackDirectoryResponse response; |
979 if (root_directory.empty()) { | 972 if (root_directory.empty()) { |
980 if (item_path_str_.empty()) | 973 if (item_path_str_.empty()) |
981 response.message = l10n_util::GetStringUTF8( | 974 response.message = l10n_util::GetStringUTF8( |
982 IDS_EXTENSION_PACK_DIALOG_ERROR_ROOT_REQUIRED); | 975 IDS_EXTENSION_PACK_DIALOG_ERROR_ROOT_REQUIRED); |
983 else | 976 else |
984 response.message = l10n_util::GetStringUTF8( | 977 response.message = l10n_util::GetStringUTF8( |
985 IDS_EXTENSION_PACK_DIALOG_ERROR_ROOT_INVALID); | 978 IDS_EXTENSION_PACK_DIALOG_ERROR_ROOT_INVALID); |
986 | 979 |
987 response.status = developer::PACK_STATUS_ERROR; | 980 response.status = developer::PACK_STATUS_ERROR; |
988 results_ = developer::PackDirectory::Results::Create(response); | 981 return RespondNow(OneArgument(response.ToValue().release())); |
989 SendResponse(true); | |
990 return true; | |
991 } | 982 } |
992 | 983 |
993 if (!key_path_str_.empty() && key_file.empty()) { | 984 if (!key_path_str_.empty() && key_file.empty()) { |
994 response.message = l10n_util::GetStringUTF8( | 985 response.message = l10n_util::GetStringUTF8( |
995 IDS_EXTENSION_PACK_DIALOG_ERROR_KEY_INVALID); | 986 IDS_EXTENSION_PACK_DIALOG_ERROR_KEY_INVALID); |
996 response.status = developer::PACK_STATUS_ERROR; | 987 response.status = developer::PACK_STATUS_ERROR; |
997 results_ = developer::PackDirectory::Results::Create(response); | 988 return RespondNow(OneArgument(response.ToValue().release())); |
998 SendResponse(true); | |
999 return true; | |
1000 } | 989 } |
1001 | 990 |
1002 // Balanced in OnPackSuccess / OnPackFailure. | 991 AddRef(); // Balanced in OnPackSuccess / OnPackFailure. |
1003 AddRef(); | |
1004 | 992 |
| 993 // TODO(devlin): Why is PackExtensionJob ref-counted? |
1005 pack_job_ = new PackExtensionJob(this, root_directory, key_file, flags); | 994 pack_job_ = new PackExtensionJob(this, root_directory, key_file, flags); |
1006 pack_job_->Start(); | 995 pack_job_->Start(); |
1007 return true; | 996 return RespondLater(); |
1008 } | 997 } |
1009 | 998 |
1010 DeveloperPrivatePackDirectoryFunction::DeveloperPrivatePackDirectoryFunction() | 999 DeveloperPrivatePackDirectoryFunction::DeveloperPrivatePackDirectoryFunction() { |
1011 {} | 1000 } |
1012 | 1001 |
1013 DeveloperPrivatePackDirectoryFunction::~DeveloperPrivatePackDirectoryFunction() | 1002 DeveloperPrivatePackDirectoryFunction:: |
1014 {} | 1003 ~DeveloperPrivatePackDirectoryFunction() {} |
1015 | 1004 |
1016 DeveloperPrivateLoadUnpackedFunction::~DeveloperPrivateLoadUnpackedFunction() {} | 1005 DeveloperPrivateLoadUnpackedFunction::~DeveloperPrivateLoadUnpackedFunction() {} |
1017 | 1006 |
1018 bool DeveloperPrivateLoadDirectoryFunction::RunAsync() { | 1007 bool DeveloperPrivateLoadDirectoryFunction::RunAsync() { |
1019 // TODO(grv) : add unittests. | 1008 // TODO(grv) : add unittests. |
1020 std::string directory_url_str; | 1009 std::string directory_url_str; |
1021 std::string filesystem_name; | 1010 std::string filesystem_name; |
1022 std::string filesystem_path; | 1011 std::string filesystem_path; |
1023 | 1012 |
1024 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); | 1013 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1239 this)); | 1228 this)); |
1240 } | 1229 } |
1241 } | 1230 } |
1242 | 1231 |
1243 DeveloperPrivateLoadDirectoryFunction::DeveloperPrivateLoadDirectoryFunction() | 1232 DeveloperPrivateLoadDirectoryFunction::DeveloperPrivateLoadDirectoryFunction() |
1244 : pending_copy_operations_count_(0), success_(true) {} | 1233 : pending_copy_operations_count_(0), success_(true) {} |
1245 | 1234 |
1246 DeveloperPrivateLoadDirectoryFunction::~DeveloperPrivateLoadDirectoryFunction() | 1235 DeveloperPrivateLoadDirectoryFunction::~DeveloperPrivateLoadDirectoryFunction() |
1247 {} | 1236 {} |
1248 | 1237 |
1249 bool DeveloperPrivateChoosePathFunction::RunAsync() { | 1238 ExtensionFunction::ResponseAction DeveloperPrivateChoosePathFunction::Run() { |
1250 scoped_ptr<developer::ChoosePath::Params> params( | 1239 scoped_ptr<developer::ChoosePath::Params> params( |
1251 developer::ChoosePath::Params::Create(*args_)); | 1240 developer::ChoosePath::Params::Create(*args_)); |
1252 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 1241 EXTENSION_FUNCTION_VALIDATE(params); |
1253 | 1242 |
1254 ui::SelectFileDialog::Type type = ui::SelectFileDialog::SELECT_FOLDER; | 1243 ui::SelectFileDialog::Type type = ui::SelectFileDialog::SELECT_FOLDER; |
1255 ui::SelectFileDialog::FileTypeInfo info; | 1244 ui::SelectFileDialog::FileTypeInfo info; |
1256 if (params->select_type == developer::SELECT_TYPE_FILE) { | 1245 |
| 1246 if (params->select_type == developer::SELECT_TYPE_FILE) |
1257 type = ui::SelectFileDialog::SELECT_OPEN_FILE; | 1247 type = ui::SelectFileDialog::SELECT_OPEN_FILE; |
1258 } | |
1259 base::string16 select_title; | 1248 base::string16 select_title; |
1260 | 1249 |
1261 int file_type_index = 0; | 1250 int file_type_index = 0; |
1262 if (params->file_type == developer::FILE_TYPE_LOAD) { | 1251 if (params->file_type == developer::FILE_TYPE_LOAD) { |
1263 select_title = l10n_util::GetStringUTF16(IDS_EXTENSION_LOAD_FROM_DIRECTORY); | 1252 select_title = l10n_util::GetStringUTF16(IDS_EXTENSION_LOAD_FROM_DIRECTORY); |
1264 } else if (params->file_type == developer::FILE_TYPE_PEM) { | 1253 } else if (params->file_type == developer::FILE_TYPE_PEM) { |
1265 select_title = l10n_util::GetStringUTF16( | 1254 select_title = l10n_util::GetStringUTF16( |
1266 IDS_EXTENSION_PACK_DIALOG_SELECT_KEY); | 1255 IDS_EXTENSION_PACK_DIALOG_SELECT_KEY); |
1267 info.extensions.push_back(std::vector<base::FilePath::StringType>()); | 1256 info.extensions.push_back(std::vector<base::FilePath::StringType>( |
1268 info.extensions.front().push_back(FILE_PATH_LITERAL("pem")); | 1257 1, FILE_PATH_LITERAL("pem"))); |
1269 info.extension_description_overrides.push_back( | 1258 info.extension_description_overrides.push_back( |
1270 l10n_util::GetStringUTF16( | 1259 l10n_util::GetStringUTF16( |
1271 IDS_EXTENSION_PACK_DIALOG_KEY_FILE_TYPE_DESCRIPTION)); | 1260 IDS_EXTENSION_PACK_DIALOG_KEY_FILE_TYPE_DESCRIPTION)); |
1272 info.include_all_files = true; | 1261 info.include_all_files = true; |
1273 file_type_index = 1; | 1262 file_type_index = 1; |
1274 } else { | 1263 } else { |
1275 NOTREACHED(); | 1264 NOTREACHED(); |
1276 } | 1265 } |
1277 | 1266 |
1278 // Balanced by FileSelected / FileSelectionCanceled. | 1267 if (!ShowPicker( |
1279 AddRef(); | 1268 type, |
1280 bool result = ShowPicker( | 1269 select_title, |
1281 type, | 1270 info, |
1282 DeveloperPrivateAPI::Get(GetProfile())->GetLastUnpackedDirectory(), | 1271 file_type_index)) { |
1283 select_title, | 1272 return RespondNow(Error(kCouldNotShowSelectFileDialogError)); |
1284 info, | 1273 } |
1285 file_type_index); | 1274 |
1286 return result; | 1275 AddRef(); // Balanced by FileSelected / FileSelectionCanceled. |
| 1276 return RespondLater(); |
1287 } | 1277 } |
1288 | 1278 |
1289 void DeveloperPrivateChoosePathFunction::FileSelected( | 1279 void DeveloperPrivateChoosePathFunction::FileSelected( |
1290 const base::FilePath& path) { | 1280 const base::FilePath& path) { |
1291 SetResult(new base::StringValue(base::UTF16ToUTF8(path.LossyDisplayName()))); | 1281 Respond(OneArgument(new base::StringValue(path.LossyDisplayName()))); |
1292 SendResponse(true); | |
1293 Release(); | 1282 Release(); |
1294 } | 1283 } |
1295 | 1284 |
1296 void DeveloperPrivateChoosePathFunction::FileSelectionCanceled() { | 1285 void DeveloperPrivateChoosePathFunction::FileSelectionCanceled() { |
1297 SendResponse(false); | 1286 // This isn't really an error, but we should keep it like this for |
| 1287 // backward compatability. |
| 1288 Respond(Error(kFileSelectionCanceled)); |
1298 Release(); | 1289 Release(); |
1299 } | 1290 } |
1300 | 1291 |
1301 DeveloperPrivateChoosePathFunction::~DeveloperPrivateChoosePathFunction() {} | 1292 DeveloperPrivateChoosePathFunction::~DeveloperPrivateChoosePathFunction() {} |
1302 | 1293 |
1303 bool DeveloperPrivateIsProfileManagedFunction::RunSync() { | 1294 bool DeveloperPrivateIsProfileManagedFunction::RunSync() { |
1304 SetResult(new base::FundamentalValue(GetProfile()->IsSupervised())); | 1295 SetResult(new base::FundamentalValue(GetProfile()->IsSupervised())); |
1305 return true; | 1296 return true; |
1306 } | 1297 } |
1307 | 1298 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1353 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0u, &dict)); | 1344 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0u, &dict)); |
1354 | 1345 |
1355 error_ui_util::HandleOpenDevTools(dict); | 1346 error_ui_util::HandleOpenDevTools(dict); |
1356 | 1347 |
1357 return true; | 1348 return true; |
1358 } | 1349 } |
1359 | 1350 |
1360 } // namespace api | 1351 } // namespace api |
1361 | 1352 |
1362 } // namespace extensions | 1353 } // namespace extensions |
OLD | NEW |