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) { ...@@ -80,7 +80,7 @@ void ImeObserver::OnBlur(int context_id) {
void ImeObserver::OnKeyEvent( void ImeObserver::OnKeyEvent(
const std::string& component_id, const std::string& component_id,
const InputMethodEngineBase::KeyboardEvent& event, const InputMethodEngineBase::KeyboardEvent& event,
IMEEngineHandlerInterface::KeyEventDoneCallback key_data) { IMEEngineHandlerInterface::KeyEventDoneCallback callback) {
if (extension_id_.empty()) if (extension_id_.empty())
return; return;
...@@ -89,7 +89,7 @@ void ImeObserver::OnKeyEvent( ...@@ -89,7 +89,7 @@ void ImeObserver::OnKeyEvent(
if (!ShouldForwardKeyEvent()) { if (!ShouldForwardKeyEvent()) {
// Continue processing the key event so that the physical keyboard can // Continue processing the key event so that the physical keyboard can
// still work. // still work.
std::move(key_data).Run(false); std::move(callback).Run(false);
return; return;
} }
...@@ -99,7 +99,7 @@ void ImeObserver::OnKeyEvent( ...@@ -99,7 +99,7 @@ void ImeObserver::OnKeyEvent(
return; return;
const std::string request_id = const std::string request_id =
event_router->GetEngineIfActive(extension_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; input_ime::KeyboardEvent key_data_value;
key_data_value.type = input_ime::ParseKeyboardEventType(event.type); key_data_value.type = input_ime::ParseKeyboardEventType(event.type);
......
...@@ -151,7 +151,6 @@ InputMethodEngineBase::InputMethodEngineBase() ...@@ -151,7 +151,6 @@ InputMethodEngineBase::InputMethodEngineBase()
context_id_(0), context_id_(0),
next_context_id_(1), next_context_id_(1),
profile_(nullptr), profile_(nullptr),
next_request_id_(1),
composition_changed_(false), composition_changed_(false),
text_(""), text_(""),
commit_text_changed_(false), commit_text_changed_(false),
...@@ -458,27 +457,38 @@ void InputMethodEngineBase::KeyEventHandled(const std::string& extension_id, ...@@ -458,27 +457,38 @@ void InputMethodEngineBase::KeyEventHandled(const std::string& extension_id,
composition_changed_ = false; composition_changed_ = false;
} }
auto request = request_map_.find(request_id); const auto it = pending_key_events_.find(request_id);
if (request == request_map_.end()) { if (it == pending_key_events_.end()) {
LOG(ERROR) << "Request ID not found: " << request_id; LOG(ERROR) << "Request ID not found: " << request_id;
return; return;
} }
std::move(request->second.second).Run(handled); std::move(it->second.callback).Run(handled);
request_map_.erase(request); pending_key_events_.erase(it);
} }
std::string InputMethodEngineBase::AddRequest( std::string InputMethodEngineBase::AddPendingKeyEvent(
const std::string& component_id, const std::string& component_id,
ui::IMEEngineHandlerInterface::KeyEventDoneCallback key_data) { ui::IMEEngineHandlerInterface::KeyEventDoneCallback callback) {
std::string request_id = base::NumberToString(next_request_id_); std::string request_id = base::NumberToString(next_request_id_);
++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; 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( void InputMethodEngineBase::DeleteSurroundingTextToInputContext(
int offset, int offset,
size_t number_of_chars) { size_t number_of_chars) {
......
...@@ -197,10 +197,10 @@ class InputMethodEngineBase : virtual public ui::IMEEngineHandlerInterface { ...@@ -197,10 +197,10 @@ class InputMethodEngineBase : virtual public ui::IMEEngineHandlerInterface {
const std::string& request_id, const std::string& request_id,
bool handled); bool handled);
// Adds unprocessed key event to |request_map_|. // Returns the request ID for this key event.
std::string AddRequest( std::string AddPendingKeyEvent(
const std::string& component_id, const std::string& component_id,
ui::IMEEngineHandlerInterface::KeyEventDoneCallback key_data); ui::IMEEngineHandlerInterface::KeyEventDoneCallback callback);
int GetContextIdForTesting() const { return context_id_; } int GetContextIdForTesting() const { return context_id_; }
...@@ -210,6 +210,20 @@ class InputMethodEngineBase : virtual public ui::IMEEngineHandlerInterface { ...@@ -210,6 +210,20 @@ class InputMethodEngineBase : virtual public ui::IMEEngineHandlerInterface {
} }
protected: 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. // Returns true if this IME is active, false if not.
virtual bool IsActive() const = 0; virtual bool IsActive() const = 0;
...@@ -253,13 +267,8 @@ class InputMethodEngineBase : virtual public ui::IMEEngineHandlerInterface { ...@@ -253,13 +267,8 @@ class InputMethodEngineBase : virtual public ui::IMEEngineHandlerInterface {
Profile* profile_; Profile* profile_;
using RequestMap = unsigned int next_request_id_ = 1;
std::map<std::string, std::map<std::string, PendingKeyEvent> pending_key_events_;
std::pair<std::string,
ui::IMEEngineHandlerInterface::KeyEventDoneCallback>>;
unsigned int next_request_id_;
RequestMap request_map_;
// The composition text to be set from calling input.ime.setComposition API. // The composition text to be set from calling input.ime.setComposition API.
ui::CompositionText composition_; 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