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" |
29 | 36 |
30 #if defined(OS_WIN) | 37 #if defined(OS_WIN) |
31 #include "ui/gfx/font.h" | 38 #include "ui/gfx/font.h" |
32 #include "ui/gfx/platform_font_win.h" | 39 #include "ui/gfx/platform_font_win.h" |
33 #endif | 40 #endif |
34 | 41 |
35 namespace { | 42 namespace { |
36 | 43 |
37 // Returns the localized name of a font so that settings can find it within the | 44 // 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 | 45 // 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. | 46 // for the system locale, but the pref value may be in the English name. |
40 std::string MaybeGetLocalizedFontName(const std::string& font_name) { | 47 std::string MaybeGetLocalizedFontName(const std::string& font_name) { |
41 #if defined(OS_WIN) | 48 #if defined(OS_WIN) |
42 gfx::Font font(font_name, 12); // dummy font size | 49 gfx::Font font(font_name, 12); // dummy font size |
43 return static_cast<gfx::PlatformFontWin*>(font.platform_font())-> | 50 return static_cast<gfx::PlatformFontWin*>(font.platform_font())-> |
44 GetLocalizedFontName(); | 51 GetLocalizedFontName(); |
45 #else | 52 #else |
46 return font_name; | 53 return font_name; |
47 #endif | 54 #endif |
48 } | 55 } |
49 | 56 |
57 const char kAdvancedFontSettingsExtensionId[] = | |
58 "caclkomlalccbpcdllchkeecicepbmbm"; | |
59 | |
50 } // namespace | 60 } // namespace |
51 | 61 |
52 | 62 |
53 namespace options { | 63 namespace options { |
54 | 64 |
55 FontSettingsHandler::FontSettingsHandler() { | 65 FontSettingsHandler::FontSettingsHandler() { |
56 } | 66 } |
57 | 67 |
58 FontSettingsHandler::~FontSettingsHandler() { | 68 FontSettingsHandler::~FontSettingsHandler() { |
59 } | 69 } |
(...skipping 14 matching lines...) Expand all Loading... | |
74 { "fontSettingsMinimumSize", | 84 { "fontSettingsMinimumSize", |
75 IDS_FONT_LANGUAGE_SETTING_MINIMUM_FONT_SIZE_TITLE }, | 85 IDS_FONT_LANGUAGE_SETTING_MINIMUM_FONT_SIZE_TITLE }, |
76 { "fontSettingsEncoding", | 86 { "fontSettingsEncoding", |
77 IDS_FONT_LANGUAGE_SETTING_FONT_SUB_DIALOG_ENCODING_TITLE }, | 87 IDS_FONT_LANGUAGE_SETTING_FONT_SUB_DIALOG_ENCODING_TITLE }, |
78 { "fontSettingsSizeTiny", | 88 { "fontSettingsSizeTiny", |
79 IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_TINY }, | 89 IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_TINY }, |
80 { "fontSettingsSizeHuge", | 90 { "fontSettingsSizeHuge", |
81 IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_HUGE }, | 91 IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_HUGE }, |
82 { "fontSettingsLoremIpsum", | 92 { "fontSettingsLoremIpsum", |
83 IDS_FONT_LANGUAGE_SETTING_LOREM_IPSUM }, | 93 IDS_FONT_LANGUAGE_SETTING_LOREM_IPSUM }, |
94 { "advancedFontSettingsOptions", | |
95 IDS_FONT_LANGUAGE_SETTING_ADVANCED_FONT_SETTINGS_OPTIONS } | |
84 }; | 96 }; |
85 | 97 |
86 RegisterStrings(localized_strings, resources, arraysize(resources)); | 98 RegisterStrings(localized_strings, resources, arraysize(resources)); |
87 RegisterTitle(localized_strings, "fontSettingsPage", | 99 RegisterTitle(localized_strings, "fontSettingsPage", |
88 IDS_FONT_LANGUAGE_SETTING_FONT_TAB_TITLE); | 100 IDS_FONT_LANGUAGE_SETTING_FONT_TAB_TITLE); |
89 localized_strings->SetString("fontSettingsPlaceholder", | 101 localized_strings->SetString("fontSettingsPlaceholder", |
90 l10n_util::GetStringUTF16( | 102 l10n_util::GetStringUTF16( |
91 IDS_FONT_LANGUAGE_SETTING_PLACEHOLDER)); | 103 IDS_FONT_LANGUAGE_SETTING_PLACEHOLDER)); |
104 | |
105 std::string install_url = extension_urls::GetWebstoreItemDetailURLPrefix() + | |
106 kAdvancedFontSettingsExtensionId; | |
107 localized_strings->SetString("advancedFontSettingsInstall", | |
108 l10n_util::GetStringFUTF16( | |
109 IDS_FONT_LANGUAGE_SETTING_ADVANCED_FONT_SETTINGS_INSTALL, | |
110 UTF8ToUTF16(install_url))); | |
111 } | |
112 | |
113 void FontSettingsHandler::InitializeHandler() { | |
114 DCHECK(web_ui()); | |
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::OpenAdvancedFontSettingsOptions, | |
175 base::Unretained(this))); | |
176 } | |
177 | |
178 void FontSettingsHandler::Observe(int type, | |
179 const content::NotificationSource& source, | |
180 const content::NotificationDetails& details) { | |
181 NotifyAdvancedFontSettingsAvailability(); | |
145 } | 182 } |
146 | 183 |
147 void FontSettingsHandler::HandleFetchFontsData(const ListValue* args) { | 184 void FontSettingsHandler::HandleFetchFontsData(const ListValue* args) { |
148 content::GetFontListAsync( | 185 content::GetFontListAsync( |
149 base::Bind(&FontSettingsHandler::FontsListHasLoaded, | 186 base::Bind(&FontSettingsHandler::FontsListHasLoaded, |
150 base::Unretained(this))); | 187 base::Unretained(this))); |
151 } | 188 } |
152 | 189 |
153 void FontSettingsHandler::FontsListHasLoaded( | 190 void FontSettingsHandler::FontsListHasLoaded( |
154 scoped_ptr<base::ListValue> list) { | 191 scoped_ptr<base::ListValue> list) { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
237 web_ui()->CallJavascriptFunction( | 274 web_ui()->CallJavascriptFunction( |
238 "FontSettings.setUpFixedFontSample", font_value, size_value); | 275 "FontSettings.setUpFixedFontSample", font_value, size_value); |
239 } | 276 } |
240 | 277 |
241 void FontSettingsHandler::SetUpMinimumFontSample() { | 278 void FontSettingsHandler::SetUpMinimumFontSample() { |
242 base::FundamentalValue size_value(minimum_font_size_.GetValue()); | 279 base::FundamentalValue size_value(minimum_font_size_.GetValue()); |
243 web_ui()->CallJavascriptFunction("FontSettings.setUpMinimumFontSample", | 280 web_ui()->CallJavascriptFunction("FontSettings.setUpMinimumFontSample", |
244 size_value); | 281 size_value); |
245 } | 282 } |
246 | 283 |
284 const extensions::Extension* | |
285 FontSettingsHandler::GetAdvancedFontSettingsExtension() { | |
286 Profile* profile = Profile::FromWebUI(web_ui()); | |
287 ExtensionService* service = | |
288 extensions::ExtensionSystem::Get(profile)->extension_service(); | |
289 const extensions::Extension* extension = | |
290 service->GetInstalledExtension(kAdvancedFontSettingsExtensionId); | |
291 if (extension && | |
292 service->IsExtensionEnabled(kAdvancedFontSettingsExtensionId)) | |
293 return extension; | |
294 return NULL; | |
295 } | |
296 | |
297 void FontSettingsHandler::NotifyAdvancedFontSettingsAvailability() { | |
298 web_ui()->CallJavascriptFunction( | |
299 "FontSettings.notifyAdvancedFontSettingsAvailability", | |
300 base::FundamentalValue(GetAdvancedFontSettingsExtension() != NULL)); | |
301 } | |
302 | |
303 void FontSettingsHandler::OpenAdvancedFontSettingsOptions( | |
304 const base::ListValue* args) { | |
305 const extensions::Extension* extension = GetAdvancedFontSettingsExtension(); | |
306 if (!extension) | |
307 return; | |
308 ExtensionTabUtil::OpenOptionsPage(extension, | |
309 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents())); | |
falken
2013/10/23 05:50:23
I'm not sure if this is overkill. Would it better
| |
310 } | |
311 | |
247 void FontSettingsHandler::OnWebKitDefaultFontSizeChanged() { | 312 void FontSettingsHandler::OnWebKitDefaultFontSizeChanged() { |
248 SetUpStandardFontSample(); | 313 SetUpStandardFontSample(); |
249 SetUpSerifFontSample(); | 314 SetUpSerifFontSample(); |
250 SetUpSansSerifFontSample(); | 315 SetUpSansSerifFontSample(); |
251 } | 316 } |
252 | 317 |
253 } // namespace options | 318 } // namespace options |
OLD | NEW |