Commit 6a908acd authored by Darren Shen's avatar Darren Shen Committed by Commit Bot

imf: Use unique_ptr for managing engine lifetimes.

We currently manually manage the lifetime of the engines in engine map.
But we can use a unique_ptr instead. It automatically gets released
when we remove an engine from the engine map.

Bug: 1009903
Change-Id: I93874b0154b949ddceef29695fc000cacfef73cb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1837443
Commit-Queue: Keith Lee <keithlee@chromium.org>
Reviewed-by: default avatarKeith Lee <keithlee@chromium.org>
Cr-Commit-Position: refs/heads/master@{#702745}
parent 7524342b
...@@ -444,12 +444,13 @@ bool InputImeEventRouter::RegisterImeExtension( ...@@ -444,12 +444,13 @@ bool InputImeEventRouter::RegisterImeExtension(
std::unique_ptr<InputMethodEngineBase::Observer> observer( std::unique_ptr<InputMethodEngineBase::Observer> observer(
new ImeObserverChromeOS(extension_id, profile)); new ImeObserverChromeOS(extension_id, profile));
auto* engine = new chromeos::InputMethodEngine(); auto engine = std::make_unique<chromeos::InputMethodEngine>();
engine->Initialize(std::move(observer), extension_id.c_str(), profile); engine->Initialize(std::move(observer), extension_id.c_str(), profile);
engine_map_[extension_id] = engine; engine_map_[extension_id] = std::move(engine);
chromeos::UserSessionManager::GetInstance() chromeos::UserSessionManager::GetInstance()
->GetDefaultIMEState(profile) ->GetDefaultIMEState(profile)
->AddInputMethodExtension(extension_id, descriptors, engine); ->AddInputMethodExtension(extension_id, descriptors,
engine_map_[extension_id].get());
return true; return true;
} }
...@@ -460,7 +461,6 @@ void InputImeEventRouter::UnregisterAllImes(const std::string& extension_id) { ...@@ -460,7 +461,6 @@ void InputImeEventRouter::UnregisterAllImes(const std::string& extension_id) {
chromeos::input_method::InputMethodManager::Get() chromeos::input_method::InputMethodManager::Get()
->GetActiveIMEState() ->GetActiveIMEState()
->RemoveInputMethodExtension(extension_id); ->RemoveInputMethodExtension(extension_id);
delete it->second;
engine_map_.erase(it); engine_map_.erase(it);
} }
} }
...@@ -468,13 +468,13 @@ void InputImeEventRouter::UnregisterAllImes(const std::string& extension_id) { ...@@ -468,13 +468,13 @@ void InputImeEventRouter::UnregisterAllImes(const std::string& extension_id) {
InputMethodEngine* InputImeEventRouter::GetEngine( InputMethodEngine* InputImeEventRouter::GetEngine(
const std::string& extension_id) { const std::string& extension_id) {
auto it = engine_map_.find(extension_id); auto it = engine_map_.find(extension_id);
return (it != engine_map_.end()) ? it->second : nullptr; return (it != engine_map_.end()) ? it->second.get() : nullptr;
} }
InputMethodEngineBase* InputImeEventRouter::GetEngineIfActive( InputMethodEngineBase* InputImeEventRouter::GetEngineIfActive(
const std::string& extension_id) { const std::string& extension_id) {
auto it = engine_map_.find(extension_id); auto it = engine_map_.find(extension_id);
return (it != engine_map_.end() && it->second->IsActive()) ? it->second return (it != engine_map_.end() && it->second->IsActive()) ? it->second.get()
: nullptr; : nullptr;
} }
......
...@@ -169,7 +169,8 @@ class InputImeEventRouter : public InputImeEventRouterBase { ...@@ -169,7 +169,8 @@ class InputImeEventRouter : public InputImeEventRouterBase {
private: private:
// The engine map from extension_id to an engine. // The engine map from extension_id to an engine.
std::map<std::string, chromeos::InputMethodEngine*> engine_map_; std::map<std::string, std::unique_ptr<chromeos::InputMethodEngine>>
engine_map_;
// The first party ime extension which is unloaded unexpectedly. // The first party ime extension which is unloaded unexpectedly.
std::string unloaded_component_extension_id_; std::string unloaded_component_extension_id_;
......
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