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/language_options_handler.h" | 5 #include "chrome/browser/ui/webui/options/language_options_handler.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <memory> | 10 #include <memory> |
11 #include <string> | 11 #include <string> |
12 #include <utility> | 12 #include <utility> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/bind.h" | 15 #include "base/bind.h" |
16 #include "base/command_line.h" | 16 #include "base/command_line.h" |
17 #include "base/i18n/rtl.h" | 17 #include "base/i18n/rtl.h" |
| 18 #include "base/memory/ptr_util.h" |
18 #include "base/metrics/user_metrics.h" | 19 #include "base/metrics/user_metrics.h" |
19 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
20 #include "base/values.h" | 21 #include "base/values.h" |
21 #include "chrome/browser/browser_process.h" | 22 #include "chrome/browser/browser_process.h" |
22 #include "chrome/browser/lifetime/application_lifetime.h" | 23 #include "chrome/browser/lifetime/application_lifetime.h" |
23 #include "chrome/common/chrome_switches.h" | 24 #include "chrome/common/chrome_switches.h" |
24 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
25 #include "chrome/grit/generated_resources.h" | 26 #include "chrome/grit/generated_resources.h" |
26 #include "components/prefs/pref_service.h" | 27 #include "components/prefs/pref_service.h" |
27 #include "content/public/browser/web_ui.h" | 28 #include "content/public/browser/web_ui.h" |
(...skipping 22 matching lines...) Expand all Loading... |
50 } | 51 } |
51 | 52 |
52 void LanguageOptionsHandler::RegisterMessages() { | 53 void LanguageOptionsHandler::RegisterMessages() { |
53 LanguageOptionsHandlerCommon::RegisterMessages(); | 54 LanguageOptionsHandlerCommon::RegisterMessages(); |
54 | 55 |
55 web_ui()->RegisterMessageCallback("uiLanguageRestart", | 56 web_ui()->RegisterMessageCallback("uiLanguageRestart", |
56 base::Bind(&LanguageOptionsHandler::RestartCallback, | 57 base::Bind(&LanguageOptionsHandler::RestartCallback, |
57 base::Unretained(this))); | 58 base::Unretained(this))); |
58 } | 59 } |
59 | 60 |
60 base::ListValue* LanguageOptionsHandler::GetLanguageList() { | 61 std::unique_ptr<base::ListValue> LanguageOptionsHandler::GetLanguageList() { |
61 // Collect the language codes from the supported accept-languages. | 62 // Collect the language codes from the supported accept-languages. |
62 const std::string app_locale = g_browser_process->GetApplicationLocale(); | 63 const std::string app_locale = g_browser_process->GetApplicationLocale(); |
63 std::vector<std::string> language_codes; | 64 std::vector<std::string> language_codes; |
64 l10n_util::GetAcceptLanguagesForLocale(app_locale, &language_codes); | 65 l10n_util::GetAcceptLanguagesForLocale(app_locale, &language_codes); |
65 | 66 |
66 // Map of display name -> {language code, native_display_name}. | 67 // Map of display name -> {language code, native_display_name}. |
67 // In theory, we should be able to create a map that is sorted by | 68 // In theory, we should be able to create a map that is sorted by |
68 // display names using ICU comparator, but doing it is hard, thus we'll | 69 // display names using ICU comparator, but doing it is hard, thus we'll |
69 // use an auxiliary vector to achieve the same result. | 70 // use an auxiliary vector to achieve the same result. |
70 typedef std::pair<std::string, base::string16> LanguagePair; | 71 typedef std::pair<std::string, base::string16> LanguagePair; |
(...skipping 13 matching lines...) Expand all Loading... |
84 display_names.push_back(display_name); | 85 display_names.push_back(display_name); |
85 language_map[display_name] = | 86 language_map[display_name] = |
86 std::make_pair(language_codes[i], native_display_name); | 87 std::make_pair(language_codes[i], native_display_name); |
87 } | 88 } |
88 DCHECK_EQ(display_names.size(), language_map.size()); | 89 DCHECK_EQ(display_names.size(), language_map.size()); |
89 | 90 |
90 // Sort display names using locale specific sorter. | 91 // Sort display names using locale specific sorter. |
91 l10n_util::SortStrings16(app_locale, &display_names); | 92 l10n_util::SortStrings16(app_locale, &display_names); |
92 | 93 |
93 // Build the language list from the language map. | 94 // Build the language list from the language map. |
94 base::ListValue* language_list = new base::ListValue(); | 95 auto language_list = base::MakeUnique<base::ListValue>(); |
95 for (size_t i = 0; i < display_names.size(); ++i) { | 96 for (size_t i = 0; i < display_names.size(); ++i) { |
96 base::string16& display_name = display_names[i]; | 97 base::string16& display_name = display_names[i]; |
97 base::string16 adjusted_display_name(display_name); | 98 base::string16 adjusted_display_name(display_name); |
98 base::i18n::AdjustStringForLocaleDirection(&adjusted_display_name); | 99 base::i18n::AdjustStringForLocaleDirection(&adjusted_display_name); |
99 | 100 |
100 const LanguagePair& pair = language_map[display_name]; | 101 const LanguagePair& pair = language_map[display_name]; |
101 base::string16 adjusted_native_display_name(pair.second); | 102 base::string16 adjusted_native_display_name(pair.second); |
102 base::i18n::AdjustStringForLocaleDirection(&adjusted_native_display_name); | 103 base::i18n::AdjustStringForLocaleDirection(&adjusted_native_display_name); |
103 | 104 |
104 bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(display_name); | 105 bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(display_name); |
(...skipping 16 matching lines...) Expand all Loading... |
121 PrefService* pref_service = g_browser_process->local_state(); | 122 PrefService* pref_service = g_browser_process->local_state(); |
122 pref_service->SetString(prefs::kApplicationLocale, language_code); | 123 pref_service->SetString(prefs::kApplicationLocale, language_code); |
123 } | 124 } |
124 | 125 |
125 void LanguageOptionsHandler::RestartCallback(const base::ListValue* args) { | 126 void LanguageOptionsHandler::RestartCallback(const base::ListValue* args) { |
126 base::RecordAction(UserMetricsAction("LanguageOptions_Restart")); | 127 base::RecordAction(UserMetricsAction("LanguageOptions_Restart")); |
127 chrome::AttemptRestart(); | 128 chrome::AttemptRestart(); |
128 } | 129 } |
129 | 130 |
130 } // namespace options | 131 } // namespace options |
OLD | NEW |