Commit 091b7db4 authored by Darren Shen's avatar Darren Shen Committed by Commit Bot

ime: Refactor request ID code.

Add a new struct representing a pending key event as we'll be adding
new data to the key event in a follow up patch.

Change-Id: I87040818e619236e2e514173ce66db64e66a1177
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1816197
Commit-Queue: Darren Shen <shend@chromium.org>
Reviewed-by: default avatarKeith Lee <keithlee@chromium.org>
Cr-Commit-Position: refs/heads/master@{#701939}
parent d7eefe3e
......@@ -80,7 +80,7 @@ void ImeObserver::OnBlur(int context_id) {
void ImeObserver::OnKeyEvent(
const std::string& component_id,
const InputMethodEngineBase::KeyboardEvent& event,
IMEEngineHandlerInterface::KeyEventDoneCallback key_data) {
IMEEngineHandlerInterface::KeyEventDoneCallback callback) {
if (extension_id_.empty())
return;
......@@ -89,7 +89,7 @@ void ImeObserver::OnKeyEvent(
if (!ShouldForwardKeyEvent()) {
// Continue processing the key event so that the physical keyboard can
// still work.
std::move(key_data).Run(false);
std::move(callback).Run(false);
return;
}
......@@ -99,7 +99,7 @@ void ImeObserver::OnKeyEvent(
return;
const std::string request_id =
event_router->GetEngineIfActive(extension_id_)
->AddRequest(component_id, std::move(key_data));
->AddPendingKeyEvent(component_id, std::move(callback));
input_ime::KeyboardEvent key_data_value;
key_data_value.type = input_ime::ParseKeyboardEventType(event.type);
......
......@@ -151,7 +151,6 @@ InputMethodEngineBase::InputMethodEngineBase()
context_id_(0),
next_context_id_(1),
profile_(nullptr),
next_request_id_(1),
composition_changed_(false),
text_(""),
commit_text_changed_(false),
......@@ -458,27 +457,38 @@ void InputMethodEngineBase::KeyEventHandled(const std::string& extension_id,
composition_changed_ = false;
}
auto request = request_map_.find(request_id);
if (request == request_map_.end()) {
const auto it = pending_key_events_.find(request_id);
if (it == pending_key_events_.end()) {
LOG(ERROR) << "Request ID not found: " << request_id;
return;
}
std::move(request->second.second).Run(handled);
request_map_.erase(request);
std::move(it->second.callback).Run(handled);
pending_key_events_.erase(it);
}
std::string InputMethodEngineBase::AddRequest(
std::string InputMethodEngineBase::AddPendingKeyEvent(
const std::string& component_id,
ui::IMEEngineHandlerInterface::KeyEventDoneCallback key_data) {
ui::IMEEngineHandlerInterface::KeyEventDoneCallback callback) {
std::string request_id = base::NumberToString(next_request_id_);
++next_request_id_;
request_map_[request_id] = std::make_pair(component_id, std::move(key_data));
pending_key_events_.emplace(
request_id, PendingKeyEvent(component_id, std::move(callback)));
return request_id;
}
InputMethodEngineBase::PendingKeyEvent::PendingKeyEvent(
const std::string& component_id,
ui::IMEEngineHandlerInterface::KeyEventDoneCallback callback)
: component_id(component_id), callback(std::move(callback)) {}
InputMethodEngineBase::PendingKeyEvent::PendingKeyEvent(
PendingKeyEvent&& other) = default;
InputMethodEngineBase::PendingKeyEvent::~PendingKeyEvent() = default;
void InputMethodEngineBase::DeleteSurroundingTextToInputContext(
int offset,
size_t number_of_chars) {
......
......@@ -197,10 +197,10 @@ class InputMethodEngineBase : virtual public ui::IMEEngineHandlerInterface {
const std::string& request_id,
bool handled);
// Adds unprocessed key event to |request_map_|.
std::string AddRequest(
// Returns the request ID for this key event.
std::string AddPendingKeyEvent(
const std::string& component_id,
ui::IMEEngineHandlerInterface::KeyEventDoneCallback key_data);
ui::IMEEngineHandlerInterface::KeyEventDoneCallback callback);
int GetContextIdForTesting() const { return context_id_; }
......@@ -210,6 +210,20 @@ class InputMethodEngineBase : virtual public ui::IMEEngineHandlerInterface {
}
protected:
struct PendingKeyEvent {
PendingKeyEvent(
const std::string& component_id,
ui::IMEEngineHandlerInterface::KeyEventDoneCallback callback);
PendingKeyEvent(PendingKeyEvent&& other);
~PendingKeyEvent();
std::string component_id;
ui::IMEEngineHandlerInterface::KeyEventDoneCallback callback;
private:
DISALLOW_COPY_AND_ASSIGN(PendingKeyEvent);
};
// Returns true if this IME is active, false if not.
virtual bool IsActive() const = 0;
......@@ -253,13 +267,8 @@ class InputMethodEngineBase : virtual public ui::IMEEngineHandlerInterface {
Profile* profile_;
using RequestMap =
std::map<std::string,
std::pair<std::string,
ui::IMEEngineHandlerInterface::KeyEventDoneCallback>>;
unsigned int next_request_id_;
RequestMap request_map_;
unsigned int next_request_id_ = 1;
std::map<std::string, PendingKeyEvent> pending_key_events_;
// The composition text to be set from calling input.ime.setComposition API.
ui::CompositionText composition_;
......
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