| 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/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 namespace keys = manifest_keys; | 21 namespace keys = manifest_keys; |
| 22 namespace values = manifest_values; | 22 namespace values = manifest_values; |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 static const char kMissing[] = "Missing"; | 26 static const char kMissing[] = "Missing"; |
| 27 | 27 |
| 28 static const char kCommandKeyNotSupported[] = | 28 static const char kCommandKeyNotSupported[] = |
| 29 "Command key is not supported. Note: Ctrl means Command on Mac"; | 29 "Command key is not supported. Note: Ctrl means Command on Mac"; |
| 30 | 30 |
| 31 #if defined(OS_CHROMEOS) |
| 32 // ChromeOS supports an additional modifier 'Search', which can result in longer |
| 33 // sequences. |
| 34 static const int kMaxTokenSize = 4; |
| 35 #else |
| 36 static const int kMaxTokenSize = 3; |
| 37 #endif // OS_CHROMEOS |
| 38 |
| 31 bool IsNamedCommand(const std::string& command_name) { | 39 bool IsNamedCommand(const std::string& command_name) { |
| 32 return command_name != values::kPageActionCommandEvent && | 40 return command_name != values::kPageActionCommandEvent && |
| 33 command_name != values::kBrowserActionCommandEvent; | 41 command_name != values::kBrowserActionCommandEvent; |
| 34 } | 42 } |
| 35 | 43 |
| 36 bool DoesRequireModifier(const std::string& accelerator) { | 44 bool DoesRequireModifier(const std::string& accelerator) { |
| 37 return accelerator != values::kKeyMediaNextTrack && | 45 return accelerator != values::kKeyMediaNextTrack && |
| 38 accelerator != values::kKeyMediaPlayPause && | 46 accelerator != values::kKeyMediaPlayPause && |
| 39 accelerator != values::kKeyMediaPrevTrack && | 47 accelerator != values::kKeyMediaPrevTrack && |
| 40 accelerator != values::kKeyMediaStop; | 48 accelerator != values::kKeyMediaStop; |
| 41 } | 49 } |
| 42 | 50 |
| 51 // Parse an |accelerator| for a given platform (specified by |platform_key|) and |
| 52 // return the result as a ui::Accelerator if successful, or VKEY_UNKNOWN if not. |
| 53 // |index| is used when constructing an |error| messages to show which command |
| 54 // in the manifest is failing and |should_parse_media_keys| specifies whether |
| 55 // media keys are to be considered for parsing. |
| 56 // Note: If the parsing rules here are changed, make sure to update the |
| 57 // corresponding extension_command_list.js validation, which validates the user |
| 58 // input for chrome://extensions/configureCommands. |
| 43 ui::Accelerator ParseImpl(const std::string& accelerator, | 59 ui::Accelerator ParseImpl(const std::string& accelerator, |
| 44 const std::string& platform_key, | 60 const std::string& platform_key, |
| 45 int index, | 61 int index, |
| 46 bool should_parse_media_keys, | 62 bool should_parse_media_keys, |
| 47 base::string16* error) { | 63 base::string16* error) { |
| 48 error->clear(); | 64 error->clear(); |
| 49 if (platform_key != values::kKeybindingPlatformWin && | 65 if (platform_key != values::kKeybindingPlatformWin && |
| 50 platform_key != values::kKeybindingPlatformMac && | 66 platform_key != values::kKeybindingPlatformMac && |
| 51 platform_key != values::kKeybindingPlatformChromeOs && | 67 platform_key != values::kKeybindingPlatformChromeOs && |
| 52 platform_key != values::kKeybindingPlatformLinux && | 68 platform_key != values::kKeybindingPlatformLinux && |
| 53 platform_key != values::kKeybindingPlatformDefault) { | 69 platform_key != values::kKeybindingPlatformDefault) { |
| 54 *error = ErrorUtils::FormatErrorMessageUTF16( | 70 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 55 errors::kInvalidKeyBindingUnknownPlatform, | 71 errors::kInvalidKeyBindingUnknownPlatform, |
| 56 base::IntToString(index), | 72 base::IntToString(index), |
| 57 platform_key); | 73 platform_key); |
| 58 return ui::Accelerator(); | 74 return ui::Accelerator(); |
| 59 } | 75 } |
| 60 | 76 |
| 61 std::vector<std::string> tokens; | 77 std::vector<std::string> tokens; |
| 62 base::SplitString(accelerator, '+', &tokens); | 78 base::SplitString(accelerator, '+', &tokens); |
| 63 if (tokens.size() == 0 || | 79 if (tokens.size() == 0 || |
| 64 (tokens.size() == 1 && DoesRequireModifier(accelerator)) || | 80 (tokens.size() == 1 && DoesRequireModifier(accelerator)) || |
| 65 tokens.size() > 3) { | 81 tokens.size() > kMaxTokenSize) { |
| 66 *error = ErrorUtils::FormatErrorMessageUTF16( | 82 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 67 errors::kInvalidKeyBinding, | 83 errors::kInvalidKeyBinding, |
| 68 base::IntToString(index), | 84 base::IntToString(index), |
| 69 platform_key, | 85 platform_key, |
| 70 accelerator); | 86 accelerator); |
| 71 return ui::Accelerator(); | 87 return ui::Accelerator(); |
| 72 } | 88 } |
| 73 | 89 |
| 74 // Now, parse it into an accelerator. | 90 // Now, parse it into an accelerator. |
| 75 int modifiers = ui::EF_NONE; | 91 int modifiers = ui::EF_NONE; |
| (...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 548 extension_data->SetBoolean("active", active); | 564 extension_data->SetBoolean("active", active); |
| 549 extension_data->SetString("keybinding", accelerator().GetShortcutText()); | 565 extension_data->SetString("keybinding", accelerator().GetShortcutText()); |
| 550 extension_data->SetString("command_name", command_name()); | 566 extension_data->SetString("command_name", command_name()); |
| 551 extension_data->SetString("extension_id", extension->id()); | 567 extension_data->SetString("extension_id", extension->id()); |
| 552 extension_data->SetBoolean("global", global()); | 568 extension_data->SetBoolean("global", global()); |
| 553 extension_data->SetBoolean("extension_action", extension_action); | 569 extension_data->SetBoolean("extension_action", extension_action); |
| 554 return extension_data; | 570 return extension_data; |
| 555 } | 571 } |
| 556 | 572 |
| 557 } // namespace extensions | 573 } // namespace extensions |
| OLD | NEW |