Index: chrome/browser/ui/webui/settings/settings_font_handler.cc |
diff --git a/chrome/browser/ui/webui/settings/settings_font_handler.cc b/chrome/browser/ui/webui/settings/settings_font_handler.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..502c4102bece11ad35619055f8c250e854a0e3d5 |
--- /dev/null |
+++ b/chrome/browser/ui/webui/settings/settings_font_handler.cc |
@@ -0,0 +1,89 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/ui/webui/settings/settings_font_handler.h" |
+ |
+#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.
|
+#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.
|
+#include "base/i18n/rtl.h" |
+#include "base/prefs/pref_service.h" |
+#include "chrome/browser/browser_process.h" |
+#include "chrome/browser/character_encoding.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/ui/webui/options/font_settings_utils.h" |
+#include "chrome/common/pref_names.h" |
+#include "content/public/browser/font_list_async.h" |
+#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
|
+ |
+namespace settings { |
+ |
+SettingsFontHandler::SettingsFontHandler(content::WebUI* webui) |
+ : weak_ptr_factory_(this) { |
+ // Perform validation for saved fonts. |
+ PrefService* pref_service = Profile::FromWebUI(webui)->GetPrefs(); |
+ options::FontSettingsUtilities::ValidateSavedFonts(pref_service); |
+} |
+ |
+SettingsFontHandler::~SettingsFontHandler() {} |
+ |
+void SettingsFontHandler::RegisterMessages() { |
+ web_ui()->RegisterMessageCallback( |
+ "fetchFontsData", base::Bind(&SettingsFontHandler::HandleFetchFontsData, |
+ 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.
|
+} |
+ |
+void SettingsFontHandler::HandleFetchFontsData( |
+ const base::ListValue* /*args*/) { |
+ content::GetFontListAsync(base::Bind(&SettingsFontHandler::FontListHasLoaded, |
+ 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.
|
+} |
+ |
+void SettingsFontHandler::FontListHasLoaded(scoped_ptr<base::ListValue> list) { |
+ // 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.
|
+ for (size_t i = 0; i < list->GetSize(); i++) { |
+ base::ListValue* font; |
+ bool has_font = list->GetList(i, &font); |
+ DCHECK(has_font); |
+ |
+ base::string16 value; |
+ bool has_value = font->GetString(1, &value); |
+ DCHECK(has_value); |
+ |
+ bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(value); |
+ font->Append(new base::StringValue(has_rtl_chars ? "rtl" : "ltr")); |
+ } |
+ |
+ // Character encoding list. |
+ const std::vector<CharacterEncoding::EncodingInfo>* encodings; |
+ PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); |
+ encodings = CharacterEncoding::GetCurrentDisplayEncodings( |
+ g_browser_process->GetApplicationLocale(), |
+ pref_service->GetString(prefs::kStaticEncodings), |
+ pref_service->GetString(prefs::kRecentlySelectedEncoding)); |
+ DCHECK(!encodings->empty()); |
+ |
+ base::ListValue encoding_list; |
+ for (const auto& it : *encodings) { |
+ scoped_ptr<base::ListValue> option(new base::ListValue()); |
+ if (it.encoding_id) { |
+ int cmd_id = it.encoding_id; |
+ std::string encoding = |
+ CharacterEncoding::GetCanonicalEncodingNameByCommandId(cmd_id); |
Dan Beam
2015/10/24 01:19:47
option->ApendString(
CharacterEncoding::GetCan
dschuyler
2015/10/26 15:54:33
Done.
|
+ base::string16 name = it.encoding_display_name; |
+ bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(name); |
+ option->Append(new base::StringValue(encoding)); |
+ option->Append(new base::StringValue(name)); |
+ option->Append(new base::StringValue(has_rtl_chars ? "rtl" : "ltr")); |
+ } else { |
+ // Add empty value to indicate a separator item. |
+ 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.
|
+ } |
+ encoding_list.Append(option.Pass()); |
+ } |
+ |
+ web_ui()->CallJavascriptFunction("Settings.setFontsData", *list, |
+ encoding_list); |
+} |
+ |
+} // namespace settings |