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

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 patch conflict. 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_));
Devlin 2016/03/10 23:21:14 EXTENSION_FUNCTION_VALIDATE(parent_params);
Azure Wei 2016/03/11 07:29:01 Done.
347 const SendKeyEvents::Params::Parameters& params = parent_params->parameters;
348 const std::vector<linked_ptr<input_ime::KeyboardEvent>>& key_data =
349 params.key_data;
350 std::vector<InputMethodEngineBase::KeyboardEvent> key_data_out;
351
352 for (size_t i = 0; i < key_data.size(); ++i) {
Devlin 2016/03/10 23:21:14 const auto& key_event : key_data
Azure Wei 2016/03/11 07:29:01 Done.
353 InputMethodEngineBase::KeyboardEvent event;
354 event.type = input_ime::ToString(key_data[i]->type);
355 event.key = key_data[i]->key;
356 event.code = key_data[i]->code;
357 event.key_code = key_data[i]->key_code.get() ? *(key_data[i]->key_code) : 0;
358 if (key_data[i]->alt_key)
359 event.alt_key = *(key_data[i]->alt_key);
360 if (key_data[i]->ctrl_key)
361 event.ctrl_key = *(key_data[i]->ctrl_key);
362 if (key_data[i]->shift_key)
363 event.shift_key = *(key_data[i]->shift_key);
364 if (key_data[i]->caps_lock)
365 event.caps_lock = *(key_data[i]->caps_lock);
366 key_data_out.push_back(event);
367 }
368 if (!engine->SendKeyEvents(params.context_id, key_data_out))
369 return RespondNow(Error(kErrorSetKeyEventsFail));
370 return RespondNow(NoArguments());
371 }
372
332 InputImeAPI::InputImeAPI(content::BrowserContext* context) 373 InputImeAPI::InputImeAPI(content::BrowserContext* context)
333 : browser_context_(context), extension_registry_observer_(this) { 374 : browser_context_(context), extension_registry_observer_(this) {
334 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); 375 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_));
335 376
336 EventRouter* event_router = EventRouter::Get(browser_context_); 377 EventRouter* event_router = EventRouter::Get(browser_context_);
337 event_router->RegisterObserver(this, input_ime::OnFocus::kEventName); 378 event_router->RegisterObserver(this, input_ime::OnFocus::kEventName);
338 } 379 }
339 380
340 InputImeAPI::~InputImeAPI() { 381 InputImeAPI::~InputImeAPI() {
341 EventRouter::Get(browser_context_)->UnregisterObserver(this); 382 EventRouter::Get(browser_context_)->UnregisterObserver(this);
(...skipping 10 matching lines...) Expand all
352 InputImeEventRouter* GetInputImeEventRouter(Profile* profile) { 393 InputImeEventRouter* GetInputImeEventRouter(Profile* profile) {
353 if (!profile) 394 if (!profile)
354 return nullptr; 395 return nullptr;
355 if (profile->HasOffTheRecordProfile()) 396 if (profile->HasOffTheRecordProfile())
356 profile = profile->GetOffTheRecordProfile(); 397 profile = profile->GetOffTheRecordProfile();
357 return extensions::InputImeEventRouterFactory::GetInstance()->GetRouter( 398 return extensions::InputImeEventRouterFactory::GetInstance()->GetRouter(
358 profile); 399 profile);
359 } 400 }
360 401
361 } // namespace extensions 402 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698