BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / chromeos / accessibility / spoken_feedback_event_rewriter.cc
blob601cfeb31c050d0f72bc53523b5bf99c893d51a4
1 // Copyright 2015 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 #include "chrome/browser/chromeos/accessibility/spoken_feedback_event_rewriter.h"
7 #include <string>
9 #include "base/values.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/common/extensions/api/commands/commands_handler.h"
14 #include "chrome/common/extensions/extension_constants.h"
15 #include "extensions/browser/event_router.h"
16 #include "extensions/browser/extension_registry.h"
17 #include "ui/events/event.h"
19 SpokenFeedbackEventRewriterDelegate::SpokenFeedbackEventRewriterDelegate()
20 : is_sequencing_(false) {}
22 bool SpokenFeedbackEventRewriterDelegate::IsSpokenFeedbackEnabled() const {
23 return true;
26 bool SpokenFeedbackEventRewriterDelegate::DispatchKeyEventToChromeVox(
27 const ui::KeyEvent& key_event) {
28 int kAllModifiers = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN |
29 ui::EF_ALT_DOWN | ui::EF_COMMAND_DOWN;
31 if (!chromeos::AccessibilityManager::Get())
32 return false;
33 content::BrowserContext* context = ProfileManager::GetActiveUserProfile();
34 if (!context)
35 return false;
37 const extensions::Extension* extension =
38 extensions::ExtensionRegistry::Get(context)->enabled_extensions().GetByID(
39 extension_misc::kChromeVoxExtensionId);
40 if (!extension)
41 return false;
43 extensions::EventRouter* event_router = extensions::EventRouter::Get(context);
44 if (!event_router->ExtensionHasEventListener(
45 extension_misc::kChromeVoxExtensionId, "commands.onCommand"))
46 return false;
48 const extensions::CommandMap* commands =
49 extensions::CommandsInfo::GetNamedCommands(extension);
50 if (!commands)
51 return false;
53 int modifiers = key_event.flags() & kAllModifiers;
54 std::string command_name;
56 if (is_sequencing_) {
57 is_sequencing_ = false;
58 modifiers |= ui::EF_SHIFT_DOWN | ui::EF_COMMAND_DOWN;
59 // This command name doesn't exist; used simply to cause ChromeVox to reset
60 // its sequencing state in case command lookup fails.
61 command_name = "sequenceCommand";
64 for (auto iter : *commands) {
65 int command_modifiers =
66 iter.second.accelerator().modifiers() & kAllModifiers;
67 if (iter.second.accelerator().key_code() == key_event.key_code() &&
68 command_modifiers == modifiers) {
69 command_name = iter.second.command_name();
70 if (!is_sequencing_ && command_name.substr(0, 14) == "sequencePrefix")
71 is_sequencing_ = true;
75 if (command_name.empty())
76 return false;
78 scoped_ptr<base::ListValue> args(new base::ListValue());
79 args->Append(new base::StringValue(command_name));
81 scoped_ptr<extensions::Event> extension_event(
82 new extensions::Event(extensions::events::COMMANDS_ON_COMMAND,
83 "commands.onCommand", args.Pass()));
84 extension_event->restrict_to_browser_context = context;
86 event_router->DispatchEventToExtension(extension_misc::kChromeVoxExtensionId,
87 extension_event.Pass());
89 return true;
92 SpokenFeedbackEventRewriter::SpokenFeedbackEventRewriter() {
93 delegate_.reset(new SpokenFeedbackEventRewriterDelegate());
96 SpokenFeedbackEventRewriter::~SpokenFeedbackEventRewriter() {
99 void SpokenFeedbackEventRewriter::SetDelegateForTest(
100 scoped_ptr<SpokenFeedbackEventRewriterDelegate> delegate) {
101 delegate_ = delegate.Pass();
104 ui::EventRewriteStatus SpokenFeedbackEventRewriter::RewriteEvent(
105 const ui::Event& event,
106 scoped_ptr<ui::Event>* new_event) {
107 if (!delegate_->IsSpokenFeedbackEnabled())
108 return ui::EVENT_REWRITE_CONTINUE;
110 if ((event.type() != ui::ET_KEY_PRESSED &&
111 event.type() != ui::ET_KEY_RELEASED))
112 return ui::EVENT_REWRITE_CONTINUE;
114 const ui::KeyEvent key_event = static_cast<const ui::KeyEvent&>(event);
115 if (event.type() == ui::ET_KEY_RELEASED) {
116 std::vector<int>::iterator it =
117 std::find(captured_key_codes_.begin(), captured_key_codes_.end(),
118 key_event.key_code());
119 if (it != captured_key_codes_.end()) {
120 captured_key_codes_.erase(it);
121 return ui::EVENT_REWRITE_DISCARD;
123 return ui::EVENT_REWRITE_CONTINUE;
126 if (delegate_->DispatchKeyEventToChromeVox(key_event)) {
127 captured_key_codes_.push_back(key_event.key_code());
128 return ui::EVENT_REWRITE_DISCARD;
130 return ui::EVENT_REWRITE_CONTINUE;
133 ui::EventRewriteStatus SpokenFeedbackEventRewriter::NextDispatchEvent(
134 const ui::Event& last_event,
135 scoped_ptr<ui::Event>* new_event) {
136 return ui::EVENT_REWRITE_CONTINUE;