Commit 29247344 authored by yusukes@google.com's avatar yusukes@google.com

Add GetInputMethodDescriptorFromXkbId function.

I'll use the new function to implement a UI for virtual keyboard management.

BUG=None
TEST=ran unit_tests

Review URL: http://codereview.chromium.org/7663008

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@97084 0039d316-1c4b-4281-b951-d872f2087c98
parent a5c1ae42
...@@ -38,11 +38,14 @@ typedef std::multimap<std::string, std::string> LanguageCodeToIdsMap; ...@@ -38,11 +38,14 @@ typedef std::multimap<std::string, std::string> LanguageCodeToIdsMap;
// Map from input method ID to associated input method descriptor. // Map from input method ID to associated input method descriptor.
typedef std::map<std::string, InputMethodDescriptor> typedef std::map<std::string, InputMethodDescriptor>
InputMethodIdToDescriptorMap; InputMethodIdToDescriptorMap;
// Map from XKB layout ID to associated input method descriptor.
typedef std::map<std::string, InputMethodDescriptor> XkbIdToDescriptorMap;
struct IdMaps { struct IdMaps {
scoped_ptr<LanguageCodeToIdsMap> language_code_to_ids; scoped_ptr<LanguageCodeToIdsMap> language_code_to_ids;
scoped_ptr<std::map<std::string, std::string> > id_to_language_code; scoped_ptr<std::map<std::string, std::string> > id_to_language_code;
scoped_ptr<InputMethodIdToDescriptorMap> id_to_descriptor; scoped_ptr<InputMethodIdToDescriptorMap> id_to_descriptor;
scoped_ptr<XkbIdToDescriptorMap> xkb_id_to_descriptor;
// Returns the singleton instance. // Returns the singleton instance.
static IdMaps* GetInstance() { static IdMaps* GetInstance() {
...@@ -61,6 +64,7 @@ struct IdMaps { ...@@ -61,6 +64,7 @@ struct IdMaps {
language_code_to_ids->clear(); language_code_to_ids->clear();
id_to_language_code->clear(); id_to_language_code->clear();
id_to_descriptor->clear(); id_to_descriptor->clear();
xkb_id_to_descriptor->clear();
for (size_t i = 0; i < supported_input_methods->size(); ++i) { for (size_t i = 0; i < supported_input_methods->size(); ++i) {
const InputMethodDescriptor& input_method = const InputMethodDescriptor& input_method =
...@@ -74,6 +78,10 @@ struct IdMaps { ...@@ -74,6 +78,10 @@ struct IdMaps {
std::make_pair(input_method.id(), language_code)); std::make_pair(input_method.id(), language_code));
id_to_descriptor->insert( id_to_descriptor->insert(
std::make_pair(input_method.id(), input_method)); std::make_pair(input_method.id(), input_method));
if (IsKeyboardLayout(input_method.id())) {
xkb_id_to_descriptor->insert(
std::make_pair(input_method.keyboard_layout(), input_method));
}
} }
// Go through the languages listed in kExtraLanguages. // Go through the languages listed in kExtraLanguages.
...@@ -95,7 +103,8 @@ struct IdMaps { ...@@ -95,7 +103,8 @@ struct IdMaps {
private: private:
IdMaps() : language_code_to_ids(new LanguageCodeToIdsMap), IdMaps() : language_code_to_ids(new LanguageCodeToIdsMap),
id_to_language_code(new std::map<std::string, std::string>), id_to_language_code(new std::map<std::string, std::string>),
id_to_descriptor(new InputMethodIdToDescriptorMap) { id_to_descriptor(new InputMethodIdToDescriptorMap),
xkb_id_to_descriptor(new XkbIdToDescriptorMap) {
ReloadMaps(); ReloadMaps();
} }
...@@ -518,6 +527,14 @@ const InputMethodDescriptor* GetInputMethodDescriptorFromId( ...@@ -518,6 +527,14 @@ const InputMethodDescriptor* GetInputMethodDescriptorFromId(
NULL : &(iter->second); NULL : &(iter->second);
} }
const InputMethodDescriptor* GetInputMethodDescriptorFromXkbId(
const std::string& xkb_id) {
InputMethodIdToDescriptorMap::const_iterator iter
= IdMaps::GetInstance()->xkb_id_to_descriptor->find(xkb_id);
return (iter == IdMaps::GetInstance()->xkb_id_to_descriptor->end()) ?
NULL : &(iter->second);
}
string16 GetLanguageDisplayNameFromCode(const std::string& language_code) { string16 GetLanguageDisplayNameFromCode(const std::string& language_code) {
if (!g_browser_process) { if (!g_browser_process) {
return string16(); return string16();
......
...@@ -94,6 +94,13 @@ std::string GetInputMethodDisplayNameFromId(const std::string& input_method_id); ...@@ -94,6 +94,13 @@ std::string GetInputMethodDisplayNameFromId(const std::string& input_method_id);
const InputMethodDescriptor* GetInputMethodDescriptorFromId( const InputMethodDescriptor* GetInputMethodDescriptorFromId(
const std::string& input_method_id); const std::string& input_method_id);
// Converts an XKB layout ID to an input method descriptor. Returns NULL when
// |xkb_id| is unknown.
// Example: "us(dvorak)" => { id: "xkb:us:dvorak:eng", display_name: "US Dv..",
// keyboard_layout: "us(dvorak)", language_code: "eng" }
const InputMethodDescriptor* GetInputMethodDescriptorFromXkbId(
const std::string& xkb_id);
// Converts a language code to a language display name, using the // Converts a language code to a language display name, using the
// current application locale. MaybeRewriteLanguageName() is called // current application locale. MaybeRewriteLanguageName() is called
// internally. // internally.
......
...@@ -148,6 +148,17 @@ TEST_F(InputMethodUtilTest, GetInputMethodDescriptorFromId) { ...@@ -148,6 +148,17 @@ TEST_F(InputMethodUtilTest, GetInputMethodDescriptorFromId) {
EXPECT_EQ("zh-CN", descriptor->language_code()); EXPECT_EQ("zh-CN", descriptor->language_code());
} }
TEST_F(InputMethodUtilTest, GetInputMethodDescriptorFromXkbId) {
EXPECT_EQ(NULL, GetInputMethodDescriptorFromXkbId("non_existent"));
const InputMethodDescriptor* descriptor =
GetInputMethodDescriptorFromXkbId("us(dvorak)");
ASSERT_TRUE(NULL != descriptor); // ASSERT_NE doesn't compile.
EXPECT_EQ("xkb:us:dvorak:eng", descriptor->id());
EXPECT_EQ("us(dvorak)", descriptor->keyboard_layout());
EXPECT_EQ("eng", descriptor->language_code());
}
TEST_F(InputMethodUtilTest, GetLanguageNativeDisplayNameFromCode) { TEST_F(InputMethodUtilTest, GetLanguageNativeDisplayNameFromCode) {
EXPECT_EQ(UTF8ToUTF16("suomi"), GetLanguageNativeDisplayNameFromCode("fi")); EXPECT_EQ(UTF8ToUTF16("suomi"), GetLanguageNativeDisplayNameFromCode("fi"));
} }
......
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