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(); | |
Shu Chen
2016/02/04 08:00:46
Please merge the below OnKeyEventHandled() call in
Azure Wei
2016/02/04 11:57:21
Done. Issue 1664513008 was created to move all the
| |
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 scoped_ptr<base::ListValue> output; | |
264 InputMethodEngineBase* engine = | |
265 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())) | |
266 ->GetActiveEngine(extension_id()); | |
267 if (!engine) { | |
268 output = SetComposition::Results::Create(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 = SetComposition::Results::Create(engine->SetComposition( | |
303 params.context_id, params.text.c_str(), selection_start, selection_end, | |
304 params.cursor, segments, &error_)); | |
305 } | |
306 return RespondNow(OneArgument(std::move(output))); | |
307 } | |
308 | |
309 ExtensionFunction::ResponseAction InputImeCommitTextFunction::Run() { | |
310 scoped_ptr<base::ListValue> output; | |
311 InputMethodEngineBase* engine = | |
312 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())) | |
313 ->GetActiveEngine(extension_id()); | |
314 if (!engine) { | |
315 output = CommitText::Results::Create(false); | |
316 } else { | |
317 scoped_ptr<CommitText::Params> parent_params( | |
318 CommitText::Params::Create(*args_)); | |
319 const CommitText::Params::Parameters& params = parent_params->parameters; | |
320 output = CommitText::Results::Create( | |
321 engine->CommitText(params.context_id, params.text.c_str(), &error_)); | |
322 } | |
323 return RespondNow(OneArgument(std::move(output))); | |
324 } | |
325 | |
254 InputImeAPI::InputImeAPI(content::BrowserContext* context) | 326 InputImeAPI::InputImeAPI(content::BrowserContext* context) |
255 : browser_context_(context), extension_registry_observer_(this) { | 327 : browser_context_(context), extension_registry_observer_(this) { |
256 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); | 328 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
257 | 329 |
258 EventRouter* event_router = EventRouter::Get(browser_context_); | 330 EventRouter* event_router = EventRouter::Get(browser_context_); |
259 event_router->RegisterObserver(this, input_ime::OnFocus::kEventName); | 331 event_router->RegisterObserver(this, input_ime::OnFocus::kEventName); |
260 } | 332 } |
261 | 333 |
262 InputImeAPI::~InputImeAPI() { | 334 InputImeAPI::~InputImeAPI() { |
263 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 335 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
264 } | 336 } |
265 | 337 |
266 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputImeAPI> > | 338 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputImeAPI> > |
267 g_factory = LAZY_INSTANCE_INITIALIZER; | 339 g_factory = LAZY_INSTANCE_INITIALIZER; |
268 | 340 |
269 // static | 341 // static |
270 BrowserContextKeyedAPIFactory<InputImeAPI>* InputImeAPI::GetFactoryInstance() { | 342 BrowserContextKeyedAPIFactory<InputImeAPI>* InputImeAPI::GetFactoryInstance() { |
271 return g_factory.Pointer(); | 343 return g_factory.Pointer(); |
272 } | 344 } |
273 | 345 |
274 InputImeEventRouter* GetInputImeEventRouter(Profile* profile) { | 346 InputImeEventRouter* GetInputImeEventRouter(Profile* profile) { |
275 if (profile->HasOffTheRecordProfile()) | 347 if (profile->HasOffTheRecordProfile()) |
276 profile = profile->GetOffTheRecordProfile(); | 348 profile = profile->GetOffTheRecordProfile(); |
277 return extensions::InputImeEventRouterFactory::GetInstance()->GetRouter( | 349 return extensions::InputImeEventRouterFactory::GetInstance()->GetRouter( |
278 profile); | 350 profile); |
279 } | 351 } |
280 | 352 |
281 } // namespace extensions | 353 } // namespace extensions |
OLD | NEW |