Roll breakpad to r1408 to bring in DWARF 4 dump_syms fix for PNaCl
[chromium-blink-merge.git] / content / browser / cocoa / system_hotkey_map.mm
blobda04fa8b8442c66fa5c47410c25adeb2190fd3df
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
3 // found in the LICENSE file.
5 #import "content/browser/cocoa/system_hotkey_map.h"
7 #pragma mark - NSDictionary Helper Functions
9 namespace {
11 // All 4 following functions return nil if the object doesn't exist, or isn't of
12 // the right class.
13 id ObjectForKey(NSDictionary* dict, NSString* key, Class aClass) {
14   id object = [dict objectForKey:key];
15   if (![object isKindOfClass:aClass])
16     return nil;
17   return object;
20 NSDictionary* DictionaryForKey(NSDictionary* dict, NSString* key) {
21   return ObjectForKey(dict, key, [NSDictionary class]);
24 NSArray* ArrayForKey(NSDictionary* dict, NSString* key) {
25   return ObjectForKey(dict, key, [NSArray class]);
28 NSNumber* NumberForKey(NSDictionary* dict, NSString* key) {
29   return ObjectForKey(dict, key, [NSNumber class]);
32 NSString* StringForKey(NSDictionary* dict, NSString* key) {
33   return ObjectForKey(dict, key, [NSString class]);
36 }  // namespace
38 #pragma mark - SystemHotkey
40 namespace content {
42 struct SystemHotkey {
43   unsigned short key_code;
44   NSUInteger modifiers;
47 #pragma mark - SystemHotkeyMap
49 SystemHotkeyMap::SystemHotkeyMap() {
51 SystemHotkeyMap::~SystemHotkeyMap() {
54 NSDictionary* SystemHotkeyMap::DictionaryFromData(NSData* data) {
55   if (!data)
56     return nil;
58   NSError* error = nil;
59   NSPropertyListFormat format;
60   NSDictionary* dictionary =
61       [NSPropertyListSerialization propertyListWithData:data
62                                                 options:0
63                                                  format:&format
64                                                   error:&error];
66   if (![dictionary isKindOfClass:[NSDictionary class]])
67     return nil;
69   return dictionary;
72 bool SystemHotkeyMap::ParseDictionary(NSDictionary* dictionary) {
73   system_hotkeys_.clear();
75   if (!dictionary)
76     return false;
78   NSDictionary* hotkey_dictionaries =
79       DictionaryForKey(dictionary, @"AppleSymbolicHotKeys");
80   if (!hotkey_dictionaries)
81     return false;
83   for (NSString* hotkey_system_effect in [hotkey_dictionaries allKeys]) {
84     if (![hotkey_system_effect isKindOfClass:[NSString class]])
85       continue;
87     NSDictionary* hotkey_dictionary =
88         [hotkey_dictionaries objectForKey:hotkey_system_effect];
89     if (![hotkey_dictionary isKindOfClass:[NSDictionary class]])
90       continue;
92     NSNumber* enabled = NumberForKey(hotkey_dictionary, @"enabled");
93     if (!enabled || enabled.boolValue == NO)
94       continue;
96     NSDictionary* value = DictionaryForKey(hotkey_dictionary, @"value");
97     if (!value)
98       continue;
100     NSString* type = StringForKey(value, @"type");
101     if (!type || ![type isEqualToString:@"standard"])
102       continue;
104     NSArray* parameters = ArrayForKey(value, @"parameters");
105     if (!parameters || [parameters count] != 3)
106       continue;
108     NSNumber* key_code = [parameters objectAtIndex:1];
109     if (![key_code isKindOfClass:[NSNumber class]])
110       continue;
112     NSNumber* modifiers = [parameters objectAtIndex:2];
113     if (![modifiers isKindOfClass:[NSNumber class]])
114       continue;
116     ReserveHotkey(key_code.unsignedShortValue,
117                   modifiers.unsignedIntegerValue,
118                   hotkey_system_effect);
119   }
121   return true;
124 bool SystemHotkeyMap::IsEventReserved(NSEvent* event) const {
125   return IsHotkeyReserved(event.keyCode, event.modifierFlags);
128 bool SystemHotkeyMap::IsHotkeyReserved(unsigned short key_code,
129                                        NSUInteger modifiers) const {
130   modifiers &= NSDeviceIndependentModifierFlagsMask;
131   std::vector<SystemHotkey>::const_iterator it;
132   for (it = system_hotkeys_.begin(); it != system_hotkeys_.end(); ++it) {
133     if (it->key_code == key_code && it->modifiers == modifiers)
134       return true;
135   }
136   return false;
139 void SystemHotkeyMap::ReserveHotkey(unsigned short key_code,
140                                     NSUInteger modifiers,
141                                     NSString* system_effect) {
142   ReserveHotkey(key_code, modifiers);
144   // If a hotkey exists for toggling through the windows of an application, then
145   // adding shift to that hotkey toggles through the windows backwards.
146   if ([system_effect isEqualToString:@"27"])
147     ReserveHotkey(key_code, modifiers | NSShiftKeyMask);
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;
159   SystemHotkey hotkey;
160   hotkey.key_code = key_code;
161   hotkey.modifiers = modifiers;
162   system_hotkeys_.push_back(hotkey);
165 }  // namespace content