OLD | NEW |
| (Empty) |
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 "chrome/browser/extensions/extension_input_ui_api.h" | |
6 | |
7 #include <algorithm> | |
8 #include <string> | |
9 | |
10 #include "base/json/json_writer.h" | |
11 #include "base/logging.h" | |
12 #include "base/string_util.h" | |
13 #include "base/values.h" | |
14 #include "chrome/browser/chromeos/cros/cros_library.h" | |
15 #include "chrome/browser/extensions/extension_event_router.h" | |
16 #include "chrome/browser/profiles/profile.h" | |
17 #include "third_party/cros/chromeos_cros_api.h" | |
18 | |
19 namespace events { | |
20 | |
21 const char kOnUpdateAuxiliaryText[] = | |
22 "experimental.input.ui.onUpdateAuxiliaryText"; | |
23 const char kOnUpdateLookupTable[] = "experimental.input.ui.onUpdateLookupTable"; | |
24 const char kOnSetCursorLocation[] = "experimental.input.ui.onSetCursorLocation"; | |
25 | |
26 } // namespace events | |
27 | |
28 ExtensionInputUiEventRouter* | |
29 ExtensionInputUiEventRouter::GetInstance() { | |
30 return Singleton<ExtensionInputUiEventRouter>::get(); | |
31 } | |
32 | |
33 ExtensionInputUiEventRouter::ExtensionInputUiEventRouter() | |
34 : profile_(NULL), | |
35 ibus_ui_controller_(NULL) { | |
36 } | |
37 | |
38 ExtensionInputUiEventRouter::~ExtensionInputUiEventRouter() { | |
39 } | |
40 | |
41 void ExtensionInputUiEventRouter::Init() { | |
42 if (ibus_ui_controller_.get() == NULL) { | |
43 ibus_ui_controller_.reset( | |
44 chromeos::input_method::IBusUiController::Create()); | |
45 // The observer should be added before Connect() so we can capture the | |
46 // initial connection change. | |
47 ibus_ui_controller_->AddObserver(this); | |
48 ibus_ui_controller_->Connect(); | |
49 } | |
50 } | |
51 | |
52 void ExtensionInputUiEventRouter::Register( | |
53 Profile* profile, const std::string& extension_id) { | |
54 profile_ = profile; | |
55 extension_id_ = extension_id; | |
56 } | |
57 | |
58 void ExtensionInputUiEventRouter::CandidateClicked(Profile* profile, | |
59 const std::string& extension_id, int index, int button) { | |
60 if (profile_ != profile || extension_id_ != extension_id) { | |
61 DLOG(WARNING) << "called from unregistered extension"; | |
62 } | |
63 ibus_ui_controller_->NotifyCandidateClicked(index, button, 0); | |
64 } | |
65 | |
66 void ExtensionInputUiEventRouter::CursorUp(Profile* profile, | |
67 const std::string& extension_id) { | |
68 if (profile_ != profile || extension_id_ != extension_id) { | |
69 DLOG(WARNING) << "called from unregistered extension"; | |
70 } | |
71 ibus_ui_controller_->NotifyCursorUp(); | |
72 } | |
73 | |
74 void ExtensionInputUiEventRouter::CursorDown(Profile* profile, | |
75 const std::string& extension_id) { | |
76 if (profile_ != profile || extension_id_ != extension_id) { | |
77 DLOG(WARNING) << "called from unregistered extension"; | |
78 } | |
79 ibus_ui_controller_->NotifyCursorDown(); | |
80 } | |
81 | |
82 void ExtensionInputUiEventRouter::PageUp(Profile* profile, | |
83 const std::string& extension_id) { | |
84 if (profile_ != profile || extension_id_ != extension_id) { | |
85 DLOG(WARNING) << "called from unregistered extension"; | |
86 } | |
87 ibus_ui_controller_->NotifyPageUp(); | |
88 } | |
89 | |
90 void ExtensionInputUiEventRouter::PageDown(Profile* profile, | |
91 const std::string& extension_id) { | |
92 if (profile_ != profile || extension_id_ != extension_id) { | |
93 DLOG(WARNING) << "called from unregistered extension"; | |
94 } | |
95 ibus_ui_controller_->NotifyPageDown(); | |
96 } | |
97 | |
98 void ExtensionInputUiEventRouter::OnHideAuxiliaryText() { | |
99 OnUpdateAuxiliaryText("", false); | |
100 } | |
101 | |
102 void ExtensionInputUiEventRouter::OnHideLookupTable() { | |
103 if (profile_ == NULL || extension_id_.empty()) | |
104 return; | |
105 | |
106 DictionaryValue* dict = new DictionaryValue(); | |
107 dict->SetBoolean("visible", false); | |
108 | |
109 ListValue *candidates = new ListValue(); | |
110 dict->Set("candidates", candidates); | |
111 | |
112 ListValue args; | |
113 args.Append(dict); | |
114 | |
115 std::string json_args; | |
116 base::JSONWriter::Write(&args, &json_args); | |
117 profile_->GetExtensionEventRouter()->DispatchEventToExtension( | |
118 extension_id_, events::kOnUpdateLookupTable, json_args, profile_, GURL()); | |
119 } | |
120 | |
121 void ExtensionInputUiEventRouter::OnHidePreeditText() { | |
122 } | |
123 | |
124 void ExtensionInputUiEventRouter::OnSetCursorLocation( | |
125 int x, int y, int width, int height) { | |
126 | |
127 if (profile_ == NULL || extension_id_.empty()) | |
128 return; | |
129 | |
130 ListValue args; | |
131 args.Append(Value::CreateIntegerValue(x)); | |
132 args.Append(Value::CreateIntegerValue(y)); | |
133 args.Append(Value::CreateIntegerValue(width)); | |
134 args.Append(Value::CreateIntegerValue(height)); | |
135 | |
136 std::string json_args; | |
137 base::JSONWriter::Write(&args, &json_args); | |
138 profile_->GetExtensionEventRouter()->DispatchEventToExtension( | |
139 extension_id_, events::kOnSetCursorLocation, json_args, profile_, GURL()); | |
140 } | |
141 | |
142 void ExtensionInputUiEventRouter::OnUpdateAuxiliaryText( | |
143 const std::string& utf8_text, | |
144 bool visible) { | |
145 if (profile_ == NULL || extension_id_.empty()) | |
146 return; | |
147 | |
148 ListValue args; | |
149 args.Append(Value::CreateStringValue(visible ? utf8_text : "")); | |
150 | |
151 std::string json_args; | |
152 base::JSONWriter::Write(&args, &json_args); | |
153 profile_->GetExtensionEventRouter()->DispatchEventToExtension( | |
154 extension_id_, events::kOnUpdateAuxiliaryText, json_args, profile_, GURL()); | |
155 } | |
156 | |
157 void ExtensionInputUiEventRouter::OnUpdateLookupTable( | |
158 const chromeos::input_method::InputMethodLookupTable& lookup_table) { | |
159 if (profile_ == NULL || extension_id_.empty()) | |
160 return; | |
161 | |
162 DictionaryValue* dict = new DictionaryValue(); | |
163 dict->SetBoolean("visible", lookup_table.visible); | |
164 | |
165 if (lookup_table.visible) { | |
166 } | |
167 | |
168 ListValue *candidates = new ListValue(); | |
169 | |
170 size_t page = lookup_table.cursor_absolute_index / lookup_table.page_size; | |
171 size_t begin = page * lookup_table.page_size; | |
172 size_t end = std::min(begin + lookup_table.page_size, | |
173 lookup_table.candidates.size()); | |
174 | |
175 for (size_t i = begin; i < end; i++) { | |
176 candidates->Append(Value::CreateStringValue(lookup_table.candidates[i])); | |
177 } | |
178 | |
179 dict->Set("candidates", candidates); | |
180 | |
181 ListValue args; | |
182 args.Append(dict); | |
183 | |
184 std::string json_args; | |
185 base::JSONWriter::Write(&args, &json_args); | |
186 profile_->GetExtensionEventRouter()->DispatchEventToExtension( | |
187 extension_id_, events::kOnUpdateLookupTable, json_args, profile_, GURL()); | |
188 } | |
189 | |
190 void ExtensionInputUiEventRouter::OnUpdatePreeditText( | |
191 const std::string& utf8_text, | |
192 unsigned int cursor, | |
193 bool visible) { | |
194 } | |
195 | |
196 void ExtensionInputUiEventRouter::OnConnectionChange(bool connected) { | |
197 } | |
198 | |
199 bool RegisterInputUiFunction::RunImpl() { | |
200 ExtensionInputUiEventRouter::GetInstance()->Register( | |
201 profile(), extension_id()); | |
202 return true; | |
203 } | |
204 | |
205 bool CandidateClickedInputUiFunction::RunImpl() { | |
206 int index = 0; | |
207 int button = 0; | |
208 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &index)); | |
209 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &button)); | |
210 | |
211 ExtensionInputUiEventRouter::GetInstance()->CandidateClicked( | |
212 profile(), extension_id(), index, button); | |
213 | |
214 return true; | |
215 } | |
216 | |
217 bool CursorUpInputUiFunction::RunImpl() { | |
218 ExtensionInputUiEventRouter::GetInstance()->CursorUp( | |
219 profile(), extension_id()); | |
220 return true; | |
221 } | |
222 | |
223 bool CursorDownInputUiFunction::RunImpl() { | |
224 ExtensionInputUiEventRouter::GetInstance()->CursorDown( | |
225 profile(), extension_id()); | |
226 return true; | |
227 } | |
228 | |
229 bool PageUpInputUiFunction::RunImpl() { | |
230 ExtensionInputUiEventRouter::GetInstance()->PageUp( | |
231 profile(), extension_id()); | |
232 return true; | |
233 } | |
234 | |
235 bool PageDownInputUiFunction::RunImpl() { | |
236 ExtensionInputUiEventRouter::GetInstance()->PageDown( | |
237 profile(), extension_id()); | |
238 return true; | |
239 } | |
OLD | NEW |