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(item_path_str_); |
974 base::FilePath::FromUTF8Unsafe(item_path_str_); | 968 base::FilePath key_file(key_path_str_); |
975 | |
976 base::FilePath key_file = base::FilePath::FromUTF8Unsafe(key_path_str_); | |
977 | 969 |
978 developer::PackDirectoryResponse response; | 970 developer::PackDirectoryResponse response; |
979 if (root_directory.empty()) { | 971 if (root_directory.empty()) { |
980 if (item_path_str_.empty()) | 972 if (item_path_str_.empty()) |
981 response.message = l10n_util::GetStringUTF8( | 973 response.message = l10n_util::GetStringUTF8( |
982 IDS_EXTENSION_PACK_DIALOG_ERROR_ROOT_REQUIRED); | 974 IDS_EXTENSION_PACK_DIALOG_ERROR_ROOT_REQUIRED); |
983 else | 975 else |
984 response.message = l10n_util::GetStringUTF8( | 976 response.message = l10n_util::GetStringUTF8( |
985 IDS_EXTENSION_PACK_DIALOG_ERROR_ROOT_INVALID); | 977 IDS_EXTENSION_PACK_DIALOG_ERROR_ROOT_INVALID); |
986 | 978 |
987 response.status = developer::PACK_STATUS_ERROR; | 979 response.status = developer::PACK_STATUS_ERROR; |
988 results_ = developer::PackDirectory::Results::Create(response); | 980 return RespondNow(OneArgument(response.ToValue().release())); |
989 SendResponse(true); | |
990 return true; | |
991 } | 981 } |
992 | 982 |
993 if (!key_path_str_.empty() && key_file.empty()) { | 983 if (!key_path_str_.empty() && key_file.empty()) { |
994 response.message = l10n_util::GetStringUTF8( | 984 response.message = l10n_util::GetStringUTF8( |
995 IDS_EXTENSION_PACK_DIALOG_ERROR_KEY_INVALID); | 985 IDS_EXTENSION_PACK_DIALOG_ERROR_KEY_INVALID); |
996 response.status = developer::PACK_STATUS_ERROR; | 986 response.status = developer::PACK_STATUS_ERROR; |
997 results_ = developer::PackDirectory::Results::Create(response); | 987 return RespondNow(OneArgument(response.ToValue().release())); |
998 SendResponse(true); | |
999 return true; | |
1000 } | 988 } |
1001 | 989 |
1002 // Balanced in OnPackSuccess / OnPackFailure. | 990 AddRef(); // Balanced in OnPackSuccess / OnPackFailure. |
1003 AddRef(); | |
1004 | 991 |
| 992 // TODO(devlin): Why is PackExtensionJob ref-counted? |
1005 pack_job_ = new PackExtensionJob(this, root_directory, key_file, flags); | 993 pack_job_ = new PackExtensionJob(this, root_directory, key_file, flags); |
1006 pack_job_->Start(); | 994 pack_job_->Start(); |
1007 return true; | 995 return RespondLater(); |
1008 } | 996 } |
1009 | 997 |
1010 DeveloperPrivatePackDirectoryFunction::DeveloperPrivatePackDirectoryFunction() | 998 DeveloperPrivatePackDirectoryFunction::DeveloperPrivatePackDirectoryFunction() { |
1011 {} | 999 } |
1012 | 1000 |
1013 DeveloperPrivatePackDirectoryFunction::~DeveloperPrivatePackDirectoryFunction() | 1001 DeveloperPrivatePackDirectoryFunction:: |
1014 {} | 1002 ~DeveloperPrivatePackDirectoryFunction() {} |
1015 | 1003 |
1016 DeveloperPrivateLoadUnpackedFunction::~DeveloperPrivateLoadUnpackedFunction() {} | 1004 DeveloperPrivateLoadUnpackedFunction::~DeveloperPrivateLoadUnpackedFunction() {} |
1017 | 1005 |
1018 bool DeveloperPrivateLoadDirectoryFunction::RunAsync() { | 1006 bool DeveloperPrivateLoadDirectoryFunction::RunAsync() { |
1019 // TODO(grv) : add unittests. | 1007 // TODO(grv) : add unittests. |
1020 std::string directory_url_str; | 1008 std::string directory_url_str; |
1021 std::string filesystem_name; | 1009 std::string filesystem_name; |
1022 std::string filesystem_path; | 1010 std::string filesystem_path; |
1023 | 1011 |
1024 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); | 1012 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1239 this)); | 1227 this)); |
1240 } | 1228 } |
1241 } | 1229 } |
1242 | 1230 |
1243 DeveloperPrivateLoadDirectoryFunction::DeveloperPrivateLoadDirectoryFunction() | 1231 DeveloperPrivateLoadDirectoryFunction::DeveloperPrivateLoadDirectoryFunction() |
1244 : pending_copy_operations_count_(0), success_(true) {} | 1232 : pending_copy_operations_count_(0), success_(true) {} |
1245 | 1233 |
1246 DeveloperPrivateLoadDirectoryFunction::~DeveloperPrivateLoadDirectoryFunction() | 1234 DeveloperPrivateLoadDirectoryFunction::~DeveloperPrivateLoadDirectoryFunction() |
1247 {} | 1235 {} |
1248 | 1236 |
1249 bool DeveloperPrivateChoosePathFunction::RunAsync() { | 1237 ExtensionFunction::ResponseAction DeveloperPrivateChoosePathFunction::Run() { |
1250 scoped_ptr<developer::ChoosePath::Params> params( | 1238 scoped_ptr<developer::ChoosePath::Params> params( |
1251 developer::ChoosePath::Params::Create(*args_)); | 1239 developer::ChoosePath::Params::Create(*args_)); |
1252 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 1240 EXTENSION_FUNCTION_VALIDATE(params); |
1253 | 1241 |
1254 ui::SelectFileDialog::Type type = ui::SelectFileDialog::SELECT_FOLDER; | 1242 ui::SelectFileDialog::Type type = ui::SelectFileDialog::SELECT_FOLDER; |
1255 ui::SelectFileDialog::FileTypeInfo info; | 1243 ui::SelectFileDialog::FileTypeInfo info; |
1256 if (params->select_type == developer::SELECT_TYPE_FILE) { | 1244 |
| 1245 if (params->select_type == developer::SELECT_TYPE_FILE) |
1257 type = ui::SelectFileDialog::SELECT_OPEN_FILE; | 1246 type = ui::SelectFileDialog::SELECT_OPEN_FILE; |
1258 } | |
1259 base::string16 select_title; | 1247 base::string16 select_title; |
1260 | 1248 |
1261 int file_type_index = 0; | 1249 int file_type_index = 0; |
1262 if (params->file_type == developer::FILE_TYPE_LOAD) { | 1250 if (params->file_type == developer::FILE_TYPE_LOAD) { |
1263 select_title = l10n_util::GetStringUTF16(IDS_EXTENSION_LOAD_FROM_DIRECTORY); | 1251 select_title = l10n_util::GetStringUTF16(IDS_EXTENSION_LOAD_FROM_DIRECTORY); |
1264 } else if (params->file_type == developer::FILE_TYPE_PEM) { | 1252 } else if (params->file_type == developer::FILE_TYPE_PEM) { |
1265 select_title = l10n_util::GetStringUTF16( | 1253 select_title = l10n_util::GetStringUTF16( |
1266 IDS_EXTENSION_PACK_DIALOG_SELECT_KEY); | 1254 IDS_EXTENSION_PACK_DIALOG_SELECT_KEY); |
1267 info.extensions.push_back(std::vector<base::FilePath::StringType>()); | 1255 info.extensions.push_back(std::vector<base::FilePath::StringType>( |
1268 info.extensions.front().push_back(FILE_PATH_LITERAL("pem")); | 1256 1, FILE_PATH_LITERAL("pem"))); |
1269 info.extension_description_overrides.push_back( | 1257 info.extension_description_overrides.push_back( |
1270 l10n_util::GetStringUTF16( | 1258 l10n_util::GetStringUTF16( |
1271 IDS_EXTENSION_PACK_DIALOG_KEY_FILE_TYPE_DESCRIPTION)); | 1259 IDS_EXTENSION_PACK_DIALOG_KEY_FILE_TYPE_DESCRIPTION)); |
1272 info.include_all_files = true; | 1260 info.include_all_files = true; |
1273 file_type_index = 1; | 1261 file_type_index = 1; |
1274 } else { | 1262 } else { |
1275 NOTREACHED(); | 1263 NOTREACHED(); |
1276 } | 1264 } |
1277 | 1265 |
1278 // Balanced by FileSelected / FileSelectionCanceled. | 1266 if (!ShowPicker( |
1279 AddRef(); | 1267 type, |
1280 bool result = ShowPicker( | 1268 select_title, |
1281 type, | 1269 info, |
1282 DeveloperPrivateAPI::Get(GetProfile())->GetLastUnpackedDirectory(), | 1270 file_type_index)) { |
1283 select_title, | 1271 return RespondNow(Error(kCouldNotShowSelectFileDialogError)); |
1284 info, | 1272 } |
1285 file_type_index); | 1273 |
1286 return result; | 1274 AddRef(); // Balanced by FileSelected / FileSelectionCanceled. |
| 1275 return RespondLater(); |
1287 } | 1276 } |
1288 | 1277 |
1289 void DeveloperPrivateChoosePathFunction::FileSelected( | 1278 void DeveloperPrivateChoosePathFunction::FileSelected( |
1290 const base::FilePath& path) { | 1279 const base::FilePath& path) { |
1291 SetResult(new base::StringValue(base::UTF16ToUTF8(path.LossyDisplayName()))); | 1280 Respond(OneArgument(new base::StringValue(path.LossyDisplayName()))); |
1292 SendResponse(true); | |
1293 Release(); | 1281 Release(); |
1294 } | 1282 } |
1295 | 1283 |
1296 void DeveloperPrivateChoosePathFunction::FileSelectionCanceled() { | 1284 void DeveloperPrivateChoosePathFunction::FileSelectionCanceled() { |
1297 SendResponse(false); | 1285 // This isn't really an error, but we should keep it like this for |
| 1286 // backward compatability. |
| 1287 Respond(Error(kFileSelectionCanceled)); |
1298 Release(); | 1288 Release(); |
1299 } | 1289 } |
1300 | 1290 |
1301 DeveloperPrivateChoosePathFunction::~DeveloperPrivateChoosePathFunction() {} | 1291 DeveloperPrivateChoosePathFunction::~DeveloperPrivateChoosePathFunction() {} |
1302 | 1292 |
1303 bool DeveloperPrivateIsProfileManagedFunction::RunSync() { | 1293 bool DeveloperPrivateIsProfileManagedFunction::RunSync() { |
1304 SetResult(new base::FundamentalValue(GetProfile()->IsSupervised())); | 1294 SetResult(new base::FundamentalValue(GetProfile()->IsSupervised())); |
1305 return true; | 1295 return true; |
1306 } | 1296 } |
1307 | 1297 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1353 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0u, &dict)); | 1343 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0u, &dict)); |
1354 | 1344 |
1355 error_ui_util::HandleOpenDevTools(dict); | 1345 error_ui_util::HandleOpenDevTools(dict); |
1356 | 1346 |
1357 return true; | 1347 return true; |
1358 } | 1348 } |
1359 | 1349 |
1360 } // namespace api | 1350 } // namespace api |
1361 | 1351 |
1362 } // namespace extensions | 1352 } // namespace extensions |
OLD | NEW |