| 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/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
| 9 #include "chrome/common/extensions/api/input_ime.h" | 9 #include "chrome/common/extensions/api/input_ime.h" |
| 10 #include "content/public/browser/notification_registrar.h" | 10 #include "content/public/browser/notification_registrar.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 } | 25 } |
| 26 namespace ui { | 26 namespace ui { |
| 27 | 27 |
| 28 ImeObserver::ImeObserver(const std::string& extension_id, Profile* profile) | 28 ImeObserver::ImeObserver(const std::string& extension_id, Profile* profile) |
| 29 : extension_id_(extension_id), profile_(profile) {} | 29 : extension_id_(extension_id), profile_(profile) {} |
| 30 | 30 |
| 31 void ImeObserver::OnActivate(const std::string& component_id) { | 31 void ImeObserver::OnActivate(const std::string& component_id) { |
| 32 if (extension_id_.empty() || !HasListener(input_ime::OnActivate::kEventName)) | 32 if (extension_id_.empty() || !HasListener(input_ime::OnActivate::kEventName)) |
| 33 return; | 33 return; |
| 34 | 34 |
| 35 scoped_ptr<base::ListValue> args(input_ime::OnActivate::Create( | 35 std::unique_ptr<base::ListValue> args(input_ime::OnActivate::Create( |
| 36 component_id, | 36 component_id, input_ime::ParseScreenType(GetCurrentScreenType()))); |
| 37 input_ime::ParseScreenType(GetCurrentScreenType()))); | |
| 38 | 37 |
| 39 DispatchEventToExtension(extensions::events::INPUT_IME_ON_ACTIVATE, | 38 DispatchEventToExtension(extensions::events::INPUT_IME_ON_ACTIVATE, |
| 40 input_ime::OnActivate::kEventName, | 39 input_ime::OnActivate::kEventName, |
| 41 std::move(args)); | 40 std::move(args)); |
| 42 } | 41 } |
| 43 | 42 |
| 44 void ImeObserver::OnFocus( | 43 void ImeObserver::OnFocus( |
| 45 const IMEEngineHandlerInterface::InputContext& context) { | 44 const IMEEngineHandlerInterface::InputContext& context) { |
| 46 if (extension_id_.empty() || !HasListener(input_ime::OnFocus::kEventName)) | 45 if (extension_id_.empty() || !HasListener(input_ime::OnFocus::kEventName)) |
| 47 return; | 46 return; |
| 48 | 47 |
| 49 input_ime::InputContext context_value; | 48 input_ime::InputContext context_value; |
| 50 context_value.context_id = context.id; | 49 context_value.context_id = context.id; |
| 51 context_value.type = | 50 context_value.type = |
| 52 input_ime::ParseInputContextType(ConvertInputContextType(context)); | 51 input_ime::ParseInputContextType(ConvertInputContextType(context)); |
| 53 context_value.auto_correct = ConvertInputContextAutoCorrect(context); | 52 context_value.auto_correct = ConvertInputContextAutoCorrect(context); |
| 54 context_value.auto_complete = ConvertInputContextAutoComplete(context); | 53 context_value.auto_complete = ConvertInputContextAutoComplete(context); |
| 55 context_value.spell_check = ConvertInputContextSpellCheck(context); | 54 context_value.spell_check = ConvertInputContextSpellCheck(context); |
| 56 | 55 |
| 57 scoped_ptr<base::ListValue> args(input_ime::OnFocus::Create(context_value)); | 56 std::unique_ptr<base::ListValue> args( |
| 57 input_ime::OnFocus::Create(context_value)); |
| 58 | 58 |
| 59 DispatchEventToExtension(extensions::events::INPUT_IME_ON_FOCUS, | 59 DispatchEventToExtension(extensions::events::INPUT_IME_ON_FOCUS, |
| 60 input_ime::OnFocus::kEventName, std::move(args)); | 60 input_ime::OnFocus::kEventName, std::move(args)); |
| 61 } | 61 } |
| 62 | 62 |
| 63 void ImeObserver::OnBlur(int context_id) { | 63 void ImeObserver::OnBlur(int context_id) { |
| 64 if (extension_id_.empty() || !HasListener(input_ime::OnBlur::kEventName)) | 64 if (extension_id_.empty() || !HasListener(input_ime::OnBlur::kEventName)) |
| 65 return; | 65 return; |
| 66 | 66 |
| 67 scoped_ptr<base::ListValue> args(input_ime::OnBlur::Create(context_id)); | 67 std::unique_ptr<base::ListValue> args(input_ime::OnBlur::Create(context_id)); |
| 68 | 68 |
| 69 DispatchEventToExtension(extensions::events::INPUT_IME_ON_BLUR, | 69 DispatchEventToExtension(extensions::events::INPUT_IME_ON_BLUR, |
| 70 input_ime::OnBlur::kEventName, std::move(args)); | 70 input_ime::OnBlur::kEventName, std::move(args)); |
| 71 } | 71 } |
| 72 | 72 |
| 73 void ImeObserver::OnKeyEvent( | 73 void ImeObserver::OnKeyEvent( |
| 74 const std::string& component_id, | 74 const std::string& component_id, |
| 75 const InputMethodEngineBase::KeyboardEvent& event, | 75 const InputMethodEngineBase::KeyboardEvent& event, |
| 76 IMEEngineHandlerInterface::KeyEventDoneCallback& key_data) { | 76 IMEEngineHandlerInterface::KeyEventDoneCallback& key_data) { |
| 77 if (extension_id_.empty()) | 77 if (extension_id_.empty()) |
| (...skipping 20 matching lines...) Expand all Loading... |
| 98 key_data_value.request_id = request_id; | 98 key_data_value.request_id = request_id; |
| 99 if (!event.extension_id.empty()) | 99 if (!event.extension_id.empty()) |
| 100 key_data_value.extension_id.reset(new std::string(event.extension_id)); | 100 key_data_value.extension_id.reset(new std::string(event.extension_id)); |
| 101 key_data_value.key = event.key; | 101 key_data_value.key = event.key; |
| 102 key_data_value.code = event.code; | 102 key_data_value.code = event.code; |
| 103 key_data_value.alt_key.reset(new bool(event.alt_key)); | 103 key_data_value.alt_key.reset(new bool(event.alt_key)); |
| 104 key_data_value.ctrl_key.reset(new bool(event.ctrl_key)); | 104 key_data_value.ctrl_key.reset(new bool(event.ctrl_key)); |
| 105 key_data_value.shift_key.reset(new bool(event.shift_key)); | 105 key_data_value.shift_key.reset(new bool(event.shift_key)); |
| 106 key_data_value.caps_lock.reset(new bool(event.caps_lock)); | 106 key_data_value.caps_lock.reset(new bool(event.caps_lock)); |
| 107 | 107 |
| 108 scoped_ptr<base::ListValue> args( | 108 std::unique_ptr<base::ListValue> args( |
| 109 input_ime::OnKeyEvent::Create(component_id, key_data_value)); | 109 input_ime::OnKeyEvent::Create(component_id, key_data_value)); |
| 110 | 110 |
| 111 DispatchEventToExtension(extensions::events::INPUT_IME_ON_KEY_EVENT, | 111 DispatchEventToExtension(extensions::events::INPUT_IME_ON_KEY_EVENT, |
| 112 input_ime::OnKeyEvent::kEventName, std::move(args)); | 112 input_ime::OnKeyEvent::kEventName, std::move(args)); |
| 113 } | 113 } |
| 114 | 114 |
| 115 void ImeObserver::OnReset(const std::string& component_id) { | 115 void ImeObserver::OnReset(const std::string& component_id) { |
| 116 if (extension_id_.empty() || !HasListener(input_ime::OnReset::kEventName)) | 116 if (extension_id_.empty() || !HasListener(input_ime::OnReset::kEventName)) |
| 117 return; | 117 return; |
| 118 | 118 |
| 119 scoped_ptr<base::ListValue> args(input_ime::OnReset::Create(component_id)); | 119 std::unique_ptr<base::ListValue> args( |
| 120 input_ime::OnReset::Create(component_id)); |
| 120 | 121 |
| 121 DispatchEventToExtension(extensions::events::INPUT_IME_ON_RESET, | 122 DispatchEventToExtension(extensions::events::INPUT_IME_ON_RESET, |
| 122 input_ime::OnReset::kEventName, std::move(args)); | 123 input_ime::OnReset::kEventName, std::move(args)); |
| 123 } | 124 } |
| 124 | 125 |
| 125 void ImeObserver::OnDeactivated(const std::string& component_id) { | 126 void ImeObserver::OnDeactivated(const std::string& component_id) { |
| 126 if (extension_id_.empty() || | 127 if (extension_id_.empty() || |
| 127 !HasListener(input_ime::OnDeactivated::kEventName)) | 128 !HasListener(input_ime::OnDeactivated::kEventName)) |
| 128 return; | 129 return; |
| 129 | 130 |
| 130 scoped_ptr<base::ListValue> args( | 131 std::unique_ptr<base::ListValue> args( |
| 131 input_ime::OnDeactivated::Create(component_id)); | 132 input_ime::OnDeactivated::Create(component_id)); |
| 132 | 133 |
| 133 DispatchEventToExtension(extensions::events::INPUT_IME_ON_DEACTIVATED, | 134 DispatchEventToExtension(extensions::events::INPUT_IME_ON_DEACTIVATED, |
| 134 input_ime::OnDeactivated::kEventName, | 135 input_ime::OnDeactivated::kEventName, |
| 135 std::move(args)); | 136 std::move(args)); |
| 136 } | 137 } |
| 137 | 138 |
| 138 // TODO(azurewei): This function implementation should be shared on all | 139 // TODO(azurewei): This function implementation should be shared on all |
| 139 // platforms, while with some changing on the current code on ChromeOS. | 140 // platforms, while with some changing on the current code on ChromeOS. |
| 140 void ImeObserver::OnCompositionBoundsChanged( | 141 void ImeObserver::OnCompositionBoundsChanged( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 151 int offset_pos) { | 152 int offset_pos) { |
| 152 if (extension_id_.empty() || | 153 if (extension_id_.empty() || |
| 153 !HasListener(input_ime::OnSurroundingTextChanged::kEventName)) | 154 !HasListener(input_ime::OnSurroundingTextChanged::kEventName)) |
| 154 return; | 155 return; |
| 155 | 156 |
| 156 input_ime::OnSurroundingTextChanged::SurroundingInfo info; | 157 input_ime::OnSurroundingTextChanged::SurroundingInfo info; |
| 157 info.text = text; | 158 info.text = text; |
| 158 info.focus = cursor_pos; | 159 info.focus = cursor_pos; |
| 159 info.anchor = anchor_pos; | 160 info.anchor = anchor_pos; |
| 160 info.offset = offset_pos; | 161 info.offset = offset_pos; |
| 161 scoped_ptr<base::ListValue> args( | 162 std::unique_ptr<base::ListValue> args( |
| 162 input_ime::OnSurroundingTextChanged::Create(component_id, info)); | 163 input_ime::OnSurroundingTextChanged::Create(component_id, info)); |
| 163 | 164 |
| 164 DispatchEventToExtension( | 165 DispatchEventToExtension( |
| 165 extensions::events::INPUT_IME_ON_SURROUNDING_TEXT_CHANGED, | 166 extensions::events::INPUT_IME_ON_SURROUNDING_TEXT_CHANGED, |
| 166 input_ime::OnSurroundingTextChanged::kEventName, std::move(args)); | 167 input_ime::OnSurroundingTextChanged::kEventName, std::move(args)); |
| 167 } | 168 } |
| 168 | 169 |
| 169 bool ImeObserver::ShouldForwardKeyEvent() const { | 170 bool ImeObserver::ShouldForwardKeyEvent() const { |
| 170 // Only forward key events to extension if there are non-lazy listeners | 171 // Only forward key events to extension if there are non-lazy listeners |
| 171 // for onKeyEvent. Because if something wrong with the lazy background | 172 // for onKeyEvent. Because if something wrong with the lazy background |
| 172 // page which doesn't register listener for onKeyEvent, it will not handle | 173 // page which doesn't register listener for onKeyEvent, it will not handle |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 if (!profile || router_map_.empty()) | 264 if (!profile || router_map_.empty()) |
| 264 return; | 265 return; |
| 265 auto it = router_map_.find(profile); | 266 auto it = router_map_.find(profile); |
| 266 if (it != router_map_.end()) { | 267 if (it != router_map_.end()) { |
| 267 delete it->second; | 268 delete it->second; |
| 268 router_map_.erase(it); | 269 router_map_.erase(it); |
| 269 } | 270 } |
| 270 } | 271 } |
| 271 | 272 |
| 272 ExtensionFunction::ResponseAction InputImeKeyEventHandledFunction::Run() { | 273 ExtensionFunction::ResponseAction InputImeKeyEventHandledFunction::Run() { |
| 273 scoped_ptr<KeyEventHandled::Params> params( | 274 std::unique_ptr<KeyEventHandled::Params> params( |
| 274 KeyEventHandled::Params::Create(*args_)); | 275 KeyEventHandled::Params::Create(*args_)); |
| 275 InputImeEventRouter* event_router = | 276 InputImeEventRouter* event_router = |
| 276 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())); | 277 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())); |
| 277 InputMethodEngineBase* engine = | 278 InputMethodEngineBase* engine = |
| 278 event_router ? event_router->GetActiveEngine(extension_id()) : nullptr; | 279 event_router ? event_router->GetActiveEngine(extension_id()) : nullptr; |
| 279 if (engine) { | 280 if (engine) { |
| 280 engine->KeyEventHandled(extension_id(), params->request_id, | 281 engine->KeyEventHandled(extension_id(), params->request_id, |
| 281 params->response); | 282 params->response); |
| 282 } | 283 } |
| 283 return RespondNow(NoArguments()); | 284 return RespondNow(NoArguments()); |
| 284 } | 285 } |
| 285 | 286 |
| 286 ExtensionFunction::ResponseAction InputImeSetCompositionFunction::Run() { | 287 ExtensionFunction::ResponseAction InputImeSetCompositionFunction::Run() { |
| 287 bool success = false; | 288 bool success = false; |
| 288 InputImeEventRouter* event_router = | 289 InputImeEventRouter* event_router = |
| 289 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())); | 290 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())); |
| 290 InputMethodEngineBase* engine = | 291 InputMethodEngineBase* engine = |
| 291 event_router ? event_router->GetActiveEngine(extension_id()) : nullptr; | 292 event_router ? event_router->GetActiveEngine(extension_id()) : nullptr; |
| 292 if (engine) { | 293 if (engine) { |
| 293 scoped_ptr<SetComposition::Params> parent_params( | 294 std::unique_ptr<SetComposition::Params> parent_params( |
| 294 SetComposition::Params::Create(*args_)); | 295 SetComposition::Params::Create(*args_)); |
| 295 const SetComposition::Params::Parameters& params = | 296 const SetComposition::Params::Parameters& params = |
| 296 parent_params->parameters; | 297 parent_params->parameters; |
| 297 std::vector<InputMethodEngineBase::SegmentInfo> segments; | 298 std::vector<InputMethodEngineBase::SegmentInfo> segments; |
| 298 if (params.segments) { | 299 if (params.segments) { |
| 299 for (const auto& segments_arg : *params.segments) { | 300 for (const auto& segments_arg : *params.segments) { |
| 300 EXTENSION_FUNCTION_VALIDATE(segments_arg.style != | 301 EXTENSION_FUNCTION_VALIDATE(segments_arg.style != |
| 301 input_ime::UNDERLINE_STYLE_NONE); | 302 input_ime::UNDERLINE_STYLE_NONE); |
| 302 InputMethodEngineBase::SegmentInfo segment_info; | 303 InputMethodEngineBase::SegmentInfo segment_info; |
| 303 segment_info.start = segments_arg.start; | 304 segment_info.start = segments_arg.start; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 316 } | 317 } |
| 317 } | 318 } |
| 318 int selection_start = | 319 int selection_start = |
| 319 params.selection_start ? *params.selection_start : params.cursor; | 320 params.selection_start ? *params.selection_start : params.cursor; |
| 320 int selection_end = | 321 int selection_end = |
| 321 params.selection_end ? *params.selection_end : params.cursor; | 322 params.selection_end ? *params.selection_end : params.cursor; |
| 322 success = engine->SetComposition(params.context_id, params.text.c_str(), | 323 success = engine->SetComposition(params.context_id, params.text.c_str(), |
| 323 selection_start, selection_end, | 324 selection_start, selection_end, |
| 324 params.cursor, segments, &error_); | 325 params.cursor, segments, &error_); |
| 325 } | 326 } |
| 326 scoped_ptr<base::ListValue> output = SetComposition::Results::Create(success); | 327 std::unique_ptr<base::ListValue> output = |
| 328 SetComposition::Results::Create(success); |
| 327 return RespondNow(ArgumentList(std::move(output))); | 329 return RespondNow(ArgumentList(std::move(output))); |
| 328 } | 330 } |
| 329 | 331 |
| 330 ExtensionFunction::ResponseAction InputImeCommitTextFunction::Run() { | 332 ExtensionFunction::ResponseAction InputImeCommitTextFunction::Run() { |
| 331 bool success = false; | 333 bool success = false; |
| 332 InputImeEventRouter* event_router = | 334 InputImeEventRouter* event_router = |
| 333 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())); | 335 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())); |
| 334 InputMethodEngineBase* engine = | 336 InputMethodEngineBase* engine = |
| 335 event_router ? event_router->GetActiveEngine(extension_id()) : nullptr; | 337 event_router ? event_router->GetActiveEngine(extension_id()) : nullptr; |
| 336 if (engine) { | 338 if (engine) { |
| 337 scoped_ptr<CommitText::Params> parent_params( | 339 std::unique_ptr<CommitText::Params> parent_params( |
| 338 CommitText::Params::Create(*args_)); | 340 CommitText::Params::Create(*args_)); |
| 339 const CommitText::Params::Parameters& params = parent_params->parameters; | 341 const CommitText::Params::Parameters& params = parent_params->parameters; |
| 340 success = | 342 success = |
| 341 engine->CommitText(params.context_id, params.text.c_str(), &error_); | 343 engine->CommitText(params.context_id, params.text.c_str(), &error_); |
| 342 } | 344 } |
| 343 scoped_ptr<base::ListValue> output = CommitText::Results::Create(success); | 345 std::unique_ptr<base::ListValue> output = |
| 346 CommitText::Results::Create(success); |
| 344 return RespondNow(ArgumentList(std::move(output))); | 347 return RespondNow(ArgumentList(std::move(output))); |
| 345 } | 348 } |
| 346 | 349 |
| 347 ExtensionFunction::ResponseAction InputImeSendKeyEventsFunction::Run() { | 350 ExtensionFunction::ResponseAction InputImeSendKeyEventsFunction::Run() { |
| 348 InputImeEventRouter* event_router = | 351 InputImeEventRouter* event_router = |
| 349 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())); | 352 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())); |
| 350 InputMethodEngineBase* engine = | 353 InputMethodEngineBase* engine = |
| 351 event_router ? event_router->GetActiveEngine(extension_id()) : nullptr; | 354 event_router ? event_router->GetActiveEngine(extension_id()) : nullptr; |
| 352 if (!engine) | 355 if (!engine) |
| 353 return RespondNow(Error(kErrorEngineNotAvailable)); | 356 return RespondNow(Error(kErrorEngineNotAvailable)); |
| 354 | 357 |
| 355 scoped_ptr<SendKeyEvents::Params> parent_params( | 358 std::unique_ptr<SendKeyEvents::Params> parent_params( |
| 356 SendKeyEvents::Params::Create(*args_)); | 359 SendKeyEvents::Params::Create(*args_)); |
| 357 EXTENSION_FUNCTION_VALIDATE(parent_params); | 360 EXTENSION_FUNCTION_VALIDATE(parent_params); |
| 358 const SendKeyEvents::Params::Parameters& params = parent_params->parameters; | 361 const SendKeyEvents::Params::Parameters& params = parent_params->parameters; |
| 359 std::vector<InputMethodEngineBase::KeyboardEvent> key_data_out; | 362 std::vector<InputMethodEngineBase::KeyboardEvent> key_data_out; |
| 360 | 363 |
| 361 for (const auto& key_event : params.key_data) { | 364 for (const auto& key_event : params.key_data) { |
| 362 key_data_out.push_back(InputMethodEngineBase::KeyboardEvent()); | 365 key_data_out.push_back(InputMethodEngineBase::KeyboardEvent()); |
| 363 InputMethodEngineBase::KeyboardEvent& event = key_data_out.back(); | 366 InputMethodEngineBase::KeyboardEvent& event = key_data_out.back(); |
| 364 event.type = input_ime::ToString(key_event.type); | 367 event.type = input_ime::ToString(key_event.type); |
| 365 event.key = key_event.key; | 368 event.key = key_event.key; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 InputImeEventRouter* GetInputImeEventRouter(Profile* profile) { | 413 InputImeEventRouter* GetInputImeEventRouter(Profile* profile) { |
| 411 if (!profile) | 414 if (!profile) |
| 412 return nullptr; | 415 return nullptr; |
| 413 if (profile->HasOffTheRecordProfile()) | 416 if (profile->HasOffTheRecordProfile()) |
| 414 profile = profile->GetOffTheRecordProfile(); | 417 profile = profile->GetOffTheRecordProfile(); |
| 415 return extensions::InputImeEventRouterFactory::GetInstance()->GetRouter( | 418 return extensions::InputImeEventRouterFactory::GetInstance()->GetRouter( |
| 416 profile); | 419 profile); |
| 417 } | 420 } |
| 418 | 421 |
| 419 } // namespace extensions | 422 } // namespace extensions |
| OLD | NEW |