| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/input_ime/input_ime_api.h" | 5 #include "chrome/browser/extensions/api/input_ime/input_ime_api.h" |
| 6 | 6 |
| 7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chrome/browser/chromeos/input_method/input_method_engine.h" | 9 #include "chrome/browser/chromeos/input_method/input_method_engine.h" |
| 10 #include "chrome/browser/chromeos/login/lock/screen_locker.h" | 10 #include "chrome/browser/chromeos/login/lock/screen_locker.h" |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 | 105 |
| 106 void OnActivate(const std::string& component_id) override { | 106 void OnActivate(const std::string& component_id) override { |
| 107 if (extension_id_.empty() || | 107 if (extension_id_.empty() || |
| 108 !HasListener(input_ime::OnActivate::kEventName)) | 108 !HasListener(input_ime::OnActivate::kEventName)) |
| 109 return; | 109 return; |
| 110 | 110 |
| 111 scoped_ptr<base::ListValue> args(input_ime::OnActivate::Create( | 111 scoped_ptr<base::ListValue> args(input_ime::OnActivate::Create( |
| 112 component_id, | 112 component_id, |
| 113 input_ime::ParseScreenType(GetCurrentScreenType()))); | 113 input_ime::ParseScreenType(GetCurrentScreenType()))); |
| 114 | 114 |
| 115 DispatchEventToExtension(input_ime::OnActivate::kEventName, args.Pass()); | 115 DispatchEventToExtension(extensions::events::INPUT_IME_ON_ACTIVATE, |
| 116 input_ime::OnActivate::kEventName, args.Pass()); |
| 116 } | 117 } |
| 117 | 118 |
| 118 void OnDeactivated(const std::string& component_id) override { | 119 void OnDeactivated(const std::string& component_id) override { |
| 119 if (extension_id_.empty() || | 120 if (extension_id_.empty() || |
| 120 !HasListener(input_ime::OnDeactivated::kEventName)) | 121 !HasListener(input_ime::OnDeactivated::kEventName)) |
| 121 return; | 122 return; |
| 122 | 123 |
| 123 scoped_ptr<base::ListValue> args( | 124 scoped_ptr<base::ListValue> args( |
| 124 input_ime::OnDeactivated::Create(component_id)); | 125 input_ime::OnDeactivated::Create(component_id)); |
| 125 | 126 |
| 126 DispatchEventToExtension(input_ime::OnDeactivated::kEventName, args.Pass()); | 127 DispatchEventToExtension(extensions::events::INPUT_IME_ON_DEACTIVATED, |
| 128 input_ime::OnDeactivated::kEventName, args.Pass()); |
| 127 } | 129 } |
| 128 | 130 |
| 129 void OnFocus( | 131 void OnFocus( |
| 130 const InputMethodEngineInterface::InputContext& context) override { | 132 const InputMethodEngineInterface::InputContext& context) override { |
| 131 if (extension_id_.empty() || !HasListener(input_ime::OnFocus::kEventName)) | 133 if (extension_id_.empty() || !HasListener(input_ime::OnFocus::kEventName)) |
| 132 return; | 134 return; |
| 133 | 135 |
| 134 input_ime::InputContext context_value; | 136 input_ime::InputContext context_value; |
| 135 context_value.context_id = context.id; | 137 context_value.context_id = context.id; |
| 136 context_value.type = input_ime::ParseInputContextType(context.type); | 138 context_value.type = input_ime::ParseInputContextType(context.type); |
| 137 context_value.auto_correct = context.auto_correct; | 139 context_value.auto_correct = context.auto_correct; |
| 138 context_value.auto_complete = context.auto_complete; | 140 context_value.auto_complete = context.auto_complete; |
| 139 context_value.spell_check = context.spell_check; | 141 context_value.spell_check = context.spell_check; |
| 140 | 142 |
| 141 scoped_ptr<base::ListValue> args(input_ime::OnFocus::Create(context_value)); | 143 scoped_ptr<base::ListValue> args(input_ime::OnFocus::Create(context_value)); |
| 142 | 144 |
| 143 DispatchEventToExtension(input_ime::OnFocus::kEventName, args.Pass()); | 145 DispatchEventToExtension(extensions::events::INPUT_IME_ON_FOCUS, |
| 146 input_ime::OnFocus::kEventName, args.Pass()); |
| 144 } | 147 } |
| 145 | 148 |
| 146 void OnBlur(int context_id) override { | 149 void OnBlur(int context_id) override { |
| 147 if (extension_id_.empty() || !HasListener(input_ime::OnBlur::kEventName)) | 150 if (extension_id_.empty() || !HasListener(input_ime::OnBlur::kEventName)) |
| 148 return; | 151 return; |
| 149 | 152 |
| 150 scoped_ptr<base::ListValue> args(input_ime::OnBlur::Create(context_id)); | 153 scoped_ptr<base::ListValue> args(input_ime::OnBlur::Create(context_id)); |
| 151 | 154 |
| 152 DispatchEventToExtension(input_ime::OnBlur::kEventName, args.Pass()); | 155 DispatchEventToExtension(extensions::events::INPUT_IME_ON_BLUR, |
| 156 input_ime::OnBlur::kEventName, args.Pass()); |
| 153 } | 157 } |
| 154 | 158 |
| 155 void OnInputContextUpdate( | 159 void OnInputContextUpdate( |
| 156 const InputMethodEngineInterface::InputContext& context) override { | 160 const InputMethodEngineInterface::InputContext& context) override { |
| 157 if (extension_id_.empty() || | 161 if (extension_id_.empty() || |
| 158 !HasListener(input_ime::OnInputContextUpdate::kEventName)) | 162 !HasListener(input_ime::OnInputContextUpdate::kEventName)) |
| 159 return; | 163 return; |
| 160 | 164 |
| 161 input_ime::InputContext context_value; | 165 input_ime::InputContext context_value; |
| 162 context_value.context_id = context.id; | 166 context_value.context_id = context.id; |
| 163 context_value.type = input_ime::ParseInputContextType(context.type); | 167 context_value.type = input_ime::ParseInputContextType(context.type); |
| 164 | 168 |
| 165 scoped_ptr<base::ListValue> args( | 169 scoped_ptr<base::ListValue> args( |
| 166 input_ime::OnInputContextUpdate::Create(context_value)); | 170 input_ime::OnInputContextUpdate::Create(context_value)); |
| 167 | 171 |
| 168 DispatchEventToExtension(input_ime::OnInputContextUpdate::kEventName, | 172 DispatchEventToExtension( |
| 169 args.Pass()); | 173 extensions::events::INPUT_IME_ON_INPUT_CONTEXT_UPDATE, |
| 174 input_ime::OnInputContextUpdate::kEventName, args.Pass()); |
| 170 } | 175 } |
| 171 | 176 |
| 172 bool IsInterestedInKeyEvent() const override { | 177 bool IsInterestedInKeyEvent() const override { |
| 173 return ShouldForwardKeyEvent(); | 178 return ShouldForwardKeyEvent(); |
| 174 } | 179 } |
| 175 | 180 |
| 176 void OnKeyEvent(const std::string& component_id, | 181 void OnKeyEvent(const std::string& component_id, |
| 177 const InputMethodEngineInterface::KeyboardEvent& event, | 182 const InputMethodEngineInterface::KeyboardEvent& event, |
| 178 chromeos::input_method::KeyEventHandle* key_data) override { | 183 chromeos::input_method::KeyEventHandle* key_data) override { |
| 179 if (extension_id_.empty()) | 184 if (extension_id_.empty()) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 199 key_data_value.key = event.key; | 204 key_data_value.key = event.key; |
| 200 key_data_value.code = event.code; | 205 key_data_value.code = event.code; |
| 201 key_data_value.alt_key.reset(new bool(event.alt_key)); | 206 key_data_value.alt_key.reset(new bool(event.alt_key)); |
| 202 key_data_value.ctrl_key.reset(new bool(event.ctrl_key)); | 207 key_data_value.ctrl_key.reset(new bool(event.ctrl_key)); |
| 203 key_data_value.shift_key.reset(new bool(event.shift_key)); | 208 key_data_value.shift_key.reset(new bool(event.shift_key)); |
| 204 key_data_value.caps_lock.reset(new bool(event.caps_lock)); | 209 key_data_value.caps_lock.reset(new bool(event.caps_lock)); |
| 205 | 210 |
| 206 scoped_ptr<base::ListValue> args( | 211 scoped_ptr<base::ListValue> args( |
| 207 input_ime::OnKeyEvent::Create(component_id, key_data_value)); | 212 input_ime::OnKeyEvent::Create(component_id, key_data_value)); |
| 208 | 213 |
| 209 DispatchEventToExtension(input_ime::OnKeyEvent::kEventName, args.Pass()); | 214 DispatchEventToExtension(extensions::events::INPUT_IME_ON_KEY_EVENT, |
| 215 input_ime::OnKeyEvent::kEventName, args.Pass()); |
| 210 } | 216 } |
| 211 | 217 |
| 212 void OnCandidateClicked( | 218 void OnCandidateClicked( |
| 213 const std::string& component_id, | 219 const std::string& component_id, |
| 214 int candidate_id, | 220 int candidate_id, |
| 215 InputMethodEngineInterface::MouseButtonEvent button) override { | 221 InputMethodEngineInterface::MouseButtonEvent button) override { |
| 216 if (extension_id_.empty() || | 222 if (extension_id_.empty() || |
| 217 !HasListener(input_ime::OnCandidateClicked::kEventName)) | 223 !HasListener(input_ime::OnCandidateClicked::kEventName)) |
| 218 return; | 224 return; |
| 219 | 225 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 230 case InputMethodEngineInterface::MOUSE_BUTTON_LEFT: | 236 case InputMethodEngineInterface::MOUSE_BUTTON_LEFT: |
| 231 // Default to left. | 237 // Default to left. |
| 232 default: | 238 default: |
| 233 button_enum = input_ime::MOUSE_BUTTON_LEFT; | 239 button_enum = input_ime::MOUSE_BUTTON_LEFT; |
| 234 break; | 240 break; |
| 235 } | 241 } |
| 236 | 242 |
| 237 scoped_ptr<base::ListValue> args(input_ime::OnCandidateClicked::Create( | 243 scoped_ptr<base::ListValue> args(input_ime::OnCandidateClicked::Create( |
| 238 component_id, candidate_id, button_enum)); | 244 component_id, candidate_id, button_enum)); |
| 239 | 245 |
| 240 DispatchEventToExtension(input_ime::OnCandidateClicked::kEventName, | 246 DispatchEventToExtension(extensions::events::INPUT_IME_ON_CANDIDATE_CLICKED, |
| 247 input_ime::OnCandidateClicked::kEventName, |
| 241 args.Pass()); | 248 args.Pass()); |
| 242 } | 249 } |
| 243 | 250 |
| 244 void OnMenuItemActivated(const std::string& component_id, | 251 void OnMenuItemActivated(const std::string& component_id, |
| 245 const std::string& menu_id) override { | 252 const std::string& menu_id) override { |
| 246 if (extension_id_.empty() || | 253 if (extension_id_.empty() || |
| 247 !HasListener(input_ime::OnMenuItemActivated::kEventName)) | 254 !HasListener(input_ime::OnMenuItemActivated::kEventName)) |
| 248 return; | 255 return; |
| 249 | 256 |
| 250 scoped_ptr<base::ListValue> args( | 257 scoped_ptr<base::ListValue> args( |
| 251 input_ime::OnMenuItemActivated::Create(component_id, menu_id)); | 258 input_ime::OnMenuItemActivated::Create(component_id, menu_id)); |
| 252 | 259 |
| 253 DispatchEventToExtension(input_ime::OnMenuItemActivated::kEventName, | 260 DispatchEventToExtension( |
| 254 args.Pass()); | 261 extensions::events::INPUT_IME_ON_MENU_ITEM_ACTIVATED, |
| 262 input_ime::OnMenuItemActivated::kEventName, args.Pass()); |
| 255 } | 263 } |
| 256 | 264 |
| 257 void OnSurroundingTextChanged(const std::string& component_id, | 265 void OnSurroundingTextChanged(const std::string& component_id, |
| 258 const std::string& text, | 266 const std::string& text, |
| 259 int cursor_pos, | 267 int cursor_pos, |
| 260 int anchor_pos) override { | 268 int anchor_pos) override { |
| 261 if (extension_id_.empty() || | 269 if (extension_id_.empty() || |
| 262 !HasListener(input_ime::OnSurroundingTextChanged::kEventName)) | 270 !HasListener(input_ime::OnSurroundingTextChanged::kEventName)) |
| 263 return; | 271 return; |
| 264 | 272 |
| 265 input_ime::OnSurroundingTextChanged::SurroundingInfo info; | 273 input_ime::OnSurroundingTextChanged::SurroundingInfo info; |
| 266 info.text = text; | 274 info.text = text; |
| 267 info.focus = cursor_pos; | 275 info.focus = cursor_pos; |
| 268 info.anchor = anchor_pos; | 276 info.anchor = anchor_pos; |
| 269 scoped_ptr<base::ListValue> args( | 277 scoped_ptr<base::ListValue> args( |
| 270 input_ime::OnSurroundingTextChanged::Create(component_id, info)); | 278 input_ime::OnSurroundingTextChanged::Create(component_id, info)); |
| 271 | 279 |
| 272 DispatchEventToExtension(input_ime::OnSurroundingTextChanged::kEventName, | 280 DispatchEventToExtension( |
| 273 args.Pass()); | 281 extensions::events::INPUT_IME_ON_SURROUNDING_TEXT_CHANGED, |
| 282 input_ime::OnSurroundingTextChanged::kEventName, args.Pass()); |
| 274 } | 283 } |
| 275 | 284 |
| 276 void OnCompositionBoundsChanged( | 285 void OnCompositionBoundsChanged( |
| 277 const std::vector<gfx::Rect>& bounds) override { | 286 const std::vector<gfx::Rect>& bounds) override { |
| 278 if (extension_id_.empty() || | 287 if (extension_id_.empty() || |
| 279 !HasListener(kOnCompositionBoundsChangedEventName)) | 288 !HasListener(kOnCompositionBoundsChangedEventName)) |
| 280 return; | 289 return; |
| 281 | 290 |
| 282 // Note: this is a private API event. | 291 // Note: this is a private API event. |
| 283 base::ListValue* bounds_list = new base::ListValue(); | 292 base::ListValue* bounds_list = new base::ListValue(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 294 return; | 303 return; |
| 295 scoped_ptr<base::ListValue> args(new base::ListValue()); | 304 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 296 | 305 |
| 297 // The old extension code uses the first parameter to get the bounds of the | 306 // The old extension code uses the first parameter to get the bounds of the |
| 298 // first composition character, so for backward compatibility, add it here. | 307 // first composition character, so for backward compatibility, add it here. |
| 299 base::Value* first_value = NULL; | 308 base::Value* first_value = NULL; |
| 300 if (bounds_list->Get(0, &first_value)) | 309 if (bounds_list->Get(0, &first_value)) |
| 301 args->Append(first_value->DeepCopy()); | 310 args->Append(first_value->DeepCopy()); |
| 302 args->Append(bounds_list); | 311 args->Append(bounds_list); |
| 303 | 312 |
| 304 DispatchEventToExtension(kOnCompositionBoundsChangedEventName, args.Pass()); | 313 DispatchEventToExtension( |
| 314 extensions::events::INPUT_METHOD_PRIVATE_ON_COMPOSITION_BOUNDS_CHANGED, |
| 315 kOnCompositionBoundsChangedEventName, args.Pass()); |
| 305 } | 316 } |
| 306 | 317 |
| 307 void OnReset(const std::string& component_id) override { | 318 void OnReset(const std::string& component_id) override { |
| 308 if (extension_id_.empty() || !HasListener(input_ime::OnReset::kEventName)) | 319 if (extension_id_.empty() || !HasListener(input_ime::OnReset::kEventName)) |
| 309 return; | 320 return; |
| 310 | 321 |
| 311 scoped_ptr<base::ListValue> args(input_ime::OnReset::Create(component_id)); | 322 scoped_ptr<base::ListValue> args(input_ime::OnReset::Create(component_id)); |
| 312 | 323 |
| 313 DispatchEventToExtension(input_ime::OnReset::kEventName, args.Pass()); | 324 DispatchEventToExtension(extensions::events::INPUT_IME_ON_RESET, |
| 325 input_ime::OnReset::kEventName, args.Pass()); |
| 314 } | 326 } |
| 315 | 327 |
| 316 private: | 328 private: |
| 317 void DispatchEventToExtension(const std::string& event_name, | 329 void DispatchEventToExtension( |
| 318 scoped_ptr<base::ListValue> args) { | 330 extensions::events::HistogramValue histogram_value, |
| 331 const std::string& event_name, |
| 332 scoped_ptr<base::ListValue> args) { |
| 319 if (event_name != input_ime::OnActivate::kEventName) { | 333 if (event_name != input_ime::OnActivate::kEventName) { |
| 320 // For suspended IME extension (e.g. XKB extension), don't awake it by IME | 334 // For suspended IME extension (e.g. XKB extension), don't awake it by IME |
| 321 // events except onActivate. The IME extension should be awake by other | 335 // events except onActivate. The IME extension should be awake by other |
| 322 // events (e.g. runtime.onMessage) from its other pages. | 336 // events (e.g. runtime.onMessage) from its other pages. |
| 323 // This is to save memory for steady state Chrome OS on which the users | 337 // This is to save memory for steady state Chrome OS on which the users |
| 324 // don't want any IME features. | 338 // don't want any IME features. |
| 325 extensions::ExtensionSystem* extension_system = | 339 extensions::ExtensionSystem* extension_system = |
| 326 extensions::ExtensionSystem::Get(profile_); | 340 extensions::ExtensionSystem::Get(profile_); |
| 327 if (extension_system) { | 341 if (extension_system) { |
| 328 const extensions::Extension* extension = | 342 const extensions::Extension* extension = |
| 329 extension_system->extension_service()->GetExtensionById( | 343 extension_system->extension_service()->GetExtensionById( |
| 330 extension_id_, false /* include_disabled */); | 344 extension_id_, false /* include_disabled */); |
| 331 if (!extension) | 345 if (!extension) |
| 332 return; | 346 return; |
| 333 extensions::ProcessManager* process_manager = | 347 extensions::ProcessManager* process_manager = |
| 334 extensions::ProcessManager::Get(profile_); | 348 extensions::ProcessManager::Get(profile_); |
| 335 if (extensions::BackgroundInfo::HasBackgroundPage(extension) && | 349 if (extensions::BackgroundInfo::HasBackgroundPage(extension) && |
| 336 !process_manager->GetBackgroundHostForExtension(extension_id_)) { | 350 !process_manager->GetBackgroundHostForExtension(extension_id_)) { |
| 337 return; | 351 return; |
| 338 } | 352 } |
| 339 } | 353 } |
| 340 } | 354 } |
| 341 | 355 |
| 342 scoped_ptr<extensions::Event> event(new extensions::Event( | 356 scoped_ptr<extensions::Event> event( |
| 343 extensions::events::UNKNOWN, event_name, args.Pass())); | 357 new extensions::Event(histogram_value, event_name, args.Pass())); |
| 344 event->restrict_to_browser_context = profile_; | 358 event->restrict_to_browser_context = profile_; |
| 345 extensions::EventRouter::Get(profile_) | 359 extensions::EventRouter::Get(profile_) |
| 346 ->DispatchEventToExtension(extension_id_, event.Pass()); | 360 ->DispatchEventToExtension(extension_id_, event.Pass()); |
| 347 } | 361 } |
| 348 | 362 |
| 349 // Returns true if the extension is ready to accept key event, otherwise | 363 // Returns true if the extension is ready to accept key event, otherwise |
| 350 // returns false. | 364 // returns false. |
| 351 bool ShouldForwardKeyEvent() const { | 365 bool ShouldForwardKeyEvent() const { |
| 352 // Only forward key events to extension if there are non-lazy listeners | 366 // Only forward key events to extension if there are non-lazy listeners |
| 353 // for onKeyEvent. Because if something wrong with the lazy background | 367 // for onKeyEvent. Because if something wrong with the lazy background |
| (...skipping 583 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 937 InputMethodEngineInterface* engine = | 951 InputMethodEngineInterface* engine = |
| 938 GetInputImeEventRouter( | 952 GetInputImeEventRouter( |
| 939 Profile::FromBrowserContext(details.browser_context)) | 953 Profile::FromBrowserContext(details.browser_context)) |
| 940 ->GetActiveEngine(details.extension_id); | 954 ->GetActiveEngine(details.extension_id); |
| 941 // Notifies the IME extension for IME ready with onActivate/onFocus events. | 955 // Notifies the IME extension for IME ready with onActivate/onFocus events. |
| 942 if (engine) | 956 if (engine) |
| 943 engine->Enable(engine->GetActiveComponentId()); | 957 engine->Enable(engine->GetActiveComponentId()); |
| 944 } | 958 } |
| 945 | 959 |
| 946 } // namespace extensions | 960 } // namespace extensions |
| OLD | NEW |