1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_INPUT_METHOD_INPUT_METHOD_ENGINE_BASE_H_
#define CHROME_BROWSER_UI_INPUT_METHOD_INPUT_METHOD_ENGINE_BASE_H_
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "base/time/time.h"
#include "ui/base/ime/chromeos/input_method_descriptor.h"
#include "ui/base/ime/composition_text.h"
#include "ui/base/ime/ime_engine_handler_interface.h"
#include "url/gurl.h"
class Profile;
namespace ui {
struct CompositionText;
class IMEEngineHandlerInterface;
class KeyEvent;
} // namespace ui
namespace input_method {
class InputMethodEngineBase : virtual public ui::IMEEngineHandlerInterface {
public:
struct KeyboardEvent {
KeyboardEvent();
KeyboardEvent(const KeyboardEvent& other);
virtual ~KeyboardEvent();
std::string type;
std::string key;
std::string code;
int key_code; // only used by on-screen keyboards.
std::string extension_id;
bool alt_key;
bool ctrl_key;
bool shift_key;
bool caps_lock;
};
enum SegmentStyle {
SEGMENT_STYLE_UNDERLINE,
SEGMENT_STYLE_DOUBLE_UNDERLINE,
SEGMENT_STYLE_NO_UNDERLINE,
};
struct SegmentInfo {
int start;
int end;
SegmentStyle style;
};
#if defined(OS_CHROMEOS)
enum MouseButtonEvent {
MOUSE_BUTTON_LEFT,
MOUSE_BUTTON_RIGHT,
MOUSE_BUTTON_MIDDLE,
};
#endif
class Observer {
public:
virtual ~Observer() {}
// Called when the IME becomes the active IME.
virtual void OnActivate(const std::string& engine_id) = 0;
// Called when a text field gains focus, and will be sending key events.
virtual void OnFocus(
const IMEEngineHandlerInterface::InputContext& context) = 0;
// Called when a text field loses focus, and will no longer generate events.
virtual void OnBlur(int context_id) = 0;
// Called when the user pressed a key with a text field focused.
virtual void OnKeyEvent(
const std::string& engine_id,
const InputMethodEngineBase::KeyboardEvent& event,
ui::IMEEngineHandlerInterface::KeyEventDoneCallback& key_data) = 0;
// Called when Chrome terminates on-going text input session.
virtual void OnReset(const std::string& engine_id) = 0;
// Called when the IME is no longer active.
virtual void OnDeactivated(const std::string& engine_id) = 0;
// Called when composition bounds are changed.
virtual void OnCompositionBoundsChanged(
const std::vector<gfx::Rect>& bounds) = 0;
// Returns whether the observer is interested in key events.
virtual bool IsInterestedInKeyEvent() const = 0;
// Called when a surrounding text is changed.
virtual void OnSurroundingTextChanged(const std::string& engine_id,
const std::string& text,
int cursor_pos,
int anchor_pos,
int offset_pos) = 0;
// Called when the engine's MaybeSwitchEngine is called.
virtual void OnRequestEngineSwitch() = 0;
#if defined(OS_CHROMEOS)
// Called when an InputContext's properties change while it is focused.
virtual void OnInputContextUpdate(
const IMEEngineHandlerInterface::InputContext& context) = 0;
// Called when the user clicks on an item in the candidate list.
virtual void OnCandidateClicked(
const std::string& component_id,
int candidate_id,
InputMethodEngineBase::MouseButtonEvent button) = 0;
// Called when a menu item for this IME is interacted with.
virtual void OnMenuItemActivated(const std::string& component_id,
const std::string& menu_id) = 0;
#endif // defined(OS_CHROMEOS)
};
InputMethodEngineBase();
~InputMethodEngineBase() override;
void Initialize(std::unique_ptr<InputMethodEngineBase::Observer> observer,
const char* extension_id,
Profile* profile);
// IMEEngineHandlerInterface overrides.
void FocusIn(const ui::IMEEngineHandlerInterface::InputContext& input_context)
override;
void FocusOut() override;
void Enable(const std::string& component_id) override;
void Disable() override;
void Reset() override;
void MaybeSwitchEngine() override;
void ProcessKeyEvent(const ui::KeyEvent& key_event,
KeyEventDoneCallback& callback) override;
void SetSurroundingText(const std::string& text,
uint32_t cursor_pos,
uint32_t anchor_pos,
uint32_t offset_pos) override;
void SetCompositionBounds(const std::vector<gfx::Rect>& bounds) override;
bool ClearComposition(int context_id, std::string* error) override;
bool CommitText(int context_id,
const char* text,
std::string* error) override;
const std::string& GetActiveComponentId() const override;
bool DeleteSurroundingText(int context_id,
int offset,
size_t number_of_chars,
std::string* error) override;
int GetCotextIdForTesting() { return context_id_; }
bool IsInterestedInKeyEvent() const override;
// Send the sequence of key events.
bool SendKeyEvents(int context_id, const std::vector<KeyboardEvent>& events);
// Set the current composition and associated properties.
bool SetComposition(int context_id,
const char* text,
int selection_start,
int selection_end,
int cursor,
const std::vector<SegmentInfo>& segments,
std::string* error);
// Called when a key event is handled.
void KeyEventHandled(const std::string& extension_id,
const std::string& request_id,
bool handled);
// Adds unprocessed key event to |request_map_|.
std::string AddRequest(
const std::string& component_id,
ui::IMEEngineHandlerInterface::KeyEventDoneCallback& key_data);
protected:
// Notifies InputContextHandler that the composition is changed.
virtual void UpdateComposition(const ui::CompositionText& composition_text,
uint32_t cursor_pos,
bool is_visible) = 0;
// Notifies InputContextHanlder to commit |text|.
virtual void CommitTextToInputContext(int context_id,
const std::string& text) = 0;
// Sends the key event to the window tree host.
virtual bool SendKeyEvent(ui::KeyEvent* ui_event,
const std::string& code) = 0;
ui::TextInputType current_input_type_;
// ID that is used for the current input context. False if there is no focus.
int context_id_;
// Next id that will be assigned to a context.
int next_context_id_;
// The input_component ID in IME extension's manifest.
std::string active_component_id_;
// The IME extension ID.
std::string extension_id_;
// The observer object recieving events for this IME.
std::unique_ptr<InputMethodEngineBase::Observer> observer_;
// The current preedit text, and it's cursor position.
std::unique_ptr<ui::CompositionText> composition_text_;
int composition_cursor_;
// Used with SendKeyEvents and ProcessKeyEvent to check if the key event
// sent to ProcessKeyEvent is sent by SendKeyEvents.
const ui::KeyEvent* sent_key_event_;
Profile* profile_;
using RequestMap =
std::map<std::string,
std::pair<std::string,
ui::IMEEngineHandlerInterface::KeyEventDoneCallback>>;
unsigned int next_request_id_;
RequestMap request_map_;
// The composition text to be set from calling input.ime.setComposition API.
ui::CompositionText composition_;
bool composition_changed_;
// The text to be committed from calling input.ime.commitText API.
std::string text_;
bool commit_text_changed_;
// Indicates whether the IME extension is currently handling a physical key
// event. This is used in CommitText/UpdateCompositionText/etc.
bool handling_key_event_;
};
} // namespace input_method
#endif // CHROME_BROWSER_UI_INPUT_METHOD_INPUT_METHOD_ENGINE_BASE_H_
|