OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/ui/input_method/input_method_engine.h" | 5 #include "chrome/browser/ui/input_method/input_method_engine.h" |
6 | 6 |
7 #include "chrome/browser/ui/browser_finder.h" | |
8 #include "chrome/browser/ui/browser_window.h" | |
9 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
10 #include "chrome/common/url_constants.h" | |
11 #include "content/public/browser/navigation_entry.h" | |
7 #include "content/public/browser/render_frame_host.h" | 12 #include "content/public/browser/render_frame_host.h" |
13 #include "content/public/common/url_constants.h" | |
14 #include "extensions/common/constants.h" | |
15 #include "ui/aura/window.h" | |
16 #include "ui/aura/window_tree_host.h" | |
8 #include "ui/base/ime/composition_text.h" | 17 #include "ui/base/ime/composition_text.h" |
9 #include "ui/base/ime/ime_bridge.h" | 18 #include "ui/base/ime/ime_bridge.h" |
10 #include "ui/base/ime/ime_input_context_handler_interface.h" | 19 #include "ui/base/ime/ime_input_context_handler_interface.h" |
11 #include "ui/events/keycodes/keyboard_code_conversion.h" | 20 #include "ui/events/keycodes/keyboard_code_conversion.h" |
12 | 21 |
13 namespace { | 22 namespace { |
14 | 23 |
15 const char kErrorFollowCursorWindowExists[] = | 24 const char kErrorFollowCursorWindowExists[] = |
16 "A follow cursor IME window exists."; | 25 "A follow cursor IME window exists."; |
17 const char kErrorNoInputFocus[] = | 26 const char kErrorNoInputFocus[] = |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 for (auto ime_window : normal_windows_) { | 187 for (auto ime_window : normal_windows_) { |
179 if (ime_window->GetFrameId() == window_id) | 188 if (ime_window->GetFrameId() == window_id) |
180 return ime_window; | 189 return ime_window; |
181 } | 190 } |
182 return nullptr; | 191 return nullptr; |
183 } | 192 } |
184 | 193 |
185 bool InputMethodEngine::SendKeyEvent(ui::KeyEvent* event, | 194 bool InputMethodEngine::SendKeyEvent(ui::KeyEvent* event, |
186 const std::string& code) { | 195 const std::string& code) { |
187 DCHECK(event); | 196 DCHECK(event); |
197 | |
198 // input.ime.sendKeyEvents API is only allowed to work on text fields. | |
199 if (current_input_type_ == ui::TEXT_INPUT_TYPE_NONE) | |
200 return false; | |
201 | |
188 if (event->key_code() == ui::VKEY_UNKNOWN) | 202 if (event->key_code() == ui::VKEY_UNKNOWN) |
189 event->set_key_code(ui::DomCodeToUsLayoutKeyboardCode(event->code())); | 203 event->set_key_code(ui::DomCodeToUsLayoutKeyboardCode(event->code())); |
190 | 204 |
191 ui::IMEInputContextHandlerInterface* input_context = | 205 ui::IMEInputContextHandlerInterface* input_context = |
192 ui::IMEBridge::Get()->GetInputContextHandler(); | 206 ui::IMEBridge::Get()->GetInputContextHandler(); |
193 if (!input_context) | 207 if (!input_context) |
194 return false; | 208 return false; |
209 | |
210 Browser* browser = chrome::FindLastActive(); | |
Shu Chen
2016/06/29 13:18:19
IMO, IsValidKeyForSpecialPage() may run faster tha
Azure Wei
2016/06/30 07:46:20
Done. And renamed as IsValidKeyForAllPages().
| |
211 DCHECK(browser); | |
212 content::WebContents* web_contents = | |
213 browser->tab_strip_model()->GetActiveWebContents(); | |
214 ui::InputMethod* input_method = | |
215 browser->window()->GetNativeWindow()->GetHost()->GetInputMethod(); | |
216 | |
217 if (web_contents) { | |
218 // input.ime.sendKeyEvents API is not allowed to send ENTER et al. keys on | |
219 // some special pages (like chrome://). | |
220 GURL url = web_contents->GetLastCommittedURL(); | |
221 // If |input_context->GetInputMethod() != input_method|, treats |url| as | |
222 // special page no mater what it is. | |
223 bool is_special_page = input_context->GetInputMethod() != input_method; | |
224 if (!is_special_page) { | |
225 is_special_page = true; | |
Devlin
2016/06/29 16:07:23
Hmm... sorry, this still looks a little weird to m
Azure Wei
2016/06/30 07:46:20
Done.
Added the new method IsSpecialPage(ui::Input
| |
226 // Checks if the current url is with whitelisted scheme. | |
227 std::vector<const char*> whitelist_schemes{ | |
228 url::kFtpScheme, url::kHttpScheme, url::kHttpsScheme}; | |
229 for (auto scheme : whitelist_schemes) { | |
230 if (url.SchemeIs(scheme)) { | |
231 is_special_page = false; | |
232 break; | |
233 } | |
234 } | |
235 // Checks if the current url is whitelisted url. | |
236 std::vector<GURL> whitelist_urls{GURL(url::kAboutBlankURL), | |
237 GURL(chrome::kChromeUINewTabURL)}; | |
238 for (auto whitelist_url : whitelist_urls) { | |
239 if (url == whitelist_url || url.GetOrigin() == whitelist_url) { | |
240 is_special_page = false; | |
241 break; | |
242 } | |
243 } | |
244 } | |
245 if (is_special_page && !IsValidKeyForSpecialPage(event)) | |
246 return false; | |
247 } | |
248 | |
195 input_context->SendKeyEvent(event); | 249 input_context->SendKeyEvent(event); |
196 | 250 |
197 return true; | 251 return true; |
198 } | 252 } |
199 | 253 |
254 bool InputMethodEngine::IsValidKeyForSpecialPage(ui::KeyEvent* ui_event) { | |
255 // Whitelists all character keys except for Enter and Tab keys. | |
256 std::vector<ui::KeyboardCode> invalid_character_keycodes{ui::VKEY_TAB, | |
257 ui::VKEY_RETURN}; | |
258 if (ui_event->GetDomKey().IsCharacter() && !ui_event->IsControlDown() && | |
259 !ui_event->IsCommandDown()) | |
260 return std::count(invalid_character_keycodes.begin(), | |
261 invalid_character_keycodes.end(), | |
262 ui_event->key_code()) == 0; | |
263 | |
264 // Whitelists Backspace key and arrow keys. | |
265 std::vector<ui::KeyboardCode> whitelist_keycodes{ | |
266 ui::VKEY_BACK, ui::VKEY_LEFT, ui::VKEY_RIGHT, ui::VKEY_UP, ui::VKEY_DOWN}; | |
267 return std::count(whitelist_keycodes.begin(), whitelist_keycodes.end(), | |
268 ui_event->key_code()) != 0; | |
269 } | |
270 | |
200 } // namespace input_method | 271 } // namespace input_method |
OLD | NEW |