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