| 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/ui/webui/extensions/pack_extension_handler.h" | 5 #include "chrome/browser/ui/webui/extensions/pack_extension_handler.h" |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/extension_creator.h" | 7 #include "chrome/browser/extensions/extension_creator.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "content/public/browser/web_contents.h" |
| 11 #include "content/public/browser/web_contents_view.h" |
| 10 #include "content/public/browser/web_ui.h" | 12 #include "content/public/browser/web_ui.h" |
| 11 #include "grit/generated_resources.h" | 13 #include "grit/generated_resources.h" |
| 12 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
| 13 | 15 |
| 14 PackExtensionHandler::PackExtensionHandler() { | 16 PackExtensionHandler::PackExtensionHandler() { |
| 15 } | 17 } |
| 16 | 18 |
| 17 PackExtensionHandler::~PackExtensionHandler() { | 19 PackExtensionHandler::~PackExtensionHandler() { |
| 20 // There may be pending file dialogs, we need to tell them that we've gone |
| 21 // away so they don't try and call back to us. |
| 22 if (load_extension_dialog_) |
| 23 load_extension_dialog_->ListenerDestroyed(); |
| 24 |
| 18 if (pack_job_.get()) | 25 if (pack_job_.get()) |
| 19 pack_job_->ClearClient(); | 26 pack_job_->ClearClient(); |
| 20 } | 27 } |
| 21 | 28 |
| 22 void PackExtensionHandler::GetLocalizedValues( | 29 void PackExtensionHandler::GetLocalizedValues( |
| 23 DictionaryValue* localized_strings) { | 30 DictionaryValue* localized_strings) { |
| 24 DCHECK(localized_strings); | 31 DCHECK(localized_strings); |
| 25 | 32 |
| 26 localized_strings->SetString("packExtensionOverlay", | 33 localized_strings->SetString("packExtensionOverlay", |
| 27 l10n_util::GetStringUTF16(IDS_EXTENSION_PACK_DIALOG_TITLE)); | 34 l10n_util::GetStringUTF16(IDS_EXTENSION_PACK_DIALOG_TITLE)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 40 l10n_util::GetStringUTF16(IDS_EXTENSION_PACK_DIALOG_BROWSE)); | 47 l10n_util::GetStringUTF16(IDS_EXTENSION_PACK_DIALOG_BROWSE)); |
| 41 localized_strings->SetString("packExtensionProceedAnyway", | 48 localized_strings->SetString("packExtensionProceedAnyway", |
| 42 l10n_util::GetStringUTF16(IDS_EXTENSION_PROCEED_ANYWAY)); | 49 l10n_util::GetStringUTF16(IDS_EXTENSION_PROCEED_ANYWAY)); |
| 43 localized_strings->SetString("packExtensionWarningTitle", | 50 localized_strings->SetString("packExtensionWarningTitle", |
| 44 l10n_util::GetStringUTF16(IDS_EXTENSION_PACK_WARNING_TITLE)); | 51 l10n_util::GetStringUTF16(IDS_EXTENSION_PACK_WARNING_TITLE)); |
| 45 localized_strings->SetString("packExtensionErrorTitle", | 52 localized_strings->SetString("packExtensionErrorTitle", |
| 46 l10n_util::GetStringUTF16(IDS_EXTENSION_PACK_ERROR_TITLE)); | 53 l10n_util::GetStringUTF16(IDS_EXTENSION_PACK_ERROR_TITLE)); |
| 47 } | 54 } |
| 48 | 55 |
| 49 void PackExtensionHandler::RegisterMessages() { | 56 void PackExtensionHandler::RegisterMessages() { |
| 50 // Setup handlers specific to this panel. | 57 web_ui()->RegisterMessageCallback( |
| 51 web_ui()->RegisterMessageCallback("pack", | 58 "pack", |
| 52 base::Bind(&PackExtensionHandler::HandlePackMessage, | 59 base::Bind(&PackExtensionHandler::HandlePackMessage, |
| 53 base::Unretained(this))); | 60 base::Unretained(this))); |
| 61 web_ui()->RegisterMessageCallback( |
| 62 "packExtensionSelectFilePath", |
| 63 base::Bind(&PackExtensionHandler::HandleSelectFilePathMessage, |
| 64 base::Unretained(this))); |
| 54 } | 65 } |
| 55 | 66 |
| 56 void PackExtensionHandler::OnPackSuccess(const FilePath& crx_file, | 67 void PackExtensionHandler::OnPackSuccess(const FilePath& crx_file, |
| 57 const FilePath& pem_file) { | 68 const FilePath& pem_file) { |
| 58 ListValue arguments; | 69 ListValue arguments; |
| 59 arguments.Append(Value::CreateStringValue( | 70 arguments.Append(Value::CreateStringValue( |
| 60 UTF16ToUTF8(PackExtensionJob::StandardSuccessMessage(crx_file, | 71 UTF16ToUTF8(PackExtensionJob::StandardSuccessMessage(crx_file, |
| 61 pem_file)))); | 72 pem_file)))); |
| 62 web_ui()->CallJavascriptFunction( | 73 web_ui()->CallJavascriptFunction( |
| 63 "PackExtensionOverlay.showSuccessMessage", arguments); | 74 "PackExtensionOverlay.showSuccessMessage", arguments); |
| 64 } | 75 } |
| 65 | 76 |
| 66 void PackExtensionHandler::OnPackFailure(const std::string& error, | 77 void PackExtensionHandler::OnPackFailure(const std::string& error, |
| 67 ExtensionCreator::ErrorType type) { | 78 ExtensionCreator::ErrorType type) { |
| 68 if (type == ExtensionCreator::kCRXExists) { | 79 if (type == ExtensionCreator::kCRXExists) { |
| 69 base::StringValue error_str(error); | 80 base::StringValue error_str(error); |
| 70 base::StringValue extension_path_str(extension_path_); | 81 base::StringValue extension_path_str(extension_path_); |
| 71 base::StringValue key_path_str(private_key_path_); | 82 base::StringValue key_path_str(private_key_path_); |
| 72 base::FundamentalValue overwrite_flag(ExtensionCreator::kOverwriteCRX); | 83 base::FundamentalValue overwrite_flag(ExtensionCreator::kOverwriteCRX); |
| 73 | 84 |
| 74 web_ui()->CallJavascriptFunction( | 85 web_ui()->CallJavascriptFunction( |
| 75 "ExtensionSettings.askToOverrideWarning", error_str, extension_path_str, | 86 "ExtensionSettings.askToOverrideWarning", error_str, extension_path_str, |
| 76 key_path_str, overwrite_flag); | 87 key_path_str, overwrite_flag); |
| 77 } else { | 88 } else { |
| 78 ShowAlert(error); | 89 ShowAlert(error); |
| 79 } | 90 } |
| 80 } | 91 } |
| 81 | 92 |
| 93 void PackExtensionHandler::FileSelected(const FilePath& path, int index, |
| 94 void* params) { |
| 95 ListValue results; |
| 96 results.Append(Value::CreateStringValue(path.value())); |
| 97 web_ui()->CallJavascriptFunction("window.handleFilePathSelected", results); |
| 98 } |
| 99 |
| 100 void PackExtensionHandler::MultiFilesSelected( |
| 101 const std::vector<FilePath>& files, void* params) { |
| 102 NOTREACHED(); |
| 103 } |
| 104 |
| 82 void PackExtensionHandler::HandlePackMessage(const ListValue* args) { | 105 void PackExtensionHandler::HandlePackMessage(const ListValue* args) { |
| 83 | 106 |
| 84 CHECK_EQ(3U, args->GetSize()); | 107 DCHECK_EQ(3U, args->GetSize()); |
| 85 CHECK(args->GetString(0, &extension_path_)); | 108 |
| 86 CHECK(args->GetString(1, &private_key_path_)); | 109 if (!args->GetString(0, &extension_path_) || |
| 110 !args->GetString(1, &private_key_path_)) |
| 111 NOTREACHED(); |
| 87 | 112 |
| 88 double flags_double = 0.0; | 113 double flags_double = 0.0; |
| 89 CHECK(args->GetDouble(2, &flags_double)); | 114 if (!args->GetDouble(2, &flags_double)) |
| 115 NOTREACHED(); |
| 116 |
| 90 int run_flags = static_cast<int>(flags_double); | 117 int run_flags = static_cast<int>(flags_double); |
| 91 | 118 |
| 92 FilePath root_directory = | 119 FilePath root_directory = |
| 93 FilePath::FromWStringHack(UTF8ToWide(extension_path_)); | 120 FilePath::FromWStringHack(UTF8ToWide(extension_path_)); |
| 94 FilePath key_file = FilePath::FromWStringHack(UTF8ToWide(private_key_path_)); | 121 FilePath key_file = FilePath::FromWStringHack(UTF8ToWide(private_key_path_)); |
| 95 | 122 |
| 96 if (root_directory.empty()) { | 123 if (root_directory.empty()) { |
| 97 if (extension_path_.empty()) { | 124 if (extension_path_.empty()) { |
| 98 ShowAlert(l10n_util::GetStringUTF8( | 125 ShowAlert(l10n_util::GetStringUTF8( |
| 99 IDS_EXTENSION_PACK_DIALOG_ERROR_ROOT_REQUIRED)); | 126 IDS_EXTENSION_PACK_DIALOG_ERROR_ROOT_REQUIRED)); |
| 100 } else { | 127 } else { |
| 101 ShowAlert(l10n_util::GetStringUTF8( | 128 ShowAlert(l10n_util::GetStringUTF8( |
| 102 IDS_EXTENSION_PACK_DIALOG_ERROR_ROOT_INVALID)); | 129 IDS_EXTENSION_PACK_DIALOG_ERROR_ROOT_INVALID)); |
| 103 } | 130 } |
| 104 | 131 |
| 105 return; | 132 return; |
| 106 } | 133 } |
| 107 | 134 |
| 108 if (!private_key_path_.empty() && key_file.empty()) { | 135 if (!private_key_path_.empty() && key_file.empty()) { |
| 109 ShowAlert(l10n_util::GetStringUTF8( | 136 ShowAlert(l10n_util::GetStringUTF8( |
| 110 IDS_EXTENSION_PACK_DIALOG_ERROR_KEY_INVALID)); | 137 IDS_EXTENSION_PACK_DIALOG_ERROR_KEY_INVALID)); |
| 111 return; | 138 return; |
| 112 } | 139 } |
| 113 | 140 |
| 114 pack_job_ = new PackExtensionJob(this, root_directory, key_file, run_flags); | 141 pack_job_ = new PackExtensionJob(this, root_directory, key_file, run_flags); |
| 115 pack_job_->Start(); | 142 pack_job_->Start(); |
| 116 } | 143 } |
| 117 | 144 |
| 145 void PackExtensionHandler::HandleSelectFilePathMessage( |
| 146 const ListValue* args) { |
| 147 DCHECK_EQ(2U, args->GetSize()); |
| 148 |
| 149 std::string select_type; |
| 150 if (!args->GetString(0, &select_type)) |
| 151 NOTREACHED(); |
| 152 |
| 153 std::string operation; |
| 154 if (!args->GetString(1, &operation)) |
| 155 NOTREACHED(); |
| 156 |
| 157 SelectFileDialog::Type type = SelectFileDialog::SELECT_FOLDER; |
| 158 SelectFileDialog::FileTypeInfo info; |
| 159 int file_type_index = 0; |
| 160 if (select_type == "file") |
| 161 type = SelectFileDialog::SELECT_OPEN_FILE; |
| 162 |
| 163 string16 select_title; |
| 164 if (operation == "load") { |
| 165 select_title = l10n_util::GetStringUTF16(IDS_EXTENSION_LOAD_FROM_DIRECTORY); |
| 166 } else if (operation == "pem") { |
| 167 select_title = l10n_util::GetStringUTF16( |
| 168 IDS_EXTENSION_PACK_DIALOG_SELECT_KEY); |
| 169 info.extensions.push_back(std::vector<FilePath::StringType>()); |
| 170 info.extensions.front().push_back(FILE_PATH_LITERAL("pem")); |
| 171 info.extension_description_overrides.push_back( |
| 172 l10n_util::GetStringUTF16( |
| 173 IDS_EXTENSION_PACK_DIALOG_KEY_FILE_TYPE_DESCRIPTION)); |
| 174 info.include_all_files = true; |
| 175 file_type_index = 1; |
| 176 } else { |
| 177 NOTREACHED(); |
| 178 } |
| 179 |
| 180 load_extension_dialog_ = SelectFileDialog::Create(this); |
| 181 load_extension_dialog_->SelectFile( |
| 182 type, select_title, FilePath(), &info, file_type_index, |
| 183 FILE_PATH_LITERAL(""), web_ui()->GetWebContents(), |
| 184 web_ui()->GetWebContents()->GetView()->GetTopLevelNativeWindow(), NULL); |
| 185 } |
| 186 |
| 118 void PackExtensionHandler::ShowAlert(const std::string& message) { | 187 void PackExtensionHandler::ShowAlert(const std::string& message) { |
| 119 ListValue arguments; | 188 ListValue arguments; |
| 120 arguments.Append(Value::CreateStringValue(message)); | 189 arguments.Append(Value::CreateStringValue(message)); |
| 121 web_ui()->CallJavascriptFunction("PackExtensionOverlay.showError", arguments); | 190 web_ui()->CallJavascriptFunction("PackExtensionOverlay.showError", arguments); |
| 122 } | 191 } |
| OLD | NEW |