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 "ppapi/cpp/dev/text_input_dev.h"
6
7#include "ppapi/c/dev/ppp_text_input_dev.h"
8#include "ppapi/cpp/instance.h"
9#include "ppapi/cpp/instance_handle.h"
10#include "ppapi/cpp/module_impl.h"
11#include "ppapi/cpp/rect.h"
12
13namespace pp {
14
15namespace {
16
17static const char kPPPTextInputInterface[] = PPP_TEXTINPUT_DEV_INTERFACE;
18
19void RequestSurroundingText(PP_Instance instance,
20                            uint32_t desired_number_of_characters) {
21  void* object = Instance::GetPerInstanceObject(instance,
22                                                kPPPTextInputInterface);
23  if (!object)
24    return;
25  static_cast<TextInput_Dev*>(object)->RequestSurroundingText(
26      desired_number_of_characters);
27}
28
29const PPP_TextInput_Dev ppp_text_input = {
30  &RequestSurroundingText
31};
32
33template <> const char* interface_name<PPB_TextInput_Dev_0_2>() {
34  return PPB_TEXTINPUT_DEV_INTERFACE_0_2;
35}
36
37template <> const char* interface_name<PPB_TextInput_Dev_0_1>() {
38  return PPB_TEXTINPUT_DEV_INTERFACE_0_1;
39}
40
41}  // namespace
42
43
44TextInput_Dev::TextInput_Dev(Instance* instance)
45    : instance_(instance) {
46  Module::Get()->AddPluginInterface(kPPPTextInputInterface,
47                                    &ppp_text_input);
48  instance->AddPerInstanceObject(kPPPTextInputInterface, this);
49}
50
51TextInput_Dev::~TextInput_Dev() {
52  Instance::RemovePerInstanceObject(instance_, kPPPTextInputInterface, this);
53}
54
55void TextInput_Dev::RequestSurroundingText(uint32_t) {
56  // Default implementation. Send a null range.
57  UpdateSurroundingText(std::string(), 0, 0);
58}
59
60void TextInput_Dev::SetTextInputType(PP_TextInput_Type_Dev type) {
61  if (has_interface<PPB_TextInput_Dev_0_2>()) {
62    get_interface<PPB_TextInput_Dev_0_2>()->SetTextInputType(
63        instance_.pp_instance(), type);
64  } else if (has_interface<PPB_TextInput_Dev_0_1>()) {
65    get_interface<PPB_TextInput_Dev_0_1>()->SetTextInputType(
66        instance_.pp_instance(), type);
67  }
68}
69
70void TextInput_Dev::UpdateCaretPosition(const Rect& caret,
71                                        const Rect& bounding_box) {
72  if (has_interface<PPB_TextInput_Dev_0_2>()) {
73    get_interface<PPB_TextInput_Dev_0_2>()->UpdateCaretPosition(
74        instance_.pp_instance(), &caret.pp_rect(), &bounding_box.pp_rect());
75  } else if (has_interface<PPB_TextInput_Dev_0_1>()) {
76    get_interface<PPB_TextInput_Dev_0_1>()->UpdateCaretPosition(
77        instance_.pp_instance(), &caret.pp_rect(), &bounding_box.pp_rect());
78  }
79}
80
81void TextInput_Dev::CancelCompositionText() {
82  if (has_interface<PPB_TextInput_Dev_0_2>()) {
83    get_interface<PPB_TextInput_Dev_0_2>()->CancelCompositionText(
84        instance_.pp_instance());
85  } else if (has_interface<PPB_TextInput_Dev_0_1>()) {
86    get_interface<PPB_TextInput_Dev_0_1>()->CancelCompositionText(
87        instance_.pp_instance());
88  }
89}
90
91void TextInput_Dev::SelectionChanged() {
92  if (has_interface<PPB_TextInput_Dev_0_2>()) {
93    get_interface<PPB_TextInput_Dev_0_2>()->SelectionChanged(
94        instance_.pp_instance());
95  }
96}
97
98void TextInput_Dev::UpdateSurroundingText(const std::string& text,
99                                          uint32_t caret,
100                                          uint32_t anchor) {
101  if (has_interface<PPB_TextInput_Dev_0_2>()) {
102    get_interface<PPB_TextInput_Dev_0_2>()->UpdateSurroundingText(
103        instance_.pp_instance(), text.c_str(), caret, anchor);
104  }
105}
106
107
108}  // namespace pp
109