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
|
// Copyright (c) 2011 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.
#include "chrome/browser/extensions/extension_input_ui_api.h"
#include <algorithm>
#include <string>
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "base/values.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "chrome/browser/extensions/extension_event_router.h"
#include "chrome/browser/profiles/profile.h"
#include "third_party/cros/chromeos_cros_api.h"
namespace events {
const char kOnUpdateAuxiliaryText[] =
"experimental.inputUI.onUpdateAuxiliaryText";
const char kOnUpdateLookupTable[] = "experimental.inputUI.onUpdateLookupTable";
const char kOnSetCursorLocation[] = "experimental.inputUI.onSetCursorLocation";
} // namespace events
ExtensionInputUiEventRouter*
ExtensionInputUiEventRouter::GetInstance() {
return Singleton<ExtensionInputUiEventRouter>::get();
}
ExtensionInputUiEventRouter::ExtensionInputUiEventRouter()
: profile_(NULL),
ibus_ui_controller_(NULL) {
}
ExtensionInputUiEventRouter::~ExtensionInputUiEventRouter() {
}
void ExtensionInputUiEventRouter::Init() {
if (ibus_ui_controller_.get() == NULL) {
ibus_ui_controller_.reset(
chromeos::input_method::IBusUiController::Create());
// The observer should be added before Connect() so we can capture the
// initial connection change.
ibus_ui_controller_->AddObserver(this);
ibus_ui_controller_->Connect();
}
}
void ExtensionInputUiEventRouter::Register(
Profile* profile, const std::string& extension_id) {
profile_ = profile;
extension_id_ = extension_id;
}
void ExtensionInputUiEventRouter::CandidateClicked(Profile* profile,
const std::string& extension_id, int index, int button) {
if (profile_ != profile || extension_id_ != extension_id) {
DLOG(WARNING) << "called from unregistered extension";
}
ibus_ui_controller_->NotifyCandidateClicked(index, button, 0);
}
void ExtensionInputUiEventRouter::CursorUp(Profile* profile,
const std::string& extension_id) {
if (profile_ != profile || extension_id_ != extension_id) {
DLOG(WARNING) << "called from unregistered extension";
}
ibus_ui_controller_->NotifyCursorUp();
}
void ExtensionInputUiEventRouter::CursorDown(Profile* profile,
const std::string& extension_id) {
if (profile_ != profile || extension_id_ != extension_id) {
DLOG(WARNING) << "called from unregistered extension";
}
ibus_ui_controller_->NotifyCursorDown();
}
void ExtensionInputUiEventRouter::PageUp(Profile* profile,
const std::string& extension_id) {
if (profile_ != profile || extension_id_ != extension_id) {
DLOG(WARNING) << "called from unregistered extension";
}
ibus_ui_controller_->NotifyPageUp();
}
void ExtensionInputUiEventRouter::PageDown(Profile* profile,
const std::string& extension_id) {
if (profile_ != profile || extension_id_ != extension_id) {
DLOG(WARNING) << "called from unregistered extension";
}
ibus_ui_controller_->NotifyPageDown();
}
void ExtensionInputUiEventRouter::OnHideAuxiliaryText() {
OnUpdateAuxiliaryText("", false);
}
void ExtensionInputUiEventRouter::OnHideLookupTable() {
if (profile_ == NULL || extension_id_.empty())
return;
DictionaryValue* dict = new DictionaryValue();
dict->SetBoolean("visible", false);
ListValue *candidates = new ListValue();
dict->Set("candidates", candidates);
ListValue args;
args.Append(dict);
std::string json_args;
base::JSONWriter::Write(&args, false, &json_args);
profile_->GetExtensionEventRouter()->DispatchEventToExtension(
extension_id_, events::kOnUpdateLookupTable, json_args, profile_, GURL());
}
void ExtensionInputUiEventRouter::OnHidePreeditText() {
}
void ExtensionInputUiEventRouter::OnSetCursorLocation(
int x, int y, int width, int height) {
if (profile_ == NULL || extension_id_.empty())
return;
ListValue args;
args.Append(Value::CreateIntegerValue(x));
args.Append(Value::CreateIntegerValue(y));
args.Append(Value::CreateIntegerValue(width));
args.Append(Value::CreateIntegerValue(height));
std::string json_args;
base::JSONWriter::Write(&args, false, &json_args);
profile_->GetExtensionEventRouter()->DispatchEventToExtension(
extension_id_, events::kOnSetCursorLocation, json_args, profile_, GURL());
}
void ExtensionInputUiEventRouter::OnUpdateAuxiliaryText(
const std::string& utf8_text,
bool visible) {
if (profile_ == NULL || extension_id_.empty())
return;
ListValue args;
args.Append(Value::CreateStringValue(visible ? utf8_text : ""));
std::string json_args;
base::JSONWriter::Write(&args, false, &json_args);
profile_->GetExtensionEventRouter()->DispatchEventToExtension(
extension_id_, events::kOnUpdateAuxiliaryText, json_args, profile_, GURL());
}
void ExtensionInputUiEventRouter::OnUpdateLookupTable(
const chromeos::input_method::InputMethodLookupTable& lookup_table) {
if (profile_ == NULL || extension_id_.empty())
return;
DictionaryValue* dict = new DictionaryValue();
dict->SetBoolean("visible", lookup_table.visible);
if (lookup_table.visible) {
}
ListValue *candidates = new ListValue();
size_t page = lookup_table.cursor_absolute_index / lookup_table.page_size;
size_t begin = page * lookup_table.page_size;
size_t end = std::min(begin + lookup_table.page_size,
lookup_table.candidates.size());
for (size_t i = begin; i < end; i++) {
candidates->Append(Value::CreateStringValue(lookup_table.candidates[i]));
}
dict->Set("candidates", candidates);
ListValue args;
args.Append(dict);
std::string json_args;
base::JSONWriter::Write(&args, false, &json_args);
profile_->GetExtensionEventRouter()->DispatchEventToExtension(
extension_id_, events::kOnUpdateLookupTable, json_args, profile_, GURL());
}
void ExtensionInputUiEventRouter::OnUpdatePreeditText(
const std::string& utf8_text,
unsigned int cursor,
bool visible) {
}
void ExtensionInputUiEventRouter::OnConnectionChange(bool connected) {
}
bool RegisterInputUiFunction::RunImpl() {
ExtensionInputUiEventRouter::GetInstance()->Register(
profile(), extension_id());
return true;
}
bool CandidateClickedInputUiFunction::RunImpl() {
int index = 0;
int button = 0;
EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &index));
EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &button));
ExtensionInputUiEventRouter::GetInstance()->CandidateClicked(
profile(), extension_id(), index, button);
return true;
}
bool CursorUpInputUiFunction::RunImpl() {
ExtensionInputUiEventRouter::GetInstance()->CursorUp(
profile(), extension_id());
return true;
}
bool CursorDownInputUiFunction::RunImpl() {
ExtensionInputUiEventRouter::GetInstance()->CursorDown(
profile(), extension_id());
return true;
}
bool PageUpInputUiFunction::RunImpl() {
ExtensionInputUiEventRouter::GetInstance()->PageUp(
profile(), extension_id());
return true;
}
bool PageDownInputUiFunction::RunImpl() {
ExtensionInputUiEventRouter::GetInstance()->PageDown(
profile(), extension_id());
return true;
}
|