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/extensions/key_identifier_conversion_views.h" | 5 #include "ui/base/events/key_identifier_conversion.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | |
9 #include <utility> | 8 #include <utility> |
10 | 9 |
11 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
12 #include "base/hash_tables.h" | 11 #include "base/hash_tables.h" |
13 #include "base/logging.h" | |
14 #include "content/public/browser/browser_thread.h" | |
15 #include "ui/base/events/event.h" | 12 #include "ui/base/events/event.h" |
16 #include "ui/base/keycodes/keyboard_codes.h" | 13 #include "ui/base/keycodes/keyboard_codes.h" |
17 | 14 |
18 using content::BrowserThread; | |
19 | |
20 namespace { | 15 namespace { |
21 | 16 |
22 static const int kNumIdentifierTypes = 3; | 17 const int kNumIdentifierTypes = 3; |
23 | 18 |
24 typedef struct KeyIdentifier { | 19 typedef struct KeyIdentifier { |
25 // In order: key identifier, character and unicode codepoint. They are | 20 // In order: key identifier, character and unicode codepoint. They are |
26 // searched in that order as well. | 21 // searched in that order as well. |
27 // These are all placed into a single array as they are treated uniformly and | 22 // These are all placed into a single array as they are treated uniformly and |
28 // we never refer to a specific type of identifier. This reduces code | 23 // we never refer to a specific type of identifier. This reduces code |
29 // duplication below. | 24 // duplication below. |
30 const char* identifiers[kNumIdentifierTypes]; | 25 const char* identifiers[kNumIdentifierTypes]; |
31 const ui::KeyboardCode key_code; | 26 const ui::KeyboardCode key_code; |
32 const int event_flags; | 27 const int event_flags; |
33 } KeyIdentifier; | 28 } KeyIdentifier; |
34 | 29 |
35 // Taken from Section 6.3.3 here: | 30 // Taken from Section 6.3.3 here: |
36 // http://www.w3.org/TR/DOM-Level-3-Events/#keyset-keyidentifiers | 31 // http://www.w3.org/TR/DOM-Level-3-Events/#keyset-keyidentifiers |
37 // TODO(bryeung): keycodes could be wrong: I took the keydown code only | 32 // WARNING: keycodes could be wrong, as they are based soley on keydown |
38 static const KeyIdentifier kKeyIdentifiers[] = { | 33 const KeyIdentifier kKeyIdentifiers[] = { |
39 { {"Accept", "", ""}, ui::VKEY_ACCEPT, 0 }, | 34 { {"Accept", "", ""}, ui::VKEY_ACCEPT, 0 }, |
40 { {"Add", "", ""}, ui::VKEY_ADD, 0 }, | 35 { {"Add", "", ""}, ui::VKEY_ADD, 0 }, |
41 { {"Again", "", ""}, ui::VKEY_UNKNOWN, 0 }, | 36 { {"Again", "", ""}, ui::VKEY_UNKNOWN, 0 }, |
42 { {"AllCandidates", "", ""}, ui::VKEY_UNKNOWN, 0 }, | 37 { {"AllCandidates", "", ""}, ui::VKEY_UNKNOWN, 0 }, |
43 { {"Alphanumeric", "", ""}, ui::VKEY_UNKNOWN, 0 }, | 38 { {"Alphanumeric", "", ""}, ui::VKEY_UNKNOWN, 0 }, |
44 { {"Alt", "", ""}, ui::VKEY_MENU, 0 }, | 39 { {"Alt", "", ""}, ui::VKEY_MENU, 0 }, |
45 { {"AltGraph", "", ""}, ui::VKEY_UNKNOWN, 0 }, | 40 { {"AltGraph", "", ""}, ui::VKEY_UNKNOWN, 0 }, |
46 { {"Apps", "", ""}, ui::VKEY_APPS, 0 }, | 41 { {"Apps", "", ""}, ui::VKEY_APPS, 0 }, |
47 { {"Attn", "", ""}, ui::VKEY_ATTN, 0 }, | 42 { {"Attn", "", ""}, ui::VKEY_ATTN, 0 }, |
48 { {"BrowserBack", "", ""}, ui::VKEY_BROWSER_BACK, 0 }, | 43 { {"BrowserBack", "", ""}, ui::VKEY_BROWSER_BACK, 0 }, |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 { {"DeadDoubleacute", "", "U+030B"}, ui::VKEY_UNKNOWN, 0 }, | 274 { {"DeadDoubleacute", "", "U+030B"}, ui::VKEY_UNKNOWN, 0 }, |
280 { {"DeadCaron", "", "U+030C"}, ui::VKEY_UNKNOWN, 0 }, | 275 { {"DeadCaron", "", "U+030C"}, ui::VKEY_UNKNOWN, 0 }, |
281 { {"DeadCedilla", "", "U+0327"}, ui::VKEY_UNKNOWN, 0 }, | 276 { {"DeadCedilla", "", "U+0327"}, ui::VKEY_UNKNOWN, 0 }, |
282 { {"DeadOgonek", "", "U+0328"}, ui::VKEY_UNKNOWN, 0 }, | 277 { {"DeadOgonek", "", "U+0328"}, ui::VKEY_UNKNOWN, 0 }, |
283 { {"DeadIota", "", "U+0345"}, ui::VKEY_UNKNOWN, 0 }, | 278 { {"DeadIota", "", "U+0345"}, ui::VKEY_UNKNOWN, 0 }, |
284 { {"Euro", "", "U+20AC"}, ui::VKEY_UNKNOWN, 0 }, | 279 { {"Euro", "", "U+20AC"}, ui::VKEY_UNKNOWN, 0 }, |
285 { {"DeadVoicedSound", "", "U+3099"}, ui::VKEY_UNKNOWN, 0 }, | 280 { {"DeadVoicedSound", "", "U+3099"}, ui::VKEY_UNKNOWN, 0 }, |
286 { {"DeadSemivoicedSound", "", "U+309A"}, ui::VKEY_UNKNOWN, 0 } | 281 { {"DeadSemivoicedSound", "", "U+309A"}, ui::VKEY_UNKNOWN, 0 } |
287 }; | 282 }; |
288 | 283 |
289 static const int kNumKeyIdentifiers = arraysize(kKeyIdentifiers); | 284 const int kNumKeyIdentifiers = arraysize(kKeyIdentifiers); |
290 | 285 |
291 typedef base::hash_map<std::string, const ui::KeyEvent*> IdentifierMap; | 286 typedef base::hash_map<std::string, const ui::KeyEvent*> IdentifierMap; |
292 typedef std::pair<std::string, const ui::KeyEvent*> IdentifierPair; | 287 typedef std::pair<std::string, const ui::KeyEvent*> IdentifierPair; |
293 static IdentifierMap* identifierMaps[kNumIdentifierTypes] = { NULL }; | 288 IdentifierMap* identifierMaps[kNumIdentifierTypes] = { NULL }; |
294 | 289 |
295 static ui::KeyEvent* kUnknownKeyEvent = NULL; | 290 ui::KeyEvent* kUnknownKeyEvent = NULL; |
296 | 291 |
297 static void InitializeMaps() { | 292 void InitializeMaps() { |
298 if (identifierMaps[0]) | 293 if (identifierMaps[0]) |
299 return; | 294 return; |
300 | 295 |
301 kUnknownKeyEvent = new ui::KeyEvent(ui::ET_KEY_PRESSED, | 296 kUnknownKeyEvent = new ui::KeyEvent(ui::ET_KEY_PRESSED, |
302 ui::VKEY_UNKNOWN, | 297 ui::VKEY_UNKNOWN, |
303 0, | 298 0, |
304 false); | 299 false); |
305 | 300 |
306 for (int i = 0; i < kNumIdentifierTypes; ++i) | 301 for (int i = 0; i < kNumIdentifierTypes; ++i) |
307 identifierMaps[i] = new IdentifierMap; | 302 identifierMaps[i] = new IdentifierMap; |
(...skipping 11 matching lines...) Expand all Loading... |
319 identifierMaps[j]->insert( | 314 identifierMaps[j]->insert( |
320 IdentifierPair(key.identifiers[j], event)); | 315 IdentifierPair(key.identifiers[j], event)); |
321 DCHECK(result.second); | 316 DCHECK(result.second); |
322 } | 317 } |
323 } | 318 } |
324 } | 319 } |
325 } | 320 } |
326 | 321 |
327 } // namespace | 322 } // namespace |
328 | 323 |
| 324 namespace ui { |
329 | 325 |
330 const ui::KeyEvent& KeyEventFromKeyIdentifier( | 326 const ui::KeyEvent& KeyEventFromKeyIdentifier( |
331 const std::string& key_identifier) { | 327 const std::string& key_identifier) { |
332 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 328 // This lazily initializes lookup tables for the conversion. To prevent |
| 329 // races, it should only ever be called from a single thread. |
333 InitializeMaps(); | 330 InitializeMaps(); |
334 | 331 |
335 for (int i = 0; i < kNumIdentifierTypes; ++i) { | 332 for (int i = 0; i < kNumIdentifierTypes; ++i) { |
336 const IdentifierMap& map = *identifierMaps[i]; | 333 const IdentifierMap& map = *identifierMaps[i]; |
337 | 334 |
338 IdentifierMap::const_iterator iter = map.find(key_identifier); | 335 IdentifierMap::const_iterator iter = map.find(key_identifier); |
339 if (iter != map.end()) | 336 if (iter != map.end()) |
340 return *iter->second; | 337 return *iter->second; |
341 } | 338 } |
342 | 339 |
343 return *kUnknownKeyEvent; | 340 return *kUnknownKeyEvent; |
344 } | 341 } |
| 342 |
| 343 } // namespace ui |
OLD | NEW |