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..7dd2293ffe8bdaab33b3fd975bbf3a9b39026bad 100644 |
| --- a/chrome/browser/ui/input_method/input_method_engine.cc |
| +++ b/chrome/browser/ui/input_method/input_method_engine.cc |
| @@ -4,6 +4,11 @@ |
| #include "chrome/browser/ui/input_method/input_method_engine.h" |
| +#include <vector> |
| + |
| +#include "chrome/browser/ui/browser_finder.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| +#include "content/public/browser/navigation_entry.h" |
| #include "content/public/browser/render_frame_host.h" |
| #include "ui/base/ime/composition_text.h" |
| #include "ui/base/ime/ime_bridge.h" |
| @@ -185,16 +190,56 @@ 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())); |
| ui::IMEInputContextHandlerInterface* input_context = |
| ui::IMEBridge::Get()->GetInputContextHandler(); |
| - if (!input_context) |
| + 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
|
| + if (!input_context || !browser) |
| return false; |
| + |
| + content::WebContents* web_contents = |
| + browser->tab_strip_model()->GetActiveWebContents(); |
| + if (web_contents) { |
| + 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.
|
| + if (!url.is_empty() && !IsValidKeyForSpecialPage(url.spec(), event)) |
| + return false; |
| + } |
| + |
| input_context->SendKeyEvent(event); |
| return true; |
| } |
| +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.
|
| + ui::KeyEvent* ui_event) { |
| + // input.ime.sendKeyEvents API is not allowed to send ENTER et al. keys on |
| + // some special pages. |
| + // Make it a vector, so we can add other pages in the future. |
| + std::vector<std::string> pages; |
| + 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.
|
| + |
| + std::vector<ui::KeyboardCode> keycodes; |
| + keycodes.push_back(ui::VKEY_ESCAPE); |
| + keycodes.push_back(ui::VKEY_TAB); |
| + 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
|
| + |
| + for (auto page : pages) { |
| + 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.
|
| + for (auto key : keycodes) { |
| + if (key == ui_event->key_code()) { |
| + return false; |
| + } |
| + } |
| + } |
| + } |
| + return true; |
| +} |
| + |
| } // namespace input_method |