OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/options/font_settings_handler.h" | 5 #include "chrome/browser/ui/webui/options/font_settings_handler.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/i18n/rtl.h" | 12 #include "base/i18n/rtl.h" |
13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/utf_string_conversions.h" |
16 #include "base/values.h" | 17 #include "base/values.h" |
17 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
18 #include "chrome/browser/character_encoding.h" | 19 #include "chrome/browser/character_encoding.h" |
19 #include "chrome/browser/chrome_notification_types.h" | 20 #include "chrome/browser/chrome_notification_types.h" |
| 21 #include "chrome/browser/extensions/extension_service.h" |
| 22 #include "chrome/browser/extensions/extension_system.h" |
| 23 #include "chrome/browser/extensions/extension_tab_util.h" |
20 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/browser/ui/browser_finder.h" |
21 #include "chrome/browser/ui/webui/options/font_settings_utils.h" | 26 #include "chrome/browser/ui/webui/options/font_settings_utils.h" |
| 27 #include "chrome/common/extensions/extension.h" |
22 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
23 #include "content/public/browser/font_list_async.h" | 29 #include "content/public/browser/font_list_async.h" |
24 #include "content/public/browser/notification_details.h" | 30 #include "content/public/browser/notification_details.h" |
| 31 #include "content/public/browser/notification_service.h" |
25 #include "content/public/browser/web_ui.h" | 32 #include "content/public/browser/web_ui.h" |
26 #include "grit/chromium_strings.h" | 33 #include "grit/chromium_strings.h" |
27 #include "grit/generated_resources.h" | 34 #include "grit/generated_resources.h" |
28 #include "ui/base/l10n/l10n_util.h" | 35 #include "ui/base/l10n/l10n_util.h" |
| 36 #include "url/gurl.h" |
29 | 37 |
30 #if defined(OS_WIN) | 38 #if defined(OS_WIN) |
31 #include "ui/gfx/font.h" | 39 #include "ui/gfx/font.h" |
32 #include "ui/gfx/platform_font_win.h" | 40 #include "ui/gfx/platform_font_win.h" |
33 #endif | 41 #endif |
34 | 42 |
35 namespace { | 43 namespace { |
36 | 44 |
37 // Returns the localized name of a font so that settings can find it within the | 45 // Returns the localized name of a font so that settings can find it within the |
38 // list of system fonts. On Windows, the list of system fonts has names only | 46 // list of system fonts. On Windows, the list of system fonts has names only |
39 // for the system locale, but the pref value may be in the English name. | 47 // for the system locale, but the pref value may be in the English name. |
40 std::string MaybeGetLocalizedFontName(const std::string& font_name) { | 48 std::string MaybeGetLocalizedFontName(const std::string& font_name) { |
41 #if defined(OS_WIN) | 49 #if defined(OS_WIN) |
42 gfx::Font font(font_name, 12); // dummy font size | 50 gfx::Font font(font_name, 12); // dummy font size |
43 return static_cast<gfx::PlatformFontWin*>(font.platform_font())-> | 51 return static_cast<gfx::PlatformFontWin*>(font.platform_font())-> |
44 GetLocalizedFontName(); | 52 GetLocalizedFontName(); |
45 #else | 53 #else |
46 return font_name; | 54 return font_name; |
47 #endif | 55 #endif |
48 } | 56 } |
49 | 57 |
| 58 const char kAdvancedFontSettingsExtensionId[] = |
| 59 "caclkomlalccbpcdllchkeecicepbmbm"; |
| 60 |
50 } // namespace | 61 } // namespace |
51 | 62 |
52 | 63 |
53 namespace options { | 64 namespace options { |
54 | 65 |
55 FontSettingsHandler::FontSettingsHandler() { | 66 FontSettingsHandler::FontSettingsHandler() { |
56 } | 67 } |
57 | 68 |
58 FontSettingsHandler::~FontSettingsHandler() { | 69 FontSettingsHandler::~FontSettingsHandler() { |
59 } | 70 } |
(...skipping 14 matching lines...) Expand all Loading... |
74 { "fontSettingsMinimumSize", | 85 { "fontSettingsMinimumSize", |
75 IDS_FONT_LANGUAGE_SETTING_MINIMUM_FONT_SIZE_TITLE }, | 86 IDS_FONT_LANGUAGE_SETTING_MINIMUM_FONT_SIZE_TITLE }, |
76 { "fontSettingsEncoding", | 87 { "fontSettingsEncoding", |
77 IDS_FONT_LANGUAGE_SETTING_FONT_SUB_DIALOG_ENCODING_TITLE }, | 88 IDS_FONT_LANGUAGE_SETTING_FONT_SUB_DIALOG_ENCODING_TITLE }, |
78 { "fontSettingsSizeTiny", | 89 { "fontSettingsSizeTiny", |
79 IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_TINY }, | 90 IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_TINY }, |
80 { "fontSettingsSizeHuge", | 91 { "fontSettingsSizeHuge", |
81 IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_HUGE }, | 92 IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_HUGE }, |
82 { "fontSettingsLoremIpsum", | 93 { "fontSettingsLoremIpsum", |
83 IDS_FONT_LANGUAGE_SETTING_LOREM_IPSUM }, | 94 IDS_FONT_LANGUAGE_SETTING_LOREM_IPSUM }, |
| 95 { "advancedFontSettingsOptions", |
| 96 IDS_FONT_LANGUAGE_SETTING_ADVANCED_FONT_SETTINGS_OPTIONS } |
84 }; | 97 }; |
85 | 98 |
86 RegisterStrings(localized_strings, resources, arraysize(resources)); | 99 RegisterStrings(localized_strings, resources, arraysize(resources)); |
87 RegisterTitle(localized_strings, "fontSettingsPage", | 100 RegisterTitle(localized_strings, "fontSettingsPage", |
88 IDS_FONT_LANGUAGE_SETTING_FONT_TAB_TITLE); | 101 IDS_FONT_LANGUAGE_SETTING_FONT_TAB_TITLE); |
89 localized_strings->SetString("fontSettingsPlaceholder", | 102 localized_strings->SetString("fontSettingsPlaceholder", |
90 l10n_util::GetStringUTF16( | 103 l10n_util::GetStringUTF16( |
91 IDS_FONT_LANGUAGE_SETTING_PLACEHOLDER)); | 104 IDS_FONT_LANGUAGE_SETTING_PLACEHOLDER)); |
| 105 |
| 106 GURL install_url(extension_urls::GetWebstoreItemDetailURLPrefix()); |
| 107 localized_strings->SetString("advancedFontSettingsInstall", |
| 108 l10n_util::GetStringFUTF16( |
| 109 IDS_FONT_LANGUAGE_SETTING_ADVANCED_FONT_SETTINGS_INSTALL, |
| 110 UTF8ToUTF16( |
| 111 install_url.Resolve(kAdvancedFontSettingsExtensionId).spec()))); |
| 112 } |
| 113 |
| 114 void FontSettingsHandler::InitializeHandler() { |
| 115 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 116 content::NotificationService::AllSources()); |
| 117 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 118 content::NotificationService::AllSources()); |
92 } | 119 } |
93 | 120 |
94 void FontSettingsHandler::InitializePage() { | 121 void FontSettingsHandler::InitializePage() { |
95 DCHECK(web_ui()); | 122 DCHECK(web_ui()); |
96 SetUpStandardFontSample(); | 123 SetUpStandardFontSample(); |
97 SetUpSerifFontSample(); | 124 SetUpSerifFontSample(); |
98 SetUpSansSerifFontSample(); | 125 SetUpSansSerifFontSample(); |
99 SetUpFixedFontSample(); | 126 SetUpFixedFontSample(); |
100 SetUpMinimumFontSample(); | 127 SetUpMinimumFontSample(); |
| 128 NotifyAdvancedFontSettingsAvailability(); |
101 } | 129 } |
102 | 130 |
103 void FontSettingsHandler::RegisterMessages() { | 131 void FontSettingsHandler::RegisterMessages() { |
104 // Perform validation for saved fonts. | 132 // Perform validation for saved fonts. |
105 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); | 133 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); |
106 FontSettingsUtilities::ValidateSavedFonts(pref_service); | 134 FontSettingsUtilities::ValidateSavedFonts(pref_service); |
107 | 135 |
108 // Register for preferences that we need to observe manually. | 136 // Register for preferences that we need to observe manually. |
109 font_encoding_.Init(prefs::kDefaultCharset, pref_service); | 137 font_encoding_.Init(prefs::kDefaultCharset, pref_service); |
110 | 138 |
(...skipping 24 matching lines...) Expand all Loading... |
135 base::Unretained(this))); | 163 base::Unretained(this))); |
136 minimum_font_size_.Init( | 164 minimum_font_size_.Init( |
137 prefs::kWebKitMinimumFontSize, | 165 prefs::kWebKitMinimumFontSize, |
138 pref_service, | 166 pref_service, |
139 base::Bind(&FontSettingsHandler::SetUpMinimumFontSample, | 167 base::Bind(&FontSettingsHandler::SetUpMinimumFontSample, |
140 base::Unretained(this))); | 168 base::Unretained(this))); |
141 | 169 |
142 web_ui()->RegisterMessageCallback("fetchFontsData", | 170 web_ui()->RegisterMessageCallback("fetchFontsData", |
143 base::Bind(&FontSettingsHandler::HandleFetchFontsData, | 171 base::Bind(&FontSettingsHandler::HandleFetchFontsData, |
144 base::Unretained(this))); | 172 base::Unretained(this))); |
| 173 web_ui()->RegisterMessageCallback("openAdvancedFontSettingsOptions", |
| 174 base::Bind(&FontSettingsHandler::HandleOpenAdvancedFontSettingsOptions, |
| 175 base::Unretained(this))); |
| 176 } |
| 177 |
| 178 void FontSettingsHandler::Observe(int type, |
| 179 const content::NotificationSource& source, |
| 180 const content::NotificationDetails& details) { |
| 181 DCHECK(type == chrome::NOTIFICATION_EXTENSION_LOADED || |
| 182 type == chrome::NOTIFICATION_EXTENSION_UNLOADED); |
| 183 NotifyAdvancedFontSettingsAvailability(); |
145 } | 184 } |
146 | 185 |
147 void FontSettingsHandler::HandleFetchFontsData(const ListValue* args) { | 186 void FontSettingsHandler::HandleFetchFontsData(const ListValue* args) { |
148 content::GetFontListAsync( | 187 content::GetFontListAsync( |
149 base::Bind(&FontSettingsHandler::FontsListHasLoaded, | 188 base::Bind(&FontSettingsHandler::FontsListHasLoaded, |
150 base::Unretained(this))); | 189 base::Unretained(this))); |
151 } | 190 } |
152 | 191 |
153 void FontSettingsHandler::FontsListHasLoaded( | 192 void FontSettingsHandler::FontsListHasLoaded( |
154 scoped_ptr<base::ListValue> list) { | 193 scoped_ptr<base::ListValue> list) { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 web_ui()->CallJavascriptFunction( | 276 web_ui()->CallJavascriptFunction( |
238 "FontSettings.setUpFixedFontSample", font_value, size_value); | 277 "FontSettings.setUpFixedFontSample", font_value, size_value); |
239 } | 278 } |
240 | 279 |
241 void FontSettingsHandler::SetUpMinimumFontSample() { | 280 void FontSettingsHandler::SetUpMinimumFontSample() { |
242 base::FundamentalValue size_value(minimum_font_size_.GetValue()); | 281 base::FundamentalValue size_value(minimum_font_size_.GetValue()); |
243 web_ui()->CallJavascriptFunction("FontSettings.setUpMinimumFontSample", | 282 web_ui()->CallJavascriptFunction("FontSettings.setUpMinimumFontSample", |
244 size_value); | 283 size_value); |
245 } | 284 } |
246 | 285 |
| 286 const extensions::Extension* |
| 287 FontSettingsHandler::GetAdvancedFontSettingsExtension() { |
| 288 Profile* profile = Profile::FromWebUI(web_ui()); |
| 289 ExtensionService* service = |
| 290 extensions::ExtensionSystem::Get(profile)->extension_service(); |
| 291 if (!service->IsExtensionEnabled(kAdvancedFontSettingsExtensionId)) |
| 292 return NULL; |
| 293 return service->GetInstalledExtension(kAdvancedFontSettingsExtensionId); |
| 294 } |
| 295 |
| 296 void FontSettingsHandler::NotifyAdvancedFontSettingsAvailability() { |
| 297 web_ui()->CallJavascriptFunction( |
| 298 "FontSettings.notifyAdvancedFontSettingsAvailability", |
| 299 base::FundamentalValue(GetAdvancedFontSettingsExtension() != NULL)); |
| 300 } |
| 301 |
| 302 void FontSettingsHandler::HandleOpenAdvancedFontSettingsOptions( |
| 303 const base::ListValue* args) { |
| 304 const extensions::Extension* extension = GetAdvancedFontSettingsExtension(); |
| 305 if (!extension) |
| 306 return; |
| 307 ExtensionTabUtil::OpenOptionsPage(extension, |
| 308 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents())); |
| 309 } |
| 310 |
247 void FontSettingsHandler::OnWebKitDefaultFontSizeChanged() { | 311 void FontSettingsHandler::OnWebKitDefaultFontSizeChanged() { |
248 SetUpStandardFontSample(); | 312 SetUpStandardFontSample(); |
249 SetUpSerifFontSample(); | 313 SetUpSerifFontSample(); |
250 SetUpSansSerifFontSample(); | 314 SetUpSansSerifFontSample(); |
251 } | 315 } |
252 | 316 |
253 } // namespace options | 317 } // namespace options |
OLD | NEW |