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