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 28 matching lines...) Expand all Loading... | |
39 namespace CommitText = extensions::api::input_ime::CommitText; | 39 namespace CommitText = extensions::api::input_ime::CommitText; |
40 namespace ClearComposition = extensions::api::input_ime::ClearComposition; | 40 namespace ClearComposition = extensions::api::input_ime::ClearComposition; |
41 namespace SetComposition = extensions::api::input_ime::SetComposition; | 41 namespace SetComposition = extensions::api::input_ime::SetComposition; |
42 using chromeos::InputMethodEngineInterface; | 42 using chromeos::InputMethodEngineInterface; |
43 | 43 |
44 namespace { | 44 namespace { |
45 | 45 |
46 const char kErrorEngineNotAvailable[] = "Engine is not available"; | 46 const char kErrorEngineNotAvailable[] = "Engine is not available"; |
47 const char kErrorSetMenuItemsFail[] = "Could not create menu Items"; | 47 const char kErrorSetMenuItemsFail[] = "Could not create menu Items"; |
48 const char kErrorUpdateMenuItemsFail[] = "Could not update menu Items"; | 48 const char kErrorUpdateMenuItemsFail[] = "Could not update menu Items"; |
49 const char kOnCompositionBoundsChangedEventName[] = | |
50 "inputMethodPrivate.onCompositionBoundsChanged"; | |
49 | 51 |
50 void SetMenuItemToMenu(const input_ime::MenuItem& input, | 52 void SetMenuItemToMenu(const input_ime::MenuItem& input, |
51 InputMethodEngineInterface::MenuItem* out) { | 53 InputMethodEngineInterface::MenuItem* out) { |
52 out->modified = 0; | 54 out->modified = 0; |
53 out->id = input.id; | 55 out->id = input.id; |
54 if (input.label) { | 56 if (input.label) { |
55 out->modified |= InputMethodEngineInterface::MENU_ITEM_MODIFIED_LABEL; | 57 out->modified |= InputMethodEngineInterface::MENU_ITEM_MODIFIED_LABEL; |
56 out->label = *input.label; | 58 out->label = *input.label; |
57 } | 59 } |
58 | 60 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
98 | 100 |
99 namespace chromeos { | 101 namespace chromeos { |
100 class ImeObserver : public InputMethodEngineInterface::Observer { | 102 class ImeObserver : public InputMethodEngineInterface::Observer { |
101 public: | 103 public: |
102 explicit ImeObserver(const std::string& extension_id) | 104 explicit ImeObserver(const std::string& extension_id) |
103 : extension_id_(extension_id) {} | 105 : extension_id_(extension_id) {} |
104 | 106 |
105 virtual ~ImeObserver() {} | 107 virtual ~ImeObserver() {} |
106 | 108 |
107 virtual void OnActivate(const std::string& component_id) override { | 109 virtual void OnActivate(const std::string& component_id) override { |
108 if (extension_id_.empty()) | 110 if (extension_id_.empty() || !HasListener( |
Yuki
2014/10/24 09:52:33
nit: Better to break at greater level (shallower l
Shu Chen
2014/10/24 15:38:06
Done.
| |
111 input_ime::OnActivate::kEventName)) | |
109 return; | 112 return; |
110 | 113 |
111 scoped_ptr<base::ListValue> args(input_ime::OnActivate::Create( | 114 scoped_ptr<base::ListValue> args(input_ime::OnActivate::Create( |
112 component_id, | 115 component_id, |
113 input_ime::OnActivate::ParseScreen(GetCurrentScreenType()))); | 116 input_ime::OnActivate::ParseScreen(GetCurrentScreenType()))); |
114 | 117 |
115 DispatchEventToExtension( | 118 DispatchEventToExtension( |
116 extension_id_, input_ime::OnActivate::kEventName, args.Pass()); | 119 extension_id_, input_ime::OnActivate::kEventName, args.Pass()); |
117 } | 120 } |
118 | 121 |
119 virtual void OnDeactivated(const std::string& component_id) override { | 122 virtual void OnDeactivated(const std::string& component_id) override { |
120 if (extension_id_.empty()) | 123 if (extension_id_.empty() || !HasListener( |
124 input_ime::OnDeactivated::kEventName)) | |
121 return; | 125 return; |
122 | 126 |
123 scoped_ptr<base::ListValue> args( | 127 scoped_ptr<base::ListValue> args( |
124 input_ime::OnDeactivated::Create(component_id)); | 128 input_ime::OnDeactivated::Create(component_id)); |
125 | 129 |
126 DispatchEventToExtension( | 130 DispatchEventToExtension( |
127 extension_id_, input_ime::OnDeactivated::kEventName, args.Pass()); | 131 extension_id_, input_ime::OnDeactivated::kEventName, args.Pass()); |
128 } | 132 } |
129 | 133 |
130 virtual void OnFocus( | 134 virtual void OnFocus( |
131 const InputMethodEngineInterface::InputContext& context) override { | 135 const InputMethodEngineInterface::InputContext& context) override { |
132 if (extension_id_.empty()) | 136 if (extension_id_.empty() || !HasListener(input_ime::OnFocus::kEventName)) |
133 return; | 137 return; |
134 | 138 |
135 input_ime::InputContext context_value; | 139 input_ime::InputContext context_value; |
136 context_value.context_id = context.id; | 140 context_value.context_id = context.id; |
137 context_value.type = input_ime::InputContext::ParseType(context.type); | 141 context_value.type = input_ime::InputContext::ParseType(context.type); |
138 context_value.auto_correct = context.auto_correct; | 142 context_value.auto_correct = context.auto_correct; |
139 context_value.auto_complete = context.auto_complete; | 143 context_value.auto_complete = context.auto_complete; |
140 context_value.spell_check = context.spell_check; | 144 context_value.spell_check = context.spell_check; |
141 | 145 |
142 scoped_ptr<base::ListValue> args(input_ime::OnFocus::Create(context_value)); | 146 scoped_ptr<base::ListValue> args(input_ime::OnFocus::Create(context_value)); |
143 | 147 |
144 DispatchEventToExtension( | 148 DispatchEventToExtension( |
145 extension_id_, input_ime::OnFocus::kEventName, args.Pass()); | 149 extension_id_, input_ime::OnFocus::kEventName, args.Pass()); |
146 } | 150 } |
147 | 151 |
148 virtual void OnBlur(int context_id) override { | 152 virtual void OnBlur(int context_id) override { |
149 if (extension_id_.empty()) | 153 if (extension_id_.empty() || !HasListener(input_ime::OnBlur::kEventName)) |
150 return; | 154 return; |
151 | 155 |
152 scoped_ptr<base::ListValue> args(input_ime::OnBlur::Create(context_id)); | 156 scoped_ptr<base::ListValue> args(input_ime::OnBlur::Create(context_id)); |
153 | 157 |
154 DispatchEventToExtension( | 158 DispatchEventToExtension( |
155 extension_id_, input_ime::OnBlur::kEventName, args.Pass()); | 159 extension_id_, input_ime::OnBlur::kEventName, args.Pass()); |
156 } | 160 } |
157 | 161 |
158 virtual void OnInputContextUpdate( | 162 virtual void OnInputContextUpdate( |
159 const InputMethodEngineInterface::InputContext& context) override { | 163 const InputMethodEngineInterface::InputContext& context) override { |
160 if (extension_id_.empty()) | 164 if (extension_id_.empty() || !HasListener( |
165 input_ime::OnInputContextUpdate::kEventName)) | |
161 return; | 166 return; |
162 | 167 |
163 input_ime::InputContext context_value; | 168 input_ime::InputContext context_value; |
164 context_value.context_id = context.id; | 169 context_value.context_id = context.id; |
165 context_value.type = input_ime::InputContext::ParseType(context.type); | 170 context_value.type = input_ime::InputContext::ParseType(context.type); |
166 | 171 |
167 scoped_ptr<base::ListValue> args( | 172 scoped_ptr<base::ListValue> args( |
168 input_ime::OnInputContextUpdate::Create(context_value)); | 173 input_ime::OnInputContextUpdate::Create(context_value)); |
169 | 174 |
170 DispatchEventToExtension(extension_id_, | 175 DispatchEventToExtension(extension_id_, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
210 input_ime::OnKeyEvent::Create(component_id, key_data_value)); | 215 input_ime::OnKeyEvent::Create(component_id, key_data_value)); |
211 | 216 |
212 DispatchEventToExtension( | 217 DispatchEventToExtension( |
213 extension_id_, input_ime::OnKeyEvent::kEventName, args.Pass()); | 218 extension_id_, input_ime::OnKeyEvent::kEventName, args.Pass()); |
214 } | 219 } |
215 | 220 |
216 virtual void OnCandidateClicked( | 221 virtual void OnCandidateClicked( |
217 const std::string& component_id, | 222 const std::string& component_id, |
218 int candidate_id, | 223 int candidate_id, |
219 InputMethodEngineInterface::MouseButtonEvent button) override { | 224 InputMethodEngineInterface::MouseButtonEvent button) override { |
220 if (extension_id_.empty()) | 225 if (extension_id_.empty() || !HasListener( |
226 input_ime::OnCandidateClicked::kEventName)) | |
221 return; | 227 return; |
222 | 228 |
223 input_ime::OnCandidateClicked::Button button_enum = | 229 input_ime::OnCandidateClicked::Button button_enum = |
224 input_ime::OnCandidateClicked::BUTTON_NONE; | 230 input_ime::OnCandidateClicked::BUTTON_NONE; |
225 switch (button) { | 231 switch (button) { |
226 case InputMethodEngineInterface::MOUSE_BUTTON_MIDDLE: | 232 case InputMethodEngineInterface::MOUSE_BUTTON_MIDDLE: |
227 button_enum = input_ime::OnCandidateClicked::BUTTON_MIDDLE; | 233 button_enum = input_ime::OnCandidateClicked::BUTTON_MIDDLE; |
228 break; | 234 break; |
229 | 235 |
230 case InputMethodEngineInterface::MOUSE_BUTTON_RIGHT: | 236 case InputMethodEngineInterface::MOUSE_BUTTON_RIGHT: |
231 button_enum = input_ime::OnCandidateClicked::BUTTON_RIGHT; | 237 button_enum = input_ime::OnCandidateClicked::BUTTON_RIGHT; |
232 break; | 238 break; |
233 | 239 |
234 case InputMethodEngineInterface::MOUSE_BUTTON_LEFT: | 240 case InputMethodEngineInterface::MOUSE_BUTTON_LEFT: |
235 // Default to left. | 241 // Default to left. |
236 default: | 242 default: |
237 button_enum = input_ime::OnCandidateClicked::BUTTON_LEFT; | 243 button_enum = input_ime::OnCandidateClicked::BUTTON_LEFT; |
238 break; | 244 break; |
239 } | 245 } |
240 | 246 |
241 scoped_ptr<base::ListValue> args(input_ime::OnCandidateClicked::Create( | 247 scoped_ptr<base::ListValue> args(input_ime::OnCandidateClicked::Create( |
242 component_id, candidate_id, button_enum)); | 248 component_id, candidate_id, button_enum)); |
243 | 249 |
244 DispatchEventToExtension( | 250 DispatchEventToExtension( |
245 extension_id_, input_ime::OnCandidateClicked::kEventName, args.Pass()); | 251 extension_id_, input_ime::OnCandidateClicked::kEventName, args.Pass()); |
246 } | 252 } |
247 | 253 |
248 virtual void OnMenuItemActivated(const std::string& component_id, | 254 virtual void OnMenuItemActivated(const std::string& component_id, |
249 const std::string& menu_id) override { | 255 const std::string& menu_id) override { |
250 if (extension_id_.empty()) | 256 if (extension_id_.empty() || !HasListener( |
257 input_ime::OnMenuItemActivated::kEventName)) | |
251 return; | 258 return; |
252 | 259 |
253 scoped_ptr<base::ListValue> args( | 260 scoped_ptr<base::ListValue> args( |
254 input_ime::OnMenuItemActivated::Create(component_id, menu_id)); | 261 input_ime::OnMenuItemActivated::Create(component_id, menu_id)); |
255 | 262 |
256 DispatchEventToExtension( | 263 DispatchEventToExtension( |
257 extension_id_, input_ime::OnMenuItemActivated::kEventName, args.Pass()); | 264 extension_id_, input_ime::OnMenuItemActivated::kEventName, args.Pass()); |
258 } | 265 } |
259 | 266 |
260 virtual void OnSurroundingTextChanged(const std::string& component_id, | 267 virtual void OnSurroundingTextChanged(const std::string& component_id, |
261 const std::string& text, | 268 const std::string& text, |
262 int cursor_pos, | 269 int cursor_pos, |
263 int anchor_pos) override { | 270 int anchor_pos) override { |
264 if (extension_id_.empty()) | 271 if (extension_id_.empty() || !HasListener( |
272 input_ime::OnSurroundingTextChanged::kEventName)) | |
265 return; | 273 return; |
266 | 274 |
267 input_ime::OnSurroundingTextChanged::SurroundingInfo info; | 275 input_ime::OnSurroundingTextChanged::SurroundingInfo info; |
268 info.text = text; | 276 info.text = text; |
269 info.focus = cursor_pos; | 277 info.focus = cursor_pos; |
270 info.anchor = anchor_pos; | 278 info.anchor = anchor_pos; |
271 scoped_ptr<base::ListValue> args( | 279 scoped_ptr<base::ListValue> args( |
272 input_ime::OnSurroundingTextChanged::Create(component_id, info)); | 280 input_ime::OnSurroundingTextChanged::Create(component_id, info)); |
273 | 281 |
274 DispatchEventToExtension(extension_id_, | 282 DispatchEventToExtension(extension_id_, |
275 input_ime::OnSurroundingTextChanged::kEventName, | 283 input_ime::OnSurroundingTextChanged::kEventName, |
276 args.Pass()); | 284 args.Pass()); |
277 } | 285 } |
278 | 286 |
287 virtual void OnCompositionBoundsChanged(const gfx::Rect& bounds) override { | |
288 if (extension_id_.empty() || !HasListener( | |
289 kOnCompositionBoundsChangedEventName)) | |
290 return; | |
291 | |
292 // Note: this is a private API event. | |
293 scoped_ptr<base::ListValue> args(new base::ListValue()); | |
294 base::DictionaryValue* bounds_value = new base::DictionaryValue(); | |
295 bounds_value->SetInteger("x", bounds.x()); | |
296 bounds_value->SetInteger("y", bounds.y()); | |
297 bounds_value->SetInteger("w", bounds.width()); | |
298 bounds_value->SetInteger("h", bounds.height()); | |
299 args->Append(bounds_value); | |
300 | |
301 DispatchEventToExtension(extension_id_, | |
302 kOnCompositionBoundsChangedEventName, | |
303 args.Pass()); | |
304 } | |
305 | |
279 virtual void OnReset(const std::string& component_id) override { | 306 virtual void OnReset(const std::string& component_id) override { |
280 if (extension_id_.empty()) | 307 if (extension_id_.empty() || !HasListener(input_ime::OnReset::kEventName)) |
281 return; | 308 return; |
282 | 309 |
283 scoped_ptr<base::ListValue> args(input_ime::OnReset::Create(component_id)); | 310 scoped_ptr<base::ListValue> args(input_ime::OnReset::Create(component_id)); |
284 | 311 |
285 DispatchEventToExtension( | 312 DispatchEventToExtension( |
286 extension_id_, input_ime::OnReset::kEventName, args.Pass()); | 313 extension_id_, input_ime::OnReset::kEventName, args.Pass()); |
287 } | 314 } |
288 | 315 |
289 private: | 316 private: |
290 // Returns true if the extension is ready to accept key event, otherwise | 317 // Returns true if the extension is ready to accept key event, otherwise |
(...skipping 11 matching lines...) Expand all Loading... | |
302 input_ime::OnKeyEvent::kEventName); | 329 input_ime::OnKeyEvent::kEventName); |
303 for (extensions::EventListenerMap::ListenerList::const_iterator it = | 330 for (extensions::EventListenerMap::ListenerList::const_iterator it = |
304 listener_list.begin(); | 331 listener_list.begin(); |
305 it != listener_list.end(); ++it) { | 332 it != listener_list.end(); ++it) { |
306 if ((*it)->extension_id() == extension_id_ && !(*it)->IsLazy()) | 333 if ((*it)->extension_id() == extension_id_ && !(*it)->IsLazy()) |
307 return true; | 334 return true; |
308 } | 335 } |
309 return false; | 336 return false; |
310 } | 337 } |
311 | 338 |
339 bool HasListener(const std::string& event_name) const { | |
340 return extensions::EventRouter::Get( | |
341 ProfileManager::GetActiveUserProfile())->HasEventListener(event_name); | |
342 } | |
343 | |
312 // The component IME extensions need to know the current screen type (e.g. | 344 // The component IME extensions need to know the current screen type (e.g. |
313 // lock screen, login screen, etc.) so that its on-screen keyboard page | 345 // lock screen, login screen, etc.) so that its on-screen keyboard page |
314 // won't open new windows/pages. See crbug.com/395621. | 346 // won't open new windows/pages. See crbug.com/395621. |
315 std::string GetCurrentScreenType() { | 347 std::string GetCurrentScreenType() { |
316 switch (chromeos::input_method::InputMethodManager::Get() | 348 switch (chromeos::input_method::InputMethodManager::Get() |
317 ->GetUISessionState()) { | 349 ->GetUISessionState()) { |
318 case chromeos::input_method::InputMethodManager::STATE_LOGIN_SCREEN: | 350 case chromeos::input_method::InputMethodManager::STATE_LOGIN_SCREEN: |
319 return "login"; | 351 return "login"; |
320 case chromeos::input_method::InputMethodManager::STATE_LOCK_SCREEN: | 352 case chromeos::input_method::InputMethodManager::STATE_LOCK_SCREEN: |
321 return "lock"; | 353 return "lock"; |
(...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
859 // Notifies the IME extension for IME ready with onActivate/onFocus events. | 891 // Notifies the IME extension for IME ready with onActivate/onFocus events. |
860 if (engine) | 892 if (engine) |
861 engine->Enable(engine->GetActiveComponentId()); | 893 engine->Enable(engine->GetActiveComponentId()); |
862 } | 894 } |
863 | 895 |
864 InputImeEventRouter* InputImeAPI::input_ime_event_router() { | 896 InputImeEventRouter* InputImeAPI::input_ime_event_router() { |
865 return InputImeEventRouter::GetInstance(); | 897 return InputImeEventRouter::GetInstance(); |
866 } | 898 } |
867 | 899 |
868 } // namespace extensions | 900 } // namespace extensions |
OLD | NEW |