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/extensions/api/commands/extension_command_service.h" | 5 #include "chrome/browser/extensions/api/commands/extension_command_service.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "chrome/browser/extensions/extension_keybinding_registry.h" | 8 #include "chrome/browser/extensions/extension_keybinding_registry.h" |
9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
10 #include "chrome/browser/extensions/extension_system.h" | 10 #include "chrome/browser/extensions/extension_system.h" |
11 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 11 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/common/chrome_notification_types.h" | 13 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "chrome/common/extensions/extension_manifest_constants.h" |
14 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
15 #include "content/public/browser/notification_service.h" | 16 #include "content/public/browser/notification_service.h" |
| 17 #include "grit/generated_resources.h" |
| 18 #include "ui/base/l10n/l10n_util.h" |
| 19 |
| 20 namespace values = extension_manifest_values; |
16 | 21 |
17 namespace { | 22 namespace { |
18 | 23 |
19 const char kExtension[] = "extension"; | 24 const char kExtension[] = "extension"; |
20 const char kCommandName[] = "command_name"; | 25 const char kCommandName[] = "command_name"; |
21 | 26 |
22 std::string GetPlatformKeybindingKeyForAccelerator( | 27 std::string GetPlatformKeybindingKeyForAccelerator( |
23 const ui::Accelerator& accelerator) { | 28 const ui::Accelerator& accelerator) { |
24 return extensions::Command::CommandPlatform() + ":" + | 29 return extensions::Command::CommandPlatform() + ":" + |
25 UTF16ToUTF8(accelerator.GetShortcutText()); | 30 UTF16ToUTF8(accelerator.GetShortcutText()); |
(...skipping 14 matching lines...) Expand all Loading... |
40 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, | 45 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, |
41 content::Source<Profile>(profile)); | 46 content::Source<Profile>(profile)); |
42 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, | 47 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
43 content::Source<Profile>(profile)); | 48 content::Source<Profile>(profile)); |
44 } | 49 } |
45 | 50 |
46 ExtensionCommandService::~ExtensionCommandService() { | 51 ExtensionCommandService::~ExtensionCommandService() { |
47 } | 52 } |
48 | 53 |
49 const extensions::Command* | 54 const extensions::Command* |
50 ExtensionCommandService::GetActiveBrowserActionCommand( | 55 ExtensionCommandService::GetBrowserActionCommand( |
51 const std::string& extension_id) { | 56 const std::string& extension_id, ExtensionCommandQueryType type) { |
52 const ExtensionSet* extensions = | 57 const ExtensionSet* extensions = |
53 ExtensionSystem::Get(profile_)->extension_service()->extensions(); | 58 ExtensionSystem::Get(profile_)->extension_service()->extensions(); |
54 const Extension* extension = extensions->GetByID(extension_id); | 59 const Extension* extension = extensions->GetByID(extension_id); |
55 CHECK(extension); | 60 CHECK(extension); |
56 | 61 |
57 const extensions::Command* command = extension->browser_action_command(); | 62 const extensions::Command* command = extension->browser_action_command(); |
58 if (!command) | 63 if (!command) |
59 return NULL; | 64 return NULL; |
60 if (!IsKeybindingActive(command->accelerator(), | 65 if (type == ACTIVE_ONLY && |
| 66 !IsKeybindingActive(command->accelerator(), |
61 extension_id, | 67 extension_id, |
62 command->command_name())) { | 68 command->command_name())) { |
63 return NULL; | 69 return NULL; |
64 } | 70 } |
65 | 71 |
66 return command; | 72 return command; |
67 } | 73 } |
68 | 74 |
69 const extensions::Command* ExtensionCommandService::GetActivePageActionCommand( | 75 const extensions::Command* ExtensionCommandService::GetPageActionCommand( |
70 const std::string& extension_id) { | 76 const std::string& extension_id, ExtensionCommandQueryType type) { |
71 const ExtensionSet* extensions = | 77 const ExtensionSet* extensions = |
72 ExtensionSystem::Get(profile_)->extension_service()->extensions(); | 78 ExtensionSystem::Get(profile_)->extension_service()->extensions(); |
73 const Extension* extension = extensions->GetByID(extension_id); | 79 const Extension* extension = extensions->GetByID(extension_id); |
74 CHECK(extension); | 80 CHECK(extension); |
75 | 81 |
76 const extensions::Command* command = extension->page_action_command(); | 82 const extensions::Command* command = extension->page_action_command(); |
77 if (!command) | 83 if (!command) |
78 return NULL; | 84 return NULL; |
79 if (!IsKeybindingActive(command->accelerator(), | 85 if (type == ACTIVE_ONLY && |
| 86 !IsKeybindingActive(command->accelerator(), |
80 extension_id, | 87 extension_id, |
81 command->command_name())) { | 88 command->command_name())) { |
82 return NULL; | 89 return NULL; |
83 } | 90 } |
84 | 91 |
85 return command; | 92 return command; |
86 } | 93 } |
87 | 94 |
88 extensions::CommandMap ExtensionCommandService::GetActiveNamedCommands( | 95 extensions::CommandMap ExtensionCommandService::GetNamedCommands( |
89 const std::string& extension_id) { | 96 const std::string& extension_id, ExtensionCommandQueryType type) { |
90 const ExtensionSet* extensions = | 97 const ExtensionSet* extensions = |
91 ExtensionSystem::Get(profile_)->extension_service()->extensions(); | 98 ExtensionSystem::Get(profile_)->extension_service()->extensions(); |
92 const Extension* extension = extensions->GetByID(extension_id); | 99 const Extension* extension = extensions->GetByID(extension_id); |
93 CHECK(extension); | 100 CHECK(extension); |
94 | 101 |
95 extensions::CommandMap result; | 102 extensions::CommandMap result; |
96 const extensions::CommandMap& commands = extension->named_commands(); | 103 const extensions::CommandMap& commands = extension->named_commands(); |
97 if (commands.empty()) | 104 if (commands.empty()) |
98 return result; | 105 return result; |
99 | 106 |
100 extensions::CommandMap::const_iterator iter = commands.begin(); | 107 extensions::CommandMap::const_iterator iter = commands.begin(); |
101 for (; iter != commands.end(); ++iter) { | 108 for (; iter != commands.end(); ++iter) { |
102 if (!IsKeybindingActive(iter->second.accelerator(), | 109 if (type == ACTIVE_ONLY && |
| 110 !IsKeybindingActive(iter->second.accelerator(), |
103 extension_id, | 111 extension_id, |
104 iter->second.command_name())) { | 112 iter->second.command_name())) { |
105 continue; | 113 continue; |
106 } | 114 } |
107 | 115 |
108 result[iter->second.command_name()] = iter->second; | 116 result[iter->second.command_name()] = iter->second; |
109 } | 117 } |
110 | 118 |
111 return result; | 119 return result; |
112 } | 120 } |
113 | 121 |
| 122 void ExtensionCommandService::GetAllCommands(DictionaryValue* commands) { |
| 123 ListValue* results = new ListValue; |
| 124 |
| 125 const ExtensionSet* extensions = |
| 126 ExtensionSystem::Get(profile_)->extension_service()->extensions(); |
| 127 for (ExtensionSet::const_iterator extension = extensions->begin(); |
| 128 extension != extensions->end(); ++extension) { |
| 129 scoped_ptr<DictionaryValue> extension_dict(new DictionaryValue); |
| 130 extension_dict->SetString("name", (*extension)->name()); |
| 131 extension_dict->SetString("id", (*extension)->id()); |
| 132 |
| 133 // Add the keybindings to a list structure. |
| 134 scoped_ptr<ListValue> extensions_list(new ListValue()); |
| 135 |
| 136 const extensions::Command* browser_action = |
| 137 GetBrowserActionCommand((*extension)->id(), ALL); |
| 138 if (browser_action) { |
| 139 extensions_list->Append(CreateExtensionDetailValue(*extension, |
| 140 browser_action)); |
| 141 } |
| 142 |
| 143 const extensions::Command* page_action = |
| 144 GetPageActionCommand((*extension)->id(), ALL); |
| 145 if (page_action) { |
| 146 extensions_list->Append(CreateExtensionDetailValue(*extension, |
| 147 page_action)); |
| 148 } |
| 149 |
| 150 extensions::CommandMap named_commands = |
| 151 GetNamedCommands((*extension)->id(), ALL); |
| 152 extensions::CommandMap::const_iterator iter = named_commands.begin(); |
| 153 for (; iter != named_commands.end(); ++iter) { |
| 154 extensions_list->Append( |
| 155 CreateExtensionDetailValue(*extension, &iter->second)); |
| 156 } |
| 157 |
| 158 if (!extensions_list->empty()) { |
| 159 extension_dict->Set("commands", extensions_list.release()); |
| 160 results->Append(extension_dict.release()); |
| 161 } |
| 162 } |
| 163 |
| 164 commands->Set("commands", results); |
| 165 } |
| 166 |
114 bool ExtensionCommandService::IsKeybindingActive( | 167 bool ExtensionCommandService::IsKeybindingActive( |
115 const ui::Accelerator& accelerator, | 168 const ui::Accelerator& accelerator, |
116 std::string extension_id, | 169 std::string extension_id, |
117 std::string command_name) { | 170 std::string command_name) { |
118 CHECK(!extension_id.empty()); | 171 CHECK(!extension_id.empty()); |
119 CHECK(!command_name.empty()); | 172 CHECK(!command_name.empty()); |
120 | 173 |
121 std::string key = GetPlatformKeybindingKeyForAccelerator(accelerator); | 174 std::string key = GetPlatformKeybindingKeyForAccelerator(accelerator); |
122 const DictionaryValue* bindings = | 175 const DictionaryValue* bindings = |
123 profile_->GetPrefs()->GetDictionary(prefs::kExtensionKeybindings); | 176 profile_->GetPrefs()->GetDictionary(prefs::kExtensionKeybindings); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 item->GetString(kExtension, &extension); | 278 item->GetString(kExtension, &extension); |
226 if (extension == extension_id) | 279 if (extension == extension_id) |
227 keys_to_remove.push_back(key); | 280 keys_to_remove.push_back(key); |
228 } | 281 } |
229 | 282 |
230 for (KeysToRemove::const_iterator it = keys_to_remove.begin(); | 283 for (KeysToRemove::const_iterator it = keys_to_remove.begin(); |
231 it != keys_to_remove.end(); ++it) { | 284 it != keys_to_remove.end(); ++it) { |
232 bindings->Remove(*it, NULL); | 285 bindings->Remove(*it, NULL); |
233 } | 286 } |
234 } | 287 } |
| 288 |
| 289 DictionaryValue* ExtensionCommandService::CreateExtensionDetailValue( |
| 290 const Extension* extension, |
| 291 const extensions::Command* command) { |
| 292 DictionaryValue* extension_data = new DictionaryValue(); |
| 293 |
| 294 string16 description; |
| 295 if (command->command_name() == values::kBrowserActionKeybindingEvent || |
| 296 command->command_name() == values::kPageActionKeybindingEvent) { |
| 297 description = |
| 298 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_GENERIC_ACTIVATE); |
| 299 } else { |
| 300 description = command->description(); |
| 301 } |
| 302 extension_data->SetString("description", description); |
| 303 extension_data->SetBoolean( |
| 304 "active", IsKeybindingActive(command->accelerator(), |
| 305 extension->id(), |
| 306 command->command_name())); |
| 307 extension_data->SetString("keybinding", |
| 308 command->accelerator().GetShortcutText()); |
| 309 |
| 310 return extension_data; |
| 311 } |
OLD | NEW |