| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/ime/component_extension_ime_manager.h" | 5 #include "chromeos/ime/component_extension_ime_manager.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "chromeos/ime/extension_ime_util.h" | 9 #include "chromeos/ime/extension_ime_util.h" |
| 10 | 10 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 login_layout_set_.insert(kLoginLayoutWhitelist[i]); | 80 login_layout_set_.insert(kLoginLayoutWhitelist[i]); |
| 81 } | 81 } |
| 82 } | 82 } |
| 83 | 83 |
| 84 ComponentExtensionIMEManager::~ComponentExtensionIMEManager() { | 84 ComponentExtensionIMEManager::~ComponentExtensionIMEManager() { |
| 85 } | 85 } |
| 86 | 86 |
| 87 void ComponentExtensionIMEManager::Initialize( | 87 void ComponentExtensionIMEManager::Initialize( |
| 88 scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate) { | 88 scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate) { |
| 89 delegate_ = delegate.Pass(); | 89 delegate_ = delegate.Pass(); |
| 90 component_extension_imes_ = delegate_->ListIME(); | 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); |
| 103 } |
| 104 } |
| 91 } | 105 } |
| 92 | 106 |
| 93 bool ComponentExtensionIMEManager::LoadComponentExtensionIME( | 107 bool ComponentExtensionIMEManager::LoadComponentExtensionIME( |
| 94 const std::string& input_method_id) { | 108 const std::string& input_method_id) { |
| 95 ComponentExtensionIME ime; | 109 ComponentExtensionIME ime; |
| 96 if (FindEngineEntry(input_method_id, &ime, NULL)) | 110 if (FindEngineEntry(input_method_id, &ime)) |
| 97 return delegate_->Load(ime.id, ime.manifest, ime.path); | 111 return delegate_->Load(ime.id, ime.manifest, ime.path); |
| 98 else | 112 else |
| 99 return false; | 113 return false; |
| 100 } | 114 } |
| 101 | 115 |
| 102 bool ComponentExtensionIMEManager::UnloadComponentExtensionIME( | 116 bool ComponentExtensionIMEManager::UnloadComponentExtensionIME( |
| 103 const std::string& input_method_id) { | 117 const std::string& input_method_id) { |
| 104 ComponentExtensionIME ime; | 118 ComponentExtensionIME ime; |
| 105 if (!FindEngineEntry(input_method_id, &ime, NULL)) | 119 if (!FindEngineEntry(input_method_id, &ime)) |
| 106 return false; | 120 return false; |
| 107 delegate_->Unload(ime.id, ime.path); | 121 delegate_->Unload(ime.id, ime.path); |
| 108 return true; | 122 return true; |
| 109 } | 123 } |
| 110 | 124 |
| 111 bool ComponentExtensionIMEManager::IsWhitelisted( | 125 bool ComponentExtensionIMEManager::IsWhitelisted( |
| 112 const std::string& input_method_id) { | 126 const std::string& input_method_id) { |
| 113 return extension_ime_util::IsComponentExtensionIME(input_method_id) && | 127 return input_method_id_set_.find(input_method_id) != |
| 114 FindEngineEntry(input_method_id, NULL, NULL); | 128 input_method_id_set_.end(); |
| 115 } | 129 } |
| 116 | 130 |
| 117 bool ComponentExtensionIMEManager::IsWhitelistedExtension( | 131 bool ComponentExtensionIMEManager::IsWhitelistedExtension( |
| 118 const std::string& extension_id) { | 132 const std::string& extension_id) { |
| 119 for (size_t i = 0; i < component_extension_imes_.size(); ++i) { | 133 return component_extension_imes_.find(extension_id) != |
| 120 if (component_extension_imes_[i].id == extension_id) | 134 component_extension_imes_.end(); |
| 121 return true; | |
| 122 } | |
| 123 return false; | |
| 124 } | |
| 125 | |
| 126 std::string ComponentExtensionIMEManager::GetId( | |
| 127 const std::string& extension_id, | |
| 128 const std::string& engine_id) { | |
| 129 ComponentExtensionEngine engine; | |
| 130 const std::string& input_method_id = | |
| 131 extension_ime_util::GetComponentInputMethodID(extension_id, engine_id); | |
| 132 if (!FindEngineEntry(input_method_id, NULL, &engine)) | |
| 133 return ""; | |
| 134 return input_method_id; | |
| 135 } | |
| 136 | |
| 137 std::string ComponentExtensionIMEManager::GetName( | |
| 138 const std::string& input_method_id) { | |
| 139 ComponentExtensionEngine engine; | |
| 140 if (!FindEngineEntry(input_method_id, NULL, &engine)) | |
| 141 return ""; | |
| 142 return engine.display_name; | |
| 143 } | |
| 144 | |
| 145 std::string ComponentExtensionIMEManager::GetDescription( | |
| 146 const std::string& input_method_id) { | |
| 147 ComponentExtensionEngine engine; | |
| 148 if (!FindEngineEntry(input_method_id, NULL, &engine)) | |
| 149 return ""; | |
| 150 return engine.description; | |
| 151 } | |
| 152 | |
| 153 std::vector<std::string> ComponentExtensionIMEManager::ListIMEByLanguage( | |
| 154 const std::string& language) { | |
| 155 std::vector<std::string> result; | |
| 156 for (size_t i = 0; i < component_extension_imes_.size(); ++i) { | |
| 157 for (size_t j = 0; j < component_extension_imes_[i].engines.size(); ++j) { | |
| 158 const ComponentExtensionIME& ime = component_extension_imes_[i]; | |
| 159 if (std::find(ime.engines[j].language_codes.begin(), | |
| 160 ime.engines[j].language_codes.end(), | |
| 161 language) != ime.engines[j].language_codes.end()) { | |
| 162 result.push_back(extension_ime_util::GetComponentInputMethodID( | |
| 163 ime.id, | |
| 164 ime.engines[j].engine_id)); | |
| 165 } | |
| 166 } | |
| 167 } | |
| 168 return result; | |
| 169 } | 135 } |
| 170 | 136 |
| 171 input_method::InputMethodDescriptors | 137 input_method::InputMethodDescriptors |
| 172 ComponentExtensionIMEManager::GetAllIMEAsInputMethodDescriptor() { | 138 ComponentExtensionIMEManager::GetAllIMEAsInputMethodDescriptor() { |
| 173 input_method::InputMethodDescriptors result; | 139 input_method::InputMethodDescriptors result; |
| 174 for (size_t i = 0; i < component_extension_imes_.size(); ++i) { | 140 for (std::map<std::string, ComponentExtensionIME>::const_iterator it = |
| 175 for (size_t j = 0; j < component_extension_imes_[i].engines.size(); ++j) { | 141 component_extension_imes_.begin(); |
| 142 it != component_extension_imes_.end(); ++it) { |
| 143 const ComponentExtensionIME& ext = it->second; |
| 144 for (size_t j = 0; j < ext.engines.size(); ++j) { |
| 145 const ComponentExtensionEngine& ime = ext.engines[j]; |
| 176 const std::string input_method_id = | 146 const std::string input_method_id = |
| 177 extension_ime_util::GetComponentInputMethodID( | 147 extension_ime_util::GetComponentInputMethodID( |
| 178 component_extension_imes_[i].id, | 148 ext.id, ime.engine_id); |
| 179 component_extension_imes_[i].engines[j].engine_id); | 149 const std::vector<std::string>& layouts = ime.layouts; |
| 180 const std::vector<std::string>& layouts = | |
| 181 component_extension_imes_[i].engines[j].layouts; | |
| 182 result.push_back( | 150 result.push_back( |
| 183 input_method::InputMethodDescriptor( | 151 input_method::InputMethodDescriptor( |
| 184 input_method_id, | 152 input_method_id, |
| 185 component_extension_imes_[i].engines[j].display_name, | 153 ime.display_name, |
| 186 std::string(), // TODO(uekawa): Set short name. | 154 std::string(), // TODO(uekawa): Set short name. |
| 187 layouts, | 155 layouts, |
| 188 component_extension_imes_[i].engines[j].language_codes, | 156 ime.language_codes, |
| 189 // Enables extension based xkb keyboards on login screen. | 157 // Enables extension based xkb keyboards on login screen. |
| 190 extension_ime_util::IsKeyboardLayoutExtension( | 158 extension_ime_util::IsKeyboardLayoutExtension( |
| 191 input_method_id) && IsInLoginLayoutWhitelist(layouts), | 159 input_method_id) && IsInLoginLayoutWhitelist(layouts), |
| 192 component_extension_imes_[i].engines[j].options_page_url, | 160 ime.options_page_url, |
| 193 component_extension_imes_[i].engines[j].input_view_url)); | 161 ime.input_view_url)); |
| 194 } | 162 } |
| 195 } | 163 } |
| 196 return result; | 164 return result; |
| 197 } | 165 } |
| 198 | 166 |
| 199 input_method::InputMethodDescriptors | 167 input_method::InputMethodDescriptors |
| 200 ComponentExtensionIMEManager::GetXkbIMEAsInputMethodDescriptor() { | 168 ComponentExtensionIMEManager::GetXkbIMEAsInputMethodDescriptor() { |
| 201 input_method::InputMethodDescriptors result; | 169 input_method::InputMethodDescriptors result; |
| 202 const input_method::InputMethodDescriptors& descriptors = | 170 const input_method::InputMethodDescriptors& descriptors = |
| 203 GetAllIMEAsInputMethodDescriptor(); | 171 GetAllIMEAsInputMethodDescriptor(); |
| 204 for (size_t i = 0; i < descriptors.size(); ++i) { | 172 for (size_t i = 0; i < descriptors.size(); ++i) { |
| 205 if (extension_ime_util::IsKeyboardLayoutExtension(descriptors[i].id())) | 173 if (extension_ime_util::IsKeyboardLayoutExtension(descriptors[i].id())) |
| 206 result.push_back(descriptors[i]); | 174 result.push_back(descriptors[i]); |
| 207 } | 175 } |
| 208 return result; | 176 return result; |
| 209 } | 177 } |
| 210 | 178 |
| 211 bool ComponentExtensionIMEManager::FindEngineEntry( | 179 bool ComponentExtensionIMEManager::FindEngineEntry( |
| 212 const std::string& input_method_id, | 180 const std::string& input_method_id, |
| 213 ComponentExtensionIME* out_extension, | 181 ComponentExtensionIME* out_extension) { |
| 214 ComponentExtensionEngine* out_engine) { | 182 if (!IsWhitelisted(input_method_id)) |
| 215 if (!extension_ime_util::IsComponentExtensionIME(input_method_id)) | |
| 216 return false; | 183 return false; |
| 217 for (size_t i = 0; i < component_extension_imes_.size(); ++i) { | |
| 218 const std::string extension_id = component_extension_imes_[i].id; | |
| 219 const std::vector<ComponentExtensionEngine>& engines = | |
| 220 component_extension_imes_[i].engines; | |
| 221 | 184 |
| 222 for (size_t j = 0; j < engines.size(); ++j) { | 185 std::string extension_id = |
| 223 const std::string trial_ime_id = | 186 extension_ime_util::GetExtensionIDFromInputMethodID(input_method_id); |
| 224 extension_ime_util::GetComponentInputMethodID( | 187 std::map<std::string, ComponentExtensionIME>::iterator it = |
| 225 extension_id, engines[j].engine_id); | 188 component_extension_imes_.find(extension_id); |
| 226 if (trial_ime_id != input_method_id) | 189 if (it == component_extension_imes_.end()) |
| 227 continue; | 190 return false; |
| 228 | 191 |
| 229 if (out_extension) | 192 if (out_extension) |
| 230 *out_extension = component_extension_imes_[i]; | 193 *out_extension = it->second; |
| 231 if (out_engine) | 194 return true; |
| 232 *out_engine = component_extension_imes_[i].engines[j]; | |
| 233 return true; | |
| 234 } | |
| 235 } | |
| 236 return false; | |
| 237 } | 195 } |
| 238 | 196 |
| 239 bool ComponentExtensionIMEManager::IsInLoginLayoutWhitelist( | 197 bool ComponentExtensionIMEManager::IsInLoginLayoutWhitelist( |
| 240 const std::vector<std::string>& layouts) { | 198 const std::vector<std::string>& layouts) { |
| 241 for (size_t i = 0; i < layouts.size(); ++i) { | 199 for (size_t i = 0; i < layouts.size(); ++i) { |
| 242 if (login_layout_set_.find(layouts[i]) != login_layout_set_.end()) | 200 if (login_layout_set_.find(layouts[i]) != login_layout_set_.end()) |
| 243 return true; | 201 return true; |
| 244 } | 202 } |
| 245 return false; | 203 return false; |
| 246 } | 204 } |
| 247 | 205 |
| 248 } // namespace chromeos | 206 } // namespace chromeos |
| OLD | NEW |