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

Side by Side Diff: chrome/browser/extensions/api/input/input.cc

Issue 14161009: WebUIHandler for chrome://keyboard (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix license headers Created 7 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/extensions/image_loader.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/api/input/input.h" 5 #include "chrome/browser/extensions/api/input/input.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/lazy_instance.h" 9 #include "base/lazy_instance.h"
10 #include "base/string_util.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/values.h"
13 #include "chrome/browser/extensions/extension_function_registry.h" 11 #include "chrome/browser/extensions/extension_function_registry.h"
14 #include "chrome/browser/ui/top_level_widget.h"
15 #include "chrome/common/chrome_notification_types.h"
16 #include "content/public/browser/browser_thread.h" 12 #include "content/public/browser/browser_thread.h"
17 #include "ui/base/events/event.h" 13 #include "ui/base/events/event.h"
18 #include "ui/base/events/key_identifier_conversion.h"
19 14
20 #if defined(USE_ASH) && defined(USE_AURA) 15 #if defined(USE_ASH)
21 #include "ash/shell.h" 16 #include "ash/shell.h"
22 #include "ui/aura/root_window.h" 17 #include "ui/aura/root_window.h"
18 #include "ui/keyboard/keyboard_util.h"
23 #endif 19 #endif
24 20
21 namespace {
22
23 const char kNotYetImplementedError[] =
24 "API is not implemented on this platform.";
25
26 } // namespace
27
25 namespace extensions { 28 namespace extensions {
26 29
27 namespace { 30 bool SendKeyboardEventInputFunction::RunImpl() {
31 #if defined(USE_ASH)
32 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
28 33
29 // Keys. 34 scoped_ptr<ui::KeyEvent> event(
30 const char kType[] = "type"; 35 keyboard::KeyEventFromArgs(args_.get(), &error_));
31 const char kKeyIdentifier[] = "keyIdentifier"; 36 if (!event)
32 const char kAlt[] = "altKey"; 37 return false;
33 const char kCtrl[] = "ctrlKey";
34 const char kMeta[] = "metaKey";
35 const char kShift[] = "shiftKey";
36 const char kKeyDown[] = "keydown";
37 const char kKeyUp[] = "keyup";
38 38
39 // Errors. 39 ash::Shell::GetActiveRootWindow()->AsRootWindowHostDelegate()->OnHostKeyEvent(
40 const char kUnknownEventTypeError[] = "Unknown event type."; 40 event.get());
41 const char kUnknownOrUnsupportedKeyIdentiferError[] = "Unknown or unsupported "
42 "key identifier.";
43 const char kUnsupportedModifier[] = "Unsupported modifier.";
44 const char kNoValidRecipientError[] = "No valid recipient for event.";
45 const char kKeyEventUnprocessedError[] = "Event was not handled.";
46 41
47 ui::EventType GetTypeFromString(const std::string& type) {
48 if (type == kKeyDown) {
49 return ui::ET_KEY_PRESSED;
50 } else if (type == kKeyUp) {
51 return ui::ET_KEY_RELEASED;
52 }
53 return ui::ET_UNKNOWN;
54 }
55
56 // Converts a hex string "U+NNNN" to uint16. Returns 0 on error.
57 uint16 UnicodeIdentifierStringToInt(const std::string& key_identifier) {
58 int character = 0;
59 if ((key_identifier.length() == 6) &&
60 (key_identifier.substr(0, 2) == "U+") &&
61 (key_identifier.substr(2).find_first_not_of("0123456789abcdefABCDEF") ==
62 std::string::npos)) {
63 const bool result =
64 base::HexStringToInt(key_identifier.substr(2), &character);
65 DCHECK(result) << key_identifier;
66 }
67 return character;
68 }
69
70 } // namespace
71
72 bool SendKeyboardEventInputFunction::RunImpl() {
73 DictionaryValue* args;
74 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args));
75
76 std::string type_name;
77 EXTENSION_FUNCTION_VALIDATE(args->GetString(kType, &type_name));
78 ui::EventType type = GetTypeFromString(type_name);
79 if (type == ui::ET_UNKNOWN) {
80 error_ = kUnknownEventTypeError;
81 return false;
82 }
83
84 std::string identifier;
85 EXTENSION_FUNCTION_VALIDATE(args->GetString(kKeyIdentifier, &identifier));
86 TrimWhitespaceASCII(identifier, TRIM_ALL, &identifier);
87
88 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
89 const ui::KeyEvent& prototype_event =
90 ui::KeyEventFromKeyIdentifier(identifier);
91 uint16 character = 0;
92 if (prototype_event.key_code() == ui::VKEY_UNKNOWN) {
93 // Check if |identifier| is "U+NNNN" format.
94 character = UnicodeIdentifierStringToInt(identifier);
95 if (!character) {
96 error_ = kUnknownOrUnsupportedKeyIdentiferError;
97 return false;
98 }
99 }
100
101 bool flag = false;
102 int flags = 0;
103 if (prototype_event.key_code() != ui::VKEY_UNKNOWN)
104 flags = prototype_event.flags();
105 flags |= (args->GetBoolean(kAlt, &flag) && flag) ? ui::EF_ALT_DOWN : 0;
106 flags |= (args->GetBoolean(kCtrl, &flag) && flag) ? ui::EF_CONTROL_DOWN : 0;
107 flags |= (args->GetBoolean(kShift, &flag) && flag) ? ui::EF_SHIFT_DOWN : 0;
108 if (args->GetBoolean(kMeta, &flag) && flag) {
109 // Views does not have a Meta event flag, so return an error for now.
110 error_ = kUnsupportedModifier;
111 return false;
112 }
113
114 ui::KeyEvent event(type,
115 prototype_event.key_code(),
116 flags,
117 prototype_event.is_char());
118 if (character) {
119 event.set_character(character);
120 event.set_unmodified_character(character);
121 }
122
123 #if defined(USE_ASH) && defined(USE_AURA)
124 ash::Shell::GetActiveRootWindow()->AsRootWindowHostDelegate()->OnHostKeyEvent(
125 &event);
126 return true; 42 return true;
127 #else 43 #endif
44 error_ = kNotYetImplementedError;
128 return false; 45 return false;
129 #endif
130 } 46 }
131 47
132 InputAPI::InputAPI(Profile* profile) { 48 InputAPI::InputAPI(Profile* profile) {
133 ExtensionFunctionRegistry* registry = 49 ExtensionFunctionRegistry* registry =
134 ExtensionFunctionRegistry::GetInstance(); 50 ExtensionFunctionRegistry::GetInstance();
135 registry->RegisterFunction<SendKeyboardEventInputFunction>(); 51 registry->RegisterFunction<SendKeyboardEventInputFunction>();
136 } 52 }
137 53
138 InputAPI::~InputAPI() { 54 InputAPI::~InputAPI() {
139 } 55 }
140 56
141 static base::LazyInstance<ProfileKeyedAPIFactory<InputAPI> > 57 static base::LazyInstance<ProfileKeyedAPIFactory<InputAPI> >
142 g_factory = LAZY_INSTANCE_INITIALIZER; 58 g_factory = LAZY_INSTANCE_INITIALIZER;
143 59
144 // static 60 // static
145 ProfileKeyedAPIFactory<InputAPI>* InputAPI::GetFactoryInstance() { 61 ProfileKeyedAPIFactory<InputAPI>* InputAPI::GetFactoryInstance() {
146 return &g_factory.Get(); 62 return &g_factory.Get();
147 } 63 }
148 64
149 } // namespace extensions 65 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/extensions/image_loader.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698