15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/webui/options/font_settings_handler.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind_helpers.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/i18n/rtl.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_util.h"
161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/browser_process.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/character_encoding.h"
201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/extensions/extension_service.h"
211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/extensions/extension_tab_util.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/ui/browser_finder.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/webui/options/font_settings_utils.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/grit/generated_resources.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/font_list_async.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_details.h"
291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/public/browser/notification_service.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_ui.h"
31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "extensions/browser/extension_registry.h"
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/browser/extension_system.h"
33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/extension.h"
341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/common/extension_urls.h"
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "url/gurl.h"
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/font.h"
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/platform_font_win.h"
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns the localized name of a font so that settings can find it within the
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// list of system fonts. On Windows, the list of system fonts has names only
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// for the system locale, but the pref value may be in the English name.
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string MaybeGetLocalizedFontName(const std::string& font_name) {
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Font font(font_name, 12);  // dummy font size
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return static_cast<gfx::PlatformFontWin*>(font.platform_font())->
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      GetLocalizedFontName();
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return font_name;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const char kAdvancedFontSettingsExtensionId[] =
591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    "caclkomlalccbpcdllchkeecicepbmbm";
601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace options {
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
66116680a4aac90f2aa7413d9095a592090648e557Ben MurdochFontSettingsHandler::FontSettingsHandler()
67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    : extension_registry_observer_(this) {
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)FontSettingsHandler::~FontSettingsHandler() {
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FontSettingsHandler::GetLocalizedValues(
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::DictionaryValue* localized_strings) {
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(localized_strings);
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static OptionsStringResource resources[] = {
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { "fontSettingsStandard",
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_STANDARD_LABEL },
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { "fontSettingsSerif",
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_SERIF_LABEL },
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { "fontSettingsSansSerif",
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_SANS_SERIF_LABEL },
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { "fontSettingsFixedWidth",
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_FIXED_WIDTH_LABEL },
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { "fontSettingsMinimumSize",
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      IDS_FONT_LANGUAGE_SETTING_MINIMUM_FONT_SIZE_TITLE },
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { "fontSettingsEncoding",
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      IDS_FONT_LANGUAGE_SETTING_FONT_SUB_DIALOG_ENCODING_TITLE },
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { "fontSettingsSizeTiny",
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_TINY },
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { "fontSettingsSizeHuge",
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_HUGE },
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { "fontSettingsLoremIpsum",
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      IDS_FONT_LANGUAGE_SETTING_LOREM_IPSUM },
961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    { "advancedFontSettingsOptions",
971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      IDS_FONT_LANGUAGE_SETTING_ADVANCED_FONT_SETTINGS_OPTIONS }
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RegisterStrings(localized_strings, resources, arraysize(resources));
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RegisterTitle(localized_strings, "fontSettingsPage",
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                IDS_FONT_LANGUAGE_SETTING_FONT_TAB_TITLE);
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  localized_strings->SetString("fontSettingsPlaceholder",
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      l10n_util::GetStringUTF16(
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          IDS_FONT_LANGUAGE_SETTING_PLACEHOLDER));
1061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  GURL install_url(extension_urls::GetWebstoreItemDetailURLPrefix());
1081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  localized_strings->SetString("advancedFontSettingsInstall",
1091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      l10n_util::GetStringFUTF16(
1101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          IDS_FONT_LANGUAGE_SETTING_ADVANCED_FONT_SETTINGS_INSTALL,
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          base::UTF8ToUTF16(
1121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)              install_url.Resolve(kAdvancedFontSettingsExtensionId).spec())));
1131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void FontSettingsHandler::InitializeHandler() {
116116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  Profile* profile = Profile::FromWebUI(web_ui());
117116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  extension_registry_observer_.Add(extensions::ExtensionRegistry::Get(profile));
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FontSettingsHandler::InitializePage() {
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(web_ui());
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetUpStandardFontSample();
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetUpSerifFontSample();
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetUpSansSerifFontSample();
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetUpFixedFontSample();
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetUpMinimumFontSample();
1271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  NotifyAdvancedFontSettingsAvailability();
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FontSettingsHandler::RegisterMessages() {
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Perform validation for saved fonts.
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs();
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FontSettingsUtilities::ValidateSavedFonts(pref_service);
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Register for preferences that we need to observe manually.
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  font_encoding_.Init(prefs::kDefaultCharset, pref_service);
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  standard_font_.Init(prefs::kWebKitStandardFontFamily,
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      pref_service,
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      base::Bind(&FontSettingsHandler::SetUpStandardFontSample,
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 base::Unretained(this)));
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  serif_font_.Init(prefs::kWebKitSerifFontFamily,
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   pref_service,
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   base::Bind(&FontSettingsHandler::SetUpSerifFontSample,
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              base::Unretained(this)));
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  sans_serif_font_.Init(
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      prefs::kWebKitSansSerifFontFamily,
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      pref_service,
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&FontSettingsHandler::SetUpSansSerifFontSample,
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(this)));
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::Closure callback = base::Bind(
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      &FontSettingsHandler::SetUpFixedFontSample, base::Unretained(this));
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  fixed_font_.Init(prefs::kWebKitFixedFontFamily, pref_service, callback);
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  default_fixed_font_size_.Init(prefs::kWebKitDefaultFixedFontSize,
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                pref_service, callback);
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  default_font_size_.Init(
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      prefs::kWebKitDefaultFontSize,
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      pref_service,
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&FontSettingsHandler::OnWebKitDefaultFontSizeChanged,
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(this)));
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  minimum_font_size_.Init(
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      prefs::kWebKitMinimumFontSize,
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      pref_service,
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&FontSettingsHandler::SetUpMinimumFontSample,
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(this)));
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_ui()->RegisterMessageCallback("fetchFontsData",
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(&FontSettingsHandler::HandleFetchFontsData,
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 base::Unretained(this)));
1721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  web_ui()->RegisterMessageCallback("openAdvancedFontSettingsOptions",
1731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::Bind(&FontSettingsHandler::HandleOpenAdvancedFontSettingsOptions,
1741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                 base::Unretained(this)));
1751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
177116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid FontSettingsHandler::OnExtensionLoaded(
178116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    content::BrowserContext* browser_context,
179116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const extensions::Extension* extension) {
180116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  NotifyAdvancedFontSettingsAvailability();
181116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
182116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
183116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid FontSettingsHandler::OnExtensionUnloaded(
184116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    content::BrowserContext* browser_context,
185116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const extensions::Extension* extension,
186116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    extensions::UnloadedExtensionInfo::Reason reason) {
1871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  NotifyAdvancedFontSettingsAvailability();
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void FontSettingsHandler::HandleFetchFontsData(const base::ListValue* args) {
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::GetFontListAsync(
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(&FontSettingsHandler::FontsListHasLoaded,
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 base::Unretained(this)));
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FontSettingsHandler::FontsListHasLoaded(
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<base::ListValue> list) {
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Selects the directionality for the fonts in the given list.
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < list->GetSize(); i++) {
2005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::ListValue* font;
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool has_font = list->GetList(i, &font);
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK(has_font);
203a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    base::string16 value;
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool has_value = font->GetString(1, &value);
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK(has_value);
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(value);
2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    font->Append(new base::StringValue(has_rtl_chars ? "rtl" : "ltr"));
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::ListValue encoding_list;
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::vector<CharacterEncoding::EncodingInfo>* encodings;
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs();
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  encodings = CharacterEncoding::GetCurrentDisplayEncodings(
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      g_browser_process->GetApplicationLocale(),
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pref_service->GetString(prefs::kStaticEncodings),
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pref_service->GetString(prefs::kRecentlySelectedEncoding));
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(encodings);
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!encodings->empty());
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<CharacterEncoding::EncodingInfo>::const_iterator it;
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (it = encodings->begin(); it != encodings->end(); ++it) {
2225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::ListValue* option = new base::ListValue();
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (it->encoding_id) {
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int cmd_id = it->encoding_id;
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::string encoding =
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CharacterEncoding::GetCanonicalEncodingNameByCommandId(cmd_id);
227a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      base::string16 name = it->encoding_display_name;
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(name);
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      option->Append(new base::StringValue(encoding));
2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      option->Append(new base::StringValue(name));
2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      option->Append(new base::StringValue(has_rtl_chars ? "rtl" : "ltr"));
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Add empty name/value to indicate a separator item.
234c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      option->Append(new base::StringValue(std::string()));
235c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      option->Append(new base::StringValue(std::string()));
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    encoding_list.Append(option);
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::ListValue selected_values;
2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  selected_values.Append(new base::StringValue(MaybeGetLocalizedFontName(
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      standard_font_.GetValue())));
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  selected_values.Append(new base::StringValue(MaybeGetLocalizedFontName(
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      serif_font_.GetValue())));
2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  selected_values.Append(new base::StringValue(MaybeGetLocalizedFontName(
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      sans_serif_font_.GetValue())));
2472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  selected_values.Append(new base::StringValue(MaybeGetLocalizedFontName(
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      fixed_font_.GetValue())));
2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  selected_values.Append(new base::StringValue(font_encoding_.GetValue()));
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_ui()->CallJavascriptFunction("FontSettings.setFontsData",
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   *list.get(), encoding_list,
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   selected_values);
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FontSettingsHandler::SetUpStandardFontSample() {
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::StringValue font_value(standard_font_.GetValue());
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::FundamentalValue size_value(default_font_size_.GetValue());
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_ui()->CallJavascriptFunction(
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "FontSettings.setUpStandardFontSample", font_value, size_value);
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FontSettingsHandler::SetUpSerifFontSample() {
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::StringValue font_value(serif_font_.GetValue());
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::FundamentalValue size_value(default_font_size_.GetValue());
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_ui()->CallJavascriptFunction(
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "FontSettings.setUpSerifFontSample", font_value, size_value);
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FontSettingsHandler::SetUpSansSerifFontSample() {
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::StringValue font_value(sans_serif_font_.GetValue());
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::FundamentalValue size_value(default_font_size_.GetValue());
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_ui()->CallJavascriptFunction(
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "FontSettings.setUpSansSerifFontSample", font_value, size_value);
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FontSettingsHandler::SetUpFixedFontSample() {
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::StringValue font_value(fixed_font_.GetValue());
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::FundamentalValue size_value(default_fixed_font_size_.GetValue());
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_ui()->CallJavascriptFunction(
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "FontSettings.setUpFixedFontSample", font_value, size_value);
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FontSettingsHandler::SetUpMinimumFontSample() {
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::FundamentalValue size_value(minimum_font_size_.GetValue());
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_ui()->CallJavascriptFunction("FontSettings.setUpMinimumFontSample",
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   size_value);
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const extensions::Extension*
2911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)FontSettingsHandler::GetAdvancedFontSettingsExtension() {
2921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  Profile* profile = Profile::FromWebUI(web_ui());
2931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ExtensionService* service =
2941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      extensions::ExtensionSystem::Get(profile)->extension_service();
2951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (!service->IsExtensionEnabled(kAdvancedFontSettingsExtensionId))
2961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return NULL;
2971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return service->GetInstalledExtension(kAdvancedFontSettingsExtensionId);
2981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
3001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void FontSettingsHandler::NotifyAdvancedFontSettingsAvailability() {
3011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  web_ui()->CallJavascriptFunction(
3021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      "FontSettings.notifyAdvancedFontSettingsAvailability",
3031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::FundamentalValue(GetAdvancedFontSettingsExtension() != NULL));
3041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
3051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
3061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void FontSettingsHandler::HandleOpenAdvancedFontSettingsOptions(
3071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    const base::ListValue* args) {
3081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  const extensions::Extension* extension = GetAdvancedFontSettingsExtension();
3091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (!extension)
3101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return;
311f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  extensions::ExtensionTabUtil::OpenOptionsPage(extension,
3121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      chrome::FindBrowserWithWebContents(web_ui()->GetWebContents()));
3131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
3141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
3152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FontSettingsHandler::OnWebKitDefaultFontSizeChanged() {
3162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SetUpStandardFontSample();
3172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SetUpSerifFontSample();
3182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SetUpSansSerifFontSample();
3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace options
322