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 | |
217 if (web_contents) { | |
218 GURL url = web_contents->GetLastCommittedURL(); | |
219 // If |input_context->GetInputMethod() != input_method|, treats |url| as | |
220 // special page no mater what it is. | |
221 bool url_trustable = input_context->GetInputMethod() == input_method; | |
222 if (!url.is_empty() && !IsValidKeyForSpecialPage(url, url_trustable, event)) | |
223 return false; | |
224 } | |
225 | |
195 input_context->SendKeyEvent(event); | 226 input_context->SendKeyEvent(event); |
196 | 227 |
197 return true; | 228 return true; |
198 } | 229 } |
199 | 230 |
231 bool InputMethodEngine::IsValidKeyForSpecialPage(const GURL& url, | |
232 bool url_trustable, | |
233 ui::KeyEvent* ui_event) { | |
234 // input.ime.sendKeyEvents API is not allowed to send ENTER et al. keys on | |
235 // some special pages (like chrome://). | |
236 std::vector<const char*> whitelist_schemes; | |
237 whitelist_schemes.push_back(url::kFtpScheme); | |
Devlin
2016/06/22 16:33:12
Let's use c++11 initialization syntax here.
Azure Wei
2016/06/23 02:45:23
Done.
| |
238 whitelist_schemes.push_back(url::kHttpScheme); | |
239 whitelist_schemes.push_back(url::kHttpsScheme); | |
240 | |
Devlin
2016/06/22 16:33:12
meacer@, do you think we can add file:// urls here
| |
241 std::vector<const char*> whitelist_urls; | |
Devlin
2016/06/22 16:33:12
why not std::vector<GURL>?
Azure Wei
2016/06/23 02:45:23
Done.
| |
242 whitelist_urls.push_back(url::kAboutBlankURL); | |
243 whitelist_urls.push_back(chrome::kChromeUINewTabURL); | |
244 | |
245 // Checks if the current url is special url. | |
246 if (url_trustable) { | |
247 for (auto scheme : whitelist_schemes) { | |
248 if (url.SchemeIs(scheme)) | |
249 return true; | |
250 } | |
251 for (auto whitelist_url : whitelist_urls) { | |
252 if (url == GURL(whitelist_url)) | |
Devlin
2016/06/22 16:33:12
This wouldn't work with e.g. fragments. I don't t
Azure Wei
2016/06/23 02:45:23
Done.
| |
253 return true; | |
254 } | |
255 } | |
256 | |
257 std::vector<ui::KeyboardCode> whitelist_keycodes; | |
258 whitelist_keycodes.push_back(ui::VKEY_BACK); | |
259 whitelist_keycodes.push_back(ui::VKEY_LEFT); | |
260 whitelist_keycodes.push_back(ui::VKEY_RIGHT); | |
261 whitelist_keycodes.push_back(ui::VKEY_UP); | |
262 whitelist_keycodes.push_back(ui::VKEY_DOWN); | |
263 | |
264 std::vector<ui::KeyboardCode> invalid_character_keycodes; | |
265 invalid_character_keycodes.push_back(ui::VKEY_TAB); | |
266 invalid_character_keycodes.push_back(ui::VKEY_RETURN); | |
267 | |
268 // Whitelists all character keys. | |
269 if (ui_event->GetDomKey().IsCharacter() && !ui_event->IsControlDown() && | |
270 !ui_event->IsCommandDown()) { | |
Devlin
2016/06/22 16:33:12
What about other keyboard modifier keys? Alt, Met
Azure Wei
2016/06/23 02:45:23
Other modifier keys like Meta/Search doesn't have
| |
271 for (auto invalid_key : invalid_character_keycodes) { | |
Devlin
2016/06/22 16:33:12
Are tab and return considered character keys? If
Azure Wei
2016/06/23 02:45:23
Yes, tab and return are also character keys.
| |
272 if (ui_event->key_code() == invalid_key) | |
273 return false; | |
274 } | |
275 return true; | |
276 } | |
277 | |
278 // Checks if the key event is white listed key. | |
279 for (auto key : whitelist_keycodes) { | |
280 if (ui_event->key_code() == key) | |
281 return true; | |
282 } | |
283 | |
284 return false; | |
285 } | |
286 | |
200 } // namespace input_method | 287 } // namespace input_method |
OLD | NEW |