- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / options / language_options_handler.cc
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/webui/options/language_options_handler.h"
6
7 #include <map>
8 #include <string>
9 #include <utility>
10 #include <vector>
11
12 #include "base/basictypes.h"
13 #include "base/bind.h"
14 #include "base/command_line.h"
15 #include "base/i18n/rtl.h"
16 #include "base/prefs/pref_service.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/values.h"
19 #include "chrome/browser/browser_process.h"
20 #include "chrome/browser/lifetime/application_lifetime.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/pref_names.h"
23 #include "content/public/browser/user_metrics.h"
24 #include "content/public/browser/web_ui.h"
25 #include "grit/chromium_strings.h"
26 #include "grit/generated_resources.h"
27 #include "ui/base/l10n/l10n_util.h"
28
29 using content::UserMetricsAction;
30
31 namespace options {
32
33 LanguageOptionsHandler::LanguageOptionsHandler() {
34 }
35
36 LanguageOptionsHandler::~LanguageOptionsHandler() {
37 }
38
39 void LanguageOptionsHandler::GetLocalizedValues(
40     DictionaryValue* localized_strings) {
41   LanguageOptionsHandlerCommon::GetLocalizedValues(localized_strings);
42
43   RegisterTitle(localized_strings, "languagePage",
44                 IDS_OPTIONS_SETTINGS_LANGUAGES_DIALOG_TITLE);
45   localized_strings->SetString("restart_button",
46       l10n_util::GetStringUTF16(
47           IDS_OPTIONS_SETTINGS_LANGUAGES_RELAUNCH_BUTTON));
48   localized_strings->Set("languageList", GetLanguageList());
49 }
50
51 void LanguageOptionsHandler::RegisterMessages() {
52   LanguageOptionsHandlerCommon::RegisterMessages();
53
54   web_ui()->RegisterMessageCallback("uiLanguageRestart",
55       base::Bind(&LanguageOptionsHandler::RestartCallback,
56                  base::Unretained(this)));
57 }
58
59 ListValue* LanguageOptionsHandler::GetLanguageList() {
60   // Collect the language codes from the supported accept-languages.
61   const std::string app_locale = g_browser_process->GetApplicationLocale();
62   std::vector<std::string> language_codes;
63   l10n_util::GetAcceptLanguagesForLocale(app_locale, &language_codes);
64
65   // Map of display name -> {language code, native_display_name}.
66   // In theory, we should be able to create a map that is sorted by
67   // display names using ICU comparator, but doing it is hard, thus we'll
68   // use an auxiliary vector to achieve the same result.
69   typedef std::pair<std::string, string16> LanguagePair;
70   typedef std::map<string16, LanguagePair> LanguageMap;
71   LanguageMap language_map;
72   // The auxiliary vector mentioned above.
73   std::vector<string16> display_names;
74
75   // Build the list of display names, and build the language map.
76   for (size_t i = 0; i < language_codes.size(); ++i) {
77     string16 display_name =
78         l10n_util::GetDisplayNameForLocale(language_codes[i], app_locale,
79                                            false);
80     string16 native_display_name =
81         l10n_util::GetDisplayNameForLocale(language_codes[i], language_codes[i],
82                                            false);
83     display_names.push_back(display_name);
84     language_map[display_name] =
85         std::make_pair(language_codes[i], native_display_name);
86   }
87   DCHECK_EQ(display_names.size(), language_map.size());
88
89   // Sort display names using locale specific sorter.
90   l10n_util::SortStrings16(app_locale, &display_names);
91
92   // Build the language list from the language map.
93   ListValue* language_list = new ListValue();
94   for (size_t i = 0; i < display_names.size(); ++i) {
95     string16& display_name = display_names[i];
96     string16 adjusted_display_name(display_name);
97     base::i18n::AdjustStringForLocaleDirection(&adjusted_display_name);
98
99     const LanguagePair& pair = language_map[display_name];
100     string16 adjusted_native_display_name(pair.second);
101     base::i18n::AdjustStringForLocaleDirection(&adjusted_native_display_name);
102
103     bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(display_name);
104     std::string directionality = has_rtl_chars ? "rtl" : "ltr";
105
106     DictionaryValue* dictionary = new DictionaryValue();
107     dictionary->SetString("code",  pair.first);
108     dictionary->SetString("displayName", adjusted_display_name);
109     dictionary->SetString("textDirection", directionality);
110     dictionary->SetString("nativeDisplayName", adjusted_native_display_name);
111     language_list->Append(dictionary);
112   }
113
114   return language_list;
115 }
116
117 string16 LanguageOptionsHandler::GetProductName() {
118   return l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
119 }
120
121 void LanguageOptionsHandler::SetApplicationLocale(
122     const std::string& language_code) {
123   PrefService* pref_service = g_browser_process->local_state();
124   pref_service->SetString(prefs::kApplicationLocale, language_code);
125 }
126
127 void LanguageOptionsHandler::RestartCallback(const ListValue* args) {
128   content::RecordAction(UserMetricsAction("LanguageOptions_Restart"));
129   chrome::AttemptRestart();
130 }
131
132 }  // namespace options