1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chromeos/ime/component_extension_ime_manager.h"
7 #include "base/logging.h"
8 #include "base/strings/string_util.h"
9 #include "chromeos/ime/extension_ime_util.h"
15 // The whitelist for enabling extension based xkb keyboards at login session.
16 const char* kLoginLayoutWhitelist
[] = {
60 ComponentExtensionEngine::ComponentExtensionEngine() {
63 ComponentExtensionEngine::~ComponentExtensionEngine() {
66 ComponentExtensionIME::ComponentExtensionIME() {
69 ComponentExtensionIME::~ComponentExtensionIME() {
72 ComponentExtensionIMEManagerDelegate::ComponentExtensionIMEManagerDelegate() {
75 ComponentExtensionIMEManagerDelegate::~ComponentExtensionIMEManagerDelegate() {
78 ComponentExtensionIMEManager::ComponentExtensionIMEManager() {
79 for (size_t i
= 0; i
< arraysize(kLoginLayoutWhitelist
); ++i
) {
80 login_layout_set_
.insert(kLoginLayoutWhitelist
[i
]);
84 ComponentExtensionIMEManager::~ComponentExtensionIMEManager() {
87 void ComponentExtensionIMEManager::Initialize(
88 scoped_ptr
<ComponentExtensionIMEManagerDelegate
> delegate
) {
89 delegate_
= delegate
.Pass();
90 std::vector
<ComponentExtensionIME
> ext_list
= delegate_
->ListIME();
91 for (size_t i
= 0; i
< ext_list
.size(); ++i
) {
92 ComponentExtensionIME
& ext
= ext_list
[i
];
93 bool extension_exists
= IsWhitelistedExtension(ext
.id
);
94 if (!extension_exists
)
95 component_extension_imes_
[ext
.id
] = ext
;
96 for (size_t j
= 0; j
< ext
.engines
.size(); ++j
) {
97 ComponentExtensionEngine
& ime
= ext
.engines
[j
];
98 const std::string input_method_id
=
99 extension_ime_util::GetComponentInputMethodID(ext
.id
, ime
.engine_id
);
100 if (extension_exists
&& !IsWhitelisted(input_method_id
))
101 component_extension_imes_
[ext
.id
].engines
.push_back(ime
);
102 input_method_id_set_
.insert(input_method_id
);
107 bool ComponentExtensionIMEManager::LoadComponentExtensionIME(
109 const std::string
& input_method_id
) {
110 ComponentExtensionIME ime
;
111 if (FindEngineEntry(input_method_id
, &ime
)) {
112 delegate_
->Load(profile
, ime
.id
, ime
.manifest
, ime
.path
);
118 bool ComponentExtensionIMEManager::UnloadComponentExtensionIME(
120 const std::string
& input_method_id
) {
121 ComponentExtensionIME ime
;
122 if (!FindEngineEntry(input_method_id
, &ime
))
124 delegate_
->Unload(profile
, ime
.id
, ime
.path
);
128 bool ComponentExtensionIMEManager::IsWhitelisted(
129 const std::string
& input_method_id
) {
130 return input_method_id_set_
.find(input_method_id
) !=
131 input_method_id_set_
.end();
134 bool ComponentExtensionIMEManager::IsWhitelistedExtension(
135 const std::string
& extension_id
) {
136 return component_extension_imes_
.find(extension_id
) !=
137 component_extension_imes_
.end();
140 input_method::InputMethodDescriptors
141 ComponentExtensionIMEManager::GetAllIMEAsInputMethodDescriptor() {
142 input_method::InputMethodDescriptors result
;
143 for (std::map
<std::string
, ComponentExtensionIME
>::const_iterator it
=
144 component_extension_imes_
.begin();
145 it
!= component_extension_imes_
.end(); ++it
) {
146 const ComponentExtensionIME
& ext
= it
->second
;
147 for (size_t j
= 0; j
< ext
.engines
.size(); ++j
) {
148 const ComponentExtensionEngine
& ime
= ext
.engines
[j
];
149 const std::string input_method_id
=
150 extension_ime_util::GetComponentInputMethodID(
151 ext
.id
, ime
.engine_id
);
152 const std::vector
<std::string
>& layouts
= ime
.layouts
;
154 input_method::InputMethodDescriptor(
157 std::string(), // TODO(uekawa): Set short name.
160 // Enables extension based xkb keyboards on login screen.
161 extension_ime_util::IsKeyboardLayoutExtension(
162 input_method_id
) && IsInLoginLayoutWhitelist(layouts
),
163 ime
.options_page_url
,
164 ime
.input_view_url
));
170 input_method::InputMethodDescriptors
171 ComponentExtensionIMEManager::GetXkbIMEAsInputMethodDescriptor() {
172 input_method::InputMethodDescriptors result
;
173 const input_method::InputMethodDescriptors
& descriptors
=
174 GetAllIMEAsInputMethodDescriptor();
175 for (size_t i
= 0; i
< descriptors
.size(); ++i
) {
176 if (extension_ime_util::IsKeyboardLayoutExtension(descriptors
[i
].id()))
177 result
.push_back(descriptors
[i
]);
182 bool ComponentExtensionIMEManager::FindEngineEntry(
183 const std::string
& input_method_id
,
184 ComponentExtensionIME
* out_extension
) {
185 if (!IsWhitelisted(input_method_id
))
188 std::string extension_id
=
189 extension_ime_util::GetExtensionIDFromInputMethodID(input_method_id
);
190 std::map
<std::string
, ComponentExtensionIME
>::iterator it
=
191 component_extension_imes_
.find(extension_id
);
192 if (it
== component_extension_imes_
.end())
196 *out_extension
= it
->second
;
200 bool ComponentExtensionIMEManager::IsInLoginLayoutWhitelist(
201 const std::vector
<std::string
>& layouts
) {
202 for (size_t i
= 0; i
< layouts
.size(); ++i
) {
203 if (login_layout_set_
.find(layouts
[i
]) != login_layout_set_
.end())
209 } // namespace chromeos