Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(857)

Side by Side Diff: chrome/browser/ui/webui/extensions/command_handler.cc

Issue 275503003: Remove deprecated extension notification and deprecated extension_service functions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address review comments Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/ui/webui/extensions/command_handler.h" 5 #include "chrome/browser/ui/webui/extensions/command_handler.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/extensions/api/commands/command_service.h" 9 #include "chrome/browser/extensions/api/commands/command_service.h"
11 #include "chrome/browser/extensions/extension_commands_global_registry.h" 10 #include "chrome/browser/extensions/extension_commands_global_registry.h"
12 #include "chrome/browser/extensions/extension_keybinding_registry.h" 11 #include "chrome/browser/extensions/extension_keybinding_registry.h"
13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
15 #include "content/public/browser/web_ui.h" 13 #include "content/public/browser/web_ui.h"
16 #include "content/public/browser/web_ui_data_source.h" 14 #include "content/public/browser/web_ui_data_source.h"
15 #include "extensions/browser/extension_registry.h"
17 #include "extensions/browser/extension_system.h" 16 #include "extensions/browser/extension_system.h"
18 #include "extensions/common/extension_set.h" 17 #include "extensions/common/extension_set.h"
19 #include "grit/generated_resources.h" 18 #include "grit/generated_resources.h"
20 #include "ui/base/l10n/l10n_util.h" 19 #include "ui/base/l10n/l10n_util.h"
21 20
22 namespace extensions { 21 namespace extensions {
23 22
24 CommandHandler::CommandHandler(Profile* profile) : profile_(profile) { 23 CommandHandler::CommandHandler(Profile* profile)
24 : profile_(profile), extension_registry_observer_(this) {
xiyuan 2014/05/24 05:46:59 nit: one per line
limasdf 2014/05/24 06:37:13 Done.
25 } 25 }
26 26
27 CommandHandler::~CommandHandler() { 27 CommandHandler::~CommandHandler() {
28 } 28 }
29 29
30 void CommandHandler::GetLocalizedValues(content::WebUIDataSource* source) { 30 void CommandHandler::GetLocalizedValues(content::WebUIDataSource* source) {
31 source->AddString("extensionCommandsOverlay", 31 source->AddString("extensionCommandsOverlay",
32 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_DIALOG_TITLE)); 32 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_DIALOG_TITLE));
33 source->AddString("extensionCommandsEmpty", 33 source->AddString("extensionCommandsEmpty",
34 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_EMPTY)); 34 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_EMPTY));
35 source->AddString("extensionCommandsInactive", 35 source->AddString("extensionCommandsInactive",
36 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_INACTIVE)); 36 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_INACTIVE));
37 source->AddString("extensionCommandsStartTyping", 37 source->AddString("extensionCommandsStartTyping",
38 l10n_util::GetStringUTF16(IDS_EXTENSION_TYPE_SHORTCUT)); 38 l10n_util::GetStringUTF16(IDS_EXTENSION_TYPE_SHORTCUT));
39 source->AddString("extensionCommandsDelete", 39 source->AddString("extensionCommandsDelete",
40 l10n_util::GetStringUTF16(IDS_EXTENSION_DELETE_SHORTCUT)); 40 l10n_util::GetStringUTF16(IDS_EXTENSION_DELETE_SHORTCUT));
41 source->AddString("extensionCommandsGlobal", 41 source->AddString("extensionCommandsGlobal",
42 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_GLOBAL)); 42 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_GLOBAL));
43 source->AddString("extensionCommandsRegular", 43 source->AddString("extensionCommandsRegular",
44 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_NOT_GLOBAL)); 44 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_NOT_GLOBAL));
45 source->AddString("ok", l10n_util::GetStringUTF16(IDS_OK)); 45 source->AddString("ok", l10n_util::GetStringUTF16(IDS_OK));
46 } 46 }
47 47
48 void CommandHandler::RegisterMessages() { 48 void CommandHandler::RegisterMessages() {
49 registrar_.Add(this, 49 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_));
50 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
51 content::Source<Profile>(profile_));
52 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
53 content::Source<Profile>(profile_));
54 50
55 web_ui()->RegisterMessageCallback("extensionCommandsRequestExtensionsData", 51 web_ui()->RegisterMessageCallback("extensionCommandsRequestExtensionsData",
56 base::Bind(&CommandHandler::HandleRequestExtensionsData, 52 base::Bind(&CommandHandler::HandleRequestExtensionsData,
57 base::Unretained(this))); 53 base::Unretained(this)));
58 web_ui()->RegisterMessageCallback("setShortcutHandlingSuspended", 54 web_ui()->RegisterMessageCallback("setShortcutHandlingSuspended",
59 base::Bind(&CommandHandler::HandleSetShortcutHandlingSuspended, 55 base::Bind(&CommandHandler::HandleSetShortcutHandlingSuspended,
60 base::Unretained(this))); 56 base::Unretained(this)));
61 web_ui()->RegisterMessageCallback("setExtensionCommandShortcut", 57 web_ui()->RegisterMessageCallback("setExtensionCommandShortcut",
62 base::Bind(&CommandHandler::HandleSetExtensionCommandShortcut, 58 base::Bind(&CommandHandler::HandleSetExtensionCommandShortcut,
63 base::Unretained(this))); 59 base::Unretained(this)));
64 web_ui()->RegisterMessageCallback("setCommandScope", 60 web_ui()->RegisterMessageCallback("setCommandScope",
65 base::Bind(&CommandHandler::HandleSetCommandScope, 61 base::Bind(&CommandHandler::HandleSetCommandScope,
66 base::Unretained(this))); 62 base::Unretained(this)));
67 } 63 }
68 64
69 void CommandHandler::Observe( 65 void CommandHandler::OnExtensionLoaded(content::BrowserContext* browser_context,
70 int type, 66 const Extension* extension) {
71 const content::NotificationSource& source,
72 const content::NotificationDetails& details) {
73 DCHECK(type == chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED ||
74 type == chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED);
75 UpdateCommandDataOnPage(); 67 UpdateCommandDataOnPage();
76 } 68 }
77 69
70 void CommandHandler::OnExtensionUnloaded(
71 content::BrowserContext* browser_context,
72 const Extension* extension,
73 UnloadedExtensionInfo::Reason reason) {
74 UpdateCommandDataOnPage();
75 }
76
78 void CommandHandler::UpdateCommandDataOnPage() { 77 void CommandHandler::UpdateCommandDataOnPage() {
79 base::DictionaryValue results; 78 base::DictionaryValue results;
80 GetAllCommands(&results); 79 GetAllCommands(&results);
81 web_ui()->CallJavascriptFunction( 80 web_ui()->CallJavascriptFunction(
82 "extensions.ExtensionCommandsOverlay.returnExtensionsData", results); 81 "extensions.ExtensionCommandsOverlay.returnExtensionsData", results);
83 } 82 }
84 83
85 void CommandHandler::HandleRequestExtensionsData(const base::ListValue* args) { 84 void CommandHandler::HandleRequestExtensionsData(const base::ListValue* args) {
86 UpdateCommandDataOnPage(); 85 UpdateCommandDataOnPage();
87 } 86 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 ExtensionCommandsGlobalRegistry::SetShortcutHandlingSuspended(suspended); 133 ExtensionCommandsGlobalRegistry::SetShortcutHandlingSuspended(suspended);
135 } 134 }
136 } 135 }
137 136
138 void CommandHandler::GetAllCommands(base::DictionaryValue* commands) { 137 void CommandHandler::GetAllCommands(base::DictionaryValue* commands) {
139 base::ListValue* results = new base::ListValue; 138 base::ListValue* results = new base::ListValue;
140 139
141 Profile* profile = Profile::FromWebUI(web_ui()); 140 Profile* profile = Profile::FromWebUI(web_ui());
142 CommandService* command_service = CommandService::Get(profile); 141 CommandService* command_service = CommandService::Get(profile);
143 142
144 const ExtensionSet* extensions = extensions::ExtensionSystem::Get(profile)-> 143 const ExtensionSet& extensions =
145 extension_service()->extensions(); 144 ExtensionRegistry::Get(profile)->enabled_extensions();
146 for (ExtensionSet::const_iterator extension = extensions->begin(); 145 for (ExtensionSet::const_iterator extension = extensions.begin();
147 extension != extensions->end(); ++extension) { 146 extension != extensions.end();
147 ++extension) {
148 scoped_ptr<base::DictionaryValue> extension_dict(new base::DictionaryValue); 148 scoped_ptr<base::DictionaryValue> extension_dict(new base::DictionaryValue);
149 extension_dict->SetString("name", (*extension)->name()); 149 extension_dict->SetString("name", (*extension)->name());
150 extension_dict->SetString("id", (*extension)->id()); 150 extension_dict->SetString("id", (*extension)->id());
151 151
152 // Add the keybindings to a list structure. 152 // Add the keybindings to a list structure.
153 scoped_ptr<base::ListValue> extensions_list(new base::ListValue()); 153 scoped_ptr<base::ListValue> extensions_list(new base::ListValue());
154 154
155 bool active = false; 155 bool active = false;
156 156
157 extensions::Command browser_action; 157 Command browser_action;
158 if (command_service->GetBrowserActionCommand((*extension)->id(), 158 if (command_service->GetBrowserActionCommand((*extension)->id(),
159 CommandService::ALL, 159 CommandService::ALL,
160 &browser_action, 160 &browser_action,
161 &active)) { 161 &active)) {
162 extensions_list->Append( 162 extensions_list->Append(
163 browser_action.ToValue((extension->get()), active)); 163 browser_action.ToValue((extension->get()), active));
164 } 164 }
165 165
166 extensions::Command page_action; 166 Command page_action;
167 if (command_service->GetPageActionCommand((*extension)->id(), 167 if (command_service->GetPageActionCommand((*extension)->id(),
168 CommandService::ALL, 168 CommandService::ALL,
169 &page_action, 169 &page_action,
170 &active)) { 170 &active)) {
171 extensions_list->Append(page_action.ToValue((extension->get()), active)); 171 extensions_list->Append(page_action.ToValue((extension->get()), active));
172 } 172 }
173 173
174 extensions::CommandMap named_commands; 174 CommandMap named_commands;
175 if (command_service->GetNamedCommands((*extension)->id(), 175 if (command_service->GetNamedCommands((*extension)->id(),
176 CommandService::ALL, 176 CommandService::ALL,
177 extensions::CommandService::ANY_SCOPE, 177 CommandService::ANY_SCOPE,
178 &named_commands)) { 178 &named_commands)) {
179 for (extensions::CommandMap::const_iterator iter = named_commands.begin(); 179 for (CommandMap::const_iterator iter = named_commands.begin();
180 iter != named_commands.end(); ++iter) { 180 iter != named_commands.end();
181 extensions::Command command = command_service->FindCommandByName( 181 ++iter) {
182 (*extension)->id(), iter->second.command_name()); 182 Command command = command_service->FindCommandByName(
183 (*extension)->id(), iter->second.command_name());
183 ui::Accelerator shortcut_assigned = command.accelerator(); 184 ui::Accelerator shortcut_assigned = command.accelerator();
184 185
185 active = (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN); 186 active = (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN);
186 187
187 extensions_list->Append( 188 extensions_list->Append(
188 iter->second.ToValue((extension->get()), active)); 189 iter->second.ToValue((extension->get()), active));
189 } 190 }
190 } 191 }
191 192
192 if (!extensions_list->empty()) { 193 if (!extensions_list->empty()) {
193 extension_dict->Set("commands", extensions_list.release()); 194 extension_dict->Set("commands", extensions_list.release());
194 results->Append(extension_dict.release()); 195 results->Append(extension_dict.release());
195 } 196 }
196 } 197 }
197 198
198 commands->Set("commands", results); 199 commands->Set("commands", results);
199 } 200 }
200 201
201 } // namespace extensions 202 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698