Commit 9f22bfb4 authored by Yuichiro Hanada's avatar Yuichiro Hanada Committed by Commit Bot

Introduce arc::InputMethodPrefs.

arc::InputMethodPrefs is a thin wrapper of the input method related
preferences.
It provides convenient methods to update the preferences according to a
state of ARC IMEs.

Bug: 1139332
Test: unit_tests

Change-Id: I98d0e2845df1031340633df77908d76c3f3cd237
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2537811
Commit-Queue: Yuichiro Hanada <yhanada@chromium.org>
Reviewed-by: default avatarTetsui Ohkubo <tetsui@chromium.org>
Cr-Commit-Position: refs/heads/master@{#827742}
parent c3fa21ed
...@@ -677,6 +677,8 @@ source_set("chromeos") { ...@@ -677,6 +677,8 @@ source_set("chromeos") {
"arc/input_method_manager/arc_input_method_state.h", "arc/input_method_manager/arc_input_method_state.h",
"arc/input_method_manager/input_connection_impl.cc", "arc/input_method_manager/input_connection_impl.cc",
"arc/input_method_manager/input_connection_impl.h", "arc/input_method_manager/input_connection_impl.h",
"arc/input_method_manager/input_method_prefs.cc",
"arc/input_method_manager/input_method_prefs.h",
"arc/instance_throttle/arc_active_window_throttle_observer.cc", "arc/instance_throttle/arc_active_window_throttle_observer.cc",
"arc/instance_throttle/arc_active_window_throttle_observer.h", "arc/instance_throttle/arc_active_window_throttle_observer.h",
"arc/instance_throttle/arc_app_launch_throttle_observer.cc", "arc/instance_throttle/arc_app_launch_throttle_observer.cc",
...@@ -3297,6 +3299,7 @@ source_set("unit_tests") { ...@@ -3297,6 +3299,7 @@ source_set("unit_tests") {
"arc/input_method_manager/arc_input_method_manager_service_unittest.cc", "arc/input_method_manager/arc_input_method_manager_service_unittest.cc",
"arc/input_method_manager/arc_input_method_state_unittest.cc", "arc/input_method_manager/arc_input_method_state_unittest.cc",
"arc/input_method_manager/input_connection_impl_unittest.cc", "arc/input_method_manager/input_connection_impl_unittest.cc",
"arc/input_method_manager/input_method_prefs_unittest.cc",
"arc/input_method_manager/test_input_method_manager_bridge.cc", "arc/input_method_manager/test_input_method_manager_bridge.cc",
"arc/input_method_manager/test_input_method_manager_bridge.h", "arc/input_method_manager/test_input_method_manager_bridge.h",
"arc/instance_throttle/arc_active_window_throttle_observer_unittest.cc", "arc/instance_throttle/arc_active_window_throttle_observer_unittest.cc",
......
...@@ -17,14 +17,11 @@ ...@@ -17,14 +17,11 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/singleton.h" #include "base/memory/singleton.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/stl_util.h"
#include "base/strings/string_piece.h" #include "base/strings/string_piece.h"
#include "base/strings/string_split.h"
#include "chrome/browser/chromeos/arc/arc_util.h" #include "chrome/browser/chromeos/arc/arc_util.h"
#include "chrome/browser/chromeos/arc/input_method_manager/arc_input_method_manager_bridge_impl.h" #include "chrome/browser/chromeos/arc/input_method_manager/arc_input_method_manager_bridge_impl.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ash/keyboard/chrome_keyboard_controller_client.h" #include "chrome/browser/ui/ash/keyboard/chrome_keyboard_controller_client.h"
#include "chrome/common/pref_names.h"
#include "components/arc/arc_browser_context_keyed_service_factory_base.h" #include "components/arc/arc_browser_context_keyed_service_factory_base.h"
#include "components/arc/mojom/ime_mojom_traits.h" #include "components/arc/mojom/ime_mojom_traits.h"
#include "components/crx_file/id_util.h" #include "components/crx_file/id_util.h"
...@@ -330,6 +327,7 @@ ArcInputMethodManagerService::ArcInputMethodManagerService( ...@@ -330,6 +327,7 @@ ArcInputMethodManagerService::ArcInputMethodManagerService(
arc_ime_state_delegate_( arc_ime_state_delegate_(
std::make_unique<ArcInputMethodStateDelegateImpl>(profile_)), std::make_unique<ArcInputMethodStateDelegateImpl>(profile_)),
arc_ime_state_(arc_ime_state_delegate_.get()), arc_ime_state_(arc_ime_state_delegate_.get()),
prefs_(profile_),
is_virtual_keyboard_shown_(false), is_virtual_keyboard_shown_(false),
is_updating_imm_entry_(false), is_updating_imm_entry_(false),
proxy_ime_extension_id_( proxy_ime_extension_id_(
...@@ -384,7 +382,7 @@ void ArcInputMethodManagerService::Shutdown() { ...@@ -384,7 +382,7 @@ void ArcInputMethodManagerService::Shutdown() {
// Remove any Arc IME entry from preferences before shutting down. // Remove any Arc IME entry from preferences before shutting down.
// IME states (installed/enabled/disabled) are stored in Android's settings, // IME states (installed/enabled/disabled) are stored in Android's settings,
// that will be restored after Arc container starts next time. // that will be restored after Arc container starts next time.
RemoveArcIMEFromPrefs(); prefs_.UpdateEnabledImes({});
profile_->GetPrefs()->CommitPendingWrite(); profile_->GetPrefs()->CommitPendingWrite();
if (input_method_) { if (input_method_) {
...@@ -429,19 +427,7 @@ void ArcInputMethodManagerService::OnImeDisabled(const std::string& ime_id) { ...@@ -429,19 +427,7 @@ void ArcInputMethodManagerService::OnImeDisabled(const std::string& ime_id) {
arc_ime_state_.DisableInputMethod(ime_id); arc_ime_state_.DisableInputMethod(ime_id);
// Remove the IME from the prefs to disable it. // Remove the IME from the prefs to disable it.
const std::string active_ime_ids = prefs_.UpdateEnabledImes(arc_ime_state_.GetEnabledInputMethods());
profile_->GetPrefs()->GetString(prefs::kLanguageEnabledImes);
std::vector<std::string> active_ime_list = base::SplitString(
active_ime_ids, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
base::EraseIf(active_ime_list, [](const auto& id) {
return chromeos::extension_ime_util::IsArcIME(id);
});
for (const auto& descriptor : arc_ime_state_.GetEnabledInputMethods())
active_ime_list.push_back(descriptor.id());
profile_->GetPrefs()->SetString(prefs::kLanguageEnabledImes,
base::JoinString(active_ime_list, ","));
// Note: Since this is not about uninstalling the IME, this method does not // Note: Since this is not about uninstalling the IME, this method does not
// modify InputMethodManager::State. // modify InputMethodManager::State.
...@@ -476,7 +462,7 @@ void ArcInputMethodManagerService::UpdateInputMethodEntryWithImeInfo() { ...@@ -476,7 +462,7 @@ void ArcInputMethodManagerService::UpdateInputMethodEntryWithImeInfo() {
if (installed_imes.empty()) { if (installed_imes.empty()) {
// If no ARC IME is installed or allowed, remove ARC IME entry from // If no ARC IME is installed or allowed, remove ARC IME entry from
// preferences. // preferences.
RemoveArcIMEFromPrefs(); prefs_.UpdateEnabledImes({});
return; return;
} }
...@@ -487,23 +473,7 @@ void ArcInputMethodManagerService::UpdateInputMethodEntryWithImeInfo() { ...@@ -487,23 +473,7 @@ void ArcInputMethodManagerService::UpdateInputMethodEntryWithImeInfo() {
// Enable IMEs that are already enabled in the container. // Enable IMEs that are already enabled in the container.
// TODO(crbug.com/845079): We should keep the order of the IMEs as same as in // TODO(crbug.com/845079): We should keep the order of the IMEs as same as in
// chrome://settings // chrome://settings
prefs_.UpdateEnabledImes(arc_ime_state_.GetEnabledInputMethods());
const std::string active_ime_ids =
profile_->GetPrefs()->GetString(prefs::kLanguageEnabledImes);
std::vector<std::string> active_ime_list = base::SplitString(
active_ime_ids, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
// Remove all ARC IMEs at first.
base::EraseIf(active_ime_list, [](const auto& id) {
return chromeos::extension_ime_util::IsArcIME(id);
});
// Re-add enabled and allowed IMEs.
for (const auto& descriptor : arc_ime_state_.GetEnabledInputMethods())
active_ime_list.push_back(descriptor.id());
// Set the pref.
profile_->GetPrefs()->SetString(prefs::kLanguageEnabledImes,
base::JoinString(active_ime_list, ","));
for (const auto& descriptor : arc_ime_state_.GetEnabledInputMethods()) for (const auto& descriptor : arc_ime_state_.GetEnabledInputMethods())
state->EnableInputMethod(descriptor.id()); state->EnableInputMethod(descriptor.id());
...@@ -558,15 +528,7 @@ void ArcInputMethodManagerService::ImeMenuListChanged() { ...@@ -558,15 +528,7 @@ void ArcInputMethodManagerService::ImeMenuListChanged() {
// TODO(yhanada|yusukes): Instead of observing ImeMenuListChanged(), it's // TODO(yhanada|yusukes): Instead of observing ImeMenuListChanged(), it's
// probably better to just observe the pref (and not disabling ones still // probably better to just observe the pref (and not disabling ones still
// in the prefs.) See also the comment below in the second for-loop. // in the prefs.) See also the comment below in the second for-loop.
std::set<std::string> active_ime_ids_on_prefs; const std::set<std::string> active_ime_ids_on_prefs = prefs_.GetEnabledImes();
{
const std::string active_ime_ids =
profile_->GetPrefs()->GetString(prefs::kLanguageEnabledImes);
std::vector<base::StringPiece> active_ime_list = base::SplitStringPiece(
active_ime_ids, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
for (const auto& id : active_ime_list)
active_ime_ids_on_prefs.insert(id.as_string());
}
for (const auto& id : new_arc_active_ime_ids) { for (const auto& id : new_arc_active_ime_ids) {
// Enable the IME which is not currently enabled. // Enable the IME which is not currently enabled.
...@@ -715,32 +677,6 @@ void ArcInputMethodManagerService::UpdateTextInputState() { ...@@ -715,32 +677,6 @@ void ArcInputMethodManagerService::UpdateTextInputState() {
false /* is_input_state_update_requested */); false /* is_input_state_update_requested */);
} }
void ArcInputMethodManagerService::RemoveArcIMEFromPrefs() {
RemoveArcIMEFromPref(prefs::kLanguageEnabledImes);
RemoveArcIMEFromPref(prefs::kLanguagePreloadEngines);
PrefService* prefs = profile_->GetPrefs();
if (chromeos::extension_ime_util::IsArcIME(
prefs->GetString(prefs::kLanguageCurrentInputMethod))) {
prefs->SetString(prefs::kLanguageCurrentInputMethod, std::string());
}
if (chromeos::extension_ime_util::IsArcIME(
prefs->GetString(prefs::kLanguagePreviousInputMethod))) {
prefs->SetString(prefs::kLanguagePreviousInputMethod, std::string());
}
}
void ArcInputMethodManagerService::RemoveArcIMEFromPref(const char* pref_name) {
const std::string ime_ids = profile_->GetPrefs()->GetString(pref_name);
std::vector<base::StringPiece> ime_id_list = base::SplitStringPiece(
ime_ids, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
base::EraseIf(ime_id_list, [](base::StringPiece id) {
return chromeos::extension_ime_util::IsArcIME(id.as_string());
});
profile_->GetPrefs()->SetString(pref_name,
base::JoinString(ime_id_list, ","));
}
void ArcInputMethodManagerService::OnTabletModeToggled(bool /* enabled */) { void ArcInputMethodManagerService::OnTabletModeToggled(bool /* enabled */) {
UpdateInputMethodEntryWithImeInfo(); UpdateInputMethodEntryWithImeInfo();
} }
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "chrome/browser/chromeos/arc/input_method_manager/arc_input_method_manager_bridge.h" #include "chrome/browser/chromeos/arc/input_method_manager/arc_input_method_manager_bridge.h"
#include "chrome/browser/chromeos/arc/input_method_manager/arc_input_method_state.h" #include "chrome/browser/chromeos/arc/input_method_manager/arc_input_method_state.h"
#include "chrome/browser/chromeos/arc/input_method_manager/input_connection_impl.h" #include "chrome/browser/chromeos/arc/input_method_manager/input_connection_impl.h"
#include "chrome/browser/chromeos/arc/input_method_manager/input_method_prefs.h"
#include "chrome/browser/chromeos/input_method/input_method_engine.h" #include "chrome/browser/chromeos/input_method/input_method_engine.h"
#include "components/arc/mojom/input_method_manager.mojom-forward.h" #include "components/arc/mojom/input_method_manager.mojom-forward.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h" #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
...@@ -115,12 +116,6 @@ class ArcInputMethodManagerService ...@@ -115,12 +116,6 @@ class ArcInputMethodManagerService
mojom::TextInputStatePtr GetTextInputState( mojom::TextInputStatePtr GetTextInputState(
bool is_input_state_update_requested); bool is_input_state_update_requested);
// Removes ARC IME from IME related prefs that are current active IME pref,
// previous active IME pref, enabled IME list pref and preloading IME list
// pref.
void RemoveArcIMEFromPrefs();
void RemoveArcIMEFromPref(const char* pref_name);
void OnTabletModeToggled(bool enabled); void OnTabletModeToggled(bool enabled);
// Update the descriptors in IMM and the prefs according to // Update the descriptors in IMM and the prefs according to
...@@ -141,6 +136,7 @@ class ArcInputMethodManagerService ...@@ -141,6 +136,7 @@ class ArcInputMethodManagerService
std::set<std::string> active_arc_ime_ids_; std::set<std::string> active_arc_ime_ids_;
std::unique_ptr<ArcInputMethodState::Delegate> arc_ime_state_delegate_; std::unique_ptr<ArcInputMethodState::Delegate> arc_ime_state_delegate_;
ArcInputMethodState arc_ime_state_; ArcInputMethodState arc_ime_state_;
InputMethodPrefs prefs_;
bool is_virtual_keyboard_shown_; bool is_virtual_keyboard_shown_;
// This flag is set to true while updating ARC IMEs entries in IMM to avoid // This flag is set to true while updating ARC IMEs entries in IMM to avoid
// exposing incomplete state. // exposing incomplete state.
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/arc/input_method_manager/input_method_prefs.h"
#include "base/stl_util.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "ui/base/ime/chromeos/extension_ime_util.h"
namespace arc {
namespace ce = ::chromeos::extension_ime_util;
using ::chromeos::input_method::InputMethodDescriptors;
InputMethodPrefs::InputMethodPrefs(Profile* profile) : profile_(profile) {}
InputMethodPrefs::~InputMethodPrefs() = default;
void InputMethodPrefs::UpdateEnabledImes(
InputMethodDescriptors enabled_arc_imes) {
PrefService* const prefs = profile_->GetPrefs();
const std::string enabled_ime_ids =
prefs->GetString(prefs::kLanguageEnabledImes);
std::vector<std::string> enabled_ime_list = base::SplitString(
enabled_ime_ids, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
base::EraseIf(enabled_ime_list, [](const auto& id) {
return chromeos::extension_ime_util::IsArcIME(id);
});
for (const auto& descriptor : enabled_arc_imes)
enabled_ime_list.push_back(descriptor.id());
prefs->SetString(prefs::kLanguageEnabledImes,
base::JoinString(enabled_ime_list, ","));
const std::string current_ime =
prefs->GetString(prefs::kLanguageCurrentInputMethod);
if (ce::IsArcIME(current_ime) &&
!base::Contains(enabled_ime_list, current_ime))
prefs->SetString(prefs::kLanguageCurrentInputMethod, std::string());
const std::string previous_ime =
prefs->GetString(prefs::kLanguagePreviousInputMethod);
if (ce::IsArcIME(previous_ime) &&
!base::Contains(enabled_ime_list, previous_ime))
prefs->SetString(prefs::kLanguagePreviousInputMethod, std::string());
}
std::set<std::string> InputMethodPrefs::GetEnabledImes() const {
const std::vector<std::string> imes = base::SplitString(
profile_->GetPrefs()->GetString(prefs::kLanguageEnabledImes), ",",
base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
return std::set<std::string>(imes.begin(), imes.end());
}
} // namespace arc
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_ARC_INPUT_METHOD_MANAGER_INPUT_METHOD_PREFS_H_
#define CHROME_BROWSER_CHROMEOS_ARC_INPUT_METHOD_MANAGER_INPUT_METHOD_PREFS_H_
#include <string>
#include <vector>
#include "chrome/browser/profiles/profile.h"
#include "ui/base/ime/chromeos/input_method_descriptor.h"
namespace arc {
// A thin wrapper of the input method related prefs.
class InputMethodPrefs {
public:
explicit InputMethodPrefs(Profile* profile);
~InputMethodPrefs();
InputMethodPrefs(const InputMethodPrefs& pref) = delete;
InputMethodPrefs& operator=(const InputMethodPrefs& pref) = delete;
// Updates input method related prefs according to the passed enabled ARC IME
// list.
void UpdateEnabledImes(
chromeos::input_method::InputMethodDescriptors enabled_arc_imes);
// Returns the list of IMEs on the pref.
std::set<std::string> GetEnabledImes() const;
private:
Profile* const profile_;
};
} // namespace arc
#endif // CHROME_BROWSER_CHROMEOS_ARC_INPUT_METHOD_MANAGER_INPUT_METHOD_PREFS_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/arc/input_method_manager/input_method_prefs.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "components/crx_file/id_util.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/ime/chromeos/extension_ime_util.h"
namespace arc {
class InputMethodPrefsTest : public testing::Test {
public:
InputMethodPrefsTest() = default;
~InputMethodPrefsTest() override = default;
TestingProfile* profile() { return &profile_; }
private:
content::BrowserTaskEnvironment task_environment_;
TestingProfile profile_;
};
TEST_F(InputMethodPrefsTest, Constructor) {
InputMethodPrefs prefs(profile());
}
TEST_F(InputMethodPrefsTest, GetEnabledImes) {
namespace ceiu = chromeos::extension_ime_util;
using crx_file::id_util::GenerateId;
const std::string component_extension_ime_id =
ceiu::GetComponentInputMethodID(
GenerateId("test.component.extension.ime"), "us");
const std::string arc_ime_id =
ceiu::GetArcInputMethodID(GenerateId("test.arc.ime"), "us");
profile()->GetPrefs()->SetString(
prefs::kLanguageEnabledImes,
base::StringPrintf("%s,%s", component_extension_ime_id.c_str(),
arc_ime_id.c_str()));
InputMethodPrefs prefs(profile());
const std::set<std::string> imes = prefs.GetEnabledImes();
EXPECT_EQ(2u, imes.size());
EXPECT_TRUE(imes.count(component_extension_ime_id) > 0);
EXPECT_TRUE(imes.count(arc_ime_id) > 0);
}
TEST_F(InputMethodPrefsTest, UpdateEnabledImes) {
namespace ceiu = chromeos::extension_ime_util;
using chromeos::input_method::InputMethodDescriptor;
using crx_file::id_util::GenerateId;
const std::string component_extension_ime_id =
ceiu::GetComponentInputMethodID(
GenerateId("test.component.extension.ime"), "us");
const std::string arc_ime_id1 =
ceiu::GetArcInputMethodID(GenerateId("test.arc.ime"), "us");
const std::string arc_ime_id2 =
ceiu::GetArcInputMethodID(GenerateId("test.arc.ime2"), "us");
PrefService* pref_service = profile()->GetPrefs();
// Enabled one component extension IME.
pref_service->SetString(prefs::kLanguageEnabledImes,
component_extension_ime_id);
InputMethodPrefs prefs(profile());
InputMethodDescriptor arc_ime_descriptor1(arc_ime_id1, "", "", {}, {}, false,
GURL(), GURL());
InputMethodDescriptor arc_ime_descriptor2(arc_ime_id2, "", "", {}, {}, false,
GURL(), GURL());
{
prefs.UpdateEnabledImes({arc_ime_descriptor1});
const std::set<std::string> imes = prefs.GetEnabledImes();
EXPECT_EQ(2u, imes.size());
EXPECT_TRUE(imes.count(component_extension_ime_id) > 0);
EXPECT_TRUE(imes.count(arc_ime_id1) > 0);
}
// Enable both IMEs and set current/previous IME.
prefs.UpdateEnabledImes({arc_ime_descriptor1, arc_ime_descriptor2});
pref_service->SetString(prefs::kLanguageCurrentInputMethod, arc_ime_id1);
pref_service->SetString(prefs::kLanguagePreviousInputMethod, arc_ime_id2);
{
const std::set<std::string> imes = prefs.GetEnabledImes();
EXPECT_EQ(3u, imes.size());
EXPECT_TRUE(imes.count(component_extension_ime_id) > 0);
EXPECT_TRUE(imes.count(arc_ime_id1) > 0);
EXPECT_TRUE(imes.count(arc_ime_id2) > 0);
}
// Disable both ARC IMEs. It should reset current/previous IMEs.
{
prefs.UpdateEnabledImes({});
const std::set<std::string> imes = prefs.GetEnabledImes();
EXPECT_EQ(1u, imes.size());
EXPECT_TRUE(imes.count(component_extension_ime_id) > 0);
EXPECT_EQ("", pref_service->GetString(prefs::kLanguageCurrentInputMethod));
EXPECT_EQ("", pref_service->GetString(prefs::kLanguagePreviousInputMethod));
}
}
} // namespace arc
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