| 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/command_service.h" | 5 #include "chrome/browser/extensions/api/commands/command_service.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/extensions/api/commands/commands.h" | 10 #include "chrome/browser/extensions/api/commands/commands.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 | 36 |
| 37 std::string GetPlatformKeybindingKeyForAccelerator( | 37 std::string GetPlatformKeybindingKeyForAccelerator( |
| 38 const ui::Accelerator& accelerator) { | 38 const ui::Accelerator& accelerator) { |
| 39 return extensions::Command::CommandPlatform() + ":" + | 39 return extensions::Command::CommandPlatform() + ":" + |
| 40 extensions::Command::AcceleratorToString(accelerator); | 40 extensions::Command::AcceleratorToString(accelerator); |
| 41 } | 41 } |
| 42 | 42 |
| 43 void SetInitialBindingsHaveBeenAssigned( | 43 void SetInitialBindingsHaveBeenAssigned( |
| 44 ExtensionPrefs* prefs, const std::string& extension_id) { | 44 ExtensionPrefs* prefs, const std::string& extension_id) { |
| 45 prefs->UpdateExtensionPref(extension_id, kInitialBindingsHaveBeenAssigned, | 45 prefs->UpdateExtensionPref(extension_id, kInitialBindingsHaveBeenAssigned, |
| 46 Value::CreateBooleanValue(true)); | 46 base::Value::CreateBooleanValue(true)); |
| 47 } | 47 } |
| 48 | 48 |
| 49 bool InitialBindingsHaveBeenAssigned( | 49 bool InitialBindingsHaveBeenAssigned( |
| 50 const ExtensionPrefs* prefs, const std::string& extension_id) { | 50 const ExtensionPrefs* prefs, const std::string& extension_id) { |
| 51 bool assigned = false; | 51 bool assigned = false; |
| 52 if (!prefs || !prefs->ReadPrefAsBoolean(extension_id, | 52 if (!prefs || !prefs->ReadPrefAsBoolean(extension_id, |
| 53 kInitialBindingsHaveBeenAssigned, | 53 kInitialBindingsHaveBeenAssigned, |
| 54 &assigned)) | 54 &assigned)) |
| 55 return false; | 55 return false; |
| 56 | 56 |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 bool CommandService::AddKeybindingPref( | 158 bool CommandService::AddKeybindingPref( |
| 159 const ui::Accelerator& accelerator, | 159 const ui::Accelerator& accelerator, |
| 160 std::string extension_id, | 160 std::string extension_id, |
| 161 std::string command_name, | 161 std::string command_name, |
| 162 bool allow_overrides) { | 162 bool allow_overrides) { |
| 163 if (accelerator.key_code() == ui::VKEY_UNKNOWN) | 163 if (accelerator.key_code() == ui::VKEY_UNKNOWN) |
| 164 return false; | 164 return false; |
| 165 | 165 |
| 166 DictionaryPrefUpdate updater(profile_->GetPrefs(), | 166 DictionaryPrefUpdate updater(profile_->GetPrefs(), |
| 167 prefs::kExtensionCommands); | 167 prefs::kExtensionCommands); |
| 168 DictionaryValue* bindings = updater.Get(); | 168 base::DictionaryValue* bindings = updater.Get(); |
| 169 | 169 |
| 170 std::string key = GetPlatformKeybindingKeyForAccelerator(accelerator); | 170 std::string key = GetPlatformKeybindingKeyForAccelerator(accelerator); |
| 171 | 171 |
| 172 if (!allow_overrides && bindings->HasKey(key)) | 172 if (!allow_overrides && bindings->HasKey(key)) |
| 173 return false; // Already taken. | 173 return false; // Already taken. |
| 174 | 174 |
| 175 DictionaryValue* keybinding = new DictionaryValue(); | 175 base::DictionaryValue* keybinding = new base::DictionaryValue(); |
| 176 keybinding->SetString(kExtension, extension_id); | 176 keybinding->SetString(kExtension, extension_id); |
| 177 keybinding->SetString(kCommandName, command_name); | 177 keybinding->SetString(kCommandName, command_name); |
| 178 | 178 |
| 179 bindings->Set(key, keybinding); | 179 bindings->Set(key, keybinding); |
| 180 | 180 |
| 181 std::pair<const std::string, const std::string> details = | 181 std::pair<const std::string, const std::string> details = |
| 182 std::make_pair(extension_id, command_name); | 182 std::make_pair(extension_id, command_name); |
| 183 content::NotificationService::current()->Notify( | 183 content::NotificationService::current()->Notify( |
| 184 chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, | 184 chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, |
| 185 content::Source<Profile>(profile_), | 185 content::Source<Profile>(profile_), |
| (...skipping 29 matching lines...) Expand all Loading... |
| 215 // The extension command might be assigned another shortcut. Remove that | 215 // The extension command might be assigned another shortcut. Remove that |
| 216 // shortcut before proceeding. | 216 // shortcut before proceeding. |
| 217 RemoveKeybindingPrefs(extension_id, command_name); | 217 RemoveKeybindingPrefs(extension_id, command_name); |
| 218 | 218 |
| 219 ui::Accelerator accelerator = Command::StringToAccelerator(keystroke); | 219 ui::Accelerator accelerator = Command::StringToAccelerator(keystroke); |
| 220 AddKeybindingPref(accelerator, extension_id, command_name, true); | 220 AddKeybindingPref(accelerator, extension_id, command_name, true); |
| 221 } | 221 } |
| 222 | 222 |
| 223 ui::Accelerator CommandService::FindShortcutForCommand( | 223 ui::Accelerator CommandService::FindShortcutForCommand( |
| 224 const std::string& extension_id, const std::string& command) { | 224 const std::string& extension_id, const std::string& command) { |
| 225 const DictionaryValue* bindings = | 225 const base::DictionaryValue* bindings = |
| 226 profile_->GetPrefs()->GetDictionary(prefs::kExtensionCommands); | 226 profile_->GetPrefs()->GetDictionary(prefs::kExtensionCommands); |
| 227 for (DictionaryValue::Iterator it(*bindings); !it.IsAtEnd(); it.Advance()) { | 227 for (base::DictionaryValue::Iterator it(*bindings); !it.IsAtEnd(); |
| 228 const DictionaryValue* item = NULL; | 228 it.Advance()) { |
| 229 const base::DictionaryValue* item = NULL; |
| 229 it.value().GetAsDictionary(&item); | 230 it.value().GetAsDictionary(&item); |
| 230 | 231 |
| 231 std::string extension; | 232 std::string extension; |
| 232 item->GetString(kExtension, &extension); | 233 item->GetString(kExtension, &extension); |
| 233 if (extension != extension_id) | 234 if (extension != extension_id) |
| 234 continue; | 235 continue; |
| 235 std::string command_name; | 236 std::string command_name; |
| 236 item->GetString(kCommandName, &command_name); | 237 item->GetString(kCommandName, &command_name); |
| 237 if (command != command_name) | 238 if (command != command_name) |
| 238 continue; | 239 continue; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 script_badge_command->command_name(), | 306 script_badge_command->command_name(), |
| 306 false); // Overwriting not allowed. | 307 false); // Overwriting not allowed. |
| 307 } | 308 } |
| 308 } | 309 } |
| 309 } | 310 } |
| 310 | 311 |
| 311 void CommandService::RemoveKeybindingPrefs(const std::string& extension_id, | 312 void CommandService::RemoveKeybindingPrefs(const std::string& extension_id, |
| 312 const std::string& command_name) { | 313 const std::string& command_name) { |
| 313 DictionaryPrefUpdate updater(profile_->GetPrefs(), | 314 DictionaryPrefUpdate updater(profile_->GetPrefs(), |
| 314 prefs::kExtensionCommands); | 315 prefs::kExtensionCommands); |
| 315 DictionaryValue* bindings = updater.Get(); | 316 base::DictionaryValue* bindings = updater.Get(); |
| 316 | 317 |
| 317 typedef std::vector<std::string> KeysToRemove; | 318 typedef std::vector<std::string> KeysToRemove; |
| 318 KeysToRemove keys_to_remove; | 319 KeysToRemove keys_to_remove; |
| 319 for (DictionaryValue::Iterator it(*bindings); !it.IsAtEnd(); it.Advance()) { | 320 for (base::DictionaryValue::Iterator it(*bindings); !it.IsAtEnd(); |
| 320 const DictionaryValue* item = NULL; | 321 it.Advance()) { |
| 322 const base::DictionaryValue* item = NULL; |
| 321 it.value().GetAsDictionary(&item); | 323 it.value().GetAsDictionary(&item); |
| 322 | 324 |
| 323 std::string extension; | 325 std::string extension; |
| 324 item->GetString(kExtension, &extension); | 326 item->GetString(kExtension, &extension); |
| 325 | 327 |
| 326 if (extension == extension_id) { | 328 if (extension == extension_id) { |
| 327 // If |command_name| is specified, delete only that command. Otherwise, | 329 // If |command_name| is specified, delete only that command. Otherwise, |
| 328 // delete all commands. | 330 // delete all commands. |
| 329 if (!command_name.empty()) { | 331 if (!command_name.empty()) { |
| 330 std::string command; | 332 std::string command; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 return false; | 397 return false; |
| 396 | 398 |
| 397 *command = *requested_command; | 399 *command = *requested_command; |
| 398 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN) | 400 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN) |
| 399 command->set_accelerator(shortcut_assigned); | 401 command->set_accelerator(shortcut_assigned); |
| 400 | 402 |
| 401 return true; | 403 return true; |
| 402 } | 404 } |
| 403 | 405 |
| 404 } // namespace extensions | 406 } // namespace extensions |
| OLD | NEW |