OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/ui/webui/options/chromeos/cros_language_options_handler .h" | 5 #include "chrome/browser/ui/webui/options/chromeos/cros_language_options_handler .h" |
6 | 6 |
7 #include <algorithm> | |
8 #include <iterator> | |
7 #include <map> | 9 #include <map> |
8 #include <set> | 10 #include <set> |
9 #include <vector> | 11 #include <vector> |
10 | 12 |
11 #include "base/bind.h" | 13 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
13 #include "base/i18n/rtl.h" | 15 #include "base/i18n/rtl.h" |
14 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
15 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
16 #include "base/values.h" | 18 #include "base/values.h" |
17 #include "chrome/app/chrome_command_ids.h" | 19 #include "chrome/app/chrome_command_ids.h" |
18 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" |
21 #include "chrome/browser/chromeos/customization_document.h" | |
19 #include "chrome/browser/chromeos/input_method/input_method_util.h" | 22 #include "chrome/browser/chromeos/input_method/input_method_util.h" |
20 #include "chrome/browser/extensions/extension_service.h" | 23 #include "chrome/browser/extensions/extension_service.h" |
21 #include "chrome/browser/extensions/extension_system.h" | 24 #include "chrome/browser/extensions/extension_system.h" |
22 #include "chrome/browser/extensions/extension_tab_util.h" | 25 #include "chrome/browser/extensions/extension_tab_util.h" |
23 #include "chrome/browser/lifetime/application_lifetime.h" | 26 #include "chrome/browser/lifetime/application_lifetime.h" |
24 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
25 #include "chrome/browser/ui/browser.h" | 28 #include "chrome/browser/ui/browser.h" |
26 #include "chrome/browser/ui/browser_finder.h" | 29 #include "chrome/browser/ui/browser_finder.h" |
27 #include "chrome/browser/ui/browser_window.h" | 30 #include "chrome/browser/ui/browser_window.h" |
28 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 31 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
(...skipping 17 matching lines...) Expand all Loading... | |
46 | 49 |
47 bool IsBlacklisted(const std::string& language_code) { | 50 bool IsBlacklisted(const std::string& language_code) { |
48 return language_code == "si"; // Sinhala | 51 return language_code == "si"; // Sinhala |
49 } | 52 } |
50 | 53 |
51 } // namespace | 54 } // namespace |
52 | 55 |
53 namespace chromeos { | 56 namespace chromeos { |
54 namespace options { | 57 namespace options { |
55 | 58 |
59 const char kVENDOR_OTHER_LANGUAGES_LIST_DIVIDER[] = | |
60 "VENDOR_OTHER_LANGUAGES_LIST_DIVIDER"; | |
Nikita (slow)
2014/01/31 15:47:20
nit: How about changing to a short constant as "di
Alexander Alekseev
2014/01/31 17:21:00
Languages are all lower-case, so any string starti
| |
61 | |
56 CrosLanguageOptionsHandler::CrosLanguageOptionsHandler() | 62 CrosLanguageOptionsHandler::CrosLanguageOptionsHandler() |
57 : composition_extension_appended_(false), | 63 : composition_extension_appended_(false), |
58 is_page_initialized_(false) { | 64 is_page_initialized_(false) { |
59 input_method::InputMethodManager::Get()->GetComponentExtensionIMEManager()-> | 65 input_method::InputMethodManager::Get()->GetComponentExtensionIMEManager()-> |
60 AddObserver(this); | 66 AddObserver(this); |
61 } | 67 } |
62 | 68 |
63 CrosLanguageOptionsHandler::~CrosLanguageOptionsHandler() { | 69 CrosLanguageOptionsHandler::~CrosLanguageOptionsHandler() { |
64 input_method::InputMethodManager::Get()->GetComponentExtensionIMEManager()-> | 70 input_method::InputMethodManager::Get()->GetComponentExtensionIMEManager()-> |
65 RemoveObserver(this); | 71 RemoveObserver(this); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
177 | 183 |
178 input_method_list->Append(dictionary); | 184 input_method_list->Append(dictionary); |
179 } | 185 } |
180 | 186 |
181 return input_method_list; | 187 return input_method_list; |
182 } | 188 } |
183 | 189 |
184 // static | 190 // static |
185 base::ListValue* CrosLanguageOptionsHandler::GetLanguageListInternal( | 191 base::ListValue* CrosLanguageOptionsHandler::GetLanguageListInternal( |
186 const input_method::InputMethodDescriptors& descriptors, | 192 const input_method::InputMethodDescriptors& descriptors, |
187 const std::vector<std::string>& base_language_codes) { | 193 const std::vector<std::string>& base_language_codes, |
194 const bool insert_divider) { | |
188 const std::string app_locale = g_browser_process->GetApplicationLocale(); | 195 const std::string app_locale = g_browser_process->GetApplicationLocale(); |
189 | 196 |
190 std::set<std::string> language_codes; | 197 std::set<std::string> language_codes; |
191 // Collect the language codes from the supported input methods. | 198 // Collect the language codes from the supported input methods. |
192 for (size_t i = 0; i < descriptors.size(); ++i) { | 199 for (size_t i = 0; i < descriptors.size(); ++i) { |
193 const input_method::InputMethodDescriptor& descriptor = descriptors[i]; | 200 const input_method::InputMethodDescriptor& descriptor = descriptors[i]; |
194 const std::vector<std::string>& languages = | 201 const std::vector<std::string>& languages = |
195 descriptor.language_codes(); | 202 descriptor.language_codes(); |
196 for (size_t i = 0; i < languages.size(); ++i) | 203 for (size_t i = 0; i < languages.size(); ++i) |
197 language_codes.insert(languages[i]); | 204 language_codes.insert(languages[i]); |
198 } | 205 } |
199 | 206 |
207 const StartupCustomizationDocument* startup_manifest = | |
208 StartupCustomizationDocument::GetInstance(); | |
209 | |
210 const std::vector<std::string>& configured_locales = | |
211 startup_manifest->configured_locales(); | |
212 | |
213 // Languages sort order. | |
214 std::map<std::string, int /* index */> language_index; | |
215 for (size_t i = 0; i < configured_locales.size(); ++i) { | |
216 language_index[configured_locales[i]] = i; | |
217 } | |
218 | |
200 // Map of display name -> {language code, native_display_name}. | 219 // Map of display name -> {language code, native_display_name}. |
201 // In theory, we should be able to create a map that is sorted by | 220 // In theory, we should be able to create a map that is sorted by |
202 // display names using ICU comparator, but doing it is hard, thus we'll | 221 // display names using ICU comparator, but doing it is hard, thus we'll |
203 // use an auxiliary vector to achieve the same result. | 222 // use an auxiliary vector to achieve the same result. |
204 typedef std::pair<std::string, base::string16> LanguagePair; | 223 typedef std::pair<std::string, base::string16> LanguagePair; |
205 typedef std::map<base::string16, LanguagePair> LanguageMap; | 224 typedef std::map<base::string16, LanguagePair> LanguageMap; |
206 LanguageMap language_map; | 225 LanguageMap language_map; |
207 // The auxiliary vector mentioned above. | 226 |
227 // The auxiliary vector mentioned above. (except vendor locales) | |
208 std::vector<base::string16> display_names; | 228 std::vector<base::string16> display_names; |
209 | 229 |
230 // Separate vector of vendor locales. | |
231 std::vector<base::string16> configured_locales_display_names( | |
232 configured_locales.size()); | |
233 | |
234 size_t configured_locales_count = 0; | |
210 // Build the list of display names, and build the language map. | 235 // Build the list of display names, and build the language map. |
211 for (std::set<std::string>::const_iterator iter = language_codes.begin(); | 236 for (std::set<std::string>::const_iterator iter = language_codes.begin(); |
212 iter != language_codes.end(); ++iter) { | 237 iter != language_codes.end(); ++iter) { |
213 // Exclude the language which is not in |base_langauge_codes| even it has | 238 // Exclude the language which is not in |base_langauge_codes| even it has |
214 // input methods. | 239 // input methods. |
215 if (std::find(base_language_codes.begin(), | 240 if (std::find(base_language_codes.begin(), |
216 base_language_codes.end(), | 241 base_language_codes.end(), |
217 *iter) == base_language_codes.end()) { | 242 *iter) == base_language_codes.end()) { |
218 continue; | 243 continue; |
219 } | 244 } |
220 | 245 |
221 const base::string16 display_name = | 246 const base::string16 display_name = |
222 l10n_util::GetDisplayNameForLocale(*iter, app_locale, true); | 247 l10n_util::GetDisplayNameForLocale(*iter, app_locale, true); |
223 const base::string16 native_display_name = | 248 const base::string16 native_display_name = |
224 l10n_util::GetDisplayNameForLocale(*iter, *iter, true); | 249 l10n_util::GetDisplayNameForLocale(*iter, *iter, true); |
225 | 250 |
226 display_names.push_back(display_name); | |
227 language_map[display_name] = | 251 language_map[display_name] = |
228 std::make_pair(*iter, native_display_name); | 252 std::make_pair(*iter, native_display_name); |
253 | |
254 const std::map<std::string, int>::const_iterator index_pos = | |
255 language_index.find(*iter); | |
256 if (index_pos != language_index.end()) { | |
257 configured_locales_display_names[index_pos->second] = display_name; | |
258 ++configured_locales_count; | |
259 } else { | |
260 display_names.push_back(display_name); | |
261 } | |
229 } | 262 } |
230 DCHECK_EQ(display_names.size(), language_map.size()); | 263 DCHECK_EQ(display_names.size() + configured_locales_count, |
264 language_map.size()); | |
231 | 265 |
232 // Build the list of display names, and build the language map. | 266 // Build the list of display names, and build the language map. |
233 for (size_t i = 0; i < base_language_codes.size(); ++i) { | 267 for (size_t i = 0; i < base_language_codes.size(); ++i) { |
234 // Skip this language if it was already added. | 268 // Skip this language if it was already added. |
235 if (language_codes.find(base_language_codes[i]) != language_codes.end()) | 269 if (language_codes.find(base_language_codes[i]) != language_codes.end()) |
236 continue; | 270 continue; |
237 | 271 |
238 // TODO(zork): Remove this blacklist when fonts are added to Chrome OS. | 272 // TODO(zork): Remove this blacklist when fonts are added to Chrome OS. |
239 // see: crbug.com/240586 | 273 // see: crbug.com/240586 |
240 if (IsBlacklisted(base_language_codes[i])) | 274 if (IsBlacklisted(base_language_codes[i])) |
241 continue; | 275 continue; |
242 | 276 |
243 base::string16 display_name = | 277 base::string16 display_name = |
244 l10n_util::GetDisplayNameForLocale( | 278 l10n_util::GetDisplayNameForLocale( |
245 base_language_codes[i], app_locale, false); | 279 base_language_codes[i], app_locale, false); |
246 base::string16 native_display_name = | 280 base::string16 native_display_name = |
247 l10n_util::GetDisplayNameForLocale( | 281 l10n_util::GetDisplayNameForLocale( |
248 base_language_codes[i], base_language_codes[i], false); | 282 base_language_codes[i], base_language_codes[i], false); |
249 display_names.push_back(display_name); | 283 display_names.push_back(display_name); |
250 language_map[display_name] = | 284 language_map[display_name] = |
251 std::make_pair(base_language_codes[i], native_display_name); | 285 std::make_pair(base_language_codes[i], native_display_name); |
286 | |
287 const std::map<std::string, int>::const_iterator index_pos = | |
288 language_index.find(base_language_codes[i]); | |
289 if (index_pos != language_index.end()) { | |
290 configured_locales_display_names[index_pos->second] = display_name; | |
291 ++configured_locales_count; | |
292 } else { | |
293 display_names.push_back(display_name); | |
294 } | |
252 } | 295 } |
253 | 296 |
254 // Sort display names using locale specific sorter. | 297 // Sort display names using locale specific sorter. |
255 l10n_util::SortStrings16(app_locale, &display_names); | 298 l10n_util::SortStrings16(app_locale, &display_names); |
299 // Concatenate configured_locales_display_names and display_names. | |
300 // Insert special divider in between. | |
301 std::vector<base::string16> out_display_names; | |
302 for (size_t i = 0; i < configured_locales_display_names.size(); ++i) { | |
303 if (configured_locales_display_names[i].size() == 0) | |
304 continue; | |
305 out_display_names.push_back(configured_locales_display_names[i]); | |
306 } | |
307 | |
308 base::string16 divider16; | |
309 if (insert_divider) { | |
310 divider16 = base::ASCIIToUTF16( | |
311 insert_divider ? "" : kVENDOR_OTHER_LANGUAGES_LIST_DIVIDER); | |
312 out_display_names.push_back(divider16); | |
313 } | |
314 | |
315 std::copy(display_names.begin(), | |
316 display_names.end(), | |
317 std::back_inserter(out_display_names)); | |
256 | 318 |
257 // Build the language list from the language map. | 319 // Build the language list from the language map. |
258 base::ListValue* language_list = new base::ListValue(); | 320 base::ListValue* language_list = new base::ListValue(); |
259 for (size_t i = 0; i < display_names.size(); ++i) { | 321 for (size_t i = 0; i < out_display_names.size(); ++i) { |
260 // Sets the directionality of the display language name. | 322 // Sets the directionality of the display language name. |
261 base::string16 display_name(display_names[i]); | 323 base::string16 display_name(out_display_names[i]); |
324 if (insert_divider && display_name == divider16) { | |
325 // Insert divider. | |
326 base::DictionaryValue* dictionary = new base::DictionaryValue(); | |
327 dictionary->SetString("code", kVENDOR_OTHER_LANGUAGES_LIST_DIVIDER); | |
328 language_list->Append(dictionary); | |
329 continue; | |
330 } | |
262 bool markup_removal = | 331 bool markup_removal = |
263 base::i18n::UnadjustStringForLocaleDirection(&display_name); | 332 base::i18n::UnadjustStringForLocaleDirection(&display_name); |
264 DCHECK(markup_removal); | 333 DCHECK(markup_removal); |
265 bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(display_name); | 334 bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(display_name); |
266 std::string directionality = has_rtl_chars ? "rtl" : "ltr"; | 335 std::string directionality = has_rtl_chars ? "rtl" : "ltr"; |
267 | 336 |
268 const LanguagePair& pair = language_map[display_names[i]]; | 337 const LanguagePair& pair = language_map[out_display_names[i]]; |
269 base::DictionaryValue* dictionary = new base::DictionaryValue(); | 338 base::DictionaryValue* dictionary = new base::DictionaryValue(); |
270 dictionary->SetString("code", pair.first); | 339 dictionary->SetString("code", pair.first); |
271 dictionary->SetString("displayName", display_names[i]); | 340 dictionary->SetString("displayName", out_display_names[i]); |
272 dictionary->SetString("textDirection", directionality); | 341 dictionary->SetString("textDirection", directionality); |
273 dictionary->SetString("nativeDisplayName", pair.second); | 342 dictionary->SetString("nativeDisplayName", pair.second); |
274 language_list->Append(dictionary); | 343 language_list->Append(dictionary); |
275 } | 344 } |
276 | 345 |
277 return language_list; | 346 return language_list; |
278 } | 347 } |
279 | 348 |
280 // static | 349 // static |
281 base::ListValue* CrosLanguageOptionsHandler::GetAcceptLanguageList( | 350 base::ListValue* CrosLanguageOptionsHandler::GetAcceptLanguageList( |
282 const input_method::InputMethodDescriptors& descriptors) { | 351 const input_method::InputMethodDescriptors& descriptors) { |
283 // Collect the language codes from the supported accept-languages. | 352 // Collect the language codes from the supported accept-languages. |
284 const std::string app_locale = g_browser_process->GetApplicationLocale(); | 353 const std::string app_locale = g_browser_process->GetApplicationLocale(); |
285 std::vector<std::string> accept_language_codes; | 354 std::vector<std::string> accept_language_codes; |
286 l10n_util::GetAcceptLanguagesForLocale(app_locale, &accept_language_codes); | 355 l10n_util::GetAcceptLanguagesForLocale(app_locale, &accept_language_codes); |
287 return GetLanguageListInternal(descriptors, accept_language_codes); | 356 return GetLanguageListInternal(descriptors, accept_language_codes, false); |
288 } | 357 } |
289 | 358 |
290 // static | 359 // static |
291 base::ListValue* CrosLanguageOptionsHandler::GetUILanguageList( | 360 base::ListValue* CrosLanguageOptionsHandler::GetUILanguageList( |
292 const input_method::InputMethodDescriptors& descriptors) { | 361 const input_method::InputMethodDescriptors& descriptors) { |
293 // Collect the language codes from the available locales. | 362 // Collect the language codes from the available locales. |
294 return GetLanguageListInternal(descriptors, l10n_util::GetAvailableLocales()); | 363 return GetLanguageListInternal( |
364 descriptors, l10n_util::GetAvailableLocales(), true); | |
295 } | 365 } |
296 | 366 |
297 base::ListValue* | 367 base::ListValue* |
298 CrosLanguageOptionsHandler::ConvertInputMethodDescriptosToIMEList( | 368 CrosLanguageOptionsHandler::ConvertInputMethodDescriptosToIMEList( |
299 const input_method::InputMethodDescriptors& descriptors) { | 369 const input_method::InputMethodDescriptors& descriptors) { |
300 scoped_ptr<base::ListValue> ime_ids_list(new base::ListValue()); | 370 scoped_ptr<base::ListValue> ime_ids_list(new base::ListValue()); |
301 for (size_t i = 0; i < descriptors.size(); ++i) { | 371 for (size_t i = 0; i < descriptors.size(); ++i) { |
302 const input_method::InputMethodDescriptor& descriptor = descriptors[i]; | 372 const input_method::InputMethodDescriptor& descriptor = descriptors[i]; |
303 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue()); | 373 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue()); |
304 dictionary->SetString("id", descriptor.id()); | 374 dictionary->SetString("id", descriptor.id()); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
416 ConvertInputMethodDescriptosToIMEList( | 486 ConvertInputMethodDescriptosToIMEList( |
417 component_extension_manager->GetAllIMEAsInputMethodDescriptor())); | 487 component_extension_manager->GetAllIMEAsInputMethodDescriptor())); |
418 web_ui()->CallJavascriptFunction( | 488 web_ui()->CallJavascriptFunction( |
419 "options.LanguageOptions.onComponentManagerInitialized", | 489 "options.LanguageOptions.onComponentManagerInitialized", |
420 *ime_list); | 490 *ime_list); |
421 composition_extension_appended_ = true; | 491 composition_extension_appended_ = true; |
422 } | 492 } |
423 | 493 |
424 } // namespace options | 494 } // namespace options |
425 } // namespace chromeos | 495 } // namespace chromeos |
OLD | NEW |