Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(390)

Side by Side Diff: chrome/browser/ui/input_method/input_method_engine.cc

Issue 2077783002: Make limitations for input.ime.sendKeyEvents API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: onKeyEvent should not work in password. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
21 #include "url/gurl.h"
12 22
13 namespace { 23 namespace {
14 24
15 const char kErrorFollowCursorWindowExists[] = 25 const char kErrorFollowCursorWindowExists[] =
16 "A follow cursor IME window exists."; 26 "A follow cursor IME window exists.";
17 const char kErrorNoInputFocus[] = 27 const char kErrorNoInputFocus[] =
18 "The follow cursor IME window cannot be created without an input focus."; 28 "The follow cursor IME window cannot be created without an input focus.";
19 const char kErrorReachMaxWindowCount[] = 29 const char kErrorReachMaxWindowCount[] =
20 "Cannot create more than 5 normal IME windows."; 30 "Cannot create more than 5 normal IME windows.";
21 31
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 for (auto ime_window : normal_windows_) { 188 for (auto ime_window : normal_windows_) {
179 if (ime_window->GetFrameId() == window_id) 189 if (ime_window->GetFrameId() == window_id)
180 return ime_window; 190 return ime_window;
181 } 191 }
182 return nullptr; 192 return nullptr;
183 } 193 }
184 194
185 bool InputMethodEngine::SendKeyEvent(ui::KeyEvent* event, 195 bool InputMethodEngine::SendKeyEvent(ui::KeyEvent* event,
186 const std::string& code) { 196 const std::string& code) {
187 DCHECK(event); 197 DCHECK(event);
198
199 // input.ime.sendKeyEvents API is only allowed to work on text fields.
200 if (current_input_type_ == ui::TEXT_INPUT_TYPE_NONE)
201 return false;
202
188 if (event->key_code() == ui::VKEY_UNKNOWN) 203 if (event->key_code() == ui::VKEY_UNKNOWN)
189 event->set_key_code(ui::DomCodeToUsLayoutKeyboardCode(event->code())); 204 event->set_key_code(ui::DomCodeToUsLayoutKeyboardCode(event->code()));
190 205
191 ui::IMEInputContextHandlerInterface* input_context = 206 ui::IMEInputContextHandlerInterface* input_context =
192 ui::IMEBridge::Get()->GetInputContextHandler(); 207 ui::IMEBridge::Get()->GetInputContextHandler();
193 if (!input_context) 208 if (!input_context)
194 return false; 209 return false;
210
211 // ENTER et al. keys are allowed to work only on http:, https: etc.
212 if (!IsValidKeyForAllPages(event)) {
213 if (IsSpecialPage(input_context->GetInputMethod()))
214 return false;
215 }
216
195 input_context->SendKeyEvent(event); 217 input_context->SendKeyEvent(event);
196
197 return true; 218 return true;
198 } 219 }
199 220
221 bool InputMethodEngine::IsSpecialPage(ui::InputMethod* input_method) {
222 Browser* browser = chrome::FindLastActive();
223 DCHECK(browser);
224
225 content::WebContents* web_contents =
226 browser->tab_strip_model()->GetActiveWebContents();
227 if (!web_contents)
228 return true;
229
230 // If the input method get from last active browser is different from the
231 // input method get from input context, it means we can't get the real url of
232 // the input view, treats |url| as special page anyway for security concerns.
233 if (browser->window()->GetNativeWindow()->GetHost()->GetInputMethod() !=
234 input_method)
235 return true;
236
237 GURL url = web_contents->GetLastCommittedURL();
238 // If we can't determine the last committed url, treat it as special.
239 if (!url.is_valid())
240 return true;
241
242 // Checks if the last committed url has the whitelisted sheme.
243 std::vector<const char*> whitelist_schemes{url::kFtpScheme, url::kHttpScheme,
244 url::kHttpsScheme};
245 for (auto scheme : whitelist_schemes) {
246 if (url.SchemeIs(scheme))
247 return false;
248 }
249
250 // Checks if the last committed url is whitelisted url.
251 url::Origin origin(url);
252 std::vector<GURL> whitelist_urls{GURL(url::kAboutBlankURL),
253 GURL(chrome::kChromeUINewTabURL)};
254 for (const GURL& whitelist_url : whitelist_urls) {
255 if (url::Origin(whitelist_url).IsSameOriginWith(origin))
256 return false;
257 }
258 return true;
259 }
260
261 bool InputMethodEngine::IsValidKeyForAllPages(ui::KeyEvent* ui_event) {
262 // Whitelists all character keys except for Enter and Tab keys.
263 std::vector<ui::KeyboardCode> invalid_character_keycodes{ui::VKEY_TAB,
264 ui::VKEY_RETURN};
265 if (ui_event->GetDomKey().IsCharacter() && !ui_event->IsControlDown() &&
266 !ui_event->IsCommandDown())
267 return std::count(invalid_character_keycodes.begin(),
Devlin 2016/06/30 14:35:46 nitty nit: prefer find() for vectors so that it ca
Azure Wei 2016/06/30 14:58:46 Done. Many thanks.
268 invalid_character_keycodes.end(),
269 ui_event->key_code()) == 0;
270
271 // Whitelists Backspace key and arrow keys.
272 std::vector<ui::KeyboardCode> whitelist_keycodes{
273 ui::VKEY_BACK, ui::VKEY_LEFT, ui::VKEY_RIGHT, ui::VKEY_UP, ui::VKEY_DOWN};
274 return std::count(whitelist_keycodes.begin(), whitelist_keycodes.end(),
275 ui_event->key_code()) != 0;
276 }
277
200 } // namespace input_method 278 } // namespace input_method
OLDNEW
« no previous file with comments | « chrome/browser/ui/input_method/input_method_engine.h ('k') | chrome/browser/ui/input_method/input_method_engine_base.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698