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/extensions/extension_commands_handler.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/values.h" |
| 9 #include "chrome/browser/extensions/api/commands/extension_command_service.h" |
| 10 #include "chrome/browser/extensions/api/commands/extension_command_service_facto
ry.h" |
| 11 #include "chrome/browser/extensions/extension_service.h" |
| 12 #include "chrome/browser/extensions/extension_system.h" |
| 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 15 #include "chrome/common/extensions/extension_set.h" |
| 16 #include "content/public/browser/web_ui.h" |
| 17 #include "grit/generated_resources.h" |
| 18 #include "ui/base/l10n/l10n_util.h" |
| 19 |
| 20 namespace values = extension_manifest_values; |
| 21 |
| 22 namespace extensions { |
| 23 |
| 24 |
| 25 ExtensionCommandsHandler::ExtensionCommandsHandler() { |
| 26 } |
| 27 |
| 28 ExtensionCommandsHandler::~ExtensionCommandsHandler() { |
| 29 } |
| 30 |
| 31 void ExtensionCommandsHandler::GetLocalizedValues( |
| 32 DictionaryValue* localized_strings) { |
| 33 DCHECK(localized_strings); |
| 34 localized_strings->SetString("extensionCommandsOverlay", |
| 35 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_DIALOG_TITLE)); |
| 36 localized_strings->SetString("extensionCommandsEmpty", |
| 37 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_EMPTY)); |
| 38 localized_strings->SetString("extensionCommandsInactive", |
| 39 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_INACTIVE)); |
| 40 localized_strings->SetString("close", l10n_util::GetStringUTF16(IDS_CLOSE)); |
| 41 } |
| 42 |
| 43 void ExtensionCommandsHandler::RegisterMessages() { |
| 44 web_ui()->RegisterMessageCallback("extensionCommandsRequestExtensionsData", |
| 45 base::Bind(&ExtensionCommandsHandler::HandleRequestExtensionsData, |
| 46 base::Unretained(this))); |
| 47 } |
| 48 |
| 49 void ExtensionCommandsHandler::HandleRequestExtensionsData( |
| 50 const ListValue* args) { |
| 51 DictionaryValue results; |
| 52 GetAllCommands(&results); |
| 53 web_ui()->CallJavascriptFunction( |
| 54 "ExtensionCommandsOverlay.returnExtensionsData", results); |
| 55 } |
| 56 |
| 57 void ExtensionCommandsHandler::GetAllCommands( |
| 58 base::DictionaryValue* commands) { |
| 59 ListValue* results = new ListValue; |
| 60 |
| 61 Profile* profile = Profile::FromWebUI(web_ui()); |
| 62 ExtensionCommandService* command_service = |
| 63 ExtensionCommandServiceFactory::GetForProfile(profile); |
| 64 |
| 65 const ExtensionSet* extensions = |
| 66 ExtensionSystem::Get(profile)->extension_service()->extensions(); |
| 67 for (ExtensionSet::const_iterator extension = extensions->begin(); |
| 68 extension != extensions->end(); ++extension) { |
| 69 scoped_ptr<DictionaryValue> extension_dict(new DictionaryValue); |
| 70 extension_dict->SetString("name", (*extension)->name()); |
| 71 extension_dict->SetString("id", (*extension)->id()); |
| 72 |
| 73 // Add the keybindings to a list structure. |
| 74 scoped_ptr<ListValue> extensions_list(new ListValue()); |
| 75 |
| 76 const extensions::Command* browser_action = |
| 77 command_service->GetBrowserActionCommand((*extension)->id(), |
| 78 ExtensionCommandService::ALL); |
| 79 if (browser_action) { |
| 80 extensions_list->Append(browser_action->ToValue( |
| 81 (*extension), |
| 82 command_service->IsKeybindingActive(browser_action->accelerator(), |
| 83 (*extension)->id(), |
| 84 browser_action->command_name()))); |
| 85 } |
| 86 |
| 87 const extensions::Command* page_action = |
| 88 command_service->GetPageActionCommand((*extension)->id(), |
| 89 ExtensionCommandService::ALL); |
| 90 if (page_action) { |
| 91 extensions_list->Append(page_action->ToValue( |
| 92 (*extension), |
| 93 command_service->IsKeybindingActive(page_action->accelerator(), |
| 94 (*extension)->id(), |
| 95 page_action->command_name()))); |
| 96 } |
| 97 |
| 98 extensions::CommandMap named_commands = |
| 99 command_service->GetNamedCommands((*extension)->id(), |
| 100 ExtensionCommandService::ALL); |
| 101 extensions::CommandMap::const_iterator iter = named_commands.begin(); |
| 102 for (; iter != named_commands.end(); ++iter) { |
| 103 extensions_list->Append(iter->second.ToValue( |
| 104 (*extension), |
| 105 command_service->IsKeybindingActive(iter->second.accelerator(), |
| 106 (*extension)->id(), |
| 107 iter->second.command_name()))); |
| 108 } |
| 109 |
| 110 if (!extensions_list->empty()) { |
| 111 extension_dict->Set("commands", extensions_list.release()); |
| 112 results->Append(extension_dict.release()); |
| 113 } |
| 114 } |
| 115 |
| 116 commands->Set("commands", results); |
| 117 } |
| 118 |
| 119 } // namespace extensions |
OLD | NEW |