| 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_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 ParseImpl(accelerator, Command::CommandPlatform(), 0, &error); | 201 ParseImpl(accelerator, Command::CommandPlatform(), 0, &error); |
| 202 return parsed; | 202 return parsed; |
| 203 } | 203 } |
| 204 | 204 |
| 205 bool Command::Parse(const DictionaryValue* command, | 205 bool Command::Parse(const DictionaryValue* command, |
| 206 const std::string& command_name, | 206 const std::string& command_name, |
| 207 int index, | 207 int index, |
| 208 string16* error) { | 208 string16* error) { |
| 209 DCHECK(!command_name.empty()); | 209 DCHECK(!command_name.empty()); |
| 210 | 210 |
| 211 string16 description; |
| 212 if (command_name != values::kPageActionCommandEvent && |
| 213 command_name != values::kBrowserActionCommandEvent && |
| 214 command_name != values::kScriptBadgeCommandEvent) { |
| 215 if (!command->GetString(keys::kDescription, &description) || |
| 216 description.empty()) { |
| 217 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 218 errors::kInvalidKeyBindingDescription, |
| 219 base::IntToString(index)); |
| 220 return false; |
| 221 } |
| 222 } |
| 223 |
| 211 // We'll build up a map of platform-to-shortcut suggestions. | 224 // We'll build up a map of platform-to-shortcut suggestions. |
| 212 typedef std::map<const std::string, std::string> SuggestionMap; | 225 typedef std::map<const std::string, std::string> SuggestionMap; |
| 213 SuggestionMap suggestions; | 226 SuggestionMap suggestions; |
| 214 | 227 |
| 215 // First try to parse the |suggested_key| as a dictionary. | 228 // First try to parse the |suggested_key| as a dictionary. |
| 216 const DictionaryValue* suggested_key_dict; | 229 const DictionaryValue* suggested_key_dict; |
| 217 if (command->GetDictionary(keys::kSuggestedKey, &suggested_key_dict)) { | 230 if (command->GetDictionary(keys::kSuggestedKey, &suggested_key_dict)) { |
| 218 for (DictionaryValue::Iterator iter(*suggested_key_dict); !iter.IsAtEnd(); | 231 for (DictionaryValue::Iterator iter(*suggested_key_dict); !iter.IsAtEnd(); |
| 219 iter.Advance()) { | 232 iter.Advance()) { |
| 220 // For each item in the dictionary, extract the platforms specified. | 233 // For each item in the dictionary, extract the platforms specified. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 235 } else { | 248 } else { |
| 236 // No dictionary was found, fall back to using just a string, so developers | 249 // No dictionary was found, fall back to using just a string, so developers |
| 237 // don't have to specify a dictionary if they just want to use one default | 250 // don't have to specify a dictionary if they just want to use one default |
| 238 // for all platforms. | 251 // for all platforms. |
| 239 std::string suggested_key_string; | 252 std::string suggested_key_string; |
| 240 if (command->GetString(keys::kSuggestedKey, &suggested_key_string) && | 253 if (command->GetString(keys::kSuggestedKey, &suggested_key_string) && |
| 241 !suggested_key_string.empty()) { | 254 !suggested_key_string.empty()) { |
| 242 // If only a single string is provided, it must be default for all. | 255 // If only a single string is provided, it must be default for all. |
| 243 suggestions["default"] = suggested_key_string; | 256 suggestions["default"] = suggested_key_string; |
| 244 } else { | 257 } else { |
| 245 *error = ErrorUtils::FormatErrorMessageUTF16( | 258 suggestions["default"] = ""; |
| 246 errors::kInvalidKeyBinding, | |
| 247 base::IntToString(index), | |
| 248 keys::kSuggestedKey, | |
| 249 kMissing); | |
| 250 return false; | |
| 251 } | 259 } |
| 252 } | 260 } |
| 253 | 261 |
| 254 // Normalize the suggestions. | 262 // Normalize the suggestions. |
| 255 for (SuggestionMap::iterator iter = suggestions.begin(); | 263 for (SuggestionMap::iterator iter = suggestions.begin(); |
| 256 iter != suggestions.end(); ++iter) { | 264 iter != suggestions.end(); ++iter) { |
| 257 // Before we normalize Ctrl to Command we must detect when the developer | 265 // Before we normalize Ctrl to Command we must detect when the developer |
| 258 // specified Command in the Default section, which will work on Mac after | 266 // specified Command in the Default section, which will work on Mac after |
| 259 // normalization but only fail on other platforms when they try it out on | 267 // normalization but only fail on other platforms when they try it out on |
| 260 // other platforms, which is not what we want. | 268 // other platforms, which is not what we want. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 283 keys::kSuggestedKey, | 291 keys::kSuggestedKey, |
| 284 platform); | 292 platform); |
| 285 return false; // No platform specified and no fallback. Bail. | 293 return false; // No platform specified and no fallback. Bail. |
| 286 } | 294 } |
| 287 | 295 |
| 288 // For developer convenience, we parse all the suggestions (and complain about | 296 // For developer convenience, we parse all the suggestions (and complain about |
| 289 // errors for platforms other than the current one) but use only what we need. | 297 // errors for platforms other than the current one) but use only what we need. |
| 290 std::map<const std::string, std::string>::const_iterator iter = | 298 std::map<const std::string, std::string>::const_iterator iter = |
| 291 suggestions.begin(); | 299 suggestions.begin(); |
| 292 for ( ; iter != suggestions.end(); ++iter) { | 300 for ( ; iter != suggestions.end(); ++iter) { |
| 293 // Note that we pass iter->first to pretend we are on a platform we're not | 301 ui::Accelerator accelerator; |
| 294 // on. | 302 if (!iter->second.empty()) { |
| 295 ui::Accelerator accelerator = | 303 // Note that we pass iter->first to pretend we are on a platform we're not |
| 296 ParseImpl(iter->second, iter->first, index, error); | 304 // on. |
| 297 if (accelerator.key_code() == ui::VKEY_UNKNOWN) { | 305 accelerator = ParseImpl(iter->second, iter->first, index, error); |
| 298 *error = ErrorUtils::FormatErrorMessageUTF16( | 306 if (accelerator.key_code() == ui::VKEY_UNKNOWN) { |
| 299 errors::kInvalidKeyBinding, | 307 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 300 base::IntToString(index), | 308 errors::kInvalidKeyBinding, |
| 301 iter->first, | 309 base::IntToString(index), |
| 302 iter->second); | 310 iter->first, |
| 303 return false; | 311 iter->second); |
| 312 return false; |
| 313 } |
| 304 } | 314 } |
| 305 | 315 |
| 306 if (iter->first == key) { | 316 if (iter->first == key) { |
| 307 // This platform is our platform, so grab this key. | 317 // This platform is our platform, so grab this key. |
| 308 accelerator_ = accelerator; | 318 accelerator_ = accelerator; |
| 309 command_name_ = command_name; | 319 command_name_ = command_name; |
| 310 | 320 description_ = description; |
| 311 if (command_name != | |
| 312 extension_manifest_values::kPageActionCommandEvent && | |
| 313 command_name != | |
| 314 extension_manifest_values::kBrowserActionCommandEvent && | |
| 315 command_name != | |
| 316 extension_manifest_values::kScriptBadgeCommandEvent) { | |
| 317 if (!command->GetString(keys::kDescription, &description_) || | |
| 318 description_.empty()) { | |
| 319 *error = ErrorUtils::FormatErrorMessageUTF16( | |
| 320 errors::kInvalidKeyBindingDescription, | |
| 321 base::IntToString(index)); | |
| 322 return false; | |
| 323 } | |
| 324 } | |
| 325 } | 321 } |
| 326 } | 322 } |
| 327 return true; | 323 return true; |
| 328 } | 324 } |
| 329 | 325 |
| 330 DictionaryValue* Command::ToValue(const Extension* extension, | 326 DictionaryValue* Command::ToValue(const Extension* extension, |
| 331 bool active) const { | 327 bool active) const { |
| 332 DictionaryValue* extension_data = new DictionaryValue(); | 328 DictionaryValue* extension_data = new DictionaryValue(); |
| 333 | 329 |
| 334 string16 command_description; | 330 string16 command_description; |
| 335 if (command_name() == values::kBrowserActionCommandEvent || | 331 if (command_name() == values::kBrowserActionCommandEvent || |
| 336 command_name() == values::kPageActionCommandEvent || | 332 command_name() == values::kPageActionCommandEvent || |
| 337 command_name() == values::kScriptBadgeCommandEvent) { | 333 command_name() == values::kScriptBadgeCommandEvent) { |
| 338 command_description = | 334 command_description = |
| 339 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_GENERIC_ACTIVATE); | 335 l10n_util::GetStringUTF16(IDS_EXTENSION_COMMANDS_GENERIC_ACTIVATE); |
| 340 } else { | 336 } else { |
| 341 command_description = description(); | 337 command_description = description(); |
| 342 } | 338 } |
| 343 extension_data->SetString("description", command_description); | 339 extension_data->SetString("description", command_description); |
| 344 extension_data->SetBoolean("active", active); | 340 extension_data->SetBoolean("active", active); |
| 345 extension_data->SetString("keybinding", accelerator().GetShortcutText()); | 341 extension_data->SetString("keybinding", accelerator().GetShortcutText()); |
| 346 extension_data->SetString("command_name", command_name()); | 342 extension_data->SetString("command_name", command_name()); |
| 347 extension_data->SetString("extension_id", extension->id()); | 343 extension_data->SetString("extension_id", extension->id()); |
| 348 | 344 |
| 349 return extension_data; | 345 return extension_data; |
| 350 } | 346 } |
| 351 | 347 |
| 352 } // namespace extensions | 348 } // namespace extensions |
| OLD | NEW |