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

Unified Diff: chrome/browser/extensions/extension_input_api.cc

Issue 3130029: Lands http://codereview.chromium.org/3153008 for bryeung: (Closed)
Patch Set: YAM Created 10 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/extension_input_api.cc
diff --git a/chrome/browser/extensions/extension_input_api.cc b/chrome/browser/extensions/extension_input_api.cc
new file mode 100644
index 0000000000000000000000000000000000000000..40c4bc086e8c85a8d2bae7a570759a407dda361b
--- /dev/null
+++ b/chrome/browser/extensions/extension_input_api.cc
@@ -0,0 +1,120 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/extensions/extension_input_api.h"
+
+#include <string>
+
+#include "base/values.h"
+#include "base/keyboard_code_conversion.h"
+#include "chrome/browser/browser.h"
+#include "chrome/browser/browser_window.h"
+#include "chrome/browser/extensions/extension_tabs_module.h"
+#include "chrome/browser/renderer_host/render_view_host.h"
+#include "chrome/browser/tab_contents/tab_contents.h"
+#include "chrome/browser/views/frame/browser_view.h"
+#include "chrome/common/native_web_keyboard_event.h"
+#include "third_party/WebKit/WebKit/chromium/public/WebInputEvent.h"
+#include "views/event.h"
+#include "views/widget/root_view.h"
+
+namespace {
+
+// Keys.
+const char kType[] = "type";
+const char kKeyIdentifier[] = "keyIdentifier";
+const char kAlt[] = "altKey";
+const char kCtrl[] = "ctrlKey";
+const char kMeta[] = "metaKey";
+const char kShift[] = "shiftKey";
+const char kKeyDown[] = "keydown";
+const char kKeyUp[] = "keyup";
+
+// Errors.
+const char kUnknownEventTypeError[] = "Unknown event type.";
+const char kUnknownOrUnsupportedKeyIdentiferError[] = "Unknown or unsupported "
+ "key identifier.";
+const char kNoValidRecipientError[] = "No valid recipient for event.";
+const char kKeyEventUnprocessedError[] = "Event was not handled.";
+
+views::Event::EventType GetTypeFromString(const std::string& type) {
+ if (type == kKeyDown) {
+ return views::Event::ET_KEY_PRESSED;
+ } else if (type == kKeyUp) {
+ return views::Event::ET_KEY_RELEASED;
+ }
+ return views::Event::ET_UNKNOWN;
+}
+
+} // namespace
+
+void InputFunction::Run() {
+ SendResponse(RunImpl());
+}
+
+views::RootView* SendKeyboardEventInputFunction::GetRootView() {
+ Browser* browser = GetCurrentBrowser();
+ if (!browser)
+ return NULL;
+
+ BrowserWindow* window = browser->window();
+ if (!window)
+ return NULL;
+
+ BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow(
+ window->GetNativeHandle());
+ if (!browser_view)
+ return NULL;
+
+ return browser_view->GetRootView();
+}
+
+bool SendKeyboardEventInputFunction::RunImpl() {
+ DictionaryValue* args;
+ EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args));
+
+ std::string type_name;
+ EXTENSION_FUNCTION_VALIDATE(args->GetString(kType, &type_name));
+ views::Event::EventType type = GetTypeFromString(type_name);
+ if (type == views::Event::ET_UNKNOWN) {
+ error_ = kUnknownEventTypeError;
+ return false;
+ }
+
+ std::string identifier;
+ EXTENSION_FUNCTION_VALIDATE(args->GetString(kKeyIdentifier, &identifier));
+ base::KeyboardCode code = base::KeyCodeFromKeyIdentifier(identifier);
+ if (code == base::VKEY_UNKNOWN) {
+ error_ = kUnknownOrUnsupportedKeyIdentiferError;
+ return false;
+ }
+
+ int flags = 0;
+ bool alt = false;
+ if (args->GetBoolean(kAlt, &alt))
+ flags |= alt ? WebKit::WebInputEvent::AltKey : 0;
+ bool ctrl = false;
+ if (args->GetBoolean(kCtrl, &ctrl))
+ flags |= ctrl ? WebKit::WebInputEvent::ControlKey : 0;
+ bool meta = false;
+ if (args->GetBoolean(kMeta, &meta))
+ flags |= meta ? WebKit::WebInputEvent::MetaKey : 0;
+ bool shift = false;
+ if (args->GetBoolean(kShift, &shift))
+ flags |= shift ? WebKit::WebInputEvent::ShiftKey : 0;
+
+ views::RootView* root_view = GetRootView();
+ if (!root_view) {
+ error_ = kNoValidRecipientError;
+ return false;
+ }
+
+ views::KeyEvent event(type, code, flags, 0, 0);
+ if (!root_view->ProcessKeyEvent(event)) {
+ error_ = kKeyEventUnprocessedError;
+ return false;
+ }
+
+ return true;
+}
« no previous file with comments | « chrome/browser/extensions/extension_input_api.h ('k') | chrome/browser/extensions/extension_input_apitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698