1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/options/font_settings_handler.h"
#include <string>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/i18n/rtl.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/character_encoding.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/webui/options/font_settings_utils.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h"
#include "content/public/browser/font_list_async.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/web_ui.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_urls.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"
#if defined(OS_WIN)
#include "ui/gfx/font.h"
#include "ui/gfx/platform_font_win.h"
#endif
namespace {
// Returns the localized name of a font so that settings can find it within the
// list of system fonts. On Windows, the list of system fonts has names only
// for the system locale, but the pref value may be in the English name.
std::string MaybeGetLocalizedFontName(const std::string& font_name) {
#if defined(OS_WIN)
gfx::Font font(font_name, 12); // dummy font size
return static_cast<gfx::PlatformFontWin*>(font.platform_font())->
GetLocalizedFontName();
#else
return font_name;
#endif
}
const char kAdvancedFontSettingsExtensionId[] =
"caclkomlalccbpcdllchkeecicepbmbm";
} // namespace
namespace options {
FontSettingsHandler::FontSettingsHandler()
: extension_registry_observer_(this) {
}
FontSettingsHandler::~FontSettingsHandler() {
}
void FontSettingsHandler::GetLocalizedValues(
base::DictionaryValue* localized_strings) {
DCHECK(localized_strings);
static OptionsStringResource resources[] = {
{ "fontSettingsStandard",
IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_STANDARD_LABEL },
{ "fontSettingsSerif",
IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_SERIF_LABEL },
{ "fontSettingsSansSerif",
IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_SANS_SERIF_LABEL },
{ "fontSettingsFixedWidth",
IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_FIXED_WIDTH_LABEL },
{ "fontSettingsMinimumSize",
IDS_FONT_LANGUAGE_SETTING_MINIMUM_FONT_SIZE_TITLE },
{ "fontSettingsEncoding",
IDS_FONT_LANGUAGE_SETTING_FONT_SUB_DIALOG_ENCODING_TITLE },
{ "fontSettingsSizeTiny",
IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_TINY },
{ "fontSettingsSizeHuge",
IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_HUGE },
{ "fontSettingsLoremIpsum",
IDS_FONT_LANGUAGE_SETTING_LOREM_IPSUM },
{ "advancedFontSettingsOptions",
IDS_FONT_LANGUAGE_SETTING_ADVANCED_FONT_SETTINGS_OPTIONS }
};
RegisterStrings(localized_strings, resources, arraysize(resources));
RegisterTitle(localized_strings, "fontSettingsPage",
IDS_FONT_LANGUAGE_SETTING_FONT_TAB_TITLE);
localized_strings->SetString("fontSettingsPlaceholder",
l10n_util::GetStringUTF16(
IDS_FONT_LANGUAGE_SETTING_PLACEHOLDER));
GURL install_url(extension_urls::GetWebstoreItemDetailURLPrefix());
localized_strings->SetString("advancedFontSettingsInstall",
l10n_util::GetStringFUTF16(
IDS_FONT_LANGUAGE_SETTING_ADVANCED_FONT_SETTINGS_INSTALL,
base::UTF8ToUTF16(
install_url.Resolve(kAdvancedFontSettingsExtensionId).spec())));
}
void FontSettingsHandler::InitializeHandler() {
Profile* profile = Profile::FromWebUI(web_ui());
extension_registry_observer_.Add(extensions::ExtensionRegistry::Get(profile));
}
void FontSettingsHandler::InitializePage() {
DCHECK(web_ui());
SetUpStandardFontSample();
SetUpSerifFontSample();
SetUpSansSerifFontSample();
SetUpFixedFontSample();
SetUpMinimumFontSample();
NotifyAdvancedFontSettingsAvailability();
}
void FontSettingsHandler::RegisterMessages() {
// Perform validation for saved fonts.
PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs();
FontSettingsUtilities::ValidateSavedFonts(pref_service);
// Register for preferences that we need to observe manually.
font_encoding_.Init(prefs::kDefaultCharset, pref_service);
standard_font_.Init(prefs::kWebKitStandardFontFamily,
pref_service,
base::Bind(&FontSettingsHandler::SetUpStandardFontSample,
base::Unretained(this)));
serif_font_.Init(prefs::kWebKitSerifFontFamily,
pref_service,
base::Bind(&FontSettingsHandler::SetUpSerifFontSample,
base::Unretained(this)));
sans_serif_font_.Init(
prefs::kWebKitSansSerifFontFamily,
pref_service,
base::Bind(&FontSettingsHandler::SetUpSansSerifFontSample,
base::Unretained(this)));
base::Closure callback = base::Bind(
&FontSettingsHandler::SetUpFixedFontSample, base::Unretained(this));
fixed_font_.Init(prefs::kWebKitFixedFontFamily, pref_service, callback);
default_fixed_font_size_.Init(prefs::kWebKitDefaultFixedFontSize,
pref_service, callback);
default_font_size_.Init(
prefs::kWebKitDefaultFontSize,
pref_service,
base::Bind(&FontSettingsHandler::OnWebKitDefaultFontSizeChanged,
base::Unretained(this)));
minimum_font_size_.Init(
prefs::kWebKitMinimumFontSize,
pref_service,
base::Bind(&FontSettingsHandler::SetUpMinimumFontSample,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("fetchFontsData",
base::Bind(&FontSettingsHandler::HandleFetchFontsData,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("openAdvancedFontSettingsOptions",
base::Bind(&FontSettingsHandler::HandleOpenAdvancedFontSettingsOptions,
base::Unretained(this)));
}
void FontSettingsHandler::OnExtensionLoaded(
content::BrowserContext* browser_context,
const extensions::Extension* extension) {
NotifyAdvancedFontSettingsAvailability();
}
void FontSettingsHandler::OnExtensionUnloaded(
content::BrowserContext* browser_context,
const extensions::Extension* extension,
extensions::UnloadedExtensionInfo::Reason reason) {
NotifyAdvancedFontSettingsAvailability();
}
void FontSettingsHandler::HandleFetchFontsData(const base::ListValue* args) {
content::GetFontListAsync(
base::Bind(&FontSettingsHandler::FontsListHasLoaded,
base::Unretained(this)));
}
void FontSettingsHandler::FontsListHasLoaded(
scoped_ptr<base::ListValue> list) {
// Selects the directionality for the fonts in the given list.
for (size_t i = 0; i < list->GetSize(); i++) {
base::ListValue* font;
bool has_font = list->GetList(i, &font);
DCHECK(has_font);
base::string16 value;
bool has_value = font->GetString(1, &value);
DCHECK(has_value);
bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(value);
font->Append(new base::StringValue(has_rtl_chars ? "rtl" : "ltr"));
}
base::ListValue encoding_list;
const std::vector<CharacterEncoding::EncodingInfo>* encodings;
PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs();
encodings = CharacterEncoding::GetCurrentDisplayEncodings(
g_browser_process->GetApplicationLocale(),
pref_service->GetString(prefs::kStaticEncodings),
pref_service->GetString(prefs::kRecentlySelectedEncoding));
DCHECK(encodings);
DCHECK(!encodings->empty());
std::vector<CharacterEncoding::EncodingInfo>::const_iterator it;
for (it = encodings->begin(); it != encodings->end(); ++it) {
base::ListValue* option = new base::ListValue();
if (it->encoding_id) {
int cmd_id = it->encoding_id;
std::string encoding =
CharacterEncoding::GetCanonicalEncodingNameByCommandId(cmd_id);
base::string16 name = it->encoding_display_name;
bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(name);
option->Append(new base::StringValue(encoding));
option->Append(new base::StringValue(name));
option->Append(new base::StringValue(has_rtl_chars ? "rtl" : "ltr"));
} else {
// Add empty name/value to indicate a separator item.
option->Append(new base::StringValue(std::string()));
option->Append(new base::StringValue(std::string()));
}
encoding_list.Append(option);
}
base::ListValue selected_values;
selected_values.Append(new base::StringValue(MaybeGetLocalizedFontName(
standard_font_.GetValue())));
selected_values.Append(new base::StringValue(MaybeGetLocalizedFontName(
serif_font_.GetValue())));
selected_values.Append(new base::StringValue(MaybeGetLocalizedFontName(
sans_serif_font_.GetValue())));
selected_values.Append(new base::StringValue(MaybeGetLocalizedFontName(
fixed_font_.GetValue())));
selected_values.Append(new base::StringValue(font_encoding_.GetValue()));
web_ui()->CallJavascriptFunction("FontSettings.setFontsData",
*list.get(), encoding_list,
selected_values);
}
void FontSettingsHandler::SetUpStandardFontSample() {
base::StringValue font_value(standard_font_.GetValue());
base::FundamentalValue size_value(default_font_size_.GetValue());
web_ui()->CallJavascriptFunction(
"FontSettings.setUpStandardFontSample", font_value, size_value);
}
void FontSettingsHandler::SetUpSerifFontSample() {
base::StringValue font_value(serif_font_.GetValue());
base::FundamentalValue size_value(default_font_size_.GetValue());
web_ui()->CallJavascriptFunction(
"FontSettings.setUpSerifFontSample", font_value, size_value);
}
void FontSettingsHandler::SetUpSansSerifFontSample() {
base::StringValue font_value(sans_serif_font_.GetValue());
base::FundamentalValue size_value(default_font_size_.GetValue());
web_ui()->CallJavascriptFunction(
"FontSettings.setUpSansSerifFontSample", font_value, size_value);
}
void FontSettingsHandler::SetUpFixedFontSample() {
base::StringValue font_value(fixed_font_.GetValue());
base::FundamentalValue size_value(default_fixed_font_size_.GetValue());
web_ui()->CallJavascriptFunction(
"FontSettings.setUpFixedFontSample", font_value, size_value);
}
void FontSettingsHandler::SetUpMinimumFontSample() {
base::FundamentalValue size_value(minimum_font_size_.GetValue());
web_ui()->CallJavascriptFunction("FontSettings.setUpMinimumFontSample",
size_value);
}
const extensions::Extension*
FontSettingsHandler::GetAdvancedFontSettingsExtension() {
Profile* profile = Profile::FromWebUI(web_ui());
ExtensionService* service =
extensions::ExtensionSystem::Get(profile)->extension_service();
if (!service->IsExtensionEnabled(kAdvancedFontSettingsExtensionId))
return NULL;
return service->GetInstalledExtension(kAdvancedFontSettingsExtensionId);
}
void FontSettingsHandler::NotifyAdvancedFontSettingsAvailability() {
web_ui()->CallJavascriptFunction(
"FontSettings.notifyAdvancedFontSettingsAvailability",
base::FundamentalValue(GetAdvancedFontSettingsExtension() != NULL));
}
void FontSettingsHandler::HandleOpenAdvancedFontSettingsOptions(
const base::ListValue* args) {
const extensions::Extension* extension = GetAdvancedFontSettingsExtension();
if (!extension)
return;
extensions::ExtensionTabUtil::OpenOptionsPage(extension,
chrome::FindBrowserWithWebContents(web_ui()->GetWebContents()));
}
void FontSettingsHandler::OnWebKitDefaultFontSizeChanged() {
SetUpStandardFontSample();
SetUpSerifFontSample();
SetUpSansSerifFontSample();
}
} // namespace options
|