| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "chrome/common/extensions/api/input_ime.h" | 10 #include "chrome/common/extensions/api/input_ime.h" |
| 11 #include "chrome/common/extensions/api/input_ime/input_components_handler.h" | 11 #include "chrome/common/extensions/api/input_ime/input_components_handler.h" |
| 12 #include "extensions/browser/extension_registry.h" | 12 #include "extensions/browser/extension_registry.h" |
| 13 | 13 |
| 14 namespace input_ime = extensions::api::input_ime; | 14 namespace input_ime = extensions::api::input_ime; |
| 15 namespace KeyEventHandled = extensions::api::input_ime::KeyEventHandled; | 15 namespace KeyEventHandled = extensions::api::input_ime::KeyEventHandled; |
| 16 using ui::IMEEngineHandlerInterface; | 16 using ui::IMEEngineHandlerInterface; |
| 17 | 17 |
| 18 namespace ui { | 18 namespace ui { |
| 19 | 19 |
| 20 ImeObserver::ImeObserver(const std::string& extension_id, Profile* profile) | 20 ImeObserver::ImeObserver(const std::string& extension_id, Profile* profile) |
| 21 : extension_id_(extension_id), profile_(profile) {} | 21 : extension_id_(extension_id), profile_(profile) {} |
| 22 | 22 |
| 23 void ImeObserver::OnActivate(const std::string& component_id) { |
| 24 if (extension_id_.empty() || !HasListener(input_ime::OnActivate::kEventName)) |
| 25 return; |
| 26 |
| 27 scoped_ptr<base::ListValue> args(input_ime::OnActivate::Create( |
| 28 component_id, |
| 29 input_ime::ParseScreenType(GetCurrentScreenType()))); |
| 30 |
| 31 DispatchEventToExtension(extensions::events::INPUT_IME_ON_ACTIVATE, |
| 32 input_ime::OnActivate::kEventName, |
| 33 std::move(args)); |
| 34 } |
| 35 |
| 23 void ImeObserver::OnFocus( | 36 void ImeObserver::OnFocus( |
| 24 const IMEEngineHandlerInterface::InputContext& context) { | 37 const IMEEngineHandlerInterface::InputContext& context) { |
| 25 if (extension_id_.empty() || !HasListener(input_ime::OnFocus::kEventName)) | 38 if (extension_id_.empty() || !HasListener(input_ime::OnFocus::kEventName)) |
| 26 return; | 39 return; |
| 27 | 40 |
| 28 input_ime::InputContext context_value; | 41 input_ime::InputContext context_value; |
| 29 context_value.context_id = context.id; | 42 context_value.context_id = context.id; |
| 30 context_value.type = | 43 context_value.type = |
| 31 input_ime::ParseInputContextType(ConvertInputContextType(context)); | 44 input_ime::ParseInputContextType(ConvertInputContextType(context)); |
| 32 context_value.auto_correct = ConvertInputContextAutoCorrect(context); | 45 context_value.auto_correct = ConvertInputContextAutoCorrect(context); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 DispatchEventToExtension(extensions::events::INPUT_IME_ON_DEACTIVATED, | 121 DispatchEventToExtension(extensions::events::INPUT_IME_ON_DEACTIVATED, |
| 109 input_ime::OnDeactivated::kEventName, | 122 input_ime::OnDeactivated::kEventName, |
| 110 std::move(args)); | 123 std::move(args)); |
| 111 } | 124 } |
| 112 | 125 |
| 113 // TODO(azurewei): This function implementation should be shared on all | 126 // TODO(azurewei): This function implementation should be shared on all |
| 114 // platforms, while with some changing on the current code on ChromeOS. | 127 // platforms, while with some changing on the current code on ChromeOS. |
| 115 void ImeObserver::OnCompositionBoundsChanged( | 128 void ImeObserver::OnCompositionBoundsChanged( |
| 116 const std::vector<gfx::Rect>& bounds) {} | 129 const std::vector<gfx::Rect>& bounds) {} |
| 117 | 130 |
| 131 bool ImeObserver::IsInterestedInKeyEvent() const { |
| 132 return ShouldForwardKeyEvent(); |
| 133 } |
| 134 |
| 135 void ImeObserver::OnSurroundingTextChanged(const std::string& component_id, |
| 136 const std::string& text, |
| 137 int cursor_pos, |
| 138 int anchor_pos, |
| 139 int offset_pos) { |
| 140 if (extension_id_.empty() || |
| 141 !HasListener(input_ime::OnSurroundingTextChanged::kEventName)) |
| 142 return; |
| 143 |
| 144 input_ime::OnSurroundingTextChanged::SurroundingInfo info; |
| 145 info.text = text; |
| 146 info.focus = cursor_pos; |
| 147 info.anchor = anchor_pos; |
| 148 info.offset = offset_pos; |
| 149 scoped_ptr<base::ListValue> args( |
| 150 input_ime::OnSurroundingTextChanged::Create(component_id, info)); |
| 151 |
| 152 DispatchEventToExtension( |
| 153 extensions::events::INPUT_IME_ON_SURROUNDING_TEXT_CHANGED, |
| 154 input_ime::OnSurroundingTextChanged::kEventName, std::move(args)); |
| 155 } |
| 156 |
| 118 bool ImeObserver::ShouldForwardKeyEvent() const { | 157 bool ImeObserver::ShouldForwardKeyEvent() const { |
| 119 // Only forward key events to extension if there are non-lazy listeners | 158 // Only forward key events to extension if there are non-lazy listeners |
| 120 // for onKeyEvent. Because if something wrong with the lazy background | 159 // for onKeyEvent. Because if something wrong with the lazy background |
| 121 // page which doesn't register listener for onKeyEvent, it will not handle | 160 // page which doesn't register listener for onKeyEvent, it will not handle |
| 122 // the key events, and therefore, all key events will be eaten. | 161 // the key events, and therefore, all key events will be eaten. |
| 123 // This is for error-tolerance, and it means that onKeyEvent will never wake | 162 // This is for error-tolerance, and it means that onKeyEvent will never wake |
| 124 // up lazy background page. | 163 // up lazy background page. |
| 125 const extensions::EventListenerMap::ListenerList& listener_list = | 164 const extensions::EventListenerMap::ListenerList& listener_list = |
| 126 extensions::EventRouter::Get(profile_) | 165 extensions::EventRouter::Get(profile_) |
| 127 ->listeners() | 166 ->listeners() |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 } | 360 } |
| 322 | 361 |
| 323 InputImeEventRouter* GetInputImeEventRouter(Profile* profile) { | 362 InputImeEventRouter* GetInputImeEventRouter(Profile* profile) { |
| 324 if (profile->HasOffTheRecordProfile()) | 363 if (profile->HasOffTheRecordProfile()) |
| 325 profile = profile->GetOffTheRecordProfile(); | 364 profile = profile->GetOffTheRecordProfile(); |
| 326 return extensions::InputImeEventRouterFactory::GetInstance()->GetRouter( | 365 return extensions::InputImeEventRouterFactory::GetInstance()->GetRouter( |
| 327 profile); | 366 profile); |
| 328 } | 367 } |
| 329 | 368 |
| 330 } // namespace extensions | 369 } // namespace extensions |
| OLD | NEW |