Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Side by Side Diff: chrome/browser/ui/webui/options/chromeos/cros_language_options_handler.cc

Issue 144363006: Expand VPD initial_locale to a list of locales. Use the expanded VPD initial_locale on the OOBE. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update after review. Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 180
178 input_method_list->Append(dictionary); 181 input_method_list->Append(dictionary);
179 } 182 }
180 183
181 return input_method_list; 184 return input_method_list;
182 } 185 }
183 186
184 // static 187 // static
185 base::ListValue* CrosLanguageOptionsHandler::GetLanguageListInternal( 188 base::ListValue* CrosLanguageOptionsHandler::GetLanguageListInternal(
186 const input_method::InputMethodDescriptors& descriptors, 189 const input_method::InputMethodDescriptors& descriptors,
187 const std::vector<std::string>& base_language_codes) { 190 const std::vector<std::string>& base_language_codes,
191 const bool insert_divider) {
188 const std::string app_locale = g_browser_process->GetApplicationLocale(); 192 const std::string app_locale = g_browser_process->GetApplicationLocale();
189 193
190 std::set<std::string> language_codes; 194 std::set<std::string> language_codes;
191 // Collect the language codes from the supported input methods. 195 // Collect the language codes from the supported input methods.
192 for (size_t i = 0; i < descriptors.size(); ++i) { 196 for (size_t i = 0; i < descriptors.size(); ++i) {
193 const input_method::InputMethodDescriptor& descriptor = descriptors[i]; 197 const input_method::InputMethodDescriptor& descriptor = descriptors[i];
194 const std::vector<std::string>& languages = 198 const std::vector<std::string>& languages =
195 descriptor.language_codes(); 199 descriptor.language_codes();
196 for (size_t i = 0; i < languages.size(); ++i) 200 for (size_t i = 0; i < languages.size(); ++i)
197 language_codes.insert(languages[i]); 201 language_codes.insert(languages[i]);
198 } 202 }
199 203
204 const StartupCustomizationDocument* startup_manifest =
205 StartupCustomizationDocument::GetInstance();
206
207 const std::vector<std::string>& configured_locales =
208 startup_manifest->configured_locales();
209
210 // Languages sort order.
211 std::map<std::string, int /* index */> language_index;
212 for (size_t i = 0; i < configured_locales.size(); ++i) {
213 language_index[configured_locales[i]] = i;
214 }
215
200 // Map of display name -> {language code, native_display_name}. 216 // Map of display name -> {language code, native_display_name}.
201 // In theory, we should be able to create a map that is sorted by 217 // 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 218 // display names using ICU comparator, but doing it is hard, thus we'll
203 // use an auxiliary vector to achieve the same result. 219 // use an auxiliary vector to achieve the same result.
204 typedef std::pair<std::string, base::string16> LanguagePair; 220 typedef std::pair<std::string, base::string16> LanguagePair;
205 typedef std::map<base::string16, LanguagePair> LanguageMap; 221 typedef std::map<base::string16, LanguagePair> LanguageMap;
206 LanguageMap language_map; 222 LanguageMap language_map;
207 // The auxiliary vector mentioned above. 223 // The auxiliary vector mentioned above. (except vendor locales)
Nikita (slow) 2014/01/30 13:23:28 nit: Insert empty line before comment (and below).
Alexander Alekseev 2014/01/31 12:46:54 Done.
208 std::vector<base::string16> display_names; 224 std::vector<base::string16> display_names;
225 // Separate vector of vendor locales.
226 std::vector<base::string16> configured_locales_display_names(
227 configured_locales.size());
209 228
229 size_t configured_locales_count = 0;
210 // Build the list of display names, and build the language map. 230 // Build the list of display names, and build the language map.
211 for (std::set<std::string>::const_iterator iter = language_codes.begin(); 231 for (std::set<std::string>::const_iterator iter = language_codes.begin();
212 iter != language_codes.end(); ++iter) { 232 iter != language_codes.end(); ++iter) {
213 // Exclude the language which is not in |base_langauge_codes| even it has 233 // Exclude the language which is not in |base_langauge_codes| even it has
214 // input methods. 234 // input methods.
215 if (std::find(base_language_codes.begin(), 235 if (std::find(base_language_codes.begin(),
216 base_language_codes.end(), 236 base_language_codes.end(),
217 *iter) == base_language_codes.end()) { 237 *iter) == base_language_codes.end()) {
218 continue; 238 continue;
219 } 239 }
220 240
221 const base::string16 display_name = 241 const base::string16 display_name =
222 l10n_util::GetDisplayNameForLocale(*iter, app_locale, true); 242 l10n_util::GetDisplayNameForLocale(*iter, app_locale, true);
223 const base::string16 native_display_name = 243 const base::string16 native_display_name =
224 l10n_util::GetDisplayNameForLocale(*iter, *iter, true); 244 l10n_util::GetDisplayNameForLocale(*iter, *iter, true);
225 245
226 display_names.push_back(display_name);
227 language_map[display_name] = 246 language_map[display_name] =
228 std::make_pair(*iter, native_display_name); 247 std::make_pair(*iter, native_display_name);
248
249 const std::map<std::string, int>::const_iterator index_pos =
250 language_index.find(*iter);
251 if (index_pos != language_index.end()) {
252 configured_locales_display_names[index_pos->second] = display_name;
253 ++configured_locales_count;
254 } else {
255 display_names.push_back(display_name);
256 }
229 } 257 }
230 DCHECK_EQ(display_names.size(), language_map.size()); 258 DCHECK_EQ(display_names.size() + configured_locales_count,
259 language_map.size());
231 260
232 // Build the list of display names, and build the language map. 261 // Build the list of display names, and build the language map.
233 for (size_t i = 0; i < base_language_codes.size(); ++i) { 262 for (size_t i = 0; i < base_language_codes.size(); ++i) {
234 // Skip this language if it was already added. 263 // Skip this language if it was already added.
235 if (language_codes.find(base_language_codes[i]) != language_codes.end()) 264 if (language_codes.find(base_language_codes[i]) != language_codes.end())
236 continue; 265 continue;
237 266
238 // TODO(zork): Remove this blacklist when fonts are added to Chrome OS. 267 // TODO(zork): Remove this blacklist when fonts are added to Chrome OS.
239 // see: crbug.com/240586 268 // see: crbug.com/240586
240 if (IsBlacklisted(base_language_codes[i])) 269 if (IsBlacklisted(base_language_codes[i]))
241 continue; 270 continue;
242 271
243 base::string16 display_name = 272 base::string16 display_name =
244 l10n_util::GetDisplayNameForLocale( 273 l10n_util::GetDisplayNameForLocale(
245 base_language_codes[i], app_locale, false); 274 base_language_codes[i], app_locale, false);
246 base::string16 native_display_name = 275 base::string16 native_display_name =
247 l10n_util::GetDisplayNameForLocale( 276 l10n_util::GetDisplayNameForLocale(
248 base_language_codes[i], base_language_codes[i], false); 277 base_language_codes[i], base_language_codes[i], false);
249 display_names.push_back(display_name); 278 display_names.push_back(display_name);
250 language_map[display_name] = 279 language_map[display_name] =
251 std::make_pair(base_language_codes[i], native_display_name); 280 std::make_pair(base_language_codes[i], native_display_name);
281
282 const std::map<std::string, int>::const_iterator index_pos =
283 language_index.find(base_language_codes[i]);
284 if (index_pos != language_index.end()) {
285 configured_locales_display_names[index_pos->second] = display_name;
286 ++configured_locales_count;
287 } else {
288 display_names.push_back(display_name);
289 }
252 } 290 }
253 291
254 // Sort display names using locale specific sorter. 292 // Sort display names using locale specific sorter.
255 l10n_util::SortStrings16(app_locale, &display_names); 293 l10n_util::SortStrings16(app_locale, &display_names);
294 // Concatenate configured_locales_display_names and display_names.
295 // Insert empty string as a divider.
296 std::vector<base::string16> out_display_names;
297 for (size_t i = 0; i < configured_locales_display_names.size(); ++i) {
298 if (configured_locales_display_names[i].size() == 0)
299 continue;
300 out_display_names.push_back(configured_locales_display_names[i]);
301 }
302 if (insert_divider)
303 out_display_names.push_back(base::string16());
304 std::copy(display_names.begin(),
305 display_names.end(),
306 std::back_inserter(out_display_names));
256 307
257 // Build the language list from the language map. 308 // Build the language list from the language map.
258 base::ListValue* language_list = new base::ListValue(); 309 base::ListValue* language_list = new base::ListValue();
259 for (size_t i = 0; i < display_names.size(); ++i) { 310 for (size_t i = 0; i < out_display_names.size(); ++i) {
260 // Sets the directionality of the display language name. 311 // Sets the directionality of the display language name.
261 base::string16 display_name(display_names[i]); 312 base::string16 display_name(out_display_names[i]);
313 if (insert_divider && display_name.empty()) {
314 // Insert divider.
315 base::DictionaryValue* dictionary = new base::DictionaryValue();
316 dictionary->SetString("code", "");
Nikita (slow) 2014/01/30 13:23:28 nit: As discussed, let's use a specific string con
Alexander Alekseev 2014/01/31 12:46:54 Done.
317 language_list->Append(dictionary);
318 continue;
319 }
262 bool markup_removal = 320 bool markup_removal =
263 base::i18n::UnadjustStringForLocaleDirection(&display_name); 321 base::i18n::UnadjustStringForLocaleDirection(&display_name);
264 DCHECK(markup_removal); 322 DCHECK(markup_removal);
265 bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(display_name); 323 bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(display_name);
266 std::string directionality = has_rtl_chars ? "rtl" : "ltr"; 324 std::string directionality = has_rtl_chars ? "rtl" : "ltr";
267 325
268 const LanguagePair& pair = language_map[display_names[i]]; 326 const LanguagePair& pair = language_map[out_display_names[i]];
269 base::DictionaryValue* dictionary = new base::DictionaryValue(); 327 base::DictionaryValue* dictionary = new base::DictionaryValue();
270 dictionary->SetString("code", pair.first); 328 dictionary->SetString("code", pair.first);
271 dictionary->SetString("displayName", display_names[i]); 329 dictionary->SetString("displayName", out_display_names[i]);
272 dictionary->SetString("textDirection", directionality); 330 dictionary->SetString("textDirection", directionality);
273 dictionary->SetString("nativeDisplayName", pair.second); 331 dictionary->SetString("nativeDisplayName", pair.second);
274 language_list->Append(dictionary); 332 language_list->Append(dictionary);
275 } 333 }
276 334
277 return language_list; 335 return language_list;
278 } 336 }
279 337
280 // static 338 // static
281 base::ListValue* CrosLanguageOptionsHandler::GetAcceptLanguageList( 339 base::ListValue* CrosLanguageOptionsHandler::GetAcceptLanguageList(
282 const input_method::InputMethodDescriptors& descriptors) { 340 const input_method::InputMethodDescriptors& descriptors) {
283 // Collect the language codes from the supported accept-languages. 341 // Collect the language codes from the supported accept-languages.
284 const std::string app_locale = g_browser_process->GetApplicationLocale(); 342 const std::string app_locale = g_browser_process->GetApplicationLocale();
285 std::vector<std::string> accept_language_codes; 343 std::vector<std::string> accept_language_codes;
286 l10n_util::GetAcceptLanguagesForLocale(app_locale, &accept_language_codes); 344 l10n_util::GetAcceptLanguagesForLocale(app_locale, &accept_language_codes);
287 return GetLanguageListInternal(descriptors, accept_language_codes); 345 return GetLanguageListInternal(descriptors, accept_language_codes, false);
288 } 346 }
289 347
290 // static 348 // static
291 base::ListValue* CrosLanguageOptionsHandler::GetUILanguageList( 349 base::ListValue* CrosLanguageOptionsHandler::GetUILanguageList(
292 const input_method::InputMethodDescriptors& descriptors) { 350 const input_method::InputMethodDescriptors& descriptors) {
293 // Collect the language codes from the available locales. 351 // Collect the language codes from the available locales.
294 return GetLanguageListInternal(descriptors, l10n_util::GetAvailableLocales()); 352 return GetLanguageListInternal(
353 descriptors, l10n_util::GetAvailableLocales(), true);
295 } 354 }
296 355
297 base::ListValue* 356 base::ListValue*
298 CrosLanguageOptionsHandler::ConvertInputMethodDescriptosToIMEList( 357 CrosLanguageOptionsHandler::ConvertInputMethodDescriptosToIMEList(
299 const input_method::InputMethodDescriptors& descriptors) { 358 const input_method::InputMethodDescriptors& descriptors) {
300 scoped_ptr<base::ListValue> ime_ids_list(new base::ListValue()); 359 scoped_ptr<base::ListValue> ime_ids_list(new base::ListValue());
301 for (size_t i = 0; i < descriptors.size(); ++i) { 360 for (size_t i = 0; i < descriptors.size(); ++i) {
302 const input_method::InputMethodDescriptor& descriptor = descriptors[i]; 361 const input_method::InputMethodDescriptor& descriptor = descriptors[i];
303 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue()); 362 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue());
304 dictionary->SetString("id", descriptor.id()); 363 dictionary->SetString("id", descriptor.id());
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 ConvertInputMethodDescriptosToIMEList( 475 ConvertInputMethodDescriptosToIMEList(
417 component_extension_manager->GetAllIMEAsInputMethodDescriptor())); 476 component_extension_manager->GetAllIMEAsInputMethodDescriptor()));
418 web_ui()->CallJavascriptFunction( 477 web_ui()->CallJavascriptFunction(
419 "options.LanguageOptions.onComponentManagerInitialized", 478 "options.LanguageOptions.onComponentManagerInitialized",
420 *ime_list); 479 *ime_list);
421 composition_extension_appended_ = true; 480 composition_extension_appended_ = true;
422 } 481 }
423 482
424 } // namespace options 483 } // namespace options
425 } // namespace chromeos 484 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698