Commit da86df1a authored by Shu Chen's avatar Shu Chen Committed by Commit Bot

Adds a new private API - getSurroundingText for the IME extension.

Change-Id: Iba7d37b3698e2092cd0cba6b117505e03ab912ff
Reviewed-on: https://chromium-review.googlesource.com/1111748
Commit-Queue: Shu Chen <shuchen@chromium.org>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Reviewed-by: default avatarYuichiro Hanada <yhanada@chromium.org>
Cr-Commit-Position: refs/heads/master@{#570311}
parent 7c05e3de
......@@ -38,6 +38,7 @@
#include "ui/base/ime/chromeos/input_method_descriptor.h"
#include "ui/base/ime/chromeos/input_method_manager.h"
#include "ui/base/ime/chromeos/input_method_util.h"
#include "ui/base/ime/ime_bridge.h"
#include "ui/keyboard/keyboard_controller.h"
#include "ui/keyboard/keyboard_util.h"
......@@ -59,6 +60,8 @@ namespace OnImeMenuListChanged =
extensions::api::input_method_private::OnImeMenuListChanged;
namespace OnImeMenuItemsChanged =
extensions::api::input_method_private::OnImeMenuItemsChanged;
namespace GetSurroundingText =
extensions::api::input_method_private::GetSurroundingText;
namespace {
......@@ -296,6 +299,50 @@ InputMethodPrivateOpenOptionsPageFunction::Run() {
#endif
}
ExtensionFunction::ResponseAction
InputMethodPrivateGetSurroundingTextFunction::Run() {
#if !defined(OS_CHROMEOS)
EXTENSION_FUNCTION_VALIDATE(false);
#else
ui::IMEInputContextHandlerInterface* input_context =
ui::IMEBridge::Get()->GetInputContextHandler();
if (!input_context)
return RespondNow(Error("No input context handler."));
std::unique_ptr<GetSurroundingText::Params> params(
GetSurroundingText::Params::Create(*args_));
if (params->before_length < 0 || params->after_length < 0)
return RespondNow(Error("Invalid parameters."));
uint32_t param_before_length = (uint32_t)params->before_length;
uint32_t param_after_length = (uint32_t)params->after_length;
auto ret = std::make_unique<base::DictionaryValue>();
ui::SurroundingTextInfo info = input_context->GetSurroundingTextInfo();
uint32_t text_before_end = info.selection_range.start();
uint32_t text_before_start = text_before_end > param_before_length
? text_before_end - param_before_length
: 0;
uint32_t text_after_start = info.selection_range.end();
uint32_t text_after_end =
text_after_start + param_after_length < info.surrounding_text.length()
? text_after_start + param_after_length
: info.surrounding_text.length();
ret->SetString("before",
info.surrounding_text.substr(
text_before_start, text_before_end - text_before_start));
ret->SetString("selected",
info.surrounding_text.substr(
text_before_end, text_after_start - text_before_end));
ret->SetString(
"after", info.surrounding_text.substr(text_after_start,
text_after_end - text_after_start));
return RespondNow(OneArgument(std::move(ret)));
#endif
}
InputMethodAPI::InputMethodAPI(content::BrowserContext* context)
: context_(context) {
EventRouter::Get(context_)->RegisterObserver(this, OnChanged::kEventName);
......
......@@ -192,6 +192,22 @@ class InputMethodPrivateOpenOptionsPageFunction
DISALLOW_COPY_AND_ASSIGN(InputMethodPrivateOpenOptionsPageFunction);
};
class InputMethodPrivateGetSurroundingTextFunction
: public UIThreadExtensionFunction {
public:
InputMethodPrivateGetSurroundingTextFunction() {}
protected:
~InputMethodPrivateGetSurroundingTextFunction() override {}
ResponseAction Run() override;
private:
DECLARE_EXTENSION_FUNCTION("inputMethodPrivate.getSurroundingText",
INPUTMETHODPRIVATE_GETSURROUNDINGTEXT)
DISALLOW_COPY_AND_ASSIGN(InputMethodPrivateGetSurroundingTextFunction);
};
class InputMethodAPI : public BrowserContextKeyedAPI,
public extensions::EventRouter::Observer {
public:
......
......@@ -295,6 +295,39 @@
]
}
]
}, {
"name": "getSurroundingText",
"type": "function",
"description": "Gets the surrounding text of the current selection",
"parameters": [
{
"name": "beforeLength",
"type": "integer",
"description": "The number of characters before the current selection."
},
{
"name": "afterLength",
"type": "integer",
"description": "The number of characters after the current selection."
},
{
"type": "function",
"name": "callback",
"description": "Callback which is called to provide the result",
"parameters": [
{
"name": "surroundingInfo",
"type": "object",
"description": "The surrouding text info.",
"properties": {
"before": {"type": "string"},
"selected": {"type": "string"},
"after": {"type": "string"}
}
}
]
}
]
}
],
"events": [
......
......@@ -1321,6 +1321,7 @@ enum HistogramValue {
VIRTUALKEYBOARDPRIVATE_SETOCCLUDEDBOUNDS = 1258,
SYSTEM_POWER_SOURCE_GETPOWERSOURCEINFO = 1259,
SYSTEM_POWER_SOURCE_REQUESTSTATUSUPDATE = 1260,
INPUTMETHODPRIVATE_GETSURROUNDINGTEXT = 1261,
// Last entry: Add new entries above, then run:
// python tools/metrics/histograms/update_extension_histograms.py
ENUM_BOUNDARY
......
......@@ -15778,6 +15778,7 @@ Called by update_net_error_codes.py.-->
<int value="1258" label="VIRTUALKEYBOARDPRIVATE_SETOCCLUDEDBOUNDS"/>
<int value="1259" label="SYSTEM_POWER_SOURCE_GETPOWERSOURCEINFO"/>
<int value="1260" label="SYSTEM_POWER_SOURCE_REQUESTSTATUSUPDATE"/>
<int value="1261" label="INPUTMETHODPRIVATE_GETSURROUNDINGTEXT"/>
</enum>
<enum name="ExtensionIconState">
......@@ -15,6 +15,11 @@
namespace ui {
struct SurroundingTextInfo {
base::string16 surrounding_text;
gfx::Range selection_range;
};
class UI_BASE_IME_EXPORT IMEInputContextHandlerInterface {
public:
// Called when the engine commit a text.
......@@ -28,6 +33,9 @@ class UI_BASE_IME_EXPORT IMEInputContextHandlerInterface {
// Called when the engine request deleting surrounding string.
virtual void DeleteSurroundingText(int32_t offset, uint32_t length) = 0;
// Called from the extension API.
virtual SurroundingTextInfo GetSurroundingTextInfo() = 0;
// Called when the engine sends a key event.
virtual void SendKeyEvent(KeyEvent* event) = 0;
......
......@@ -267,6 +267,18 @@ void InputMethodBase::UpdateCompositionText(const CompositionText& composition_,
void InputMethodBase::DeleteSurroundingText(int32_t offset, uint32_t length) {}
SurroundingTextInfo InputMethodBase::GetSurroundingTextInfo() {
gfx::Range text_range;
SurroundingTextInfo info;
TextInputClient* client = GetTextInputClient();
if (!client->GetTextRange(&text_range) ||
!client->GetTextFromRange(text_range, &info.surrounding_text) ||
!client->GetSelectionRange(&info.selection_range)) {
return SurroundingTextInfo();
}
return info;
}
void InputMethodBase::SendKeyEvent(KeyEvent* event) {
sending_key_event_ = true;
if (track_key_events_for_testing_) {
......
......@@ -85,6 +85,7 @@ class UI_BASE_IME_EXPORT InputMethodBase
uint32_t cursor_pos,
bool visible) override;
void DeleteSurroundingText(int32_t offset, uint32_t length) override;
SurroundingTextInfo GetSurroundingTextInfo() override;
void SendKeyEvent(KeyEvent* event) override;
InputMethod* GetInputMethod() override;
......
......@@ -38,6 +38,10 @@ void MockIMEInputContextHandler::DeleteSurroundingText(int32_t offset,
last_delete_surrounding_text_arg_.length = length;
}
SurroundingTextInfo MockIMEInputContextHandler::GetSurroundingTextInfo() {
return SurroundingTextInfo();
}
void MockIMEInputContextHandler::Reset() {
commit_text_call_count_ = 0;
update_preedit_text_call_count_ = 0;
......
......@@ -36,6 +36,7 @@ class UI_BASE_IME_EXPORT MockIMEInputContextHandler
uint32_t cursor_pos,
bool visible) override;
void DeleteSurroundingText(int32_t offset, uint32_t length) override;
SurroundingTextInfo GetSurroundingTextInfo() override;
void SendKeyEvent(KeyEvent* event) override;
InputMethod* GetInputMethod() override;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment