Commit 09df86a4 authored by Darren Shen's avatar Darren Shen Committed by Commit Bot

ime: Use ScopedObservation for input_method::Accessibility.

ScopedObservation is much safer and cleaner.

Change-Id: I189991b7aafb2cda41fa77a468b1f9a8a213fa10
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2522811
Commit-Queue: Darren Shen <shend@chromium.org>
Reviewed-by: default avatarAlexey Baskakov <loyso@chromium.org>
Cr-Commit-Position: refs/heads/master@{#826210}
parent e82e46fb
...@@ -13,29 +13,24 @@ ...@@ -13,29 +13,24 @@
namespace chromeos { namespace chromeos {
namespace input_method { namespace input_method {
Accessibility::Accessibility(InputMethodManager* imm) Accessibility::Accessibility(InputMethodManager* imm) {
: imm_(imm) { DCHECK(imm);
DCHECK(imm_); observed_input_method_manager_.Observe(imm);
imm_->AddObserver(this);
} }
Accessibility::~Accessibility() { Accessibility::~Accessibility() = default;
DCHECK(imm_);
imm_->RemoveObserver(this);
}
void Accessibility::InputMethodChanged(InputMethodManager* imm, void Accessibility::InputMethodChanged(InputMethodManager* imm,
Profile* profile, Profile* profile,
bool show_message) { bool show_message) {
DCHECK_EQ(imm, imm_);
if (!show_message) if (!show_message)
return; return;
// Get the medium name of the changed input method (e.g. US, INTL, etc.) // Get the medium name of the changed input method (e.g. US, INTL, etc.)
const InputMethodDescriptor descriptor = const InputMethodDescriptor descriptor =
imm_->GetActiveIMEState()->GetCurrentInputMethod(); imm->GetActiveIMEState()->GetCurrentInputMethod();
const std::string medium_name = base::UTF16ToUTF8( const std::string medium_name = base::UTF16ToUTF8(
imm_->GetInputMethodUtil()->GetInputMethodMediumName(descriptor)); imm->GetInputMethodUtil()->GetInputMethodMediumName(descriptor));
AutomationManagerAura::GetInstance()->HandleAlert(medium_name); AutomationManagerAura::GetInstance()->HandleAlert(medium_name);
} }
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_ACCESSIBILITY_H_ #define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_ACCESSIBILITY_H_
#include "base/macros.h" #include "base/macros.h"
#include "base/scoped_observation.h"
#include "ui/base/ime/chromeos/input_method_manager.h" #include "ui/base/ime/chromeos/input_method_manager.h"
namespace chromeos { namespace chromeos {
...@@ -15,17 +16,20 @@ namespace input_method { ...@@ -15,17 +16,20 @@ namespace input_method {
class Accessibility class Accessibility
: public InputMethodManager::Observer { : public InputMethodManager::Observer {
public: public:
// `imm` needs to be alive for the lifetime of this instance.
explicit Accessibility(InputMethodManager* imm); explicit Accessibility(InputMethodManager* imm);
~Accessibility() override; ~Accessibility() override;
Accessibility(const Accessibility&) = delete;
Accessibility& operator=(const Accessibility&) = delete;
private: private:
// InputMethodManager::Observer implementation. // InputMethodManager::Observer implementation.
void InputMethodChanged(InputMethodManager* imm, void InputMethodChanged(InputMethodManager* imm,
Profile* profile, Profile* profile,
bool show_message) override; bool show_message) override;
InputMethodManager* imm_;
DISALLOW_COPY_AND_ASSIGN(Accessibility); base::ScopedObservation<InputMethodManager, InputMethodManager::Observer>
observed_input_method_manager_{this};
}; };
} // namespace input_method } // namespace input_method
......
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