| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/extensions/key_identifier_conversion_views.h" | 12 #include "chrome/browser/extensions/key_identifier_conversion_views.h" |
| 13 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_window.h" | 14 #include "chrome/browser/ui/browser_window.h" |
| 15 #include "chrome/browser/ui/views/frame/browser_view.h" | 15 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 16 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "ui/views/events/event.h" | 17 #include "ui/views/events/event.h" |
| 18 #include "ui/views/ime/input_method.h" | 18 #include "ui/views/ime/input_method.h" |
| 19 #include "ui/views/views_delegate.h" | 19 #include "ui/views/views_delegate.h" |
| 20 #include "ui/views/widget/widget.h" | 20 #include "ui/views/widget/widget.h" |
| 21 | 21 |
| 22 #if defined(USE_VIRTUAL_KEYBOARD) | |
| 23 #include "content/public/browser/notification_service.h" | |
| 24 #endif | |
| 25 | |
| 26 #if defined(OS_CHROMEOS) && defined(USE_VIRTUAL_KEYBOARD) | |
| 27 #include "chrome/browser/chromeos/input_method/input_method_manager.h" | |
| 28 #include "chrome/browser/chromeos/login/base_login_display_host.h" | |
| 29 #endif | |
| 30 | |
| 31 namespace { | 22 namespace { |
| 32 | 23 |
| 33 // Keys. | 24 // Keys. |
| 34 const char kType[] = "type"; | 25 const char kType[] = "type"; |
| 35 const char kKeyIdentifier[] = "keyIdentifier"; | 26 const char kKeyIdentifier[] = "keyIdentifier"; |
| 36 const char kAlt[] = "altKey"; | 27 const char kAlt[] = "altKey"; |
| 37 const char kCtrl[] = "ctrlKey"; | 28 const char kCtrl[] = "ctrlKey"; |
| 38 const char kMeta[] = "metaKey"; | 29 const char kMeta[] = "metaKey"; |
| 39 const char kShift[] = "shiftKey"; | 30 const char kShift[] = "shiftKey"; |
| 40 const char kKeyDown[] = "keydown"; | 31 const char kKeyDown[] = "keydown"; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 66 (key_identifier.substr(2).find_first_not_of("0123456789abcdefABCDEF") == | 57 (key_identifier.substr(2).find_first_not_of("0123456789abcdefABCDEF") == |
| 67 std::string::npos)) { | 58 std::string::npos)) { |
| 68 const bool result = | 59 const bool result = |
| 69 base::HexStringToInt(key_identifier.substr(2), &character); | 60 base::HexStringToInt(key_identifier.substr(2), &character); |
| 70 DCHECK(result) << key_identifier; | 61 DCHECK(result) << key_identifier; |
| 71 } | 62 } |
| 72 return character; | 63 return character; |
| 73 } | 64 } |
| 74 | 65 |
| 75 views::Widget* GetTopLevelWidget(Browser* browser) { | 66 views::Widget* GetTopLevelWidget(Browser* browser) { |
| 76 #if defined(OS_CHROMEOS) && defined(USE_VIRTUAL_KEYBOARD) | |
| 77 chromeos::LoginDisplayHost* host = | |
| 78 chromeos::BaseLoginDisplayHost::default_host(); | |
| 79 if (host) | |
| 80 return views::Widget::GetWidgetForNativeWindow(host->GetNativeWindow()); | |
| 81 #endif | |
| 82 | |
| 83 if (!browser) | 67 if (!browser) |
| 84 return NULL; | 68 return NULL; |
| 85 | 69 |
| 86 BrowserWindow* window = browser->window(); | 70 BrowserWindow* window = browser->window(); |
| 87 if (!window) | 71 if (!window) |
| 88 return NULL; | 72 return NULL; |
| 89 | 73 |
| 90 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser); | 74 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser); |
| 91 return browser_view ? browser_view->GetWidget() : NULL; | 75 return browser_view ? browser_view->GetWidget() : NULL; |
| 92 } | 76 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 views::InputMethod* ime = widget->GetInputMethod(); | 133 views::InputMethod* ime = widget->GetInputMethod(); |
| 150 if (ime) { | 134 if (ime) { |
| 151 ime->DispatchKeyEvent(event); | 135 ime->DispatchKeyEvent(event); |
| 152 } else if (!widget->OnKeyEvent(event)) { | 136 } else if (!widget->OnKeyEvent(event)) { |
| 153 error_ = kKeyEventUnprocessedError; | 137 error_ = kKeyEventUnprocessedError; |
| 154 return false; | 138 return false; |
| 155 } | 139 } |
| 156 | 140 |
| 157 return true; | 141 return true; |
| 158 } | 142 } |
| 159 | |
| 160 #if defined(USE_VIRTUAL_KEYBOARD) | |
| 161 bool HideKeyboardFunction::RunImpl() { | |
| 162 content::NotificationService::current()->Notify( | |
| 163 chrome::NOTIFICATION_HIDE_KEYBOARD_INVOKED, | |
| 164 content::Source<HideKeyboardFunction>(this), | |
| 165 content::NotificationService::NoDetails()); | |
| 166 return true; | |
| 167 } | |
| 168 | |
| 169 bool SetKeyboardHeightFunction::RunImpl() { | |
| 170 int height = 0; | |
| 171 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &height)); | |
| 172 | |
| 173 if (height < 0) { | |
| 174 error_ = kInvalidHeight; | |
| 175 return false; | |
| 176 } | |
| 177 | |
| 178 // TODO(penghuang) Check the height is not greater than height of browser view | |
| 179 // and set the height of virtual keyboard directly instead of using | |
| 180 // notification. | |
| 181 content::NotificationService::current()->Notify( | |
| 182 chrome::NOTIFICATION_SET_KEYBOARD_HEIGHT_INVOKED, | |
| 183 content::Source<SetKeyboardHeightFunction>(this), | |
| 184 content::Details<int>(&height)); | |
| 185 return true; | |
| 186 } | |
| 187 #endif | |
| 188 | |
| 189 #if defined(OS_CHROMEOS) && defined(USE_VIRTUAL_KEYBOARD) | |
| 190 // TODO(yusukes): This part should be moved to extension_input_api_chromeos.cc. | |
| 191 bool SendHandwritingStrokeFunction::RunImpl() { | |
| 192 // TODO(yusukes): Add a parameter for an input context ID. | |
| 193 ListValue* value = NULL; | |
| 194 EXTENSION_FUNCTION_VALIDATE(args_->GetList(0, &value)); | |
| 195 | |
| 196 chromeos::input_method::HandwritingStroke stroke; | |
| 197 for (size_t i = 0; i < value->GetSize(); ++i) { | |
| 198 DictionaryValue* dict; | |
| 199 double x = 0.0; | |
| 200 double y = 0.0; | |
| 201 EXTENSION_FUNCTION_VALIDATE(value->GetDictionary(i, &dict)); | |
| 202 EXTENSION_FUNCTION_VALIDATE(dict->GetDouble("x", &x)); | |
| 203 EXTENSION_FUNCTION_VALIDATE(dict->GetDouble("y", &y)); | |
| 204 stroke.push_back(std::make_pair(x, y)); | |
| 205 } | |
| 206 | |
| 207 views::Widget* widget = GetTopLevelWidget(GetCurrentBrowser()); | |
| 208 views::InputMethod* ime = widget ? widget->GetInputMethod() : NULL; | |
| 209 if (ime) { | |
| 210 static const views::KeyEvent* dummy_keydown = new views::KeyEvent( | |
| 211 ui::ET_KEY_PRESSED, ui::VKEY_UNKNOWN, 0); | |
| 212 static const views::KeyEvent* dummy_keyup = new views::KeyEvent( | |
| 213 ui::ET_KEY_RELEASED, ui::VKEY_UNKNOWN, 0); | |
| 214 // These fake key events are necessary for clearing |suppress_next_result_| | |
| 215 // flag in view/ime/input_method_*.cc. Otherwise, clicking a candidate in | |
| 216 // the candidate window might be ignored. | |
| 217 ime->DispatchKeyEvent(*dummy_keydown); | |
| 218 ime->DispatchKeyEvent(*dummy_keyup); | |
| 219 } | |
| 220 | |
| 221 chromeos::input_method::InputMethodManager::GetInstance()-> | |
| 222 SendHandwritingStroke(stroke); | |
| 223 return true; | |
| 224 } | |
| 225 | |
| 226 bool CancelHandwritingStrokesFunction::RunImpl() { | |
| 227 // TODO(yusukes): Add a parameter for an input context ID. | |
| 228 int stroke_count = 0; // zero means 'clear all strokes'. | |
| 229 if (HasOptionalArgument(0)) { | |
| 230 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &stroke_count)); | |
| 231 EXTENSION_FUNCTION_VALIDATE(stroke_count >= 0); | |
| 232 } | |
| 233 chromeos::input_method::InputMethodManager::GetInstance()-> | |
| 234 CancelHandwritingStrokes(stroke_count); | |
| 235 return true; | |
| 236 } | |
| 237 #endif | |
| OLD | NEW |