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.
5 #include "chrome/browser/ui/webui/extensions/command_handler.h"
8 #include "base/values.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/extensions/api/commands/command_service.h"
11 #include "chrome/browser/extensions/extension_keybinding_registry.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/extension_system.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "content/public/browser/web_ui.h"
16 #include "content/public/browser/web_ui_data_source.h"
17 #include "extensions/common/extension_set.h"
18 #include "grit/generated_resources.h"
19 #include "ui/base/l10n/l10n_util.h"
21 namespace extensions
{
23 CommandHandler::CommandHandler(Profile
* profile
) : profile_(profile
) {
26 CommandHandler::~CommandHandler() {
29 void CommandHandler::GetLocalizedValues(content::WebUIDataSource
* source
) {
30 source
->AddString("extensionCommandsOverlay",
31 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_DIALOG_TITLE
));
32 source
->AddString("extensionCommandsEmpty",
33 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_EMPTY
));
34 source
->AddString("extensionCommandsInactive",
35 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_INACTIVE
));
36 source
->AddString("extensionCommandsStartTyping",
37 l10n_util::GetStringUTF16(IDS_EXTENSION_TYPE_SHORTCUT
));
38 source
->AddString("extensionCommandsDelete",
39 l10n_util::GetStringUTF16(IDS_EXTENSION_DELETE_SHORTCUT
));
40 source
->AddString("extensionCommandsGlobal",
41 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_GLOBAL
));
42 source
->AddString("extensionCommandsRegular",
43 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_NOT_GLOBAL
));
44 source
->AddString("ok", l10n_util::GetStringUTF16(IDS_OK
));
47 void CommandHandler::RegisterMessages() {
48 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED
,
49 content::Source
<Profile
>(profile_
));
50 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED
,
51 content::Source
<Profile
>(profile_
));
53 web_ui()->RegisterMessageCallback("extensionCommandsRequestExtensionsData",
54 base::Bind(&CommandHandler::HandleRequestExtensionsData
,
55 base::Unretained(this)));
56 web_ui()->RegisterMessageCallback("setShortcutHandlingSuspended",
57 base::Bind(&CommandHandler::HandleSetShortcutHandlingSuspended
,
58 base::Unretained(this)));
59 web_ui()->RegisterMessageCallback("setExtensionCommandShortcut",
60 base::Bind(&CommandHandler::HandleSetExtensionCommandShortcut
,
61 base::Unretained(this)));
62 web_ui()->RegisterMessageCallback("setCommandScope",
63 base::Bind(&CommandHandler::HandleSetCommandScope
,
64 base::Unretained(this)));
67 void CommandHandler::Observe(
69 const content::NotificationSource
& source
,
70 const content::NotificationDetails
& details
) {
71 DCHECK(type
== chrome::NOTIFICATION_EXTENSION_LOADED
||
72 type
== chrome::NOTIFICATION_EXTENSION_UNLOADED
);
73 UpdateCommandDataOnPage();
76 void CommandHandler::UpdateCommandDataOnPage() {
77 base::DictionaryValue results
;
78 GetAllCommands(&results
);
79 web_ui()->CallJavascriptFunction(
80 "extensions.ExtensionCommandsOverlay.returnExtensionsData", results
);
83 void CommandHandler::HandleRequestExtensionsData(const base::ListValue
* args
) {
84 UpdateCommandDataOnPage();
87 void CommandHandler::HandleSetExtensionCommandShortcut(
88 const base::ListValue
* args
) {
89 std::string extension_id
;
90 std::string command_name
;
91 std::string keystroke
;
92 if (!args
->GetString(0, &extension_id
) ||
93 !args
->GetString(1, &command_name
) ||
94 !args
->GetString(2, &keystroke
)) {
99 Profile
* profile
= Profile::FromWebUI(web_ui());
100 CommandService
* command_service
= CommandService::Get(profile
);
101 command_service
->UpdateKeybindingPrefs(extension_id
, command_name
, keystroke
);
103 UpdateCommandDataOnPage();
106 void CommandHandler::HandleSetCommandScope(
107 const base::ListValue
* args
) {
108 std::string extension_id
;
109 std::string command_name
;
111 if (!args
->GetString(0, &extension_id
) ||
112 !args
->GetString(1, &command_name
) ||
113 !args
->GetBoolean(2, &global
)) {
118 Profile
* profile
= Profile::FromWebUI(web_ui());
119 CommandService
* command_service
= CommandService::Get(profile
);
120 if (command_service
->SetScope(extension_id
, command_name
, global
))
121 UpdateCommandDataOnPage();
124 void CommandHandler::HandleSetShortcutHandlingSuspended(
125 const base::ListValue
* args
) {
127 if (args
->GetBoolean(0, &suspended
))
128 ExtensionKeybindingRegistry::SetShortcutHandlingSuspended(suspended
);
131 void CommandHandler::GetAllCommands(base::DictionaryValue
* commands
) {
132 base::ListValue
* results
= new base::ListValue
;
134 Profile
* profile
= Profile::FromWebUI(web_ui());
135 CommandService
* command_service
= CommandService::Get(profile
);
137 const ExtensionSet
* extensions
= extensions::ExtensionSystem::Get(profile
)->
138 extension_service()->extensions();
139 for (ExtensionSet::const_iterator extension
= extensions
->begin();
140 extension
!= extensions
->end(); ++extension
) {
141 scoped_ptr
<base::DictionaryValue
> extension_dict(new base::DictionaryValue
);
142 extension_dict
->SetString("name", (*extension
)->name());
143 extension_dict
->SetString("id", (*extension
)->id());
145 // Add the keybindings to a list structure.
146 scoped_ptr
<base::ListValue
> extensions_list(new base::ListValue());
150 extensions::Command browser_action
;
151 if (command_service
->GetBrowserActionCommand((*extension
)->id(),
155 extensions_list
->Append(
156 browser_action
.ToValue((extension
->get()), active
));
159 extensions::Command page_action
;
160 if (command_service
->GetPageActionCommand((*extension
)->id(),
164 extensions_list
->Append(page_action
.ToValue((extension
->get()), active
));
167 extensions::Command script_badge
;
168 if (command_service
->GetScriptBadgeCommand((*extension
)->id(),
172 extensions_list
->Append(script_badge
.ToValue((extension
->get()), active
));
175 extensions::CommandMap named_commands
;
176 if (command_service
->GetNamedCommands((*extension
)->id(),
178 extensions::CommandService::ANY_SCOPE
,
180 for (extensions::CommandMap::const_iterator iter
= named_commands
.begin();
181 iter
!= named_commands
.end(); ++iter
) {
182 extensions::Command command
= command_service
->FindCommandByName(
183 (*extension
)->id(), iter
->second
.command_name());
184 ui::Accelerator shortcut_assigned
= command
.accelerator();
186 active
= (shortcut_assigned
.key_code() != ui::VKEY_UNKNOWN
);
188 extensions_list
->Append(
189 iter
->second
.ToValue((extension
->get()), active
));
193 if (!extensions_list
->empty()) {
194 extension_dict
->Set("commands", extensions_list
.release());
195 results
->Append(extension_dict
.release());
199 commands
->Set("commands", results
);
202 } // namespace extensions