OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/ui/webui/options/pack_extension_handler.h" | |
6 | |
7 #include "chrome/browser/extensions/extension_creator.h" | |
8 #include "base/bind.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "content/public/browser/web_ui.h" | |
11 #include "grit/generated_resources.h" | |
12 #include "ui/base/l10n/l10n_util.h" | |
13 | |
14 PackExtensionHandler::PackExtensionHandler() { | |
15 } | |
16 | |
17 PackExtensionHandler::~PackExtensionHandler() { | |
18 if (pack_job_.get()) | |
19 pack_job_->ClearClient(); | |
20 } | |
21 | |
22 void PackExtensionHandler::GetLocalizedValues( | |
23 DictionaryValue* localized_strings) { | |
24 DCHECK(localized_strings); | |
25 RegisterTitle(localized_strings, "packExtensionOverlay", | |
26 IDS_EXTENSION_PACK_DIALOG_TITLE); | |
27 | |
28 localized_strings->SetString("packExtensionOverlay", | |
29 l10n_util::GetStringUTF16(IDS_EXTENSION_PACK_DIALOG_TITLE)); | |
30 localized_strings->SetString("packExtensionHeading", | |
31 l10n_util::GetStringUTF16(IDS_EXTENSION_PACK_DIALOG_HEADING)); | |
32 localized_strings->SetString("packExtensionCommit", | |
33 l10n_util::GetStringUTF16(IDS_EXTENSION_PACK_BUTTON)); | |
34 localized_strings->SetString("ok", l10n_util::GetStringUTF16(IDS_OK)); | |
35 localized_strings->SetString("cancel", l10n_util::GetStringUTF16(IDS_CANCEL)); | |
36 localized_strings->SetString("packExtensionRootDir", | |
37 l10n_util::GetStringUTF16( | |
38 IDS_EXTENSION_PACK_DIALOG_ROOT_DIRECTORY_LABEL)); | |
39 localized_strings->SetString("packExtensionPrivateKey", | |
40 l10n_util::GetStringUTF16(IDS_EXTENSION_PACK_DIALOG_PRIVATE_KEY_LABEL)); | |
41 localized_strings->SetString("packExtensionBrowseButton", | |
42 l10n_util::GetStringUTF16(IDS_EXTENSION_PACK_DIALOG_BROWSE)); | |
43 localized_strings->SetString("packExtensionProceedAnyway", | |
44 l10n_util::GetStringUTF16(IDS_EXTENSION_PROCEED_ANYWAY)); | |
45 localized_strings->SetString("packExtensionWarningTitle", | |
46 l10n_util::GetStringUTF16(IDS_EXTENSION_PACK_WARNING_TITLE)); | |
47 localized_strings->SetString("packExtensionErrorTitle", | |
48 l10n_util::GetStringUTF16(IDS_EXTENSION_PACK_ERROR_TITLE)); | |
49 } | |
50 | |
51 void PackExtensionHandler::RegisterMessages() { | |
52 // Setup handlers specific to this panel. | |
53 web_ui()->RegisterMessageCallback("pack", | |
54 base::Bind(&PackExtensionHandler::HandlePackMessage, | |
55 base::Unretained(this))); | |
56 } | |
57 | |
58 void PackExtensionHandler::OnPackSuccess(const FilePath& crx_file, | |
59 const FilePath& pem_file) { | |
60 ListValue arguments; | |
61 arguments.Append(Value::CreateStringValue( | |
62 UTF16ToUTF8(PackExtensionJob::StandardSuccessMessage(crx_file, | |
63 pem_file)))); | |
64 web_ui()->CallJavascriptFunction( | |
65 "PackExtensionOverlay.showSuccessMessage", arguments); | |
66 } | |
67 | |
68 void PackExtensionHandler::OnPackFailure(const std::string& error, | |
69 ExtensionCreator::ErrorType type) { | |
70 if (type == ExtensionCreator::kCRXExists) { | |
71 base::StringValue error_str(error); | |
72 base::StringValue extension_path_str(extension_path_); | |
73 base::StringValue key_path_str(private_key_path_); | |
74 base::FundamentalValue overwrite_flag(ExtensionCreator::kOverwriteCRX); | |
75 | |
76 web_ui()->CallJavascriptFunction( | |
77 "ExtensionSettings.askToOverrideWarning", error_str, extension_path_str, | |
78 key_path_str, overwrite_flag); | |
79 } else { | |
80 ShowAlert(error); | |
81 } | |
82 } | |
83 | |
84 void PackExtensionHandler::HandlePackMessage(const ListValue* args) { | |
85 | |
86 CHECK_EQ(3U, args->GetSize()); | |
87 CHECK(args->GetString(0, &extension_path_)); | |
88 CHECK(args->GetString(1, &private_key_path_)); | |
89 | |
90 double flags_double = 0.0; | |
91 CHECK(args->GetDouble(2, &flags_double)); | |
92 int run_flags = static_cast<int>(flags_double); | |
93 | |
94 FilePath root_directory = | |
95 FilePath::FromWStringHack(UTF8ToWide(extension_path_)); | |
96 FilePath key_file = FilePath::FromWStringHack(UTF8ToWide(private_key_path_)); | |
97 | |
98 if (root_directory.empty()) { | |
99 if (extension_path_.empty()) { | |
100 ShowAlert(l10n_util::GetStringUTF8( | |
101 IDS_EXTENSION_PACK_DIALOG_ERROR_ROOT_REQUIRED)); | |
102 } else { | |
103 ShowAlert(l10n_util::GetStringUTF8( | |
104 IDS_EXTENSION_PACK_DIALOG_ERROR_ROOT_INVALID)); | |
105 } | |
106 | |
107 return; | |
108 } | |
109 | |
110 if (!private_key_path_.empty() && key_file.empty()) { | |
111 ShowAlert(l10n_util::GetStringUTF8( | |
112 IDS_EXTENSION_PACK_DIALOG_ERROR_KEY_INVALID)); | |
113 return; | |
114 } | |
115 | |
116 pack_job_ = new PackExtensionJob(this, root_directory, key_file, run_flags); | |
117 pack_job_->Start(); | |
118 } | |
119 | |
120 void PackExtensionHandler::ShowAlert(const std::string& message) { | |
121 ListValue arguments; | |
122 arguments.Append(Value::CreateStringValue(message)); | |
123 web_ui()->CallJavascriptFunction("PackExtensionOverlay.showError", arguments); | |
124 } | |
OLD | NEW |