Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / chromeos / accessibility / spoken_feedback_event_rewriter.cc
bloba976a0a33054c4995ae3552da91973dae12f01be
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 bool SpokenFeedbackEventRewriterDelegate::IsSpokenFeedbackEnabled() const {
20 return true;
23 bool SpokenFeedbackEventRewriterDelegate::DispatchKeyEventToChromeVox(
24 const ui::KeyEvent& key_event) {
25 int kAllModifiers = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN |
26 ui::EF_ALT_DOWN | ui::EF_COMMAND_DOWN;
28 if (!chromeos::AccessibilityManager::Get())
29 return false;
30 content::BrowserContext* context =
31 chromeos::AccessibilityManager::Get()->profile();
32 if (!context)
33 return false;
35 const extensions::Extension* extension =
36 extensions::ExtensionRegistry::Get(context)->enabled_extensions().GetByID(
37 extension_misc::kChromeVoxExtensionId);
38 if (!extension)
39 return false;
41 extensions::EventRouter* event_router = extensions::EventRouter::Get(context);
42 if (!event_router->ExtensionHasEventListener(
43 extension_misc::kChromeVoxExtensionId, "commands.onCommand"))
44 return false;
46 const extensions::CommandMap* commands =
47 extensions::CommandsInfo::GetNamedCommands(extension);
48 if (!commands)
49 return false;
51 int modifiers = key_event.flags() & kAllModifiers;
52 std::string command_name;
53 for (auto iter : *commands) {
54 int command_modifiers =
55 iter.second.accelerator().modifiers() & kAllModifiers;
56 if (iter.second.accelerator().key_code() == key_event.key_code() &&
57 command_modifiers == modifiers)
58 command_name = iter.second.command_name();
61 if (command_name.empty())
62 return false;
64 scoped_ptr<base::ListValue> args(new base::ListValue());
65 args->Append(new base::StringValue(command_name));
67 scoped_ptr<extensions::Event> extension_event(
68 new extensions::Event(extensions::events::COMMANDS_ON_COMMAND,
69 "commands.onCommand", args.Pass()));
70 extension_event->restrict_to_browser_context = context;
72 event_router->DispatchEventToExtension(extension_misc::kChromeVoxExtensionId,
73 extension_event.Pass());
75 return true;
78 SpokenFeedbackEventRewriter::SpokenFeedbackEventRewriter() {
79 delegate_.reset(new SpokenFeedbackEventRewriterDelegate());
82 SpokenFeedbackEventRewriter::~SpokenFeedbackEventRewriter() {
85 void SpokenFeedbackEventRewriter::SetDelegateForTest(
86 scoped_ptr<SpokenFeedbackEventRewriterDelegate> delegate) {
87 delegate_ = delegate.Pass();
90 ui::EventRewriteStatus SpokenFeedbackEventRewriter::RewriteEvent(
91 const ui::Event& event,
92 scoped_ptr<ui::Event>* new_event) {
93 if (!delegate_->IsSpokenFeedbackEnabled())
94 return ui::EVENT_REWRITE_CONTINUE;
96 if ((event.type() != ui::ET_KEY_PRESSED &&
97 event.type() != ui::ET_KEY_RELEASED))
98 return ui::EVENT_REWRITE_CONTINUE;
100 const ui::KeyEvent key_event = static_cast<const ui::KeyEvent&>(event);
101 if (event.type() == ui::ET_KEY_RELEASED) {
102 std::vector<int>::iterator it =
103 std::find(captured_key_codes_.begin(), captured_key_codes_.end(),
104 key_event.key_code());
105 if (it != captured_key_codes_.end()) {
106 captured_key_codes_.erase(it);
107 return ui::EVENT_REWRITE_DISCARD;
109 return ui::EVENT_REWRITE_CONTINUE;
112 if (delegate_->DispatchKeyEventToChromeVox(key_event)) {
113 captured_key_codes_.push_back(key_event.key_code());
114 return ui::EVENT_REWRITE_DISCARD;
116 return ui::EVENT_REWRITE_CONTINUE;
119 ui::EventRewriteStatus SpokenFeedbackEventRewriter::NextDispatchEvent(
120 const ui::Event& last_event,
121 scoped_ptr<ui::Event>* new_event) {
122 return ui::EVENT_REWRITE_CONTINUE;