Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: content/browser/cocoa/system_hotkey_map.mm

Issue 408973002: mac: Load the system hotkeys after launch. (reland) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add more unit tests. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #import "chrome/browser/ui/cocoa/system_hotkey_map.h" 5 #import "content/browser/cocoa/system_hotkey_map.h"
6
7 #import <Cocoa/Cocoa.h>
8 6
9 #pragma mark - NSDictionary Helper Functions 7 #pragma mark - NSDictionary Helper Functions
10 8
11 namespace { 9 namespace {
12 10
13 // All 4 following functions return nil if the object doesn't exist, or isn't of 11 // All 4 following functions return nil if the object doesn't exist, or isn't of
14 // the right class. 12 // the right class.
15 id ObjectForKey(NSDictionary* dict, NSString* key, Class aClass) { 13 id ObjectForKey(NSDictionary* dict, NSString* key, Class aClass) {
16 id object = [dict objectForKey:key]; 14 id object = [dict objectForKey:key];
17 if (![object isKindOfClass:aClass]) 15 if (![object isKindOfClass:aClass])
18 return nil; 16 return nil;
19 return object; 17 return object;
20 } 18 }
21 19
22 NSDictionary* DictionaryForKey(NSDictionary* dict, NSString* key) { 20 NSDictionary* DictionaryForKey(NSDictionary* dict, NSString* key) {
23 return ObjectForKey(dict, key, [NSDictionary class]); 21 return ObjectForKey(dict, key, [NSDictionary class]);
24 } 22 }
25 23
26 NSArray* ArrayForKey(NSDictionary* dict, NSString* key) { 24 NSArray* ArrayForKey(NSDictionary* dict, NSString* key) {
27 return ObjectForKey(dict, key, [NSArray class]); 25 return ObjectForKey(dict, key, [NSArray class]);
28 } 26 }
29 27
30 NSNumber* NumberForKey(NSDictionary* dict, NSString* key) { 28 NSNumber* NumberForKey(NSDictionary* dict, NSString* key) {
31 return ObjectForKey(dict, key, [NSNumber class]); 29 return ObjectForKey(dict, key, [NSNumber class]);
32 } 30 }
33 31
32 NSString* StringForKey(NSDictionary* dict, NSString* key) {
33 return ObjectForKey(dict, key, [NSString class]);
34 }
35
34 } // namespace 36 } // namespace
35 37
36 #pragma mark - SystemHotkey 38 #pragma mark - SystemHotkey
37 39
40 namespace content {
41
38 struct SystemHotkey { 42 struct SystemHotkey {
39 int key_code; 43 unsigned short key_code;
40 int modifiers; 44 NSUInteger modifiers;
41 }; 45 };
42 46
43 #pragma mark - SystemHotkeyMap 47 #pragma mark - SystemHotkeyMap
44 48
45 SystemHotkeyMap::SystemHotkeyMap() { 49 SystemHotkeyMap::SystemHotkeyMap() {
46 } 50 }
47 SystemHotkeyMap::~SystemHotkeyMap() { 51 SystemHotkeyMap::~SystemHotkeyMap() {
48 } 52 }
49 53
50 bool SystemHotkeyMap::ParseData(NSData* data) { 54 NSDictionary* SystemHotkeyMap::DictionaryFromData(NSData* data) {
51 system_hotkeys_.clear(); 55 if (!data)
56 return nil;
52 57
53 NSError* error = nil; 58 NSError* error = nil;
54 NSPropertyListFormat format; 59 NSPropertyListFormat format;
55 NSDictionary* dictionary = 60 NSDictionary* dictionary =
56 [NSPropertyListSerialization propertyListWithData:data 61 [NSPropertyListSerialization propertyListWithData:data
57 options:0 62 options:0
58 format:&format 63 format:&format
59 error:&error]; 64 error:&error];
60 if (error)
61 return false;
62 65
63 if (![dictionary isKindOfClass:[NSDictionary class]]) 66 if (![dictionary isKindOfClass:[NSDictionary class]])
67 return nil;
68
69 return dictionary;
70 }
71
72 bool SystemHotkeyMap::ParseDictionary(NSDictionary* dictionary) {
73 system_hotkeys_.clear();
74
75 if (!dictionary)
64 return false; 76 return false;
65 77
66 NSDictionary* hotkey_dictionaries = 78 NSDictionary* hotkey_dictionaries =
67 DictionaryForKey(dictionary, @"AppleSymbolicHotKeys"); 79 DictionaryForKey(dictionary, @"AppleSymbolicHotKeys");
68 if (!hotkey_dictionaries) 80 if (!hotkey_dictionaries)
69 return false; 81 return false;
70 82
71 for (NSString* hotkey_system_effect in [hotkey_dictionaries allKeys]) { 83 for (NSString* hotkey_system_effect in [hotkey_dictionaries allKeys]) {
72 if (![hotkey_system_effect isKindOfClass:[NSString class]]) 84 if (![hotkey_system_effect isKindOfClass:[NSString class]])
73 continue; 85 continue;
74 86
75 NSDictionary* hotkey_dictionary = 87 NSDictionary* hotkey_dictionary =
76 [hotkey_dictionaries objectForKey:hotkey_system_effect]; 88 [hotkey_dictionaries objectForKey:hotkey_system_effect];
77 if (![hotkey_dictionary isKindOfClass:[NSDictionary class]]) 89 if (![hotkey_dictionary isKindOfClass:[NSDictionary class]])
78 continue; 90 continue;
79 91
80 NSNumber* enabled = NumberForKey(hotkey_dictionary, @"enabled"); 92 NSNumber* enabled = NumberForKey(hotkey_dictionary, @"enabled");
81 if (!enabled || enabled.boolValue == NO) 93 if (!enabled || enabled.boolValue == NO)
82 continue; 94 continue;
83 95
84 NSDictionary* value = DictionaryForKey(hotkey_dictionary, @"value"); 96 NSDictionary* value = DictionaryForKey(hotkey_dictionary, @"value");
85 if (!value) 97 if (!value)
86 continue; 98 continue;
87 99
100 NSString* type = StringForKey(value, @"type");
101 if (!type || ![type isEqualToString:@"standard"])
102 continue;
103
88 NSArray* parameters = ArrayForKey(value, @"parameters"); 104 NSArray* parameters = ArrayForKey(value, @"parameters");
89 if (!parameters || [parameters count] != 3) 105 if (!parameters || [parameters count] != 3)
90 continue; 106 continue;
91 107
92 NSNumber* key_code = [parameters objectAtIndex:1]; 108 NSNumber* key_code = [parameters objectAtIndex:1];
93 if (![key_code isKindOfClass:[NSNumber class]]) 109 if (![key_code isKindOfClass:[NSNumber class]])
94 continue; 110 continue;
95 111
96 NSNumber* modifiers = [parameters objectAtIndex:2]; 112 NSNumber* modifiers = [parameters objectAtIndex:2];
97 if (![modifiers isKindOfClass:[NSNumber class]]) 113 if (![modifiers isKindOfClass:[NSNumber class]])
98 continue; 114 continue;
99 115
100 ReserveHotkey(key_code.intValue, modifiers.intValue, hotkey_system_effect); 116 ReserveHotkey(key_code.unsignedShortValue,
117 modifiers.unsignedIntegerValue,
118 hotkey_system_effect);
101 } 119 }
102 120
103 return true; 121 return true;
104 } 122 }
105 123
106 bool SystemHotkeyMap::IsHotkeyReserved(int key_code, int modifiers) { 124 bool SystemHotkeyMap::IsEventReserved(NSEvent* event) const {
107 std::vector<SystemHotkey>::iterator it; 125 return IsHotkeyReserved(event.keyCode, event.modifierFlags);
126 }
127
128 bool SystemHotkeyMap::IsHotkeyReserved(unsigned short key_code,
129 NSUInteger modifiers) const {
130 modifiers &= NSDeviceIndependentModifierFlagsMask;
131 std::vector<SystemHotkey>::const_iterator it;
108 for (it = system_hotkeys_.begin(); it != system_hotkeys_.end(); ++it) { 132 for (it = system_hotkeys_.begin(); it != system_hotkeys_.end(); ++it) {
109 if (it->key_code == key_code && it->modifiers == modifiers) 133 if (it->key_code == key_code && it->modifiers == modifiers)
110 return true; 134 return true;
111 } 135 }
112 return false; 136 return false;
113 } 137 }
114 138
115 void SystemHotkeyMap::ReserveHotkey(int key_code, 139 void SystemHotkeyMap::ReserveHotkey(unsigned short key_code,
116 int modifiers, 140 NSUInteger modifiers,
117 NSString* system_effect) { 141 NSString* system_effect) {
118 ReserveHotkey(key_code, modifiers); 142 ReserveHotkey(key_code, modifiers);
119 143
120 // If a hotkey exists for toggling through the windows of an application, then 144 // If a hotkey exists for toggling through the windows of an application, then
121 // adding shift to that hotkey toggles through the windows backwards. 145 // adding shift to that hotkey toggles through the windows backwards.
122 if ([system_effect isEqualToString:@"27"]) 146 if ([system_effect isEqualToString:@"27"])
123 ReserveHotkey(key_code, modifiers | NSShiftKeyMask); 147 ReserveHotkey(key_code, modifiers | NSShiftKeyMask);
124 } 148 }
125 149
126 void SystemHotkeyMap::ReserveHotkey(int key_code, int modifiers) { 150 void SystemHotkeyMap::ReserveHotkey(unsigned short key_code,
151 NSUInteger modifiers) {
152 // Hotkeys require at least one of control, command, or alternate keys to be
153 // down.
154 NSUInteger required_modifiers =
155 NSControlKeyMask | NSCommandKeyMask | NSAlternateKeyMask;
156 if ((modifiers & required_modifiers) == 0)
157 return;
158
127 SystemHotkey hotkey; 159 SystemHotkey hotkey;
128 hotkey.key_code = key_code; 160 hotkey.key_code = key_code;
129 hotkey.modifiers = modifiers; 161 hotkey.modifiers = modifiers;
130 system_hotkeys_.push_back(hotkey); 162 system_hotkeys_.push_back(hotkey);
131 } 163 }
164
165 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/cocoa/system_hotkey_map.h ('k') | content/browser/cocoa/system_hotkey_map_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698