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