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