summaryrefslogtreecommitdiffstats
path: root/ui
diff options
context:
space:
mode:
authorbshe@chromium.org <bshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-29 20:50:19 +0000
committerbshe@chromium.org <bshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-29 20:50:19 +0000
commit2f866dbf1f44a6b24d1c264844d8de2538bc72bb (patch)
tree379d7669e6d3081028ba56c965d60a58e354d7e0 /ui
parenta5058a2d3f50e11dc444dcf760991210b0938bee (diff)
downloadchromium_src-2f866dbf1f44a6b24d1c264844d8de2538bc72bb.zip
chromium_src-2f866dbf1f44a6b24d1c264844d8de2538bc72bb.tar.gz
chromium_src-2f866dbf1f44a6b24d1c264844d8de2538bc72bb.tar.bz2
To get input type of current input field, call
chrome.send('getInputContext'). And the input type will be returned in a callback function named GetInputContextCallback. BUG=268949 Review URL: https://chromiumcodereview.appspot.com/22642015 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@220359 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r--ui/keyboard/keyboard_ui_handler.cc39
-rw-r--r--ui/keyboard/keyboard_ui_handler.h4
-rw-r--r--ui/keyboard/resources/webui/api_adapter.js56
3 files changed, 99 insertions, 0 deletions
diff --git a/ui/keyboard/keyboard_ui_handler.cc b/ui/keyboard/keyboard_ui_handler.cc
index 0c41267..398d2f4 100644
--- a/ui/keyboard/keyboard_ui_handler.cc
+++ b/ui/keyboard/keyboard_ui_handler.cc
@@ -12,7 +12,11 @@
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view.h"
#include "content/public/browser/web_ui.h"
+#include "ui/aura/client/aura_constants.h"
+#include "ui/aura/root_window.h"
#include "ui/aura/window.h"
+#include "ui/base/ime/input_method.h"
+#include "ui/base/ime/text_input_client.h"
#include "ui/keyboard/keyboard_util.h"
namespace keyboard {
@@ -28,6 +32,10 @@ void KeyboardUIHandler::RegisterMessages() {
"insertText",
base::Bind(&KeyboardUIHandler::HandleInsertTextMessage,
base::Unretained(this)));
+ web_ui()->RegisterMessageCallback(
+ "getInputContext",
+ base::Bind(&KeyboardUIHandler::HandleGetInputContextMessage,
+ base::Unretained(this)));
}
void KeyboardUIHandler::HandleInsertTextMessage(const base::ListValue* args) {
@@ -48,4 +56,35 @@ void KeyboardUIHandler::HandleInsertTextMessage(const base::ListValue* args) {
LOG(ERROR) << "insertText failed";
}
+void KeyboardUIHandler::HandleGetInputContextMessage(
+ const base::ListValue* args) {
+ int request_id;
+ if (!args->GetInteger(0, &request_id)) {
+ LOG(ERROR) << "getInputContext failed: bad argument";
+ return;
+ }
+ base::DictionaryValue results;
+ results.SetInteger("requestId", request_id);
+
+ aura::RootWindow* root_window =
+ web_ui()->GetWebContents()->GetView()->GetNativeView()->GetRootWindow();
+ if (!root_window) {
+ LOG(ERROR) << "getInputContext failed: no root window";
+ return;
+ }
+ ui::InputMethod* input_method =
+ root_window->GetProperty(aura::client::kRootWindowInputMethodKey);
+ if (!input_method) {
+ LOG(ERROR) << "getInputContext failed: no input method";
+ return;
+ }
+
+ ui::TextInputClient* tic = input_method->GetTextInputClient();
+ results.SetInteger("type",
+ tic ? tic->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE);
+
+ web_ui()->CallJavascriptFunction("GetInputContextCallback",
+ results);
+}
+
} // namespace keyboard
diff --git a/ui/keyboard/keyboard_ui_handler.h b/ui/keyboard/keyboard_ui_handler.h
index 0800453..5ff05a0 100644
--- a/ui/keyboard/keyboard_ui_handler.h
+++ b/ui/keyboard/keyboard_ui_handler.h
@@ -25,6 +25,10 @@ class KeyboardUIHandler : public content::WebUIMessageHandler {
// Callback for the "insertText" message.
void HandleInsertTextMessage(const base::ListValue* args);
+ // Callback for the "getInputContext" message. The first element in
+ // |args| should be an integer representing request ID.
+ void HandleGetInputContextMessage(const base::ListValue* args);
+
DISALLOW_COPY_AND_ASSIGN(KeyboardUIHandler);
};
diff --git a/ui/keyboard/resources/webui/api_adapter.js b/ui/keyboard/resources/webui/api_adapter.js
index 1d24ba6..3072775 100644
--- a/ui/keyboard/resources/webui/api_adapter.js
+++ b/ui/keyboard/resources/webui/api_adapter.js
@@ -5,3 +5,59 @@
function insertText(text) {
chrome.send('insertText', [ text ]);
}
+
+(function(exports) {
+ /**
+ * An array to save callbacks of each request.
+ * @type {Array.<function(Object)>}
+ */
+ var requestIdCallbackMap = [];
+
+ /**
+ * An incremental integer that represents a unique requestId.
+ * @type {number}
+ */
+ var requestId = 0;
+
+ /**
+ * Gets the context of the focused input field. The context is returned as a
+ * paramter in the |callback|.
+ * @param {function(Object)} callback The callback function after the webui
+ * function finished.
+ * @return {number} The ID of the new request.
+ */
+ function GetInputContext(callback) {
+ var id = requestId;
+ requestIdCallbackMap[id] = callback;
+ chrome.send('getInputContext', [ id ]);
+ requestId++;
+ return id;
+ }
+
+ /**
+ * Cancel the callback specified by requestId.
+ * @param {number} requestId The requestId of the callback that about to
+ * cancel.
+ */
+ function CancelRequest(requestId) {
+ requestIdCallbackMap[requestId] = undefined;
+ }
+
+ /**
+ * Webui function callback. Any call to chrome.send('getInputContext', [id])
+ * should trigger this function being called with the parameter
+ * inputContext.requestId == id.
+ * @param {Object} inputContext The context of focused input field. Note we
+ * only have type(input box type) and requestId fields now.
+ */
+ function GetInputContextCallback(inputContext) {
+ var requestId = inputContext.requestId;
+ if (!requestIdCallbackMap[requestId])
+ return;
+ requestIdCallbackMap[requestId](inputContext);
+ }
+
+ exports.getInputContext = GetInputContext;
+ exports.cancelRequest = CancelRequest;
+ exports.GetInputContextCallback = GetInputContextCallback;
+})(this);