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