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(); | |
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 // If |input_context->GetInputMethod() != input_method|, treats |url| as | |
217 // special page no mater what it is. | |
218 if (web_contents && | |
219 !IsValidKeyForSpecialPage(web_contents->GetLastCommittedURL(), | |
220 input_context->GetInputMethod() == input_method, | |
221 event)) | |
222 return false; | |
223 | |
195 input_context->SendKeyEvent(event); | 224 input_context->SendKeyEvent(event); |
196 | 225 |
197 return true; | 226 return true; |
198 } | 227 } |
199 | 228 |
229 bool InputMethodEngine::IsValidKeyForSpecialPage(const GURL& url, | |
230 bool url_trustable, | |
Devlin
2016/06/28 20:56:44
The more I look at the "url_trustable" bool, the m
Azure Wei
2016/06/29 02:53:46
Do you mean put all the logic of checking the url
Devlin
2016/06/29 16:07:23
Ah, right, because otherwise we send all key event
| |
231 ui::KeyEvent* ui_event) { | |
232 // input.ime.sendKeyEvents API is not allowed to send ENTER et al. keys on | |
233 // some special pages (like chrome://). | |
234 std::vector<const char*> whitelist_schemes{url::kFtpScheme, url::kHttpScheme, | |
235 url::kHttpsScheme}; | |
236 std::vector<GURL> whitelist_urls{GURL(url::kAboutBlankURL), | |
237 GURL(chrome::kChromeUINewTabURL)}; | |
238 | |
239 // Checks if the current url is special url. | |
240 if (url_trustable) { | |
241 for (auto scheme : whitelist_schemes) { | |
242 if (url.SchemeIs(scheme)) | |
243 return true; | |
244 } | |
245 for (auto whitelist_url : whitelist_urls) { | |
246 if (url == whitelist_url || url.GetOrigin() == whitelist_url) | |
247 return true; | |
248 } | |
249 } | |
250 | |
251 std::vector<ui::KeyboardCode> whitelist_keycodes{ | |
252 ui::VKEY_BACK, ui::VKEY_LEFT, ui::VKEY_RIGHT, ui::VKEY_UP, ui::VKEY_DOWN}; | |
253 std::vector<ui::KeyboardCode> invalid_character_keycodes{ui::VKEY_TAB, | |
254 ui::VKEY_RETURN}; | |
255 | |
256 // Whitelists all character keys. | |
257 if (ui_event->GetDomKey().IsCharacter() && !ui_event->IsControlDown() && | |
258 !ui_event->IsCommandDown()) { | |
259 for (auto invalid_key : invalid_character_keycodes) { | |
Devlin
2016/06/28 20:56:44
nit: I find it slightly more readable to make keyc
Azure Wei
2016/06/29 02:53:46
Done.
| |
260 if (ui_event->key_code() == invalid_key) | |
261 return false; | |
262 } | |
263 return true; | |
264 } | |
265 | |
266 // Checks if the key event is white listed key. | |
Devlin
2016/06/28 20:56:44
and here
return whitelist_keycodes.count(ui_event-
Azure Wei
2016/06/29 02:53:46
Done.
| |
267 for (auto key : whitelist_keycodes) { | |
268 if (ui_event->key_code() == key) | |
269 return true; | |
270 } | |
271 | |
272 return false; | |
273 } | |
274 | |
200 } // namespace input_method | 275 } // namespace input_method |
OLD | NEW |