Chromium Code Reviews| Index: chrome/browser/ui/input_method/input_method_engine.cc |
| diff --git a/chrome/browser/ui/input_method/input_method_engine.cc b/chrome/browser/ui/input_method/input_method_engine.cc |
| index 81a8f4cac492fad5ae3dff8a4eca70e5d67a154e..60a3a459dd9dda21aa69292a4e4858710f5b94db 100644 |
| --- a/chrome/browser/ui/input_method/input_method_engine.cc |
| +++ b/chrome/browser/ui/input_method/input_method_engine.cc |
| @@ -4,7 +4,16 @@ |
| #include "chrome/browser/ui/input_method/input_method_engine.h" |
| +#include "chrome/browser/ui/browser_finder.h" |
| +#include "chrome/browser/ui/browser_window.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| +#include "chrome/common/url_constants.h" |
| +#include "content/public/browser/navigation_entry.h" |
| #include "content/public/browser/render_frame_host.h" |
| +#include "content/public/common/url_constants.h" |
| +#include "extensions/common/constants.h" |
| +#include "ui/aura/window.h" |
| +#include "ui/aura/window_tree_host.h" |
| #include "ui/base/ime/composition_text.h" |
| #include "ui/base/ime/ime_bridge.h" |
| #include "ui/base/ime/ime_input_context_handler_interface.h" |
| @@ -185,6 +194,11 @@ ui::ImeWindow* InputMethodEngine::FindWindowById(int window_id) const { |
| bool InputMethodEngine::SendKeyEvent(ui::KeyEvent* event, |
| const std::string& code) { |
| DCHECK(event); |
| + |
| + // input.ime.sendKeyEvents API is only allowed to work on text fields. |
| + if (current_input_type_ == ui::TEXT_INPUT_TYPE_NONE) |
| + return false; |
| + |
| if (event->key_code() == ui::VKEY_UNKNOWN) |
| event->set_key_code(ui::DomCodeToUsLayoutKeyboardCode(event->code())); |
| @@ -192,9 +206,82 @@ bool InputMethodEngine::SendKeyEvent(ui::KeyEvent* event, |
| ui::IMEBridge::Get()->GetInputContextHandler(); |
| if (!input_context) |
| return false; |
| + |
| + Browser* browser = chrome::FindLastActive(); |
| + DCHECK(browser); |
| + content::WebContents* web_contents = |
| + browser->tab_strip_model()->GetActiveWebContents(); |
| + ui::InputMethod* input_method = |
| + browser->window()->GetNativeWindow()->GetHost()->GetInputMethod(); |
| + |
| + if (web_contents) { |
| + GURL url = web_contents->GetLastCommittedURL(); |
| + // If |input_context->GetInputMethod() != input_method|, treats |url| as |
| + // special page no mater what it is. |
| + bool url_trustable = input_context->GetInputMethod() == input_method; |
| + if (!url.is_empty() && !IsValidKeyForSpecialPage(url, url_trustable, event)) |
| + return false; |
| + } |
| + |
| input_context->SendKeyEvent(event); |
| return true; |
| } |
| +bool InputMethodEngine::IsValidKeyForSpecialPage(const GURL& url, |
| + bool url_trustable, |
| + ui::KeyEvent* ui_event) { |
| + // input.ime.sendKeyEvents API is not allowed to send ENTER et al. keys on |
| + // some special pages (like chrome://). |
| + std::vector<const char*> whitelist_schemes; |
| + 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.
|
| + whitelist_schemes.push_back(url::kHttpScheme); |
| + whitelist_schemes.push_back(url::kHttpsScheme); |
| + |
|
Devlin
2016/06/22 16:33:12
meacer@, do you think we can add file:// urls here
|
| + 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.
|
| + whitelist_urls.push_back(url::kAboutBlankURL); |
| + whitelist_urls.push_back(chrome::kChromeUINewTabURL); |
| + |
| + // Checks if the current url is special url. |
| + if (url_trustable) { |
| + for (auto scheme : whitelist_schemes) { |
| + if (url.SchemeIs(scheme)) |
| + return true; |
| + } |
| + for (auto whitelist_url : whitelist_urls) { |
| + 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.
|
| + return true; |
| + } |
| + } |
| + |
| + std::vector<ui::KeyboardCode> whitelist_keycodes; |
| + whitelist_keycodes.push_back(ui::VKEY_BACK); |
| + whitelist_keycodes.push_back(ui::VKEY_LEFT); |
| + whitelist_keycodes.push_back(ui::VKEY_RIGHT); |
| + whitelist_keycodes.push_back(ui::VKEY_UP); |
| + whitelist_keycodes.push_back(ui::VKEY_DOWN); |
| + |
| + std::vector<ui::KeyboardCode> invalid_character_keycodes; |
| + invalid_character_keycodes.push_back(ui::VKEY_TAB); |
| + invalid_character_keycodes.push_back(ui::VKEY_RETURN); |
| + |
| + // Whitelists all character keys. |
| + if (ui_event->GetDomKey().IsCharacter() && !ui_event->IsControlDown() && |
| + !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
|
| + 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.
|
| + if (ui_event->key_code() == invalid_key) |
| + return false; |
| + } |
| + return true; |
| + } |
| + |
| + // Checks if the key event is white listed key. |
| + for (auto key : whitelist_keycodes) { |
| + if (ui_event->key_code() == key) |
| + return true; |
| + } |
| + |
| + return false; |
| +} |
| + |
| } // namespace input_method |