OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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/extensions_ui.h" | 5 #include "chrome/browser/extensions/extensions_ui.h" |
6 | 6 |
7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/thread.h" | 10 #include "base/thread.h" |
11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/chrome_thread.h" |
12 #include "chrome/browser/debugger/devtools_manager.h" | 13 #include "chrome/browser/debugger/devtools_manager.h" |
13 #include "chrome/browser/extensions/extension_message_service.h" | 14 #include "chrome/browser/extensions/extension_message_service.h" |
14 #include "chrome/browser/extensions/extensions_service.h" | 15 #include "chrome/browser/extensions/extensions_service.h" |
15 #include "chrome/browser/extensions/extension_updater.h" | 16 #include "chrome/browser/extensions/extension_updater.h" |
16 #include "chrome/browser/profile.h" | 17 #include "chrome/browser/profile.h" |
17 #include "chrome/browser/renderer_host/render_process_host.h" | 18 #include "chrome/browser/renderer_host/render_process_host.h" |
18 #include "chrome/browser/renderer_host/render_widget_host.h" | 19 #include "chrome/browser/renderer_host/render_widget_host.h" |
19 #include "chrome/browser/renderer_host/render_view_host.h" | 20 #include "chrome/browser/renderer_host/render_view_host.h" |
20 #include "chrome/browser/tab_contents/tab_contents.h" | 21 #include "chrome/browser/tab_contents/tab_contents.h" |
21 #include "chrome/browser/tab_contents/tab_contents_view.h" | 22 #include "chrome/browser/tab_contents/tab_contents_view.h" |
(...skipping 18 matching lines...) Expand all Loading... |
40 | 41 |
41 ExtensionsUIHTMLSource::ExtensionsUIHTMLSource() | 42 ExtensionsUIHTMLSource::ExtensionsUIHTMLSource() |
42 : DataSource(chrome::kChromeUIExtensionsHost, MessageLoop::current()) { | 43 : DataSource(chrome::kChromeUIExtensionsHost, MessageLoop::current()) { |
43 } | 44 } |
44 | 45 |
45 void ExtensionsUIHTMLSource::StartDataRequest(const std::string& path, | 46 void ExtensionsUIHTMLSource::StartDataRequest(const std::string& path, |
46 int request_id) { | 47 int request_id) { |
47 DictionaryValue localized_strings; | 48 DictionaryValue localized_strings; |
48 localized_strings.SetString(L"title", | 49 localized_strings.SetString(L"title", |
49 l10n_util::GetString(IDS_EXTENSIONS_TITLE)); | 50 l10n_util::GetString(IDS_EXTENSIONS_TITLE)); |
| 51 localized_strings.SetString(L"packDialogHeading", |
| 52 l10n_util::GetString(IDS_EXTENSION_PACK_DIALOG_HEADING)); |
| 53 localized_strings.SetString(L"rootDirectoryLabel", |
| 54 l10n_util::GetString(IDS_EXTENSION_PACK_DIALOG_ROOT_DIRECTORY_LABEL)); |
| 55 localized_strings.SetString(L"packDialogBrowse", |
| 56 l10n_util::GetString(IDS_EXTENSION_PACK_DIALOG_BROWSE)); |
| 57 localized_strings.SetString(L"privateKeyLabel", |
| 58 l10n_util::GetString(IDS_EXTENSION_PACK_DIALOG_PRIVATE_KEY_LABEL)); |
50 | 59 |
51 static const base::StringPiece extensions_html( | 60 static const base::StringPiece extensions_html( |
52 ResourceBundle::GetSharedInstance().GetRawDataResource( | 61 ResourceBundle::GetSharedInstance().GetRawDataResource( |
53 IDR_EXTENSIONS_UI_HTML)); | 62 IDR_EXTENSIONS_UI_HTML)); |
54 std::string full_html(extensions_html.data(), extensions_html.size()); | 63 std::string full_html(extensions_html.data(), extensions_html.size()); |
55 jstemplate_builder::AppendJsonHtml(&localized_strings, &full_html); | 64 jstemplate_builder::AppendJsonHtml(&localized_strings, &full_html); |
56 jstemplate_builder::AppendI18nTemplateSourceHtml(&full_html); | 65 jstemplate_builder::AppendI18nTemplateSourceHtml(&full_html); |
57 jstemplate_builder::AppendI18nTemplateProcessHtml(&full_html); | 66 jstemplate_builder::AppendI18nTemplateProcessHtml(&full_html); |
58 jstemplate_builder::AppendJsTemplateSourceHtml(&full_html); | 67 jstemplate_builder::AppendJsTemplateSourceHtml(&full_html); |
59 | 68 |
(...skipping 24 matching lines...) Expand all Loading... |
84 dom_ui_->RegisterMessageCallback("enable", | 93 dom_ui_->RegisterMessageCallback("enable", |
85 NewCallback(this, &ExtensionsDOMHandler::HandleEnableMessage)); | 94 NewCallback(this, &ExtensionsDOMHandler::HandleEnableMessage)); |
86 dom_ui_->RegisterMessageCallback("uninstall", | 95 dom_ui_->RegisterMessageCallback("uninstall", |
87 NewCallback(this, &ExtensionsDOMHandler::HandleUninstallMessage)); | 96 NewCallback(this, &ExtensionsDOMHandler::HandleUninstallMessage)); |
88 dom_ui_->RegisterMessageCallback("load", | 97 dom_ui_->RegisterMessageCallback("load", |
89 NewCallback(this, &ExtensionsDOMHandler::HandleLoadMessage)); | 98 NewCallback(this, &ExtensionsDOMHandler::HandleLoadMessage)); |
90 dom_ui_->RegisterMessageCallback("pack", | 99 dom_ui_->RegisterMessageCallback("pack", |
91 NewCallback(this, &ExtensionsDOMHandler::HandlePackMessage)); | 100 NewCallback(this, &ExtensionsDOMHandler::HandlePackMessage)); |
92 dom_ui_->RegisterMessageCallback("autoupdate", | 101 dom_ui_->RegisterMessageCallback("autoupdate", |
93 NewCallback(this, &ExtensionsDOMHandler::HandleAutoUpdateMessage)); | 102 NewCallback(this, &ExtensionsDOMHandler::HandleAutoUpdateMessage)); |
| 103 dom_ui_->RegisterMessageCallback("selectFilePath", |
| 104 NewCallback(this, &ExtensionsDOMHandler::HandleSelectFilePathMessage)); |
94 } | 105 } |
95 | 106 |
96 void ExtensionsDOMHandler::HandleRequestExtensionsData(const Value* value) { | 107 void ExtensionsDOMHandler::HandleRequestExtensionsData(const Value* value) { |
97 DictionaryValue results; | 108 DictionaryValue results; |
98 | 109 |
99 // Add the extensions to the results structure. | 110 // Add the extensions to the results structure. |
100 ListValue *extensions_list = new ListValue(); | 111 ListValue *extensions_list = new ListValue(); |
101 const ExtensionList* extensions = extensions_service_->extensions(); | 112 const ExtensionList* extensions = extensions_service_->extensions(); |
102 for (ExtensionList::const_iterator extension = extensions->begin(); | 113 for (ExtensionList::const_iterator extension = extensions->begin(); |
103 extension != extensions->end(); ++extension) { | 114 extension != extensions->end(); ++extension) { |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 void ExtensionsDOMHandler::HandleUninstallMessage(const Value* value) { | 191 void ExtensionsDOMHandler::HandleUninstallMessage(const Value* value) { |
181 CHECK(value->IsType(Value::TYPE_LIST)); | 192 CHECK(value->IsType(Value::TYPE_LIST)); |
182 const ListValue* list = static_cast<const ListValue*>(value); | 193 const ListValue* list = static_cast<const ListValue*>(value); |
183 CHECK(list->GetSize() == 1); | 194 CHECK(list->GetSize() == 1); |
184 std::string extension_id; | 195 std::string extension_id; |
185 CHECK(list->GetString(0, &extension_id)); | 196 CHECK(list->GetString(0, &extension_id)); |
186 extensions_service_->UninstallExtension(extension_id, false); | 197 extensions_service_->UninstallExtension(extension_id, false); |
187 } | 198 } |
188 | 199 |
189 void ExtensionsDOMHandler::HandleLoadMessage(const Value* value) { | 200 void ExtensionsDOMHandler::HandleLoadMessage(const Value* value) { |
190 load_extension_dialog_ = SelectFileDialog::Create(this); | 201 std::string string_path; |
191 load_extension_dialog_->SelectFile( | 202 CHECK(value->IsType(Value::TYPE_LIST)); |
192 SelectFileDialog::SELECT_FOLDER, | 203 const ListValue* list = static_cast<const ListValue*>(value); |
193 l10n_util::GetStringUTF16(IDS_EXTENSION_LOAD_FROM_DIRECTORY), | 204 CHECK(list->GetSize() == 1) << list->GetSize(); |
194 FilePath(), NULL, 0, FILE_PATH_LITERAL(""), | 205 CHECK(list->GetString(0, &string_path)); |
195 dom_ui_->tab_contents()->view()->GetTopLevelNativeWindow(), NULL); | 206 FilePath file_path = FilePath::FromWStringHack(ASCIIToWide(string_path)); |
| 207 extensions_service_->LoadExtension(file_path); |
| 208 } |
| 209 |
| 210 void ExtensionsDOMHandler::ShowAlert(const std::string& message) { |
| 211 ListValue arguments; |
| 212 arguments.Append(Value::CreateStringValue(message)); |
| 213 dom_ui_->CallJavascriptFunction(L"alert", arguments); |
196 } | 214 } |
197 | 215 |
198 void ExtensionsDOMHandler::HandlePackMessage(const Value* value) { | 216 void ExtensionsDOMHandler::HandlePackMessage(const Value* value) { |
199 #if defined(OS_WIN) | 217 std::string extension_path; |
200 ShowPackDialog(); | 218 std::string private_key_path; |
201 #endif | 219 CHECK(value->IsType(Value::TYPE_LIST)); |
| 220 const ListValue* list = static_cast<const ListValue*>(value); |
| 221 CHECK(list->GetSize() == 2); |
| 222 CHECK(list->GetString(0, &extension_path)); |
| 223 CHECK(list->GetString(1, &private_key_path)); |
| 224 |
| 225 FilePath root_directory = FilePath::FromWStringHack(ASCIIToWide( |
| 226 extension_path)); |
| 227 FilePath key_file = FilePath::FromWStringHack(ASCIIToWide(private_key_path)); |
| 228 |
| 229 if (root_directory.empty()) { |
| 230 if (extension_path.empty()) { |
| 231 ShowAlert(l10n_util::GetStringUTF8( |
| 232 IDS_EXTENSION_PACK_DIALOG_ERROR_ROOT_REQUIRED)); |
| 233 } else { |
| 234 ShowAlert(l10n_util::GetStringUTF8( |
| 235 IDS_EXTENSION_PACK_DIALOG_ERROR_ROOT_INVALID)); |
| 236 } |
| 237 |
| 238 return; |
| 239 } |
| 240 |
| 241 if (!private_key_path.empty() && key_file.empty()) { |
| 242 ShowAlert(l10n_util::GetStringUTF8( |
| 243 IDS_EXTENSION_PACK_DIALOG_ERROR_KEY_INVALID)); |
| 244 return; |
| 245 } |
| 246 |
| 247 pack_job_ = new PackExtensionJob(this, root_directory, key_file, |
| 248 ChromeThread::GetMessageLoop(ChromeThread::FILE)); |
| 249 } |
| 250 |
| 251 void ExtensionsDOMHandler::OnPackSuccess(const FilePath& crx_file, |
| 252 const FilePath& pem_file) { |
| 253 std::string message; |
| 254 if (!pem_file.empty()) { |
| 255 message = WideToASCII(l10n_util::GetStringF( |
| 256 IDS_EXTENSION_PACK_DIALOG_SUCCESS_BODY_NEW, |
| 257 crx_file.ToWStringHack(), |
| 258 pem_file.ToWStringHack())); |
| 259 } else { |
| 260 message = WideToASCII(l10n_util::GetStringF( |
| 261 IDS_EXTENSION_PACK_DIALOG_SUCCESS_BODY_UPDATE, |
| 262 crx_file.ToWStringHack())); |
| 263 } |
| 264 ShowAlert(message); |
| 265 |
| 266 ListValue results; |
| 267 dom_ui_->CallJavascriptFunction(L"hidePackDialog", results); |
| 268 } |
| 269 |
| 270 void ExtensionsDOMHandler::OnPackFailure(const std::wstring& error) { |
| 271 ShowAlert(WideToASCII(error)); |
202 } | 272 } |
203 | 273 |
204 void ExtensionsDOMHandler::HandleAutoUpdateMessage(const Value* value) { | 274 void ExtensionsDOMHandler::HandleAutoUpdateMessage(const Value* value) { |
205 ExtensionUpdater* updater = extensions_service_->updater(); | 275 ExtensionUpdater* updater = extensions_service_->updater(); |
206 if (updater) { | 276 if (updater) { |
207 updater->CheckNow(); | 277 updater->CheckNow(); |
208 } | 278 } |
209 } | 279 } |
210 | 280 |
| 281 void ExtensionsDOMHandler::HandleSelectFilePathMessage(const Value* value) { |
| 282 std::string select_type; |
| 283 std::string operation; |
| 284 CHECK(value->IsType(Value::TYPE_LIST)); |
| 285 const ListValue* list = static_cast<const ListValue*>(value); |
| 286 CHECK(list->GetSize() == 2); |
| 287 CHECK(list->GetString(0, &select_type)); |
| 288 CHECK(list->GetString(1, &operation)); |
| 289 |
| 290 SelectFileDialog::Type type = SelectFileDialog::SELECT_FOLDER; |
| 291 static SelectFileDialog::FileTypeInfo info; |
| 292 int file_type_index = 0; |
| 293 if (select_type == "file") |
| 294 type = SelectFileDialog::SELECT_OPEN_FILE; |
| 295 |
| 296 string16 select_title; |
| 297 if (operation == "load") |
| 298 select_title = l10n_util::GetStringUTF16(IDS_EXTENSION_LOAD_FROM_DIRECTORY); |
| 299 else if (operation == "packRoot") |
| 300 select_title = l10n_util::GetStringUTF16( |
| 301 IDS_EXTENSION_PACK_DIALOG_SELECT_ROOT); |
| 302 else if (operation == "pem") { |
| 303 select_title = l10n_util::GetStringUTF16( |
| 304 IDS_EXTENSION_PACK_DIALOG_SELECT_KEY); |
| 305 info.extensions.push_back(std::vector<FilePath::StringType>()); |
| 306 info.extensions.front().push_back(FILE_PATH_LITERAL("pem")); |
| 307 info.extension_description_overrides.push_back(WideToUTF16( |
| 308 l10n_util::GetString( |
| 309 IDS_EXTENSION_PACK_DIALOG_KEY_FILE_TYPE_DESCRIPTION))); |
| 310 info.include_all_files = true; |
| 311 file_type_index = 1; |
| 312 } else { |
| 313 NOTREACHED(); |
| 314 return; |
| 315 } |
| 316 |
| 317 load_extension_dialog_ = SelectFileDialog::Create(this); |
| 318 load_extension_dialog_->SelectFile(type, select_title, FilePath(), &info, |
| 319 file_type_index, FILE_PATH_LITERAL(""), |
| 320 dom_ui_->tab_contents()->view()->GetTopLevelNativeWindow(), NULL); |
| 321 } |
| 322 |
| 323 |
211 void ExtensionsDOMHandler::FileSelected(const FilePath& path, int index, | 324 void ExtensionsDOMHandler::FileSelected(const FilePath& path, int index, |
212 void* params) { | 325 void* params) { |
213 extensions_service_->LoadExtension(path); | 326 // Add the extensions to the results structure. |
| 327 ListValue results; |
| 328 results.Append(Value::CreateStringValue(path.value())); |
| 329 dom_ui_->CallJavascriptFunction(L"window.handleFilePathSelected", results); |
214 } | 330 } |
215 | 331 |
216 void ExtensionsDOMHandler::Observe(NotificationType type, | 332 void ExtensionsDOMHandler::Observe(NotificationType type, |
217 const NotificationSource& source, | 333 const NotificationSource& source, |
218 const NotificationDetails& details) { | 334 const NotificationDetails& details) { |
219 switch (type.value) { | 335 switch (type.value) { |
220 case NotificationType::EXTENSION_LOADED: | 336 case NotificationType::EXTENSION_LOADED: |
221 case NotificationType::EXTENSION_UNLOADED: | 337 case NotificationType::EXTENSION_UNLOADED: |
222 case NotificationType::EXTENSION_UPDATE_DISABLED: | 338 case NotificationType::EXTENSION_UPDATE_DISABLED: |
223 case NotificationType::EXTENSION_UNLOADED_DISABLED: | 339 case NotificationType::EXTENSION_UNLOADED_DISABLED: |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 result.push_back(ExtensionPage((*iter)->url(), | 448 result.push_back(ExtensionPage((*iter)->url(), |
333 view->process()->id(), | 449 view->process()->id(), |
334 view->routing_id())); | 450 view->routing_id())); |
335 } | 451 } |
336 } | 452 } |
337 | 453 |
338 return result; | 454 return result; |
339 } | 455 } |
340 | 456 |
341 ExtensionsDOMHandler::~ExtensionsDOMHandler() { | 457 ExtensionsDOMHandler::~ExtensionsDOMHandler() { |
| 458 if (pack_job_.get()) |
| 459 pack_job_->ClearClient(); |
342 } | 460 } |
343 | 461 |
344 // ExtensionsDOMHandler, public: ----------------------------------------------- | 462 // ExtensionsDOMHandler, public: ----------------------------------------------- |
345 | 463 |
346 ExtensionsUI::ExtensionsUI(TabContents* contents) : DOMUI(contents) { | 464 ExtensionsUI::ExtensionsUI(TabContents* contents) : DOMUI(contents) { |
347 ExtensionsService *exstension_service = | 465 ExtensionsService *exstension_service = |
348 GetProfile()->GetOriginalProfile()->GetExtensionsService(); | 466 GetProfile()->GetOriginalProfile()->GetExtensionsService(); |
349 | 467 |
350 ExtensionsDOMHandler* handler = new ExtensionsDOMHandler(exstension_service); | 468 ExtensionsDOMHandler* handler = new ExtensionsDOMHandler(exstension_service); |
351 AddMessageHandler(handler); | 469 AddMessageHandler(handler); |
352 handler->Attach(this); | 470 handler->Attach(this); |
353 | 471 |
354 ExtensionsUIHTMLSource* html_source = new ExtensionsUIHTMLSource(); | 472 ExtensionsUIHTMLSource* html_source = new ExtensionsUIHTMLSource(); |
355 | 473 |
356 // Set up the chrome://extensions/ source. | 474 // Set up the chrome://extensions/ source. |
357 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE, | 475 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE, |
358 NewRunnableMethod(&chrome_url_data_manager, | 476 NewRunnableMethod(&chrome_url_data_manager, |
359 &ChromeURLDataManager::AddDataSource, html_source)); | 477 &ChromeURLDataManager::AddDataSource, html_source)); |
360 } | 478 } |
OLD | NEW |