OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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" | |
Dan Beam
2015/10/24 01:19:47
^ no longer required for size_t
dschuyler
2015/10/26 15:54:33
Done.
| |
8 #include "base/bind.h" | |
Dan Beam
2015/10/24 01:19:47
base/bind_helpers.h for base::Unretained
dschuyler
2015/10/26 15:54:33
Done.
| |
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" | |
Dan Beam
2015/10/24 01:19:47
^ why do you need web_ui.h?
dschuyler
2015/10/26 15:54:33
It's for web_ui(). Is there a better way to do th
| |
18 | |
19 namespace settings { | |
20 | |
21 SettingsFontHandler::SettingsFontHandler(content::WebUI* webui) | |
22 : weak_ptr_factory_(this) { | |
23 // Perform validation for saved fonts. | |
24 PrefService* pref_service = Profile::FromWebUI(webui)->GetPrefs(); | |
25 options::FontSettingsUtilities::ValidateSavedFonts(pref_service); | |
26 } | |
27 | |
28 SettingsFontHandler::~SettingsFontHandler() {} | |
29 | |
30 void SettingsFontHandler::RegisterMessages() { | |
31 web_ui()->RegisterMessageCallback( | |
32 "fetchFontsData", base::Bind(&SettingsFontHandler::HandleFetchFontsData, | |
33 weak_ptr_factory_.GetWeakPtr())); | |
Dan Beam
2015/10/24 01:19:47
in this case, use base::Unretained(this) instead o
dschuyler
2015/10/26 15:54:33
Done.
| |
34 } | |
35 | |
36 void SettingsFontHandler::HandleFetchFontsData( | |
37 const base::ListValue* /*args*/) { | |
38 content::GetFontListAsync(base::Bind(&SettingsFontHandler::FontListHasLoaded, | |
39 weak_ptr_factory_.GetWeakPtr())); | |
Dan Beam
2015/10/24 01:19:47
keep weak_ptr_factory_.GetWeakPtr() for this
dschuyler
2015/10/26 15:54:33
Acknowledged.
| |
40 } | |
41 | |
42 void SettingsFontHandler::FontListHasLoaded(scoped_ptr<base::ListValue> list) { | |
43 // Font list. Selects the directionality for the fonts in the given list. | |
Dan Beam
2015/10/24 01:19:47
\s\s -> \s
dschuyler
2015/10/26 15:54:33
Done.
| |
44 for (size_t i = 0; i < list->GetSize(); i++) { | |
45 base::ListValue* font; | |
46 bool has_font = list->GetList(i, &font); | |
47 DCHECK(has_font); | |
48 | |
49 base::string16 value; | |
50 bool has_value = font->GetString(1, &value); | |
51 DCHECK(has_value); | |
52 | |
53 bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(value); | |
54 font->Append(new base::StringValue(has_rtl_chars ? "rtl" : "ltr")); | |
55 } | |
56 | |
57 // Character encoding list. | |
58 const std::vector<CharacterEncoding::EncodingInfo>* encodings; | |
59 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); | |
60 encodings = CharacterEncoding::GetCurrentDisplayEncodings( | |
61 g_browser_process->GetApplicationLocale(), | |
62 pref_service->GetString(prefs::kStaticEncodings), | |
63 pref_service->GetString(prefs::kRecentlySelectedEncoding)); | |
64 DCHECK(!encodings->empty()); | |
65 | |
66 base::ListValue encoding_list; | |
67 for (const auto& it : *encodings) { | |
68 scoped_ptr<base::ListValue> option(new base::ListValue()); | |
69 if (it.encoding_id) { | |
70 int cmd_id = it.encoding_id; | |
71 std::string encoding = | |
72 CharacterEncoding::GetCanonicalEncodingNameByCommandId(cmd_id); | |
Dan Beam
2015/10/24 01:19:47
option->ApendString(
CharacterEncoding::GetCan
dschuyler
2015/10/26 15:54:33
Done.
| |
73 base::string16 name = it.encoding_display_name; | |
74 bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(name); | |
75 option->Append(new base::StringValue(encoding)); | |
76 option->Append(new base::StringValue(name)); | |
77 option->Append(new base::StringValue(has_rtl_chars ? "rtl" : "ltr")); | |
78 } else { | |
79 // Add empty value to indicate a separator item. | |
80 option->Append(new base::StringValue(std::string())); | |
Dan Beam
2015/10/24 01:19:47
option->AppendString(std::string());
dschuyler
2015/10/26 15:54:33
Done.
| |
81 } | |
82 encoding_list.Append(option.Pass()); | |
83 } | |
84 | |
85 web_ui()->CallJavascriptFunction("Settings.setFontsData", *list, | |
86 encoding_list); | |
87 } | |
88 | |
89 } // namespace settings | |
OLD | NEW |