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