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/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "chrome/common/extensions/api/input_ime.h" | 8 #include "chrome/common/extensions/api/input_ime.h" |
9 #include "extensions/browser/extension_registry.h" | 9 #include "extensions/browser/extension_registry.h" |
10 | 10 |
11 namespace input_ime = extensions::api::input_ime; | 11 namespace input_ime = extensions::api::input_ime; |
12 namespace KeyEventHandled = extensions::api::input_ime::KeyEventHandled; | 12 namespace KeyEventHandled = extensions::api::input_ime::KeyEventHandled; |
13 namespace SetComposition = extensions::api::input_ime::SetComposition; | |
14 namespace CommitText = extensions::api::input_ime::CommitText; | |
13 using ui::IMEEngineHandlerInterface; | 15 using ui::IMEEngineHandlerInterface; |
14 using input_method::InputMethodEngineBase; | 16 using input_method::InputMethodEngineBase; |
15 | 17 |
16 namespace ui { | 18 namespace ui { |
17 | 19 |
18 ImeObserver::ImeObserver(const std::string& extension_id, Profile* profile) | 20 ImeObserver::ImeObserver(const std::string& extension_id, Profile* profile) |
19 : extension_id_(extension_id), profile_(profile) {} | 21 : extension_id_(extension_id), profile_(profile) {} |
20 | 22 |
21 void ImeObserver::OnActivate(const std::string& component_id) { | 23 void ImeObserver::OnActivate(const std::string& component_id) { |
22 if (extension_id_.empty() || !HasListener(input_ime::OnActivate::kEventName)) | 24 if (extension_id_.empty() || !HasListener(input_ime::OnActivate::kEventName)) |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
237 InputImeEventRouter* InputImeEventRouterFactory::GetRouter(Profile* profile) { | 239 InputImeEventRouter* InputImeEventRouterFactory::GetRouter(Profile* profile) { |
238 InputImeEventRouter* router = router_map_[profile]; | 240 InputImeEventRouter* router = router_map_[profile]; |
239 if (!router) { | 241 if (!router) { |
240 router = new InputImeEventRouter(profile); | 242 router = new InputImeEventRouter(profile); |
241 router_map_[profile] = router; | 243 router_map_[profile] = router; |
242 } | 244 } |
243 return router; | 245 return router; |
244 } | 246 } |
245 | 247 |
246 bool InputImeKeyEventHandledFunction::RunAsync() { | 248 bool InputImeKeyEventHandledFunction::RunAsync() { |
249 InputMethodEngineBase* engine = | |
250 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())) | |
251 ->GetActiveEngine(extension_id()); | |
252 if (engine) | |
253 engine->KeyEventHandled(); | |
254 | |
247 scoped_ptr<KeyEventHandled::Params> params( | 255 scoped_ptr<KeyEventHandled::Params> params( |
248 KeyEventHandled::Params::Create(*args_)); | 256 KeyEventHandled::Params::Create(*args_)); |
249 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())) | 257 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())) |
250 ->OnKeyEventHandled(extension_id(), params->request_id, params->response); | 258 ->OnKeyEventHandled(extension_id(), params->request_id, params->response); |
251 return true; | 259 return true; |
252 } | 260 } |
253 | 261 |
262 ExtensionFunction::ResponseAction InputImeSetCompositionFunction::Run() { | |
263 InputMethodEngineBase* engine = | |
264 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())) | |
265 ->GetActiveEngine(extension_id()); | |
266 base::DictionaryValue* output = new base::DictionaryValue(); | |
Devlin
2016/02/04 00:09:17
prefer scoped ptr generally, but also we should ju
Azure Wei
2016/02/04 03:21:19
Done.
| |
267 if (!engine) { | |
268 output->SetBoolean("success", false); | |
269 } else { | |
270 scoped_ptr<SetComposition::Params> parent_params( | |
271 SetComposition::Params::Create(*args_)); | |
272 const SetComposition::Params::Parameters& params = | |
273 parent_params->parameters; | |
274 std::vector<InputMethodEngineBase::SegmentInfo> segments; | |
275 if (params.segments) { | |
276 const std::vector< | |
277 linked_ptr<SetComposition::Params::Parameters::SegmentsType>>& | |
278 segments_args = *params.segments; | |
279 for (const auto& segments_arg : segments_args) { | |
280 EXTENSION_FUNCTION_VALIDATE(segments_arg->style != | |
281 input_ime::UNDERLINE_STYLE_NONE); | |
282 InputMethodEngineBase::SegmentInfo segment_info; | |
283 segment_info.start = segments_arg->start; | |
284 segment_info.end = segments_arg->end; | |
285 if (segments_arg->style == input_ime::UNDERLINE_STYLE_UNDERLINE) { | |
286 segment_info.style = InputMethodEngineBase::SEGMENT_STYLE_UNDERLINE; | |
287 } else if (segments_arg->style == | |
288 input_ime::UNDERLINE_STYLE_DOUBLEUNDERLINE) { | |
289 segment_info.style = | |
290 InputMethodEngineBase::SEGMENT_STYLE_DOUBLE_UNDERLINE; | |
291 } else { | |
292 segment_info.style = | |
293 InputMethodEngineBase::SEGMENT_STYLE_NO_UNDERLINE; | |
294 } | |
295 segments.push_back(segment_info); | |
296 } | |
297 } | |
298 int selection_start = | |
299 params.selection_start ? *params.selection_start : params.cursor; | |
300 int selection_end = | |
301 params.selection_end ? *params.selection_end : params.cursor; | |
302 output->SetBoolean( | |
303 "success", engine->SetComposition( | |
304 params.context_id, params.text.c_str(), selection_start, | |
305 selection_end, params.cursor, segments, &error_)); | |
306 } | |
307 return RespondNow(OneArgument(output)); | |
308 } | |
309 | |
310 ExtensionFunction::ResponseAction InputImeCommitTextFunction::Run() { | |
311 base::DictionaryValue* output = new base::DictionaryValue(); | |
312 InputMethodEngineBase* engine = | |
313 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())) | |
314 ->GetActiveEngine(extension_id()); | |
315 | |
316 if (!engine) { | |
317 output->SetBoolean("success", false); | |
318 } else { | |
319 scoped_ptr<CommitText::Params> parent_params( | |
320 CommitText::Params::Create(*args_)); | |
321 const CommitText::Params::Parameters& params = parent_params->parameters; | |
322 output->SetBoolean( | |
323 "success", | |
324 engine->CommitText(params.context_id, params.text.c_str(), &error_)); | |
325 } | |
326 return RespondNow(OneArgument(output)); | |
327 } | |
328 | |
254 InputImeAPI::InputImeAPI(content::BrowserContext* context) | 329 InputImeAPI::InputImeAPI(content::BrowserContext* context) |
255 : browser_context_(context), extension_registry_observer_(this) { | 330 : browser_context_(context), extension_registry_observer_(this) { |
256 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); | 331 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
257 | 332 |
258 EventRouter* event_router = EventRouter::Get(browser_context_); | 333 EventRouter* event_router = EventRouter::Get(browser_context_); |
259 event_router->RegisterObserver(this, input_ime::OnFocus::kEventName); | 334 event_router->RegisterObserver(this, input_ime::OnFocus::kEventName); |
260 } | 335 } |
261 | 336 |
262 InputImeAPI::~InputImeAPI() { | 337 InputImeAPI::~InputImeAPI() { |
263 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 338 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
264 } | 339 } |
265 | 340 |
266 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputImeAPI> > | 341 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputImeAPI> > |
267 g_factory = LAZY_INSTANCE_INITIALIZER; | 342 g_factory = LAZY_INSTANCE_INITIALIZER; |
268 | 343 |
269 // static | 344 // static |
270 BrowserContextKeyedAPIFactory<InputImeAPI>* InputImeAPI::GetFactoryInstance() { | 345 BrowserContextKeyedAPIFactory<InputImeAPI>* InputImeAPI::GetFactoryInstance() { |
271 return g_factory.Pointer(); | 346 return g_factory.Pointer(); |
272 } | 347 } |
273 | 348 |
274 InputImeEventRouter* GetInputImeEventRouter(Profile* profile) { | 349 InputImeEventRouter* GetInputImeEventRouter(Profile* profile) { |
275 if (profile->HasOffTheRecordProfile()) | 350 if (profile->HasOffTheRecordProfile()) |
276 profile = profile->GetOffTheRecordProfile(); | 351 profile = profile->GetOffTheRecordProfile(); |
277 return extensions::InputImeEventRouterFactory::GetInstance()->GetRouter( | 352 return extensions::InputImeEventRouterFactory::GetInstance()->GetRouter( |
278 profile); | 353 profile); |
279 } | 354 } |
280 | 355 |
281 } // namespace extensions | 356 } // namespace extensions |
OLD | NEW |