Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" | 12 #include "chrome/browser/extensions/extension_service.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 15 #include "content/public/browser/web_ui.h" | 14 #include "content/public/browser/web_ui.h" |
| 16 #include "content/public/browser/web_ui_data_source.h" | 15 #include "content/public/browser/web_ui_data_source.h" |
| 16 #include "extensions/browser/extension_registry.h" | |
| 17 #include "extensions/browser/extension_system.h" | 17 #include "extensions/browser/extension_system.h" |
| 18 #include "extensions/common/extension_set.h" | 18 #include "extensions/common/extension_set.h" |
| 19 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
| 21 | 21 |
| 22 namespace extensions { | 22 namespace extensions { |
| 23 | 23 |
| 24 CommandHandler::CommandHandler(Profile* profile) : profile_(profile) { | 24 CommandHandler::CommandHandler(Profile* profile) |
| 25 : profile_(profile), 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( |
| 50 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | 51 extensions::ExtensionRegistry::Get(profile_)); |
| 51 content::Source<Profile>(profile_)); | |
| 52 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | |
| 53 content::Source<Profile>(profile_)); | |
| 54 | 52 |
| 55 web_ui()->RegisterMessageCallback("extensionCommandsRequestExtensionsData", | 53 web_ui()->RegisterMessageCallback("extensionCommandsRequestExtensionsData", |
| 56 base::Bind(&CommandHandler::HandleRequestExtensionsData, | 54 base::Bind(&CommandHandler::HandleRequestExtensionsData, |
| 57 base::Unretained(this))); | 55 base::Unretained(this))); |
| 58 web_ui()->RegisterMessageCallback("setShortcutHandlingSuspended", | 56 web_ui()->RegisterMessageCallback("setShortcutHandlingSuspended", |
| 59 base::Bind(&CommandHandler::HandleSetShortcutHandlingSuspended, | 57 base::Bind(&CommandHandler::HandleSetShortcutHandlingSuspended, |
| 60 base::Unretained(this))); | 58 base::Unretained(this))); |
| 61 web_ui()->RegisterMessageCallback("setExtensionCommandShortcut", | 59 web_ui()->RegisterMessageCallback("setExtensionCommandShortcut", |
| 62 base::Bind(&CommandHandler::HandleSetExtensionCommandShortcut, | 60 base::Bind(&CommandHandler::HandleSetExtensionCommandShortcut, |
| 63 base::Unretained(this))); | 61 base::Unretained(this))); |
| 64 web_ui()->RegisterMessageCallback("setCommandScope", | 62 web_ui()->RegisterMessageCallback("setCommandScope", |
| 65 base::Bind(&CommandHandler::HandleSetCommandScope, | 63 base::Bind(&CommandHandler::HandleSetCommandScope, |
| 66 base::Unretained(this))); | 64 base::Unretained(this))); |
| 67 } | 65 } |
| 68 | 66 |
| 69 void CommandHandler::Observe( | 67 void CommandHandler::OnExtensionLoaded(content::BrowserContext* browser_context, |
| 70 int type, | 68 const extensions::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(); | 69 UpdateCommandDataOnPage(); |
| 76 } | 70 } |
| 77 | 71 |
| 72 void CommandHandler::OnExtensionUnloaded( | |
| 73 content::BrowserContext* browser_context, | |
| 74 const extensions::Extension* extension, | |
| 75 extensions::UnloadedExtensionInfo::Reason reason) { | |
| 76 UpdateCommandDataOnPage(); | |
| 77 } | |
| 78 | |
| 78 void CommandHandler::UpdateCommandDataOnPage() { | 79 void CommandHandler::UpdateCommandDataOnPage() { |
| 79 base::DictionaryValue results; | 80 base::DictionaryValue results; |
| 80 GetAllCommands(&results); | 81 GetAllCommands(&results); |
| 81 web_ui()->CallJavascriptFunction( | 82 web_ui()->CallJavascriptFunction( |
| 82 "extensions.ExtensionCommandsOverlay.returnExtensionsData", results); | 83 "extensions.ExtensionCommandsOverlay.returnExtensionsData", results); |
| 83 } | 84 } |
| 84 | 85 |
| 85 void CommandHandler::HandleRequestExtensionsData(const base::ListValue* args) { | 86 void CommandHandler::HandleRequestExtensionsData(const base::ListValue* args) { |
| 86 UpdateCommandDataOnPage(); | 87 UpdateCommandDataOnPage(); |
| 87 } | 88 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 ExtensionCommandsGlobalRegistry::SetShortcutHandlingSuspended(suspended); | 135 ExtensionCommandsGlobalRegistry::SetShortcutHandlingSuspended(suspended); |
| 135 } | 136 } |
| 136 } | 137 } |
| 137 | 138 |
| 138 void CommandHandler::GetAllCommands(base::DictionaryValue* commands) { | 139 void CommandHandler::GetAllCommands(base::DictionaryValue* commands) { |
| 139 base::ListValue* results = new base::ListValue; | 140 base::ListValue* results = new base::ListValue; |
| 140 | 141 |
| 141 Profile* profile = Profile::FromWebUI(web_ui()); | 142 Profile* profile = Profile::FromWebUI(web_ui()); |
| 142 CommandService* command_service = CommandService::Get(profile); | 143 CommandService* command_service = CommandService::Get(profile); |
| 143 | 144 |
| 144 const ExtensionSet* extensions = extensions::ExtensionSystem::Get(profile)-> | 145 const ExtensionSet* extensions = extensions::ExtensionSystem::Get(profile)-> |
|
Devlin
2014/05/16 17:34:04
Please change to ExtensionRegistry::enabled_extens
| |
| 145 extension_service()->extensions(); | 146 extension_service()->extensions(); |
| 146 for (ExtensionSet::const_iterator extension = extensions->begin(); | 147 for (ExtensionSet::const_iterator extension = extensions->begin(); |
| 147 extension != extensions->end(); ++extension) { | 148 extension != extensions->end(); ++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 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 |
| OLD | NEW |