OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 <string> |
| 8 |
| 9 #include "base/json/json_writer.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/string_util.h" |
| 12 #include "base/values.h" |
| 13 #include "chrome/browser/extensions/extension_event_router.h" |
| 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "third_party/cros/chromeos_cros_api.h" |
| 16 #include "third_party/cros/chromeos_input_method_ui.h" |
| 17 |
| 18 namespace events { |
| 19 |
| 20 const char kOnUpdateAuxiliaryText[] = |
| 21 "experimental.inputUI.onUpdateAuxiliaryText"; |
| 22 const char kOnUpdateLookupTable[] = "experimental.inputUI.onUpdateLookupTable"; |
| 23 const char kOnSetCursorLocation[] = "experimental.inputUI.onSetCursorLocation"; |
| 24 |
| 25 } // namespace events |
| 26 |
| 27 class InputUiController { |
| 28 public: |
| 29 explicit InputUiController(ExtensionInputUiEventRouter* router); |
| 30 ~InputUiController(); |
| 31 |
| 32 void CandidateClicked(int index, int button, int flags); |
| 33 void CursorUp(); |
| 34 void CursorDown(); |
| 35 void PageUp(); |
| 36 void PageDown(); |
| 37 |
| 38 private: |
| 39 // The function is called when |HideAuxiliaryText| signal is received in |
| 40 // libcros. |ui_controller| is a void pointer to this object. |
| 41 static void OnHideAuxiliaryText(void* ui_controller); |
| 42 |
| 43 // The function is called when |HideLookupTable| signal is received in |
| 44 // libcros. |ui_controller| is a void pointer to this object. |
| 45 static void OnHideLookupTable(void* ui_controller); |
| 46 |
| 47 // The function is called when |SetCursorLocation| signal is received |
| 48 // in libcros. |ui_controller| is a void pointer to this object. |
| 49 static void OnSetCursorLocation(void* ui_controller, |
| 50 int x, |
| 51 int y, |
| 52 int width, |
| 53 int height); |
| 54 |
| 55 // The function is called when |UpdateAuxiliaryText| signal is received |
| 56 // in libcros. |ui_controller| is a void pointer to this object. |
| 57 static void OnUpdateAuxiliaryText(void* ui_controller, |
| 58 const std::string& utf8_text, |
| 59 bool visible); |
| 60 |
| 61 // The function is called when |UpdateLookupTable| signal is received |
| 62 // in libcros. |ui_controller| is a void pointer to this object. |
| 63 static void OnUpdateLookupTable(void* ui_controller, |
| 64 const chromeos::InputMethodLookupTable& lookup_table); |
| 65 |
| 66 // This function is called by libcros when ibus connects or disconnects. |
| 67 // |ui_controller| is a void pointer to this object. |
| 68 static void OnConnectionChange(void* ui_controller, bool connected); |
| 69 |
| 70 ExtensionInputUiEventRouter* router_; |
| 71 chromeos::InputMethodUiStatusConnection* ui_status_connection_; |
| 72 |
| 73 DISALLOW_COPY_AND_ASSIGN(InputUiController); |
| 74 }; |
| 75 |
| 76 InputUiController::InputUiController( |
| 77 ExtensionInputUiEventRouter* router) : |
| 78 router_(router), |
| 79 ui_status_connection_(NULL) { |
| 80 std::string error; |
| 81 chromeos::LoadLibcros(NULL, error); |
| 82 |
| 83 chromeos::InputMethodUiStatusMonitorFunctions functions; |
| 84 functions.hide_auxiliary_text = |
| 85 &InputUiController::OnHideAuxiliaryText; |
| 86 functions.hide_lookup_table = |
| 87 &InputUiController::OnHideLookupTable; |
| 88 functions.set_cursor_location = |
| 89 &InputUiController::OnSetCursorLocation; |
| 90 functions.update_auxiliary_text = |
| 91 &InputUiController::OnUpdateAuxiliaryText; |
| 92 functions.update_lookup_table = |
| 93 &InputUiController::OnUpdateLookupTable; |
| 94 ui_status_connection_ = chromeos::MonitorInputMethodUiStatus(functions, this); |
| 95 |
| 96 if (!ui_status_connection_) |
| 97 LOG(ERROR) << "chromeos::MonitorInputMethodUiStatus() failed!"; |
| 98 } |
| 99 |
| 100 InputUiController::~InputUiController() { |
| 101 chromeos::DisconnectInputMethodUiStatus(ui_status_connection_); |
| 102 } |
| 103 |
| 104 void InputUiController::CandidateClicked( |
| 105 int index, int button, int flags) { |
| 106 chromeos::NotifyCandidateClicked(ui_status_connection_, index, button, flags); |
| 107 } |
| 108 |
| 109 void InputUiController::CursorUp() { |
| 110 chromeos::NotifyCursorUp(ui_status_connection_); |
| 111 } |
| 112 |
| 113 void InputUiController::CursorDown() { |
| 114 chromeos::NotifyCursorDown(ui_status_connection_); |
| 115 } |
| 116 |
| 117 void InputUiController::PageUp() { |
| 118 chromeos::NotifyPageUp(ui_status_connection_); |
| 119 } |
| 120 |
| 121 void InputUiController::PageDown() { |
| 122 chromeos::NotifyPageDown(ui_status_connection_); |
| 123 } |
| 124 |
| 125 void InputUiController::OnHideAuxiliaryText( |
| 126 void* ui_controller) { |
| 127 InputUiController *self = static_cast<InputUiController*>(ui_controller); |
| 128 self->router_->OnHideAuxiliaryText(); |
| 129 } |
| 130 |
| 131 void InputUiController::OnHideLookupTable( |
| 132 void* ui_controller) { |
| 133 InputUiController *self = static_cast<InputUiController*>(ui_controller); |
| 134 self->router_->OnHideLookupTable(); |
| 135 } |
| 136 |
| 137 void InputUiController::OnSetCursorLocation( |
| 138 void* ui_controller, |
| 139 int x, int y, int width, int height) { |
| 140 InputUiController *self = static_cast<InputUiController*>(ui_controller); |
| 141 self->router_->OnSetCursorLocation(x, y, width, height); |
| 142 } |
| 143 |
| 144 void InputUiController::OnUpdateAuxiliaryText( |
| 145 void* ui_controller, |
| 146 const std::string& utf8_text, |
| 147 bool visible) { |
| 148 InputUiController *self = static_cast<InputUiController*>(ui_controller); |
| 149 if (!visible) { |
| 150 self->router_->OnHideAuxiliaryText(); |
| 151 } else { |
| 152 self->router_->OnUpdateAuxiliaryText(utf8_text); |
| 153 } |
| 154 } |
| 155 |
| 156 void InputUiController::OnUpdateLookupTable( |
| 157 void* ui_controller, |
| 158 const chromeos::InputMethodLookupTable& lookup_table) { |
| 159 InputUiController *self = static_cast<InputUiController*>(ui_controller); |
| 160 self->router_->OnUpdateLookupTable(lookup_table); |
| 161 } |
| 162 |
| 163 ExtensionInputUiEventRouter* |
| 164 ExtensionInputUiEventRouter::GetInstance() { |
| 165 return Singleton<ExtensionInputUiEventRouter>::get(); |
| 166 } |
| 167 |
| 168 ExtensionInputUiEventRouter::ExtensionInputUiEventRouter() |
| 169 : profile_(NULL), |
| 170 ui_controller_(NULL) { |
| 171 } |
| 172 |
| 173 ExtensionInputUiEventRouter::~ExtensionInputUiEventRouter() { |
| 174 } |
| 175 |
| 176 void ExtensionInputUiEventRouter::Init() { |
| 177 if (ui_controller_.get() == NULL) { |
| 178 ui_controller_.reset(new InputUiController(this)); |
| 179 } |
| 180 } |
| 181 |
| 182 void ExtensionInputUiEventRouter::Register( |
| 183 Profile* profile, const std::string& extension_id) { |
| 184 profile_ = profile; |
| 185 extension_id_ = extension_id; |
| 186 } |
| 187 |
| 188 void ExtensionInputUiEventRouter::CandidateClicked(Profile* profile, |
| 189 const std::string& extension_id, int index, int button) { |
| 190 if (profile_ != profile || extension_id_ != extension_id) { |
| 191 DLOG(WARNING) << "called from unregistered extension"; |
| 192 } |
| 193 ui_controller_->CandidateClicked(index, button, 0); |
| 194 } |
| 195 |
| 196 void ExtensionInputUiEventRouter::CursorUp(Profile* profile, |
| 197 const std::string& extension_id) { |
| 198 if (profile_ != profile || extension_id_ != extension_id) { |
| 199 DLOG(WARNING) << "called from unregistered extension"; |
| 200 } |
| 201 ui_controller_->CursorUp(); |
| 202 } |
| 203 |
| 204 void ExtensionInputUiEventRouter::CursorDown(Profile* profile, |
| 205 const std::string& extension_id) { |
| 206 if (profile_ != profile || extension_id_ != extension_id) { |
| 207 DLOG(WARNING) << "called from unregistered extension"; |
| 208 } |
| 209 ui_controller_->CursorDown(); |
| 210 } |
| 211 |
| 212 void ExtensionInputUiEventRouter::PageUp(Profile* profile, |
| 213 const std::string& extension_id) { |
| 214 if (profile_ != profile || extension_id_ != extension_id) { |
| 215 DLOG(WARNING) << "called from unregistered extension"; |
| 216 } |
| 217 ui_controller_->PageUp(); |
| 218 } |
| 219 |
| 220 void ExtensionInputUiEventRouter::PageDown(Profile* profile, |
| 221 const std::string& extension_id) { |
| 222 if (profile_ != profile || extension_id_ != extension_id) { |
| 223 DLOG(WARNING) << "called from unregistered extension"; |
| 224 } |
| 225 ui_controller_->PageDown(); |
| 226 } |
| 227 |
| 228 void ExtensionInputUiEventRouter::OnHideAuxiliaryText() { |
| 229 OnUpdateAuxiliaryText(""); |
| 230 } |
| 231 |
| 232 void ExtensionInputUiEventRouter::OnHideLookupTable() { |
| 233 if (profile_ == NULL || extension_id_.empty()) |
| 234 return; |
| 235 |
| 236 DictionaryValue* dict = new DictionaryValue(); |
| 237 dict->SetBoolean("visible", false); |
| 238 |
| 239 ListValue *candidates = new ListValue(); |
| 240 dict->Set("candidates", candidates); |
| 241 |
| 242 ListValue args; |
| 243 args.Append(dict); |
| 244 |
| 245 std::string json_args; |
| 246 base::JSONWriter::Write(&args, false, &json_args); |
| 247 profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
| 248 extension_id_, events::kOnUpdateLookupTable, json_args, profile_, GURL()); |
| 249 } |
| 250 |
| 251 void ExtensionInputUiEventRouter::OnSetCursorLocation( |
| 252 int x, int y, int width, int height) { |
| 253 |
| 254 if (profile_ == NULL || extension_id_.empty()) |
| 255 return; |
| 256 |
| 257 ListValue args; |
| 258 args.Append(Value::CreateIntegerValue(x)); |
| 259 args.Append(Value::CreateIntegerValue(y)); |
| 260 args.Append(Value::CreateIntegerValue(width)); |
| 261 args.Append(Value::CreateIntegerValue(height)); |
| 262 |
| 263 std::string json_args; |
| 264 base::JSONWriter::Write(&args, false, &json_args); |
| 265 profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
| 266 extension_id_, events::kOnSetCursorLocation, json_args, profile_, GURL()); |
| 267 } |
| 268 |
| 269 void ExtensionInputUiEventRouter::OnUpdateAuxiliaryText( |
| 270 const std::string& utf8_text) { |
| 271 if (profile_ == NULL || extension_id_.empty()) |
| 272 return; |
| 273 |
| 274 ListValue args; |
| 275 args.Append(Value::CreateStringValue(utf8_text)); |
| 276 |
| 277 std::string json_args; |
| 278 base::JSONWriter::Write(&args, false, &json_args); |
| 279 profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
| 280 extension_id_, events::kOnUpdateAuxiliaryText, json_args, profile_, GURL()); |
| 281 } |
| 282 |
| 283 void ExtensionInputUiEventRouter::OnUpdateLookupTable( |
| 284 const chromeos::InputMethodLookupTable& lookup_table) { |
| 285 if (profile_ == NULL || extension_id_.empty()) |
| 286 return; |
| 287 |
| 288 DictionaryValue* dict = new DictionaryValue(); |
| 289 dict->SetBoolean("visible", lookup_table.visible); |
| 290 |
| 291 if (lookup_table.visible) { |
| 292 } |
| 293 |
| 294 ListValue *candidates = new ListValue(); |
| 295 for (size_t i = 0; i < lookup_table.candidates.size(); i++) { |
| 296 candidates->Append(Value::CreateStringValue(lookup_table.candidates[i])); |
| 297 } |
| 298 |
| 299 dict->Set("candidates", candidates); |
| 300 |
| 301 ListValue args; |
| 302 args.Append(dict); |
| 303 |
| 304 std::string json_args; |
| 305 base::JSONWriter::Write(&args, false, &json_args); |
| 306 profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
| 307 extension_id_, events::kOnUpdateLookupTable, json_args, profile_, GURL()); |
| 308 } |
| 309 |
| 310 bool RegisterInputUiFunction::RunImpl() { |
| 311 ExtensionInputUiEventRouter::GetInstance()->Register( |
| 312 profile(), extension_id()); |
| 313 return true; |
| 314 } |
| 315 |
| 316 bool CandidateClickedInputUiFunction::RunImpl() { |
| 317 int index = 0; |
| 318 int button = 0; |
| 319 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &index)); |
| 320 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &button)); |
| 321 |
| 322 ExtensionInputUiEventRouter::GetInstance()->CandidateClicked( |
| 323 profile(), extension_id(), index, button); |
| 324 |
| 325 return true; |
| 326 } |
| 327 |
| 328 bool CursorUpInputUiFunction::RunImpl() { |
| 329 ExtensionInputUiEventRouter::GetInstance()->CursorUp( |
| 330 profile(), extension_id()); |
| 331 return true; |
| 332 } |
| 333 |
| 334 bool CursorDownInputUiFunction::RunImpl() { |
| 335 ExtensionInputUiEventRouter::GetInstance()->CursorDown( |
| 336 profile(), extension_id()); |
| 337 return true; |
| 338 } |
| 339 |
| 340 bool PageUpInputUiFunction::RunImpl() { |
| 341 ExtensionInputUiEventRouter::GetInstance()->PageUp( |
| 342 profile(), extension_id()); |
| 343 return true; |
| 344 } |
| 345 |
| 346 bool PageDownInputUiFunction::RunImpl() { |
| 347 ExtensionInputUiEventRouter::GetInstance()->PageDown( |
| 348 profile(), extension_id()); |
| 349 return true; |
| 350 } |
| 351 |
OLD | NEW |