Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / views / controls / webview / unhandled_keyboard_event_handler.cc
blob38feaf99a9eb2944ef2212b9a8f16fbd7e6e5e6b
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
3 // found in the LICENSE file.
5 #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h"
7 #include "ui/content_accelerators/accelerator_util.h"
8 #include "ui/views/focus/focus_manager.h"
10 namespace views {
12 UnhandledKeyboardEventHandler::UnhandledKeyboardEventHandler()
13 : ignore_next_char_event_(false) {
16 UnhandledKeyboardEventHandler::~UnhandledKeyboardEventHandler() {
19 void UnhandledKeyboardEventHandler::HandleKeyboardEvent(
20 const content::NativeWebKeyboardEvent& event,
21 FocusManager* focus_manager) {
22 if (!focus_manager) {
23 NOTREACHED();
24 return;
26 // Previous calls to TranslateMessage can generate Char events as well as
27 // RawKeyDown events, even if the latter triggered an accelerator. In these
28 // cases, we discard the Char events.
29 if (event.type == blink::WebInputEvent::Char && ignore_next_char_event_) {
30 ignore_next_char_event_ = false;
31 return;
33 // It's necessary to reset this flag, because a RawKeyDown event may not
34 // always generate a Char event.
35 ignore_next_char_event_ = false;
37 if (event.type == blink::WebInputEvent::RawKeyDown) {
38 ui::Accelerator accelerator =
39 ui::GetAcceleratorFromNativeWebKeyboardEvent(event);
41 // This is tricky: we want to set ignore_next_char_event_ if
42 // ProcessAccelerator returns true. But ProcessAccelerator might delete
43 // |this| if the accelerator is a "close tab" one. So we speculatively
44 // set the flag and fix it if no event was handled.
45 ignore_next_char_event_ = true;
47 if (focus_manager->ProcessAccelerator(accelerator)) {
48 return;
51 // ProcessAccelerator didn't handle the accelerator, so we know both
52 // that |this| is still valid, and that we didn't want to set the flag.
53 ignore_next_char_event_ = false;
56 if (event.os_event && !event.skip_in_browser)
57 HandleNativeKeyboardEvent(event.os_event, focus_manager);
60 } // namespace views