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/font_handler.h"
6
7 #include <stddef.h>
8 #include <string>
9 #include <utility>
10 #include <vector>
11
12 #include "base/bind.h"
13 #include "base/bind_helpers.h"
14 #include "base/i18n/rtl.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser_finder.h"
18 #include "chrome/common/pref_names.h"
19 #include "components/prefs/pref_service.h"
20 #include "content/public/browser/font_list_async.h"
21 #include "content/public/browser/web_ui.h"
22
23 #if defined(OS_MACOSX)
24 #include "chrome/browser/ui/webui/settings_utils.h"
25 #endif
26
27 namespace settings {
28
FontHandler(content::WebUI * webui)29 FontHandler::FontHandler(content::WebUI* webui)
30 : profile_(Profile::FromWebUI(webui)) {
31 #if defined(OS_MACOSX)
32 // Perform validation for saved fonts.
33 settings_utils::ValidateSavedFonts(profile_->GetPrefs());
34 #endif
35 }
36
~FontHandler()37 FontHandler::~FontHandler() {}
38
RegisterMessages()39 void FontHandler::RegisterMessages() {
40 web_ui()->RegisterMessageCallback(
41 "fetchFontsData", base::BindRepeating(&FontHandler::HandleFetchFontsData,
42 base::Unretained(this)));
43 }
44
OnJavascriptAllowed()45 void FontHandler::OnJavascriptAllowed() {}
46
OnJavascriptDisallowed()47 void FontHandler::OnJavascriptDisallowed() {}
48
HandleFetchFontsData(const base::ListValue * args)49 void FontHandler::HandleFetchFontsData(const base::ListValue* args) {
50 CHECK_EQ(1U, args->GetSize());
51 std::string callback_id;
52 CHECK(args->GetString(0, &callback_id));
53
54 AllowJavascript();
55 content::GetFontListAsync(base::Bind(&FontHandler::FontListHasLoaded,
56 weak_ptr_factory_.GetWeakPtr(),
57 callback_id));
58 }
59
FontListHasLoaded(std::string callback_id,std::unique_ptr<base::ListValue> list)60 void FontHandler::FontListHasLoaded(std::string callback_id,
61 std::unique_ptr<base::ListValue> list) {
62 // Font list. Selects the directionality for the fonts in the given list.
63 for (size_t i = 0; i < list->GetSize(); i++) {
64 base::ListValue* font;
65 bool has_font = list->GetList(i, &font);
66 DCHECK(has_font);
67
68 base::string16 value;
69 bool has_value = font->GetString(1, &value);
70 DCHECK(has_value);
71
72 bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(value);
73 font->AppendString(has_rtl_chars ? "rtl" : "ltr");
74 }
75
76 base::DictionaryValue response;
77 response.Set("fontList", std::move(list));
78
79 ResolveJavascriptCallback(base::Value(callback_id), response);
80 }
81
82 } // namespace settings
83