| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/extensions/extension_input_api.h" | 5 #include "chrome/browser/extensions/extension_input_api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/extensions/extension_tabs_module.h" | |
| 12 #include "chrome/browser/extensions/key_identifier_conversion_views.h" | 11 #include "chrome/browser/extensions/key_identifier_conversion_views.h" |
| 13 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_window.h" | 13 #include "chrome/browser/ui/browser_window.h" |
| 15 #include "chrome/browser/ui/views/frame/browser_view.h" | 14 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 16 #include "content/browser/renderer_host/render_view_host.h" | |
| 17 #include "content/common/native_web_keyboard_event.h" | |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" | |
| 19 #include "views/events/event.h" | 15 #include "views/events/event.h" |
| 20 #include "views/ime/input_method.h" | 16 #include "views/ime/input_method.h" |
| 21 #include "views/widget/root_view.h" | |
| 22 #include "views/widget/widget.h" | 17 #include "views/widget/widget.h" |
| 23 | 18 |
| 24 namespace { | 19 namespace { |
| 25 | 20 |
| 26 // Keys. | 21 // Keys. |
| 27 const char kType[] = "type"; | 22 const char kType[] = "type"; |
| 28 const char kKeyIdentifier[] = "keyIdentifier"; | 23 const char kKeyIdentifier[] = "keyIdentifier"; |
| 29 const char kAlt[] = "altKey"; | 24 const char kAlt[] = "altKey"; |
| 30 const char kCtrl[] = "ctrlKey"; | 25 const char kCtrl[] = "ctrlKey"; |
| 31 const char kMeta[] = "metaKey"; | 26 const char kMeta[] = "metaKey"; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 49 } | 44 } |
| 50 return ui::ET_UNKNOWN; | 45 return ui::ET_UNKNOWN; |
| 51 } | 46 } |
| 52 | 47 |
| 53 } // namespace | 48 } // namespace |
| 54 | 49 |
| 55 void InputFunction::Run() { | 50 void InputFunction::Run() { |
| 56 SendResponse(RunImpl()); | 51 SendResponse(RunImpl()); |
| 57 } | 52 } |
| 58 | 53 |
| 59 views::RootView* SendKeyboardEventInputFunction::GetRootView() { | 54 views::Widget* SendKeyboardEventInputFunction::GetTopLevelWidget() { |
| 60 Browser* browser = GetCurrentBrowser(); | 55 Browser* browser = GetCurrentBrowser(); |
| 61 if (!browser) | 56 if (!browser) |
| 62 return NULL; | 57 return NULL; |
| 63 | 58 |
| 64 BrowserWindow* window = browser->window(); | 59 BrowserWindow* window = browser->window(); |
| 65 if (!window) | 60 if (!window) |
| 66 return NULL; | 61 return NULL; |
| 67 | 62 |
| 68 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow( | 63 BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow( |
| 69 window->GetNativeHandle()); | 64 window->GetNativeHandle()); |
| 70 if (!browser_view) | 65 return browser_view ? browser_view->GetWidget() : NULL; |
| 71 return NULL; | |
| 72 | |
| 73 return browser_view->GetRootView(); | |
| 74 } | 66 } |
| 75 | 67 |
| 76 bool SendKeyboardEventInputFunction::RunImpl() { | 68 bool SendKeyboardEventInputFunction::RunImpl() { |
| 77 DictionaryValue* args; | 69 DictionaryValue* args; |
| 78 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); | 70 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); |
| 79 | 71 |
| 80 std::string type_name; | 72 std::string type_name; |
| 81 EXTENSION_FUNCTION_VALIDATE(args->GetString(kType, &type_name)); | 73 EXTENSION_FUNCTION_VALIDATE(args->GetString(kType, &type_name)); |
| 82 ui::EventType type = GetTypeFromString(type_name); | 74 ui::EventType type = GetTypeFromString(type_name); |
| 83 if (type == ui::ET_UNKNOWN) { | 75 if (type == ui::ET_UNKNOWN) { |
| 84 error_ = kUnknownEventTypeError; | 76 error_ = kUnknownEventTypeError; |
| 85 return false; | 77 return false; |
| 86 } | 78 } |
| 87 | 79 |
| 88 std::string identifier; | 80 std::string identifier; |
| 89 EXTENSION_FUNCTION_VALIDATE(args->GetString(kKeyIdentifier, &identifier)); | 81 EXTENSION_FUNCTION_VALIDATE(args->GetString(kKeyIdentifier, &identifier)); |
| 90 TrimWhitespaceASCII(identifier, TRIM_ALL, &identifier); | 82 TrimWhitespaceASCII(identifier, TRIM_ALL, &identifier); |
| 91 | 83 |
| 92 const views::KeyEvent& prototype_event = | 84 const views::KeyEvent& prototype_event = |
| 93 KeyEventFromKeyIdentifier(identifier); | 85 KeyEventFromKeyIdentifier(identifier); |
| 94 if (prototype_event.key_code() == ui::VKEY_UNKNOWN) { | 86 if (prototype_event.key_code() == ui::VKEY_UNKNOWN) { |
| 95 error_ = kUnknownOrUnsupportedKeyIdentiferError; | 87 error_ = kUnknownOrUnsupportedKeyIdentiferError; |
| 96 return false; | 88 return false; |
| 97 } | 89 } |
| 98 | 90 |
| 91 bool flag = false; |
| 99 int flags = prototype_event.flags(); | 92 int flags = prototype_event.flags(); |
| 100 bool alt = false; | 93 flags |= (args->GetBoolean(kAlt, &flag) && flag) ? ui::EF_ALT_DOWN : 0; |
| 101 if (args->GetBoolean(kAlt, &alt)) | 94 flags |= (args->GetBoolean(kCtrl, &flag) && flag) ? ui::EF_CONTROL_DOWN : 0; |
| 102 flags |= alt ? ui::EF_ALT_DOWN : 0; | 95 flags |= (args->GetBoolean(kShift, &flag) && flag) ? ui::EF_SHIFT_DOWN : 0; |
| 103 bool ctrl = false; | 96 if (args->GetBoolean(kMeta, &flag) && flag) { |
| 104 if (args->GetBoolean(kCtrl, &ctrl)) | |
| 105 flags |= ctrl ? ui::EF_CONTROL_DOWN : 0; | |
| 106 bool shift = false; | |
| 107 if (args->GetBoolean(kShift, &shift)) | |
| 108 flags |= shift ? ui::EF_SHIFT_DOWN : 0; | |
| 109 bool meta = false; | |
| 110 if (args->GetBoolean(kMeta, &meta)) { | |
| 111 // Views does not have a Meta event flag, so return an error for now. | 97 // Views does not have a Meta event flag, so return an error for now. |
| 112 if (meta) { | 98 error_ = kUnsupportedModifier; |
| 113 error_ = kUnsupportedModifier; | 99 return false; |
| 114 return false; | |
| 115 } | |
| 116 } | 100 } |
| 117 | 101 |
| 118 views::RootView* root_view = GetRootView(); | 102 views::Widget* widget = GetTopLevelWidget(); |
| 119 if (!root_view) { | 103 if (!widget) { |
| 120 error_ = kNoValidRecipientError; | 104 error_ = kNoValidRecipientError; |
| 121 return false; | 105 return false; |
| 122 } | 106 } |
| 123 | 107 |
| 124 views::KeyEvent event(type, prototype_event.key_code(), flags); | 108 views::KeyEvent event(type, prototype_event.key_code(), flags); |
| 125 views::InputMethod* ime = root_view->GetWidget()->GetInputMethod(); | 109 views::InputMethod* ime = widget->GetInputMethod(); |
| 126 if (ime) { | 110 if (ime) { |
| 127 ime->DispatchKeyEvent(event); | 111 ime->DispatchKeyEvent(event); |
| 128 } else if (!root_view->ProcessKeyEvent(event)) { | 112 } else if (!widget->OnKeyEvent(event)) { |
| 129 error_ = kKeyEventUnprocessedError; | 113 error_ = kKeyEventUnprocessedError; |
| 130 return false; | 114 return false; |
| 131 } | 115 } |
| 132 | 116 |
| 133 return true; | 117 return true; |
| 134 } | 118 } |
| OLD | NEW |