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; | 13 namespace SetComposition = extensions::api::input_ime::SetComposition; |
14 namespace CommitText = extensions::api::input_ime::CommitText; | 14 namespace CommitText = extensions::api::input_ime::CommitText; |
15 namespace SendKeyEvents = extensions::api::input_ime::SendKeyEvents; | |
15 using ui::IMEEngineHandlerInterface; | 16 using ui::IMEEngineHandlerInterface; |
16 using input_method::InputMethodEngineBase; | 17 using input_method::InputMethodEngineBase; |
17 | 18 |
19 namespace { | |
20 const char kErrorEngineNotAvailable[] = "Engine is not available"; | |
21 } | |
18 namespace ui { | 22 namespace ui { |
19 | 23 |
20 ImeObserver::ImeObserver(const std::string& extension_id, Profile* profile) | 24 ImeObserver::ImeObserver(const std::string& extension_id, Profile* profile) |
21 : extension_id_(extension_id), profile_(profile) {} | 25 : extension_id_(extension_id), profile_(profile) {} |
22 | 26 |
23 void ImeObserver::OnActivate(const std::string& component_id) { | 27 void ImeObserver::OnActivate(const std::string& component_id) { |
24 if (extension_id_.empty() || !HasListener(input_ime::OnActivate::kEventName)) | 28 if (extension_id_.empty() || !HasListener(input_ime::OnActivate::kEventName)) |
25 return; | 29 return; |
26 | 30 |
27 scoped_ptr<base::ListValue> args(input_ime::OnActivate::Create( | 31 scoped_ptr<base::ListValue> args(input_ime::OnActivate::Create( |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
322 scoped_ptr<CommitText::Params> parent_params( | 326 scoped_ptr<CommitText::Params> parent_params( |
323 CommitText::Params::Create(*args_)); | 327 CommitText::Params::Create(*args_)); |
324 const CommitText::Params::Parameters& params = parent_params->parameters; | 328 const CommitText::Params::Parameters& params = parent_params->parameters; |
325 success = | 329 success = |
326 engine->CommitText(params.context_id, params.text.c_str(), &error_); | 330 engine->CommitText(params.context_id, params.text.c_str(), &error_); |
327 } | 331 } |
328 scoped_ptr<base::ListValue> output = CommitText::Results::Create(success); | 332 scoped_ptr<base::ListValue> output = CommitText::Results::Create(success); |
329 return RespondNow(ArgumentList(std::move(output))); | 333 return RespondNow(ArgumentList(std::move(output))); |
330 } | 334 } |
331 | 335 |
336 ExtensionFunction::ResponseAction InputImeSendKeyEventsFunction::Run() { | |
337 InputImeEventRouter* event_router = | |
338 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())); | |
339 InputMethodEngineBase* engine = | |
340 event_router ? event_router->GetActiveEngine(extension_id()) : nullptr; | |
341 if (!engine) | |
342 return RespondNow(Error(kErrorEngineNotAvailable)); | |
343 | |
344 scoped_ptr<SendKeyEvents::Params> parent_params( | |
345 SendKeyEvents::Params::Create(*args_)); | |
346 const SendKeyEvents::Params::Parameters& params = parent_params->parameters; | |
347 const std::vector<linked_ptr<input_ime::KeyboardEvent>>& key_data = | |
348 params.key_data; | |
349 std::vector<InputMethodEngineBase::KeyboardEvent> key_data_out; | |
350 | |
351 for (size_t i = 0; i < key_data.size(); ++i) { | |
352 InputMethodEngineBase::KeyboardEvent event; | |
353 event.type = input_ime::ToString(key_data[i]->type); | |
354 event.key = key_data[i]->key; | |
355 event.code = key_data[i]->code; | |
356 event.key_code = key_data[i]->key_code.get() ? *(key_data[i]->key_code) : 0; | |
357 if (key_data[i]->alt_key) | |
358 event.alt_key = *(key_data[i]->alt_key); | |
359 if (key_data[i]->ctrl_key) | |
360 event.ctrl_key = *(key_data[i]->ctrl_key); | |
361 if (key_data[i]->shift_key) | |
362 event.shift_key = *(key_data[i]->shift_key); | |
363 if (key_data[i]->caps_lock) | |
364 event.caps_lock = *(key_data[i]->caps_lock); | |
365 key_data_out.push_back(event); | |
366 } | |
367 engine->SendKeyEvents(params.context_id, key_data_out); | |
Shu Chen
2016/03/08 05:59:44
Better to expose the errors.
Azure Wei
2016/03/08 11:42:29
Done.
| |
368 return RespondNow(NoArguments()); | |
369 } | |
370 | |
332 InputImeAPI::InputImeAPI(content::BrowserContext* context) | 371 InputImeAPI::InputImeAPI(content::BrowserContext* context) |
333 : browser_context_(context), extension_registry_observer_(this) { | 372 : browser_context_(context), extension_registry_observer_(this) { |
334 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); | 373 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
335 | 374 |
336 EventRouter* event_router = EventRouter::Get(browser_context_); | 375 EventRouter* event_router = EventRouter::Get(browser_context_); |
337 event_router->RegisterObserver(this, input_ime::OnFocus::kEventName); | 376 event_router->RegisterObserver(this, input_ime::OnFocus::kEventName); |
338 } | 377 } |
339 | 378 |
340 InputImeAPI::~InputImeAPI() { | 379 InputImeAPI::~InputImeAPI() { |
341 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 380 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
(...skipping 10 matching lines...) Expand all Loading... | |
352 InputImeEventRouter* GetInputImeEventRouter(Profile* profile) { | 391 InputImeEventRouter* GetInputImeEventRouter(Profile* profile) { |
353 if (!profile) | 392 if (!profile) |
354 return nullptr; | 393 return nullptr; |
355 if (profile->HasOffTheRecordProfile()) | 394 if (profile->HasOffTheRecordProfile()) |
356 profile = profile->GetOffTheRecordProfile(); | 395 profile = profile->GetOffTheRecordProfile(); |
357 return extensions::InputImeEventRouterFactory::GetInstance()->GetRouter( | 396 return extensions::InputImeEventRouterFactory::GetInstance()->GetRouter( |
358 profile); | 397 profile); |
359 } | 398 } |
360 | 399 |
361 } // namespace extensions | 400 } // namespace extensions |
OLD | NEW |