Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: chrome/browser/ui/webui/settings/settings_font_handler.cc

Issue 1405453002: [MD settings] adding c++ side of font settings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: touch ups Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/webui/settings/settings_font_handler.h"
6
7 #include "base/basictypes.h"
8 #include "base/bind.h"
9 #include "base/i18n/rtl.h"
10 #include "base/prefs/pref_service.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/character_encoding.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/webui/options/font_settings_utils.h"
15 #include "chrome/common/pref_names.h"
16 #include "content/public/browser/font_list_async.h"
17 #include "content/public/browser/web_ui.h"
18
19 #if defined(OS_WIN)
20 #include "ui/gfx/font.h"
21 #include "ui/gfx/platform_font_win.h"
22 #endif
23
24 namespace {
25
26 // Returns the localized name of a font so that settings can find it within the
27 // list of system fonts. On Windows, the list of system fonts has names only
28 // for the system locale, but the pref value may be in the English name.
29 std::string MaybeGetLocalizedFontName(const std::string& font_name) {
dschuyler 2015/10/12 23:24:51 I'm taking it on face value that this is still nee
Dan Beam 2015/10/15 02:34:41 probably is
dschuyler 2015/10/15 21:54:20 Acknowledged.
30 #if defined(OS_WIN)
31 gfx::Font font(font_name, 12); // dummy font size
32 return static_cast<gfx::PlatformFontWin*>(
33 font.platform_font())->GetLocalizedFontName();
34 #else
35 return font_name;
36 #endif
37 }
38
39 } // namespace
40
41 namespace settings {
42
43 SettingsFontHandler::SettingsFontHandler(content::WebUI* webui) {
44 // Perform validation for saved fonts.
45 PrefService* pref_service = Profile::FromWebUI(webui)->GetPrefs();
46 options::FontSettingsUtilities::ValidateSavedFonts(pref_service);
47
48 // Register for preferences that we need to observe manually.
49 font_encoding_.Init(prefs::kDefaultCharset, pref_service);
50
51 standard_font_.Init(prefs::kWebKitStandardFontFamily, pref_service);
52 serif_font_.Init(prefs::kWebKitSerifFontFamily, pref_service);
53 sans_serif_font_.Init(prefs::kWebKitSansSerifFontFamily, pref_service);
54 fixed_font_.Init(prefs::kWebKitFixedFontFamily, pref_service);
dschuyler 2015/10/12 23:24:51 We could pull out these values and strictly use th
Dan Beam 2015/10/15 02:34:41 i think it'd be pretty simple to bind prefs={{pref
dschuyler 2015/10/15 21:54:20 These have been removed with the change to use the
55 }
56
57 SettingsFontHandler::~SettingsFontHandler() {}
58
59 void SettingsFontHandler::RegisterMessages() {
60 web_ui()->RegisterMessageCallback(
61 "fetchFontsData", base::Bind(&SettingsFontHandler::HandleFetchFontsData,
62 base::Unretained(this)));
63 }
64
65 void SettingsFontHandler::HandleFetchFontsData(const base::ListValue* args) {
66 content::GetFontListAsync(base::Bind(&SettingsFontHandler::FontListHasLoaded,
67 base::Unretained(this)));
Dan Beam 2015/10/15 02:34:41 this class should use a WeakPtrFactory<> and inste
dschuyler 2015/10/15 21:54:19 Done.
68 }
69
70 void SettingsFontHandler::FontListHasLoaded(scoped_ptr<base::ListValue> list) {
71 // Font list. Selects the directionality for the fonts in the given list.
72 for (size_t i = 0; i < list->GetSize(); i++) {
73 base::ListValue* font;
74 bool has_font = list->GetList(i, &font);
75 DCHECK(has_font);
Dan Beam 2015/10/15 02:34:41 \n
dschuyler 2015/10/15 21:54:19 Done.
76 base::string16 value;
77 bool has_value = font->GetString(1, &value);
78 DCHECK(has_value);
Dan Beam 2015/10/15 02:34:41 \n
dschuyler 2015/10/15 21:54:20 Done.
79 bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(value);
80 font->Append(new base::StringValue(has_rtl_chars ? "rtl" : "ltr"));
dschuyler 2015/10/12 23:24:51 Identifying the rtl and ltr fonts is something I'm
Dan Beam 2015/10/15 02:34:41 this is mildly weird to me. this is checking for
dschuyler 2015/10/15 21:54:20 Acknowledged.
81 }
82
83 // Character encoding list.
84 base::ListValue encoding_list;
Dan Beam 2015/10/15 02:34:41 declare lower
dschuyler 2015/10/15 21:54:19 Done.
85 const std::vector<CharacterEncoding::EncodingInfo>* encodings;
86 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs();
87 encodings = CharacterEncoding::GetCurrentDisplayEncodings(
88 g_browser_process->GetApplicationLocale(),
89 pref_service->GetString(prefs::kStaticEncodings),
90 pref_service->GetString(prefs::kRecentlySelectedEncoding));
91 DCHECK(encodings);
Dan Beam 2015/10/15 02:34:41 nit: DCHECK(ptr); followed by DCHECK(ptr->DoSometh
dschuyler 2015/10/15 21:54:20 Done.
92 DCHECK(!encodings->empty());
Dan Beam 2015/10/15 02:34:41 \n
dschuyler 2015/10/15 21:54:19 Done.
93 std::vector<CharacterEncoding::EncodingInfo>::const_iterator it;
94 for (it = encodings->begin(); it != encodings->end(); ++it) {
Dan Beam 2015/10/15 02:34:41 you could probably use for ( : ) now
dschuyler 2015/10/15 21:54:19 Done.
95 base::ListValue* option = new base::ListValue();
Dan Beam 2015/10/15 02:34:41 nit: use scoped_ptr to make the ownership more exp
dschuyler 2015/10/15 21:54:19 Done.
96 if (it->encoding_id) {
97 int cmd_id = it->encoding_id;
98 std::string encoding =
99 CharacterEncoding::GetCanonicalEncodingNameByCommandId(cmd_id);
100 base::string16 name = it->encoding_display_name;
101 bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(name);
102 option->Append(new base::StringValue(encoding));
103 option->Append(new base::StringValue(name));
104 option->Append(new base::StringValue(has_rtl_chars ? "rtl" : "ltr"));
105 } else {
106 // Add empty name/value to indicate a separator item.
dschuyler 2015/10/12 23:24:51 Using a blank entry is what the old options code d
107 option->Append(new base::StringValue(std::string()));
108 option->Append(new base::StringValue(std::string()));
Dan Beam 2015/10/15 02:34:41 why do we need 2 empty strings?
dschuyler 2015/10/15 21:54:19 It seems to work ok with one. Done.
109 }
110 encoding_list.Append(option);
111 }
112
113 // Current selections. The order of items in this tuple are important.
Dan Beam 2015/10/15 02:34:41 would be nice to make this a dictionary instead so
dschuyler 2015/10/15 21:54:20 Done.
114 base::ListValue selected_values;
115 selected_values.Append(new base::StringValue(
116 MaybeGetLocalizedFontName(standard_font_.GetValue())));
117 selected_values.Append(
118 new base::StringValue(MaybeGetLocalizedFontName(serif_font_.GetValue())));
119 selected_values.Append(new base::StringValue(
120 MaybeGetLocalizedFontName(sans_serif_font_.GetValue())));
121 selected_values.Append(
122 new base::StringValue(MaybeGetLocalizedFontName(fixed_font_.GetValue())));
123 selected_values.Append(new base::StringValue(font_encoding_.GetValue()));
124
125 web_ui()->CallJavascriptFunction("Settings.setFontsData", *list.get(),
Dan Beam 2015/10/15 02:34:41 why is this doing *.get()? scoped_ptr implements
dschuyler 2015/10/15 21:54:19 Done.
126 encoding_list, selected_values);
127 }
128
129 } // namespace settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698