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

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

Issue 1657593007: Implement chrome.input.ime.setComposition/commitText API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 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;
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 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 } 246 }
245 247
246 bool InputImeKeyEventHandledFunction::RunAsync() { 248 bool InputImeKeyEventHandledFunction::RunAsync() {
247 scoped_ptr<KeyEventHandled::Params> params( 249 scoped_ptr<KeyEventHandled::Params> params(
248 KeyEventHandled::Params::Create(*args_)); 250 KeyEventHandled::Params::Create(*args_));
249 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())) 251 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context()))
250 ->OnKeyEventHandled(extension_id(), params->request_id, params->response); 252 ->OnKeyEventHandled(extension_id(), params->request_id, params->response);
251 return true; 253 return true;
252 } 254 }
253 255
256 ExtensionFunction::ResponseAction InputImeSetCompositionFunction::Run() {
257 InputMethodEngineBase* engine =
258 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context()))
259 ->GetActiveEngine(extension_id());
260 base::DictionaryValue* output = new base::DictionaryValue();
261 if (!engine) {
262 output->SetBoolean("success", false);
263 } else {
264 scoped_ptr<SetComposition::Params> parent_params(
265 SetComposition::Params::Create(*args_));
266 const SetComposition::Params::Parameters& params =
267 parent_params->parameters;
268 std::vector<InputMethodEngineBase::SegmentInfo> segments;
269 if (params.segments) {
270 const std::vector<
271 linked_ptr<SetComposition::Params::Parameters::SegmentsType>>&
272 segments_args = *params.segments;
273 for (const auto& segments_arg : segments_args) {
274 EXTENSION_FUNCTION_VALIDATE(segments_arg->style !=
275 input_ime::UNDERLINE_STYLE_NONE);
276 InputMethodEngineBase::SegmentInfo segment_info;
277 segment_info.start = segments_arg->start;
278 segment_info.end = segments_arg->end;
279 if (segments_arg->style == input_ime::UNDERLINE_STYLE_UNDERLINE) {
280 segment_info.style = InputMethodEngineBase::SEGMENT_STYLE_UNDERLINE;
281 } else if (segments_arg->style ==
282 input_ime::UNDERLINE_STYLE_DOUBLEUNDERLINE) {
283 segment_info.style =
284 InputMethodEngineBase::SEGMENT_STYLE_DOUBLE_UNDERLINE;
285 } else {
286 segment_info.style =
287 InputMethodEngineBase::SEGMENT_STYLE_NO_UNDERLINE;
288 }
289 segments.push_back(segment_info);
290 }
291 }
292 int selection_start =
293 params.selection_start ? *params.selection_start : params.cursor;
294 int selection_end =
295 params.selection_end ? *params.selection_end : params.cursor;
296 output->SetBoolean(
297 "success", engine->SetComposition(
298 params.context_id, params.text.c_str(), selection_start,
299 selection_end, params.cursor, segments, &error_));
300 }
301 return RespondNow(OneArgument(output));
302 }
303
304 ExtensionFunction::ResponseAction InputImeCommitTextFunction::Run() {
305 base::DictionaryValue* output = new base::DictionaryValue();
306 InputMethodEngineBase* engine =
307 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context()))
308 ->GetActiveEngine(extension_id());
309
310 if (!engine) {
311 output->SetBoolean("success", false);
312 } else {
313 scoped_ptr<CommitText::Params> parent_params(
314 CommitText::Params::Create(*args_));
315 const CommitText::Params::Parameters& params = parent_params->parameters;
316 output->SetBoolean(
317 "success",
318 engine->CommitText(params.context_id, params.text.c_str(), &error_));
319 }
320 return RespondNow(OneArgument(output));
321 }
322
254 InputImeAPI::InputImeAPI(content::BrowserContext* context) 323 InputImeAPI::InputImeAPI(content::BrowserContext* context)
255 : browser_context_(context), extension_registry_observer_(this) { 324 : browser_context_(context), extension_registry_observer_(this) {
256 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); 325 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_));
257 326
258 EventRouter* event_router = EventRouter::Get(browser_context_); 327 EventRouter* event_router = EventRouter::Get(browser_context_);
259 event_router->RegisterObserver(this, input_ime::OnFocus::kEventName); 328 event_router->RegisterObserver(this, input_ime::OnFocus::kEventName);
260 } 329 }
261 330
262 InputImeAPI::~InputImeAPI() { 331 InputImeAPI::~InputImeAPI() {
263 EventRouter::Get(browser_context_)->UnregisterObserver(this); 332 EventRouter::Get(browser_context_)->UnregisterObserver(this);
264 } 333 }
265 334
266 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputImeAPI> > 335 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputImeAPI> >
267 g_factory = LAZY_INSTANCE_INITIALIZER; 336 g_factory = LAZY_INSTANCE_INITIALIZER;
268 337
269 // static 338 // static
270 BrowserContextKeyedAPIFactory<InputImeAPI>* InputImeAPI::GetFactoryInstance() { 339 BrowserContextKeyedAPIFactory<InputImeAPI>* InputImeAPI::GetFactoryInstance() {
271 return g_factory.Pointer(); 340 return g_factory.Pointer();
272 } 341 }
273 342
274 InputImeEventRouter* GetInputImeEventRouter(Profile* profile) { 343 InputImeEventRouter* GetInputImeEventRouter(Profile* profile) {
275 if (profile->HasOffTheRecordProfile()) 344 if (profile->HasOffTheRecordProfile())
276 profile = profile->GetOffTheRecordProfile(); 345 profile = profile->GetOffTheRecordProfile();
277 return extensions::InputImeEventRouterFactory::GetInstance()->GetRouter( 346 return extensions::InputImeEventRouterFactory::GetInstance()->GetRouter(
278 profile); 347 profile);
279 } 348 }
280 349
281 } // namespace extensions 350 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698