native_web_keyboard_event_android.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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.
4
5#include "content/public/browser/native_web_keyboard_event.h"
6
7#include "base/android/jni_android.h"
8#include "third_party/WebKit/public/web/android/WebInputEventFactory.h"
9#include "ui/gfx/native_widget_types.h"
10
11using WebKit::WebInputEventFactory;
12
13namespace {
14
15jobject NewGlobalRefForKeyEvent(jobject key_event) {
16  if (key_event == NULL) return NULL;
17  return base::android::AttachCurrentThread()->NewGlobalRef(key_event);
18}
19
20void DeleteGlobalRefForKeyEvent(jobject key_event) {
21  if (key_event != NULL)
22    base::android::AttachCurrentThread()->DeleteGlobalRef(key_event);
23}
24
25}
26
27namespace content {
28
29NativeWebKeyboardEvent::NativeWebKeyboardEvent()
30    : os_event(NULL),
31      skip_in_browser(false) {
32}
33
34NativeWebKeyboardEvent::NativeWebKeyboardEvent(
35    WebKit::WebInputEvent::Type type,
36    int modifiers, double time_secs, int keycode, int unicode_character,
37    bool is_system_key)
38    : WebKeyboardEvent(WebInputEventFactory::keyboardEvent(
39        type, modifiers, time_secs, keycode, unicode_character,
40        is_system_key)) {
41  os_event = NULL;
42  skip_in_browser = false;
43}
44
45NativeWebKeyboardEvent::NativeWebKeyboardEvent(
46    jobject android_key_event, WebKit::WebInputEvent::Type type,
47    int modifiers, double time_secs, int keycode, int unicode_character,
48    bool is_system_key)
49    : WebKeyboardEvent(WebInputEventFactory::keyboardEvent(
50        type, modifiers, time_secs, keycode, unicode_character,
51        is_system_key)) {
52  os_event = NewGlobalRefForKeyEvent(android_key_event);
53  skip_in_browser = false;
54}
55
56NativeWebKeyboardEvent::NativeWebKeyboardEvent(
57    const NativeWebKeyboardEvent& other)
58    : WebKeyboardEvent(other),
59      os_event(NewGlobalRefForKeyEvent(other.os_event)),
60      skip_in_browser(other.skip_in_browser) {
61}
62
63NativeWebKeyboardEvent& NativeWebKeyboardEvent::operator=(
64    const NativeWebKeyboardEvent& other) {
65  WebKeyboardEvent::operator=(other);
66
67  os_event = NewGlobalRefForKeyEvent(other.os_event);
68  skip_in_browser = other.skip_in_browser;
69
70  return *this;
71}
72
73NativeWebKeyboardEvent::~NativeWebKeyboardEvent() {
74  DeleteGlobalRefForKeyEvent(os_event);
75}
76
77}  // namespace content
78