OLD | NEW |
| (Empty) |
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/font_settings_handler.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/basictypes.h" | |
10 #include "base/bind.h" | |
11 #include "base/bind_helpers.h" | |
12 #include "base/i18n/rtl.h" | |
13 #include "base/string_number_conversions.h" | |
14 #include "base/string_util.h" | |
15 #include "base/values.h" | |
16 #include "chrome/browser/browser_process.h" | |
17 #include "chrome/browser/character_encoding.h" | |
18 #include "chrome/browser/prefs/pref_service.h" | |
19 #include "chrome/browser/profiles/profile.h" | |
20 #include "chrome/browser/ui/webui/options/font_settings_utils.h" | |
21 #include "chrome/common/chrome_notification_types.h" | |
22 #include "chrome/common/pref_names.h" | |
23 #include "content/public/browser/font_list_async.h" | |
24 #include "content/public/browser/notification_details.h" | |
25 #include "content/public/browser/web_ui.h" | |
26 #include "grit/chromium_strings.h" | |
27 #include "grit/generated_resources.h" | |
28 #include "ui/base/l10n/l10n_util.h" | |
29 | |
30 FontSettingsHandler::FontSettingsHandler() { | |
31 } | |
32 | |
33 FontSettingsHandler::~FontSettingsHandler() { | |
34 } | |
35 | |
36 void FontSettingsHandler::GetLocalizedValues( | |
37 DictionaryValue* localized_strings) { | |
38 DCHECK(localized_strings); | |
39 | |
40 static OptionsStringResource resources[] = { | |
41 { "fontSettingsStandard", | |
42 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_STANDARD_LABEL }, | |
43 { "fontSettingsSerif", | |
44 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_SERIF_LABEL }, | |
45 { "fontSettingsSansSerif", | |
46 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_SANS_SERIF_LABEL }, | |
47 { "fontSettingsFixedWidth", | |
48 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_FIXED_WIDTH_LABEL }, | |
49 { "fontSettingsMinimumSize", | |
50 IDS_FONT_LANGUAGE_SETTING_MINIMUM_FONT_SIZE_TITLE }, | |
51 { "fontSettingsEncoding", | |
52 IDS_FONT_LANGUAGE_SETTING_FONT_SUB_DIALOG_ENCODING_TITLE }, | |
53 { "fontSettingsSizeTiny", | |
54 IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_TINY }, | |
55 { "fontSettingsSizeHuge", | |
56 IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_HUGE }, | |
57 { "fontSettingsLoremIpsum", | |
58 IDS_FONT_LANGUAGE_SETTING_LOREM_IPSUM }, | |
59 }; | |
60 | |
61 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
62 RegisterTitle(localized_strings, "fontSettingsPage", | |
63 IDS_FONT_LANGUAGE_SETTING_FONT_TAB_TITLE); | |
64 localized_strings->SetString("fontSettingsPlaceholder", | |
65 l10n_util::GetStringUTF16( | |
66 IDS_FONT_LANGUAGE_SETTING_PLACEHOLDER)); | |
67 } | |
68 | |
69 void FontSettingsHandler::InitializeHandler() { | |
70 DCHECK(web_ui()); | |
71 SetUpStandardFontSample(); | |
72 SetUpSerifFontSample(); | |
73 SetUpSansSerifFontSample(); | |
74 SetUpFixedFontSample(); | |
75 SetUpMinimumFontSample(); | |
76 } | |
77 | |
78 void FontSettingsHandler::RegisterMessages() { | |
79 // Perform validation for saved fonts. | |
80 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); | |
81 FontSettingsUtilities::ValidateSavedFonts(pref_service); | |
82 | |
83 // Register for preferences that we need to observe manually. | |
84 standard_font_.Init(prefs::kWebKitGlobalStandardFontFamily, | |
85 pref_service, this); | |
86 serif_font_.Init(prefs::kWebKitGlobalSerifFontFamily, pref_service, this); | |
87 sans_serif_font_.Init(prefs::kWebKitGlobalSansSerifFontFamily, | |
88 pref_service, this); | |
89 fixed_font_.Init(prefs::kWebKitGlobalFixedFontFamily, pref_service, this); | |
90 font_encoding_.Init(prefs::kGlobalDefaultCharset, pref_service, this); | |
91 default_font_size_.Init(prefs::kWebKitGlobalDefaultFontSize, | |
92 pref_service, this); | |
93 default_fixed_font_size_.Init(prefs::kWebKitGlobalDefaultFixedFontSize, | |
94 pref_service, this); | |
95 minimum_font_size_.Init(prefs::kWebKitGlobalMinimumFontSize, | |
96 pref_service, this); | |
97 | |
98 web_ui()->RegisterMessageCallback("fetchFontsData", | |
99 base::Bind(&FontSettingsHandler::HandleFetchFontsData, | |
100 base::Unretained(this))); | |
101 } | |
102 | |
103 void FontSettingsHandler::HandleFetchFontsData(const ListValue* args) { | |
104 content::GetFontListAsync( | |
105 base::Bind(&FontSettingsHandler::FontsListHasLoaded, | |
106 base::Unretained(this))); | |
107 } | |
108 | |
109 void FontSettingsHandler::FontsListHasLoaded( | |
110 scoped_ptr<base::ListValue> list) { | |
111 ListValue encoding_list; | |
112 const std::vector<CharacterEncoding::EncodingInfo>* encodings; | |
113 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); | |
114 encodings = CharacterEncoding::GetCurrentDisplayEncodings( | |
115 g_browser_process->GetApplicationLocale(), | |
116 pref_service->GetString(prefs::kStaticEncodings), | |
117 pref_service->GetString(prefs::kRecentlySelectedEncoding)); | |
118 DCHECK(encodings); | |
119 DCHECK(!encodings->empty()); | |
120 | |
121 std::vector<CharacterEncoding::EncodingInfo>::const_iterator it; | |
122 for (it = encodings->begin(); it != encodings->end(); ++it) { | |
123 ListValue* option = new ListValue(); | |
124 if (it->encoding_id) { | |
125 int cmd_id = it->encoding_id; | |
126 std::string encoding = | |
127 CharacterEncoding::GetCanonicalEncodingNameByCommandId(cmd_id); | |
128 string16 name = it->encoding_display_name; | |
129 base::i18n::AdjustStringForLocaleDirection(&name); | |
130 option->Append(Value::CreateStringValue(encoding)); | |
131 option->Append(Value::CreateStringValue(name)); | |
132 } else { | |
133 // Add empty name/value to indicate a separator item. | |
134 option->Append(Value::CreateStringValue("")); | |
135 option->Append(Value::CreateStringValue("")); | |
136 } | |
137 encoding_list.Append(option); | |
138 } | |
139 | |
140 ListValue selected_values; | |
141 selected_values.Append(Value::CreateStringValue(standard_font_.GetValue())); | |
142 selected_values.Append(Value::CreateStringValue(serif_font_.GetValue())); | |
143 selected_values.Append(Value::CreateStringValue(sans_serif_font_.GetValue())); | |
144 selected_values.Append(Value::CreateStringValue(fixed_font_.GetValue())); | |
145 selected_values.Append(Value::CreateStringValue(font_encoding_.GetValue())); | |
146 | |
147 web_ui()->CallJavascriptFunction("FontSettings.setFontsData", | |
148 *list.get(), encoding_list, | |
149 selected_values); | |
150 } | |
151 | |
152 void FontSettingsHandler::Observe(int type, | |
153 const content::NotificationSource& source, | |
154 const content::NotificationDetails& details) { | |
155 if (type == chrome::NOTIFICATION_PREF_CHANGED) { | |
156 std::string* pref_name = content::Details<std::string>(details).ptr(); | |
157 if (*pref_name == prefs::kWebKitGlobalStandardFontFamily) { | |
158 SetUpStandardFontSample(); | |
159 } else if (*pref_name == prefs::kWebKitGlobalSerifFontFamily) { | |
160 SetUpSerifFontSample(); | |
161 } else if (*pref_name == prefs::kWebKitGlobalSansSerifFontFamily) { | |
162 SetUpSansSerifFontSample(); | |
163 } else if (*pref_name == prefs::kWebKitGlobalFixedFontFamily || | |
164 *pref_name == prefs::kWebKitGlobalDefaultFixedFontSize) { | |
165 SetUpFixedFontSample(); | |
166 } else if (*pref_name == prefs::kWebKitGlobalDefaultFontSize) { | |
167 SetUpStandardFontSample(); | |
168 SetUpSerifFontSample(); | |
169 SetUpSansSerifFontSample(); | |
170 } else if (*pref_name == prefs::kWebKitGlobalMinimumFontSize) { | |
171 SetUpMinimumFontSample(); | |
172 } | |
173 } | |
174 } | |
175 | |
176 void FontSettingsHandler::SetUpStandardFontSample() { | |
177 base::StringValue font_value(standard_font_.GetValue()); | |
178 base::FundamentalValue size_value(default_font_size_.GetValue()); | |
179 web_ui()->CallJavascriptFunction( | |
180 "FontSettings.setUpStandardFontSample", font_value, size_value); | |
181 } | |
182 | |
183 void FontSettingsHandler::SetUpSerifFontSample() { | |
184 base::StringValue font_value(serif_font_.GetValue()); | |
185 base::FundamentalValue size_value(default_font_size_.GetValue()); | |
186 web_ui()->CallJavascriptFunction( | |
187 "FontSettings.setUpSerifFontSample", font_value, size_value); | |
188 } | |
189 | |
190 void FontSettingsHandler::SetUpSansSerifFontSample() { | |
191 base::StringValue font_value(sans_serif_font_.GetValue()); | |
192 base::FundamentalValue size_value(default_font_size_.GetValue()); | |
193 web_ui()->CallJavascriptFunction( | |
194 "FontSettings.setUpSansSerifFontSample", font_value, size_value); | |
195 } | |
196 | |
197 void FontSettingsHandler::SetUpFixedFontSample() { | |
198 base::StringValue font_value(fixed_font_.GetValue()); | |
199 base::FundamentalValue size_value(default_fixed_font_size_.GetValue()); | |
200 web_ui()->CallJavascriptFunction( | |
201 "FontSettings.setUpFixedFontSample", font_value, size_value); | |
202 } | |
203 | |
204 void FontSettingsHandler::SetUpMinimumFontSample() { | |
205 base::FundamentalValue size_value(minimum_font_size_.GetValue()); | |
206 web_ui()->CallJavascriptFunction("FontSettings.setUpMinimumFontSample", | |
207 size_value); | |
208 } | |
OLD | NEW |