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 <vector> | |
8 | |
9 #include "chrome/browser/ui/browser_finder.h" | |
10 #include "chrome/browser/ui/tabs/tab_strip_model.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" |
8 #include "ui/base/ime/composition_text.h" | 13 #include "ui/base/ime/composition_text.h" |
9 #include "ui/base/ime/ime_bridge.h" | 14 #include "ui/base/ime/ime_bridge.h" |
10 #include "ui/base/ime/ime_input_context_handler_interface.h" | 15 #include "ui/base/ime/ime_input_context_handler_interface.h" |
11 #include "ui/events/keycodes/keyboard_code_conversion.h" | 16 #include "ui/events/keycodes/keyboard_code_conversion.h" |
12 | 17 |
13 namespace { | 18 namespace { |
14 | 19 |
15 const char kErrorFollowCursorWindowExists[] = | 20 const char kErrorFollowCursorWindowExists[] = |
16 "A follow cursor IME window exists."; | 21 "A follow cursor IME window exists."; |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 for (auto ime_window : normal_windows_) { | 183 for (auto ime_window : normal_windows_) { |
179 if (ime_window->GetFrameId() == window_id) | 184 if (ime_window->GetFrameId() == window_id) |
180 return ime_window; | 185 return ime_window; |
181 } | 186 } |
182 return nullptr; | 187 return nullptr; |
183 } | 188 } |
184 | 189 |
185 bool InputMethodEngine::SendKeyEvent(ui::KeyEvent* event, | 190 bool InputMethodEngine::SendKeyEvent(ui::KeyEvent* event, |
186 const std::string& code) { | 191 const std::string& code) { |
187 DCHECK(event); | 192 DCHECK(event); |
193 | |
194 // input.ime.sendKeyEvents API is only allowed to work on text fields. | |
195 if (current_input_type_ == ui::TEXT_INPUT_TYPE_NONE) | |
196 return false; | |
197 | |
188 if (event->key_code() == ui::VKEY_UNKNOWN) | 198 if (event->key_code() == ui::VKEY_UNKNOWN) |
189 event->set_key_code(ui::DomCodeToUsLayoutKeyboardCode(event->code())); | 199 event->set_key_code(ui::DomCodeToUsLayoutKeyboardCode(event->code())); |
190 | 200 |
191 ui::IMEInputContextHandlerInterface* input_context = | 201 ui::IMEInputContextHandlerInterface* input_context = |
192 ui::IMEBridge::Get()->GetInputContextHandler(); | 202 ui::IMEBridge::Get()->GetInputContextHandler(); |
193 if (!input_context) | 203 Browser* browser = chrome::FindLastActive(); |
Devlin
2016/06/17 16:22:43
I'm a little dubious of this - might it be possibl
Azure Wei
2016/06/21 10:04:58
The last active browser/web content could be diffe
| |
204 if (!input_context || !browser) | |
194 return false; | 205 return false; |
206 | |
207 content::WebContents* web_contents = | |
208 browser->tab_strip_model()->GetActiveWebContents(); | |
209 if (web_contents) { | |
210 GURL url = web_contents->GetController().GetActiveEntry()->GetURL(); | |
Devlin
2016/06/17 16:22:43
GetActiveEntry() is deprecated, and can also retur
Azure Wei
2016/06/21 10:04:58
Updated with GetLastCommittedURL(). Thanks.
| |
211 if (!url.is_empty() && !IsValidKeyForSpecialPage(url.spec(), event)) | |
212 return false; | |
213 } | |
214 | |
195 input_context->SendKeyEvent(event); | 215 input_context->SendKeyEvent(event); |
196 | 216 |
197 return true; | 217 return true; |
198 } | 218 } |
199 | 219 |
220 bool InputMethodEngine::IsValidKeyForSpecialPage(const std::string& url, | |
Devlin
2016/06/17 16:22:43
why not pass as a gurl?
Azure Wei
2016/06/21 10:04:58
Done.
| |
221 ui::KeyEvent* ui_event) { | |
222 // input.ime.sendKeyEvents API is not allowed to send ENTER et al. keys on | |
223 // some special pages. | |
224 // Make it a vector, so we can add other pages in the future. | |
225 std::vector<std::string> pages; | |
226 pages.push_back(std::string("chrome://")); | |
Devlin
2016/06/17 16:22:43
What about e.g. chrome-devtools?
Devlin
2016/06/17 16:22:43
use the scheme constant.
meacer
2016/06/21 01:06:37
Drive-by: I'd rather use a whitelist here than a b
Azure Wei
2016/06/21 10:04:58
Done.
| |
227 | |
228 std::vector<ui::KeyboardCode> keycodes; | |
229 keycodes.push_back(ui::VKEY_ESCAPE); | |
230 keycodes.push_back(ui::VKEY_TAB); | |
231 keycodes.push_back(ui::VKEY_RETURN); | |
meacer
2016/06/21 01:13:10
I suggested these keys in the thread but I'm not s
| |
232 | |
233 for (auto page : pages) { | |
234 if (url.substr(0, page.size()) == page) { | |
Devlin
2016/06/17 16:22:43
prefer GURL::SchemeIs()
Azure Wei
2016/06/21 10:04:58
Done.
| |
235 for (auto key : keycodes) { | |
236 if (key == ui_event->key_code()) { | |
237 return false; | |
238 } | |
239 } | |
240 } | |
241 } | |
242 return true; | |
243 } | |
244 | |
200 } // namespace input_method | 245 } // namespace input_method |
OLD | NEW |