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/browser/ui/cocoa/extensions/extension_keybinding_registry_cocoa
.h" | 5 #include "chrome/browser/ui/cocoa/extensions/extension_keybinding_registry_cocoa
.h" |
6 | 6 |
7 #include "chrome/browser/extensions/api/commands/command_service.h" | 7 #include "chrome/browser/extensions/api/commands/command_service.h" |
8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/ui/cocoa/accelerators_cocoa.h" |
| 11 #import "chrome/browser/ui/cocoa/browser_window_utils.h" |
10 #include "chrome/common/extensions/extension.h" | 12 #include "chrome/common/extensions/extension.h" |
11 #include "chrome/common/chrome_notification_types.h" | 13 #include "chrome/common/chrome_notification_types.h" |
12 #include "chrome/common/extensions/extension_manifest_constants.h" | 14 #include "chrome/common/extensions/extension_manifest_constants.h" |
13 #include "content/public/browser/native_web_keyboard_event.h" | 15 #include "content/public/browser/native_web_keyboard_event.h" |
14 #include "content/public/browser/notification_service.h" | 16 #include "content/public/browser/notification_service.h" |
| 17 #include "ui/base/accelerators/platform_accelerator_cocoa.h" |
| 18 #include "ui/base/keycodes/keyboard_code_conversion_mac.h" |
15 | 19 |
16 namespace values = extension_manifest_values; | 20 namespace values = extension_manifest_values; |
17 | 21 |
18 // static | 22 // static |
19 void extensions::ExtensionKeybindingRegistry::SetShortcutHandlingSuspended( | 23 void extensions::ExtensionKeybindingRegistry::SetShortcutHandlingSuspended( |
20 bool suspended) { | 24 bool suspended) { |
21 ExtensionKeybindingRegistryCocoa::set_shortcut_handling_suspended(suspended); | 25 ExtensionKeybindingRegistryCocoa::set_shortcut_handling_suspended(suspended); |
22 } | 26 } |
23 | 27 |
| 28 // static |
| 29 bool extensions::ExtensionKeybindingRegistry::IsChromeAccelerator( |
| 30 const ui::Accelerator& accelerator, Profile* profile) { |
| 31 // The |accelerator| passed in contains a Windows key code but no platform |
| 32 // accelerator info. The Accelerator list is the opposite: It has accelerators |
| 33 // that have key_code() == VKEY_UNKNOWN but they contain a platform |
| 34 // accelerator. We find common ground by converting the passed in Windows key |
| 35 // code to a character and use that when comparing against the Accelerator |
| 36 // list. |
| 37 unichar character; |
| 38 unichar characterIgnoringModifiers; |
| 39 ui::MacKeyCodeForWindowsKeyCode(accelerator.key_code(), |
| 40 0, |
| 41 &character, |
| 42 &characterIgnoringModifiers); |
| 43 NSString* characters = |
| 44 [[[NSString alloc] initWithCharacters:&character length:1] autorelease]; |
| 45 |
| 46 NSUInteger modifiers = |
| 47 (accelerator.IsCtrlDown() ? NSControlKeyMask : 0) | |
| 48 (accelerator.IsCmdDown() ? NSCommandKeyMask : 0) | |
| 49 (accelerator.IsAltDown() ? NSAlternateKeyMask : 0) | |
| 50 (accelerator.IsShiftDown() ? NSShiftKeyMask : 0); |
| 51 |
| 52 NSEvent* event = [NSEvent keyEventWithType:NSKeyDown |
| 53 location:NSZeroPoint |
| 54 modifierFlags:modifiers |
| 55 timestamp:0 |
| 56 windowNumber:0 |
| 57 context:nil |
| 58 characters:characters |
| 59 charactersIgnoringModifiers:characters |
| 60 isARepeat:NO |
| 61 keyCode:accelerator.key_code()]; |
| 62 |
| 63 content::NativeWebKeyboardEvent keyboard_event(event); |
| 64 int id = [BrowserWindowUtils getCommandId:keyboard_event]; |
| 65 if (id != -1) |
| 66 return true; |
| 67 |
| 68 AcceleratorsCocoa* accelerators = AcceleratorsCocoa::GetInstance(); |
| 69 for (AcceleratorsCocoa::const_iterator iter = accelerators->begin(); |
| 70 iter != accelerators->end(); ++iter) { |
| 71 const ui::PlatformAcceleratorCocoa* platform_accelerator = |
| 72 static_cast<const ui::PlatformAcceleratorCocoa*>( |
| 73 iter->second.platform_accelerator()); |
| 74 if ([characters isEqualToString:platform_accelerator->characters()] && |
| 75 modifiers == platform_accelerator->modifier_mask()) |
| 76 return true; |
| 77 } |
| 78 |
| 79 return false; |
| 80 } |
| 81 |
24 bool ExtensionKeybindingRegistryCocoa::shortcut_handling_suspended_ = false; | 82 bool ExtensionKeybindingRegistryCocoa::shortcut_handling_suspended_ = false; |
25 | 83 |
26 ExtensionKeybindingRegistryCocoa::ExtensionKeybindingRegistryCocoa( | 84 ExtensionKeybindingRegistryCocoa::ExtensionKeybindingRegistryCocoa( |
27 Profile* profile, | 85 Profile* profile, |
28 gfx::NativeWindow window, | 86 gfx::NativeWindow window, |
29 ExtensionFilter extension_filter, | 87 ExtensionFilter extension_filter, |
30 Delegate* delegate) | 88 Delegate* delegate) |
31 : ExtensionKeybindingRegistry(profile, extension_filter, delegate), | 89 : ExtensionKeybindingRegistry(profile, extension_filter, delegate), |
32 profile_(profile), | 90 profile_(profile), |
33 window_(window) { | 91 window_(window) { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 const extensions::Extension* extension, | 197 const extensions::Extension* extension, |
140 const std::string& command_name) { | 198 const std::string& command_name) { |
141 EventTargets::iterator iter = event_targets_.begin(); | 199 EventTargets::iterator iter = event_targets_.begin(); |
142 while (iter != event_targets_.end()) { | 200 while (iter != event_targets_.end()) { |
143 EventTargets::iterator old = iter++; | 201 EventTargets::iterator old = iter++; |
144 if (old->second.first == extension->id() && | 202 if (old->second.first == extension->id() && |
145 (command_name.empty() || (old->second.second == command_name))) | 203 (command_name.empty() || (old->second.second == command_name))) |
146 event_targets_.erase(old); | 204 event_targets_.erase(old); |
147 } | 205 } |
148 } | 206 } |
OLD | NEW |