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/extension.h" | 5 #include "chrome/common/extensions/extension.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 239 Extension::TtsVoice::~TtsVoice() {} | 239 Extension::TtsVoice::~TtsVoice() {} |
| 240 | 240 |
| 241 Extension::ExtensionKeybinding::ExtensionKeybinding() {} | 241 Extension::ExtensionKeybinding::ExtensionKeybinding() {} |
| 242 Extension::ExtensionKeybinding::~ExtensionKeybinding() {} | 242 Extension::ExtensionKeybinding::~ExtensionKeybinding() {} |
| 243 | 243 |
| 244 bool Extension::ExtensionKeybinding::Parse(DictionaryValue* command, | 244 bool Extension::ExtensionKeybinding::Parse(DictionaryValue* command, |
| 245 const std::string& command_name, | 245 const std::string& command_name, |
| 246 int index, | 246 int index, |
| 247 string16* error) { | 247 string16* error) { |
| 248 DCHECK(!command_name.empty()); | 248 DCHECK(!command_name.empty()); |
| 249 std::string key_binding; | 249 std::string key_binding_win; |
| 250 if (!command->GetString(keys::kKey, &key_binding) || | 250 std::string key_binding_mac; |
| 251 key_binding.empty()) { | 251 std::string key_binding_other; |
| 252 if (!command->GetString(keys::kSuggestedKeyWin, &key_binding_win) || | |
|
Aaron Boodman
2012/03/21 21:05:04
How about setting this up like:
suggested_key: {
Finnur
2012/03/21 22:42:12
If only 'default' is provided, do we map Ctrl to C
| |
| 253 key_binding_win.empty()) { | |
| 252 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( | 254 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( |
| 253 errors::kInvalidKeyBinding, | 255 errors::kInvalidKeyBinding, |
| 254 base::IntToString(index), | 256 base::IntToString(index), |
| 257 keys::kSuggestedKeyWin, | |
| 258 "Missing"); | |
| 259 return false; | |
| 260 } | |
| 261 if (!command->GetString(keys::kSuggestedKeyMac, &key_binding_mac) || | |
| 262 key_binding_mac.empty()) { | |
| 263 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( | |
| 264 errors::kInvalidKeyBinding, | |
| 265 base::IntToString(index), | |
| 266 keys::kSuggestedKeyMac, | |
| 267 "Missing"); | |
| 268 return false; | |
| 269 } | |
| 270 if (!command->GetString(keys::kSuggestedKeyOther, &key_binding_other) || | |
| 271 key_binding_other.empty()) { | |
| 272 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( | |
| 273 errors::kInvalidKeyBinding, | |
| 274 base::IntToString(index), | |
| 275 keys::kSuggestedKeyOther, | |
| 255 "Missing"); | 276 "Missing"); |
| 256 return false; | 277 return false; |
| 257 } | 278 } |
| 258 | 279 |
| 259 std::string original_keybinding = key_binding; | 280 #if defined(OS_WIN) |
|
Aaron Boodman
2012/03/21 21:05:04
Could you do this work before parsing the JSON abo
Finnur
2012/03/21 22:42:12
I'm thinking I should probably refactor this to re
| |
| 260 // Normalize '-' to '+'. | 281 std::string original_keybinding = key_binding_win; |
| 261 ReplaceSubstringsAfterOffset(&key_binding, 0, "-", "+"); | 282 std::string key_binding = StringToLowerASCII(key_binding_win); |
| 262 // Remove all spaces. | 283 std::string platformKey = keys::kSuggestedKeyWin; |
|
Aaron Boodman
2012/03/21 21:05:04
platform_key
| |
| 263 ReplaceSubstringsAfterOffset(&key_binding, 0, " ", ""); | 284 #elif defined(OS_MACOSX) |
| 264 // And finally, lower-case it. | 285 std::string original_keybinding = key_binding_mac; |
| 265 key_binding = StringToLowerASCII(key_binding); | 286 std::string key_binding = StringToLowerASCII(key_binding_mac); |
| 287 std::string platformKey = keys::kSuggestedKeyMac; | |
| 288 #else | |
| 289 std::string original_keybinding = key_binding_other; | |
| 290 std::string key_binding = StringToLowerASCII(key_binding_other); | |
| 291 std::string platformKey = keys::kSuggestedKeyOther; | |
| 292 #endif | |
| 266 | 293 |
| 267 std::vector<std::string> tokens; | 294 std::vector<std::string> tokens; |
| 268 base::SplitString(key_binding, '+', &tokens); | 295 base::SplitString(key_binding, '+', &tokens); |
| 269 if (tokens.size() < 2 || tokens.size() > 3) { | 296 if (tokens.size() < 2 || tokens.size() > 3) { |
| 270 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( | 297 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( |
| 271 errors::kInvalidKeyBinding, | 298 errors::kInvalidKeyBinding, |
| 272 base::IntToString(index), | 299 base::IntToString(index), |
| 300 platformKey, | |
| 273 original_keybinding); | 301 original_keybinding); |
| 274 return false; | 302 return false; |
| 275 } | 303 } |
| 276 | 304 |
| 277 // Now, parse it into an accelerator. | 305 // Now, parse it into an accelerator. |
| 278 bool ctrl = false; | 306 bool ctrl = false; |
| 279 bool alt = false; | 307 bool alt = false; |
| 280 bool shift = false; | 308 bool shift = false; |
| 281 ui::KeyboardCode key = ui::VKEY_UNKNOWN; | 309 ui::KeyboardCode key = ui::VKEY_UNKNOWN; |
| 282 for (size_t i = 0; i < tokens.size(); i++) { | 310 for (size_t i = 0; i < tokens.size(); i++) { |
| 283 if (tokens[i] == "ctrl") { | 311 if (tokens[i] == "ctrl") { |
| 284 ctrl = true; | 312 ctrl = true; |
| 285 } else if (tokens[i] == "alt") { | 313 } else if (tokens[i] == "alt") { |
| 286 alt = true; | 314 alt = true; |
| 287 } else if (tokens[i] == "shift") { | 315 } else if (tokens[i] == "shift") { |
| 288 shift = true; | 316 shift = true; |
| 289 } else if (tokens[i].size() == 1 && | 317 } else if (tokens[i].size() == 1 && |
| 290 tokens[i][0] >= 'a' && tokens[i][0] <= 'z') { | 318 tokens[i][0] >= 'a' && tokens[i][0] <= 'z') { |
| 291 if (key != ui::VKEY_UNKNOWN) { | 319 if (key != ui::VKEY_UNKNOWN) { |
| 292 // Multiple key assignments. | 320 // Multiple key assignments. |
| 293 key = ui::VKEY_UNKNOWN; | 321 key = ui::VKEY_UNKNOWN; |
| 294 break; | 322 break; |
| 295 } | 323 } |
| 296 | 324 |
| 297 key = static_cast<ui::KeyboardCode>(ui::VKEY_A + (tokens[i][0] - 'a')); | 325 key = static_cast<ui::KeyboardCode>(ui::VKEY_A + (tokens[i][0] - 'a')); |
| 298 } else { | 326 } else { |
| 299 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( | 327 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( |
| 300 errors::kInvalidKeyBinding, | 328 errors::kInvalidKeyBinding, |
| 301 base::IntToString(index), | 329 base::IntToString(index), |
| 330 platformKey, | |
| 302 original_keybinding); | 331 original_keybinding); |
| 303 return false; | 332 return false; |
| 304 } | 333 } |
| 305 } | 334 } |
| 306 | 335 |
| 307 // We support Ctrl+foo, Alt+foo, Ctrl+Shift+foo, Alt+Shift+foo, but not | 336 // We support Ctrl+foo, Alt+foo, Ctrl+Shift+foo, Alt+Shift+foo, but not |
| 308 // Ctrl+Alt+foo. For a more detailed reason why we don't support Ctrl+Alt+foo: | 337 // Ctrl+Alt+foo. For a more detailed reason why we don't support Ctrl+Alt+foo: |
| 309 // http://blogs.msdn.com/b/oldnewthing/archive/2004/03/29/101121.aspx. | 338 // http://blogs.msdn.com/b/oldnewthing/archive/2004/03/29/101121.aspx. |
| 310 if (key == ui::VKEY_UNKNOWN || (ctrl && alt)) { | 339 if (key == ui::VKEY_UNKNOWN || (ctrl && alt)) { |
| 311 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( | 340 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( |
| 312 errors::kInvalidKeyBinding, | 341 errors::kInvalidKeyBinding, |
| 313 base::IntToString(index), | 342 base::IntToString(index), |
| 343 platformKey, | |
| 314 original_keybinding); | 344 original_keybinding); |
| 315 return false; | 345 return false; |
| 316 } | 346 } |
| 317 | 347 |
| 318 accelerator_ = ui::Accelerator(key, shift, ctrl, alt); | 348 accelerator_ = ui::Accelerator(key, shift, ctrl, alt); |
| 319 | 349 |
| 320 if (command_name != | 350 if (command_name != |
| 321 extension_manifest_values::kPageActionKeybindingEvent && | 351 extension_manifest_values::kPageActionKeybindingEvent && |
| 322 command_name != | 352 command_name != |
| 323 extension_manifest_values::kBrowserActionKeybindingEvent) { | 353 extension_manifest_values::kBrowserActionKeybindingEvent) { |
| (...skipping 3132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3456 // their positions should sync. | 3486 // their positions should sync. |
| 3457 return location() == Extension::INTERNAL || | 3487 return location() == Extension::INTERNAL || |
| 3458 ShouldDisplayInLauncher(); | 3488 ShouldDisplayInLauncher(); |
| 3459 } | 3489 } |
| 3460 | 3490 |
| 3461 bool Extension::ShouldDisplayInLauncher() const { | 3491 bool Extension::ShouldDisplayInLauncher() const { |
| 3462 // All apps should be displayed on the NTP except for the Cloud Print App. | 3492 // All apps should be displayed on the NTP except for the Cloud Print App. |
| 3463 return is_app() && id() != extension_misc::kCloudPrintAppId; | 3493 return is_app() && id() != extension_misc::kCloudPrintAppId; |
| 3464 } | 3494 } |
| 3465 | 3495 |
| 3496 void Extension::GetCommandByType( | |
| 3497 Extension::ExtensionKeybinding::Type type, | |
| 3498 std::vector<Extension::ExtensionKeybinding>* keybindings) const { | |
| 3499 keybindings->clear(); | |
| 3500 if (!commands_.empty()) { | |
| 3501 for (size_t i = 0; i < commands_.size(); ++i) { | |
| 3502 switch (type) { | |
| 3503 case Extension::ExtensionKeybinding::BROWSER_ACTION: | |
| 3504 if (commands_[i].command_name() == | |
| 3505 extension_manifest_values::kBrowserActionKeybindingEvent) { | |
| 3506 keybindings->push_back(commands_[i]); | |
| 3507 return; | |
| 3508 } | |
| 3509 break; | |
| 3510 case Extension::ExtensionKeybinding::PAGE_ACTION: | |
| 3511 if (commands_[i].command_name() == | |
| 3512 extension_manifest_values::kPageActionKeybindingEvent) { | |
| 3513 keybindings->push_back(commands_[i]); | |
| 3514 return; | |
| 3515 } | |
| 3516 break; | |
| 3517 case Extension::ExtensionKeybinding::COMMANDS: | |
| 3518 if (commands_[i].command_name() != | |
| 3519 extension_manifest_values::kBrowserActionKeybindingEvent && | |
| 3520 commands_[i].command_name() != | |
| 3521 extension_manifest_values::kPageActionKeybindingEvent) { | |
| 3522 keybindings->push_back(commands_[i]); | |
| 3523 } | |
| 3524 break; | |
| 3525 } | |
| 3526 } | |
| 3527 } | |
| 3528 } | |
| 3529 | |
| 3466 ExtensionInfo::ExtensionInfo(const DictionaryValue* manifest, | 3530 ExtensionInfo::ExtensionInfo(const DictionaryValue* manifest, |
| 3467 const std::string& id, | 3531 const std::string& id, |
| 3468 const FilePath& path, | 3532 const FilePath& path, |
| 3469 Extension::Location location) | 3533 Extension::Location location) |
| 3470 : extension_id(id), | 3534 : extension_id(id), |
| 3471 extension_path(path), | 3535 extension_path(path), |
| 3472 extension_location(location) { | 3536 extension_location(location) { |
| 3473 if (manifest) | 3537 if (manifest) |
| 3474 extension_manifest.reset(manifest->DeepCopy()); | 3538 extension_manifest.reset(manifest->DeepCopy()); |
| 3475 } | 3539 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 3498 already_disabled(false), | 3562 already_disabled(false), |
| 3499 extension(extension) {} | 3563 extension(extension) {} |
| 3500 | 3564 |
| 3501 UpdatedExtensionPermissionsInfo::UpdatedExtensionPermissionsInfo( | 3565 UpdatedExtensionPermissionsInfo::UpdatedExtensionPermissionsInfo( |
| 3502 const Extension* extension, | 3566 const Extension* extension, |
| 3503 const ExtensionPermissionSet* permissions, | 3567 const ExtensionPermissionSet* permissions, |
| 3504 Reason reason) | 3568 Reason reason) |
| 3505 : reason(reason), | 3569 : reason(reason), |
| 3506 extension(extension), | 3570 extension(extension), |
| 3507 permissions(permissions) {} | 3571 permissions(permissions) {} |
| OLD | NEW |