| 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/common/extensions/command.h" | 5 #include "chrome/common/extensions/command.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 9 #include "base/string_split.h" | 9 #include "base/string_split.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 accelerator); | 46 accelerator); |
| 47 return ui::Accelerator(); | 47 return ui::Accelerator(); |
| 48 } | 48 } |
| 49 | 49 |
| 50 // Now, parse it into an accelerator. | 50 // Now, parse it into an accelerator. |
| 51 int modifiers = ui::EF_NONE; | 51 int modifiers = ui::EF_NONE; |
| 52 ui::KeyboardCode key = ui::VKEY_UNKNOWN; | 52 ui::KeyboardCode key = ui::VKEY_UNKNOWN; |
| 53 for (size_t i = 0; i < tokens.size(); i++) { | 53 for (size_t i = 0; i < tokens.size(); i++) { |
| 54 if (tokens[i] == "Ctrl") { | 54 if (tokens[i] == "Ctrl") { |
| 55 modifiers |= ui::EF_CONTROL_DOWN; | 55 modifiers |= ui::EF_CONTROL_DOWN; |
| 56 } else if (tokens[i] == "Alt") { | 56 } else if (tokens[i] == "Alt" || |
| 57 (tokens[i] == "Option" && platform_key == "mac")) { |
| 57 modifiers |= ui::EF_ALT_DOWN; | 58 modifiers |= ui::EF_ALT_DOWN; |
| 58 } else if (tokens[i] == "Shift") { | 59 } else if (tokens[i] == "Shift") { |
| 59 modifiers |= ui::EF_SHIFT_DOWN; | 60 modifiers |= ui::EF_SHIFT_DOWN; |
| 60 } else if (tokens[i] == "Command" && platform_key == "mac") { | 61 } else if (tokens[i] == "Command" && platform_key == "mac") { |
| 61 // TODO(finnur): Implement for Mac. | 62 modifiers |= ui::EF_COMMAND_DOWN; |
| 62 // TODO(finnur): Reject Shift modifier if no Cmd/Opt (see below). | |
| 63 } else if (tokens[i] == "Option" && platform_key == "mac") { | |
| 64 // TODO(finnur): Implement for Mac. | |
| 65 } else if (tokens[i].size() == 1) { | 63 } else if (tokens[i].size() == 1) { |
| 66 if (key != ui::VKEY_UNKNOWN) { | 64 if (key != ui::VKEY_UNKNOWN) { |
| 67 // Multiple key assignments. | 65 // Multiple key assignments. |
| 68 key = ui::VKEY_UNKNOWN; | 66 key = ui::VKEY_UNKNOWN; |
| 69 break; | 67 break; |
| 70 } | 68 } |
| 71 if (tokens[i][0] >= 'A' && tokens[i][0] <= 'Z') { | 69 if (tokens[i][0] >= 'A' && tokens[i][0] <= 'Z') { |
| 72 key = static_cast<ui::KeyboardCode>(ui::VKEY_A + (tokens[i][0] - 'A')); | 70 key = static_cast<ui::KeyboardCode>(ui::VKEY_A + (tokens[i][0] - 'A')); |
| 73 } else if (tokens[i][0] >= '0' && tokens[i][0] <= '9') { | 71 } else if (tokens[i][0] >= '0' && tokens[i][0] <= '9') { |
| 74 key = static_cast<ui::KeyboardCode>(ui::VKEY_0 + (tokens[i][0] - '0')); | 72 key = static_cast<ui::KeyboardCode>(ui::VKEY_0 + (tokens[i][0] - '0')); |
| 75 } else { | 73 } else { |
| 76 key = ui::VKEY_UNKNOWN; | 74 key = ui::VKEY_UNKNOWN; |
| 77 break; | 75 break; |
| 78 } | 76 } |
| 79 } else { | 77 } else { |
| 80 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( | 78 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( |
| 81 errors::kInvalidKeyBinding, | 79 errors::kInvalidKeyBinding, |
| 82 base::IntToString(index), | 80 base::IntToString(index), |
| 83 platform_key, | 81 platform_key, |
| 84 accelerator); | 82 accelerator); |
| 85 return ui::Accelerator(); | 83 return ui::Accelerator(); |
| 86 } | 84 } |
| 87 } | 85 } |
| 86 bool command = (modifiers & ui::EF_COMMAND_DOWN) != 0; |
| 88 bool ctrl = (modifiers & ui::EF_CONTROL_DOWN) != 0; | 87 bool ctrl = (modifiers & ui::EF_CONTROL_DOWN) != 0; |
| 89 bool alt = (modifiers & ui::EF_ALT_DOWN) != 0; | 88 bool alt = (modifiers & ui::EF_ALT_DOWN) != 0; |
| 90 bool shift = (modifiers & ui::EF_SHIFT_DOWN) != 0; | 89 bool shift = (modifiers & ui::EF_SHIFT_DOWN) != 0; |
| 90 |
| 91 // We support Ctrl+foo, Alt+foo, Ctrl+Shift+foo, Alt+Shift+foo, but not | 91 // We support Ctrl+foo, Alt+foo, Ctrl+Shift+foo, Alt+Shift+foo, but not |
| 92 // Ctrl+Alt+foo and not Shift+foo either. For a more detailed reason why we | 92 // Ctrl+Alt+foo and not Shift+foo either. For a more detailed reason why we |
| 93 // don't support Ctrl+Alt+foo see this article: | 93 // don't support Ctrl+Alt+foo see this article: |
| 94 // http://blogs.msdn.com/b/oldnewthing/archive/2004/03/29/101121.aspx. | 94 // http://blogs.msdn.com/b/oldnewthing/archive/2004/03/29/101121.aspx. |
| 95 // On Mac Command can also be used in combination with Shift or on its own, |
| 96 // as a modifier. |
| 95 if (key == ui::VKEY_UNKNOWN || (ctrl && alt) || | 97 if (key == ui::VKEY_UNKNOWN || (ctrl && alt) || |
| 96 ((platform_key != "mac") && shift && !ctrl && !alt)) { | 98 (shift && !ctrl && !alt && !command)) { |
| 97 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( | 99 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( |
| 98 errors::kInvalidKeyBinding, | 100 errors::kInvalidKeyBinding, |
| 99 base::IntToString(index), | 101 base::IntToString(index), |
| 100 platform_key, | 102 platform_key, |
| 101 accelerator); | 103 accelerator); |
| 102 return ui::Accelerator(); | 104 return ui::Accelerator(); |
| 103 } | 105 } |
| 104 | 106 |
| 105 return ui::Accelerator(key, modifiers); | 107 return ui::Accelerator(key, modifiers); |
| 106 } | 108 } |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 extension_data->SetString("description", command_description); | 267 extension_data->SetString("description", command_description); |
| 266 extension_data->SetBoolean("active", active); | 268 extension_data->SetBoolean("active", active); |
| 267 extension_data->SetString("keybinding", accelerator().GetShortcutText()); | 269 extension_data->SetString("keybinding", accelerator().GetShortcutText()); |
| 268 extension_data->SetString("command_name", command_name()); | 270 extension_data->SetString("command_name", command_name()); |
| 269 extension_data->SetString("extension_id", extension->id()); | 271 extension_data->SetString("extension_id", extension->id()); |
| 270 | 272 |
| 271 return extension_data; | 273 return extension_data; |
| 272 } | 274 } |
| 273 | 275 |
| 274 } // namespace extensions | 276 } // namespace extensions |
| OLD | NEW |