Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(972)

Side by Side Diff: chrome/browser/extensions/api/input_ime/input_ime_api.cc

Issue 1771173002: Implement input.ime.sendKeyEvents API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix test failure. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 const char kErrorSetKeyEventsFail[] = "Could not send key events";
22 }
18 namespace ui { 23 namespace ui {
19 24
20 ImeObserver::ImeObserver(const std::string& extension_id, Profile* profile) 25 ImeObserver::ImeObserver(const std::string& extension_id, Profile* profile)
21 : extension_id_(extension_id), profile_(profile) {} 26 : extension_id_(extension_id), profile_(profile) {}
22 27
23 void ImeObserver::OnActivate(const std::string& component_id) { 28 void ImeObserver::OnActivate(const std::string& component_id) {
24 if (extension_id_.empty() || !HasListener(input_ime::OnActivate::kEventName)) 29 if (extension_id_.empty() || !HasListener(input_ime::OnActivate::kEventName))
25 return; 30 return;
26 31
27 scoped_ptr<base::ListValue> args(input_ime::OnActivate::Create( 32 scoped_ptr<base::ListValue> args(input_ime::OnActivate::Create(
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 scoped_ptr<CommitText::Params> parent_params( 327 scoped_ptr<CommitText::Params> parent_params(
323 CommitText::Params::Create(*args_)); 328 CommitText::Params::Create(*args_));
324 const CommitText::Params::Parameters& params = parent_params->parameters; 329 const CommitText::Params::Parameters& params = parent_params->parameters;
325 success = 330 success =
326 engine->CommitText(params.context_id, params.text.c_str(), &error_); 331 engine->CommitText(params.context_id, params.text.c_str(), &error_);
327 } 332 }
328 scoped_ptr<base::ListValue> output = CommitText::Results::Create(success); 333 scoped_ptr<base::ListValue> output = CommitText::Results::Create(success);
329 return RespondNow(ArgumentList(std::move(output))); 334 return RespondNow(ArgumentList(std::move(output)));
330 } 335 }
331 336
337 ExtensionFunction::ResponseAction InputImeSendKeyEventsFunction::Run() {
338 InputImeEventRouter* event_router =
339 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context()));
340 InputMethodEngineBase* engine =
341 event_router ? event_router->GetActiveEngine(extension_id()) : nullptr;
342 if (!engine)
343 return RespondNow(Error(kErrorEngineNotAvailable));
344
345 scoped_ptr<SendKeyEvents::Params> parent_params(
346 SendKeyEvents::Params::Create(*args_));
347 EXTENSION_FUNCTION_VALIDATE(parent_params);
348 const SendKeyEvents::Params::Parameters& params = parent_params->parameters;
349 const std::vector<linked_ptr<input_ime::KeyboardEvent>>& key_data =
350 params.key_data;
351 std::vector<InputMethodEngineBase::KeyboardEvent> key_data_out;
352
353 for (const auto& key_event : key_data) {
354 InputMethodEngineBase::KeyboardEvent event;
355 event.type = input_ime::ToString(key_event->type);
356 event.key = key_event->key;
357 event.code = key_event->code;
358 event.key_code = key_event->key_code.get() ? *(key_event->key_code) : 0;
359 event.alt_key = key_event->alt_key ? *(key_event->alt_key) : false;
360 event.ctrl_key = key_event->ctrl_key ? *(key_event->ctrl_key) : false;
361 event.shift_key = key_event->shift_key ? *(key_event->shift_key) : false;
362 event.caps_lock = key_event->caps_lock ? *(key_event->caps_lock) : false;
363 key_data_out.push_back(event);
364 }
365 if (!engine->SendKeyEvents(params.context_id, key_data_out))
366 return RespondNow(Error(kErrorSetKeyEventsFail));
367 return RespondNow(NoArguments());
368 }
369
332 InputImeAPI::InputImeAPI(content::BrowserContext* context) 370 InputImeAPI::InputImeAPI(content::BrowserContext* context)
333 : browser_context_(context), extension_registry_observer_(this) { 371 : browser_context_(context), extension_registry_observer_(this) {
334 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); 372 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_));
335 373
336 EventRouter* event_router = EventRouter::Get(browser_context_); 374 EventRouter* event_router = EventRouter::Get(browser_context_);
337 event_router->RegisterObserver(this, input_ime::OnFocus::kEventName); 375 event_router->RegisterObserver(this, input_ime::OnFocus::kEventName);
338 } 376 }
339 377
340 InputImeAPI::~InputImeAPI() { 378 InputImeAPI::~InputImeAPI() {
341 EventRouter::Get(browser_context_)->UnregisterObserver(this); 379 EventRouter::Get(browser_context_)->UnregisterObserver(this);
(...skipping 10 matching lines...) Expand all
352 InputImeEventRouter* GetInputImeEventRouter(Profile* profile) { 390 InputImeEventRouter* GetInputImeEventRouter(Profile* profile) {
353 if (!profile) 391 if (!profile)
354 return nullptr; 392 return nullptr;
355 if (profile->HasOffTheRecordProfile()) 393 if (profile->HasOffTheRecordProfile())
356 profile = profile->GetOffTheRecordProfile(); 394 profile = profile->GetOffTheRecordProfile();
357 return extensions::InputImeEventRouterFactory::GetInstance()->GetRouter( 395 return extensions::InputImeEventRouterFactory::GetInstance()->GetRouter(
358 profile); 396 profile);
359 } 397 }
360 398
361 } // namespace extensions 399 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698