Home | History | Annotate | Download | only in renderer_host
      1 // Copyright (c) 2011 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.
      4 
      5 #include "content/public/browser/native_web_keyboard_event.h"
      6 
      7 #include "base/logging.h"
      8 #include "content/browser/renderer_host/web_input_event_aura.h"
      9 #include "ui/base/events/event.h"
     10 
     11 namespace {
     12 
     13 // We need to copy |os_event| in NativeWebKeyboardEvent because it is
     14 // queued in RenderWidgetHost and may be passed and used
     15 // RenderViewHostDelegate::HandledKeybardEvent after the original aura
     16 // event is destroyed.
     17 ui::Event* CopyEvent(ui::Event* event) {
     18   return event ? static_cast<ui::KeyEvent*>(event)->Copy() : NULL;
     19 }
     20 
     21 int EventFlagsToWebInputEventModifiers(int flags) {
     22   return
     23       (flags & ui::EF_SHIFT_DOWN ? WebKit::WebInputEvent::ShiftKey : 0) |
     24       (flags & ui::EF_CONTROL_DOWN ? WebKit::WebInputEvent::ControlKey : 0) |
     25       (flags & ui::EF_CAPS_LOCK_DOWN ? WebKit::WebInputEvent::CapsLockOn : 0) |
     26       (flags & ui::EF_ALT_DOWN ? WebKit::WebInputEvent::AltKey : 0);
     27 }
     28 
     29 }  // namespace
     30 
     31 using WebKit::WebKeyboardEvent;
     32 
     33 namespace content {
     34 
     35 NativeWebKeyboardEvent::NativeWebKeyboardEvent()
     36     : os_event(NULL),
     37       skip_in_browser(false) {
     38 }
     39 
     40 NativeWebKeyboardEvent::NativeWebKeyboardEvent(gfx::NativeEvent native_event)
     41     : WebKeyboardEvent(MakeWebKeyboardEvent(
     42           static_cast<ui::KeyEvent*>(native_event))),
     43       os_event(CopyEvent(native_event)),
     44       skip_in_browser(false) {
     45 }
     46 
     47 NativeWebKeyboardEvent::NativeWebKeyboardEvent(
     48     const NativeWebKeyboardEvent& other)
     49     : WebKeyboardEvent(other),
     50       os_event(CopyEvent(other.os_event)),
     51       skip_in_browser(other.skip_in_browser) {
     52 }
     53 
     54 NativeWebKeyboardEvent::NativeWebKeyboardEvent(
     55     ui::EventType key_event_type,
     56     bool is_char,
     57     wchar_t character,
     58     int state,
     59     double time_stamp_seconds)
     60     : os_event(NULL),
     61       skip_in_browser(false) {
     62   switch (key_event_type) {
     63     case ui::ET_KEY_PRESSED:
     64       type = is_char ? WebKit::WebInputEvent::Char :
     65           WebKit::WebInputEvent::RawKeyDown;
     66       break;
     67     case ui::ET_KEY_RELEASED:
     68       type = WebKit::WebInputEvent::KeyUp;
     69       break;
     70     default:
     71       NOTREACHED();
     72   }
     73 
     74   modifiers = EventFlagsToWebInputEventModifiers(state);
     75   timeStampSeconds = time_stamp_seconds;
     76   windowsKeyCode = character;
     77   nativeKeyCode = character;
     78   text[0] = character;
     79   unmodifiedText[0] = character;
     80   isSystemKey = (state & ui::EF_ALT_DOWN) != 0;
     81   setKeyIdentifierFromWindowsKeyCode();
     82 }
     83 
     84 NativeWebKeyboardEvent& NativeWebKeyboardEvent::operator=(
     85     const NativeWebKeyboardEvent& other) {
     86   WebKeyboardEvent::operator=(other);
     87   delete os_event;
     88   os_event = CopyEvent(other.os_event);
     89   skip_in_browser = other.skip_in_browser;
     90 
     91   return *this;
     92 }
     93 
     94 NativeWebKeyboardEvent::~NativeWebKeyboardEvent() {
     95   delete os_event;
     96 }
     97 
     98 }  // namespace content
     99