| 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 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 if (tokens[i] == values::kKeyCtrl) | 243 if (tokens[i] == values::kKeyCtrl) |
| 244 tokens[i] = values::kKeyCommand; | 244 tokens[i] = values::kKeyCommand; |
| 245 else if (tokens[i] == values::kKeyMacCtrl) | 245 else if (tokens[i] == values::kKeyMacCtrl) |
| 246 tokens[i] = values::kKeyCtrl; | 246 tokens[i] = values::kKeyCtrl; |
| 247 } | 247 } |
| 248 return JoinString(tokens, '+'); | 248 return JoinString(tokens, '+'); |
| 249 } | 249 } |
| 250 | 250 |
| 251 } // namespace | 251 } // namespace |
| 252 | 252 |
| 253 Command::Command() {} | 253 Command::Command() : global_(false) {} |
| 254 | 254 |
| 255 Command::Command(const std::string& command_name, | 255 Command::Command(const std::string& command_name, |
| 256 const string16& description, | 256 const string16& description, |
| 257 const std::string& accelerator) | 257 const std::string& accelerator, |
| 258 bool global) |
| 258 : command_name_(command_name), | 259 : command_name_(command_name), |
| 259 description_(description) { | 260 description_(description), |
| 261 global_(global) { |
| 260 string16 error; | 262 string16 error; |
| 261 accelerator_ = ParseImpl(accelerator, CommandPlatform(), 0, | 263 accelerator_ = ParseImpl(accelerator, CommandPlatform(), 0, |
| 262 IsNamedCommand(command_name), &error); | 264 IsNamedCommand(command_name), &error); |
| 263 } | 265 } |
| 264 | 266 |
| 265 Command::~Command() {} | 267 Command::~Command() {} |
| 266 | 268 |
| 267 // static | 269 // static |
| 268 std::string Command::CommandPlatform() { | 270 std::string Command::CommandPlatform() { |
| 269 #if defined(OS_WIN) | 271 #if defined(OS_WIN) |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 std::string suggested_key_string; | 427 std::string suggested_key_string; |
| 426 if (command->GetString(keys::kSuggestedKey, &suggested_key_string) && | 428 if (command->GetString(keys::kSuggestedKey, &suggested_key_string) && |
| 427 !suggested_key_string.empty()) { | 429 !suggested_key_string.empty()) { |
| 428 // If only a single string is provided, it must be default for all. | 430 // If only a single string is provided, it must be default for all. |
| 429 suggestions[values::kKeybindingPlatformDefault] = suggested_key_string; | 431 suggestions[values::kKeybindingPlatformDefault] = suggested_key_string; |
| 430 } else { | 432 } else { |
| 431 suggestions[values::kKeybindingPlatformDefault] = ""; | 433 suggestions[values::kKeybindingPlatformDefault] = ""; |
| 432 } | 434 } |
| 433 } | 435 } |
| 434 | 436 |
| 437 // Check if this is a global or a regular shortcut. |
| 438 bool global = false; |
| 439 command->GetBoolean(keys::kGlobal, &global); |
| 440 |
| 435 // Normalize the suggestions. | 441 // Normalize the suggestions. |
| 436 for (SuggestionMap::iterator iter = suggestions.begin(); | 442 for (SuggestionMap::iterator iter = suggestions.begin(); |
| 437 iter != suggestions.end(); ++iter) { | 443 iter != suggestions.end(); ++iter) { |
| 438 // Before we normalize Ctrl to Command we must detect when the developer | 444 // Before we normalize Ctrl to Command we must detect when the developer |
| 439 // specified Command in the Default section, which will work on Mac after | 445 // specified Command in the Default section, which will work on Mac after |
| 440 // normalization but only fail on other platforms when they try it out on | 446 // normalization but only fail on other platforms when they try it out on |
| 441 // other platforms, which is not what we want. | 447 // other platforms, which is not what we want. |
| 442 if (iter->first == values::kKeybindingPlatformDefault && | 448 if (iter->first == values::kKeybindingPlatformDefault && |
| 443 iter->second.find("Command+") != std::string::npos) { | 449 iter->second.find("Command+") != std::string::npos) { |
| 444 *error = ErrorUtils::FormatErrorMessageUTF16( | 450 *error = ErrorUtils::FormatErrorMessageUTF16( |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 487 } | 493 } |
| 488 return false; | 494 return false; |
| 489 } | 495 } |
| 490 } | 496 } |
| 491 | 497 |
| 492 if (iter->first == key) { | 498 if (iter->first == key) { |
| 493 // This platform is our platform, so grab this key. | 499 // This platform is our platform, so grab this key. |
| 494 accelerator_ = accelerator; | 500 accelerator_ = accelerator; |
| 495 command_name_ = command_name; | 501 command_name_ = command_name; |
| 496 description_ = description; | 502 description_ = description; |
| 503 global_ = global; |
| 497 } | 504 } |
| 498 } | 505 } |
| 499 return true; | 506 return true; |
| 500 } | 507 } |
| 501 | 508 |
| 502 base::DictionaryValue* Command::ToValue(const Extension* extension, | 509 base::DictionaryValue* Command::ToValue(const Extension* extension, |
| 503 bool active) const { | 510 bool active) const { |
| 504 base::DictionaryValue* extension_data = new base::DictionaryValue(); | 511 base::DictionaryValue* extension_data = new base::DictionaryValue(); |
| 505 | 512 |
| 506 string16 command_description; | 513 string16 command_description; |
| 507 if (command_name() == values::kBrowserActionCommandEvent || | 514 if (command_name() == values::kBrowserActionCommandEvent || |
| 508 command_name() == values::kPageActionCommandEvent || | 515 command_name() == values::kPageActionCommandEvent || |
| 509 command_name() == values::kScriptBadgeCommandEvent) { | 516 command_name() == values::kScriptBadgeCommandEvent) { |
| 510 command_description = | 517 command_description = |
| 511 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_GENERIC_ACTIVATE); | 518 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_GENERIC_ACTIVATE); |
| 512 } else { | 519 } else { |
| 513 command_description = description(); | 520 command_description = description(); |
| 514 } | 521 } |
| 515 extension_data->SetString("description", command_description); | 522 extension_data->SetString("description", command_description); |
| 516 extension_data->SetBoolean("active", active); | 523 extension_data->SetBoolean("active", active); |
| 517 extension_data->SetString("keybinding", accelerator().GetShortcutText()); | 524 extension_data->SetString("keybinding", accelerator().GetShortcutText()); |
| 518 extension_data->SetString("command_name", command_name()); | 525 extension_data->SetString("command_name", command_name()); |
| 519 extension_data->SetString("extension_id", extension->id()); | 526 extension_data->SetString("extension_id", extension->id()); |
| 520 | 527 |
| 521 return extension_data; | 528 return extension_data; |
| 522 } | 529 } |
| 523 | 530 |
| 524 } // namespace extensions | 531 } // namespace extensions |
| OLD | NEW |