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

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 comment from xiyuan 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
« no previous file with comments | « chrome/browser/ui/webui/extensions/command_handler.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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),
25 extension_registry_observer_(this) {
25 } 26 }
26 27
27 CommandHandler::~CommandHandler() { 28 CommandHandler::~CommandHandler() {
28 } 29 }
29 30
30 void CommandHandler::GetLocalizedValues(content::WebUIDataSource* source) { 31 void CommandHandler::GetLocalizedValues(content::WebUIDataSource* source) {
31 source->AddString("extensionCommandsOverlay", 32 source->AddString("extensionCommandsOverlay",
32 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_DIALOG_TITLE)); 33 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_DIALOG_TITLE));
33 source->AddString("extensionCommandsEmpty", 34 source->AddString("extensionCommandsEmpty",
34 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_EMPTY)); 35 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_EMPTY));
35 source->AddString("extensionCommandsInactive", 36 source->AddString("extensionCommandsInactive",
36 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_INACTIVE)); 37 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_INACTIVE));
37 source->AddString("extensionCommandsStartTyping", 38 source->AddString("extensionCommandsStartTyping",
38 l10n_util::GetStringUTF16(IDS_EXTENSION_TYPE_SHORTCUT)); 39 l10n_util::GetStringUTF16(IDS_EXTENSION_TYPE_SHORTCUT));
39 source->AddString("extensionCommandsDelete", 40 source->AddString("extensionCommandsDelete",
40 l10n_util::GetStringUTF16(IDS_EXTENSION_DELETE_SHORTCUT)); 41 l10n_util::GetStringUTF16(IDS_EXTENSION_DELETE_SHORTCUT));
41 source->AddString("extensionCommandsGlobal", 42 source->AddString("extensionCommandsGlobal",
42 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_GLOBAL)); 43 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_GLOBAL));
43 source->AddString("extensionCommandsRegular", 44 source->AddString("extensionCommandsRegular",
44 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_NOT_GLOBAL)); 45 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_NOT_GLOBAL));
45 source->AddString("ok", l10n_util::GetStringUTF16(IDS_OK)); 46 source->AddString("ok", l10n_util::GetStringUTF16(IDS_OK));
46 } 47 }
47 48
48 void CommandHandler::RegisterMessages() { 49 void CommandHandler::RegisterMessages() {
49 registrar_.Add(this, 50 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 51
55 web_ui()->RegisterMessageCallback("extensionCommandsRequestExtensionsData", 52 web_ui()->RegisterMessageCallback("extensionCommandsRequestExtensionsData",
56 base::Bind(&CommandHandler::HandleRequestExtensionsData, 53 base::Bind(&CommandHandler::HandleRequestExtensionsData,
57 base::Unretained(this))); 54 base::Unretained(this)));
58 web_ui()->RegisterMessageCallback("setShortcutHandlingSuspended", 55 web_ui()->RegisterMessageCallback("setShortcutHandlingSuspended",
59 base::Bind(&CommandHandler::HandleSetShortcutHandlingSuspended, 56 base::Bind(&CommandHandler::HandleSetShortcutHandlingSuspended,
60 base::Unretained(this))); 57 base::Unretained(this)));
61 web_ui()->RegisterMessageCallback("setExtensionCommandShortcut", 58 web_ui()->RegisterMessageCallback("setExtensionCommandShortcut",
62 base::Bind(&CommandHandler::HandleSetExtensionCommandShortcut, 59 base::Bind(&CommandHandler::HandleSetExtensionCommandShortcut,
63 base::Unretained(this))); 60 base::Unretained(this)));
64 web_ui()->RegisterMessageCallback("setCommandScope", 61 web_ui()->RegisterMessageCallback("setCommandScope",
65 base::Bind(&CommandHandler::HandleSetCommandScope, 62 base::Bind(&CommandHandler::HandleSetCommandScope,
66 base::Unretained(this))); 63 base::Unretained(this)));
67 } 64 }
68 65
69 void CommandHandler::Observe( 66 void CommandHandler::OnExtensionLoaded(content::BrowserContext* browser_context,
70 int type, 67 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(); 68 UpdateCommandDataOnPage();
76 } 69 }
77 70
71 void CommandHandler::OnExtensionUnloaded(
72 content::BrowserContext* browser_context,
73 const Extension* extension,
74 UnloadedExtensionInfo::Reason reason) {
75 UpdateCommandDataOnPage();
76 }
77
78 void CommandHandler::UpdateCommandDataOnPage() { 78 void CommandHandler::UpdateCommandDataOnPage() {
79 base::DictionaryValue results; 79 base::DictionaryValue results;
80 GetAllCommands(&results); 80 GetAllCommands(&results);
81 web_ui()->CallJavascriptFunction( 81 web_ui()->CallJavascriptFunction(
82 "extensions.ExtensionCommandsOverlay.returnExtensionsData", results); 82 "extensions.ExtensionCommandsOverlay.returnExtensionsData", results);
83 } 83 }
84 84
85 void CommandHandler::HandleRequestExtensionsData(const base::ListValue* args) { 85 void CommandHandler::HandleRequestExtensionsData(const base::ListValue* args) {
86 UpdateCommandDataOnPage(); 86 UpdateCommandDataOnPage();
87 } 87 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 ExtensionCommandsGlobalRegistry::SetShortcutHandlingSuspended(suspended); 134 ExtensionCommandsGlobalRegistry::SetShortcutHandlingSuspended(suspended);
135 } 135 }
136 } 136 }
137 137
138 void CommandHandler::GetAllCommands(base::DictionaryValue* commands) { 138 void CommandHandler::GetAllCommands(base::DictionaryValue* commands) {
139 base::ListValue* results = new base::ListValue; 139 base::ListValue* results = new base::ListValue;
140 140
141 Profile* profile = Profile::FromWebUI(web_ui()); 141 Profile* profile = Profile::FromWebUI(web_ui());
142 CommandService* command_service = CommandService::Get(profile); 142 CommandService* command_service = CommandService::Get(profile);
143 143
144 const ExtensionSet* extensions = extensions::ExtensionSystem::Get(profile)-> 144 const ExtensionSet& extensions =
145 extension_service()->extensions(); 145 ExtensionRegistry::Get(profile)->enabled_extensions();
146 for (ExtensionSet::const_iterator extension = extensions->begin(); 146 for (ExtensionSet::const_iterator extension = extensions.begin();
147 extension != extensions->end(); ++extension) { 147 extension != extensions.end();
148 ++extension) {
148 scoped_ptr<base::DictionaryValue> extension_dict(new base::DictionaryValue); 149 scoped_ptr<base::DictionaryValue> extension_dict(new base::DictionaryValue);
149 extension_dict->SetString("name", (*extension)->name()); 150 extension_dict->SetString("name", (*extension)->name());
150 extension_dict->SetString("id", (*extension)->id()); 151 extension_dict->SetString("id", (*extension)->id());
151 152
152 // Add the keybindings to a list structure. 153 // Add the keybindings to a list structure.
153 scoped_ptr<base::ListValue> extensions_list(new base::ListValue()); 154 scoped_ptr<base::ListValue> extensions_list(new base::ListValue());
154 155
155 bool active = false; 156 bool active = false;
156 157
157 extensions::Command browser_action; 158 Command browser_action;
158 if (command_service->GetBrowserActionCommand((*extension)->id(), 159 if (command_service->GetBrowserActionCommand((*extension)->id(),
159 CommandService::ALL, 160 CommandService::ALL,
160 &browser_action, 161 &browser_action,
161 &active)) { 162 &active)) {
162 extensions_list->Append( 163 extensions_list->Append(
163 browser_action.ToValue((extension->get()), active)); 164 browser_action.ToValue((extension->get()), active));
164 } 165 }
165 166
166 extensions::Command page_action; 167 Command page_action;
167 if (command_service->GetPageActionCommand((*extension)->id(), 168 if (command_service->GetPageActionCommand((*extension)->id(),
168 CommandService::ALL, 169 CommandService::ALL,
169 &page_action, 170 &page_action,
170 &active)) { 171 &active)) {
171 extensions_list->Append(page_action.ToValue((extension->get()), active)); 172 extensions_list->Append(page_action.ToValue((extension->get()), active));
172 } 173 }
173 174
174 extensions::CommandMap named_commands; 175 CommandMap named_commands;
175 if (command_service->GetNamedCommands((*extension)->id(), 176 if (command_service->GetNamedCommands((*extension)->id(),
176 CommandService::ALL, 177 CommandService::ALL,
177 extensions::CommandService::ANY_SCOPE, 178 CommandService::ANY_SCOPE,
178 &named_commands)) { 179 &named_commands)) {
179 for (extensions::CommandMap::const_iterator iter = named_commands.begin(); 180 for (CommandMap::const_iterator iter = named_commands.begin();
180 iter != named_commands.end(); ++iter) { 181 iter != named_commands.end();
181 extensions::Command command = command_service->FindCommandByName( 182 ++iter) {
182 (*extension)->id(), iter->second.command_name()); 183 Command command = command_service->FindCommandByName(
184 (*extension)->id(), iter->second.command_name());
183 ui::Accelerator shortcut_assigned = command.accelerator(); 185 ui::Accelerator shortcut_assigned = command.accelerator();
184 186
185 active = (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN); 187 active = (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN);
186 188
187 extensions_list->Append( 189 extensions_list->Append(
188 iter->second.ToValue((extension->get()), active)); 190 iter->second.ToValue((extension->get()), active));
189 } 191 }
190 } 192 }
191 193
192 if (!extensions_list->empty()) { 194 if (!extensions_list->empty()) {
193 extension_dict->Set("commands", extensions_list.release()); 195 extension_dict->Set("commands", extensions_list.release());
194 results->Append(extension_dict.release()); 196 results->Append(extension_dict.release());
195 } 197 }
196 } 198 }
197 199
198 commands->Set("commands", results); 200 commands->Set("commands", results);
199 } 201 }
200 202
201 } // namespace extensions 203 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/extensions/command_handler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698