blob: 9d302c8d1b2948edbb69d57b7137423e96ca7f06 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2012 The Chromium Authors
[email protected]93623c5d2009-12-10 21:40:322// 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/renderer_preferences_util.h"
6
Adam Rice3d178a82021-04-12 13:49:537#include <stdint.h>
8
guidouca055a542016-08-11 11:15:539#include <string>
Adam Rice3d178a82021-04-12 13:49:5310#include <vector>
guidouca055a542016-08-11 11:15:5311
Guido Urdanetafdaf45e2024-12-17 20:47:2912#include "base/logging.h"
guidouca055a542016-08-11 11:15:5313#include "base/strings/string_number_conversions.h"
14#include "base/strings/string_util.h"
avib896c712015-12-26 02:10:4315#include "build/build_config.h"
John Abd-El-Malek7d032b372019-05-07 18:01:3116#include "chrome/browser/browser_process.h"
Adam Rice3d178a82021-04-12 13:49:5317#include "chrome/browser/net/convert_explicitly_allowed_network_ports_pref.h"
Fiona Macintoshafa01d22023-09-18 12:55:4918#include "chrome/browser/privacy_sandbox/tracking_protection_settings_factory.h"
Yuta Hijikataaabe6312025-03-06 02:03:0019#if BUILDFLAG(IS_CHROMEOS)
Henrique Ferreiro606669a2021-02-24 13:36:5520#include "chrome/browser/ash/login/demo_mode/demo_session.h"
Yves Arrouye39b8ef62021-02-23 00:39:2921#endif
[email protected]8ecad5e2010-12-02 21:18:3322#include "chrome/browser/profiles/profile.h"
[email protected]0a8db0d2011-04-13 15:15:4023#include "chrome/common/pref_names.h"
Friedrich Horschig91c02102025-01-10 13:45:5824#include "components/autofill/core/common/autofill_prefs.h"
Trevor Perrierfa7339d2020-09-11 00:42:4025#include "components/language/core/browser/language_prefs.h"
Alexandre Frechette572755b2019-02-13 22:30:2026#include "components/language/core/browser/pref_names.h"
brettwb1fc1b82016-02-02 00:19:0827#include "components/prefs/pref_service.h"
Fiona Macintoshafa01d22023-09-18 12:55:4928#include "components/privacy_sandbox/tracking_protection_settings.h"
Ian Vollick0c34a072019-02-15 03:40:2729#include "content/public/browser/renderer_preferences_util.h"
Scott Violeta35f9a42018-03-22 22:00:4430#include "media/media_buildflags.h"
Antonio Gomesb5bf548f2019-09-12 17:40:1531#include "third_party/blink/public/common/peerconnection/webrtc_ip_handling_policy.h"
Mario Sanchez Prada0bd8b8c2020-10-21 17:49:2332#include "third_party/blink/public/common/renderer_preferences/renderer_preferences.h"
Guido Urdanetad8bfc6f92024-12-11 14:01:2233#include "third_party/blink/public/mojom/peerconnection/webrtc_ip_handling_policy.mojom.h"
Blink Reformata30d4232018-04-07 15:31:0634#include "third_party/blink/public/public_buildflags.h"
[email protected]38a85712013-01-02 22:45:0235#include "third_party/skia/include/core/SkColor.h"
Jacques Newman58690f2c2024-04-30 20:47:0336#include "ui/accessibility/platform/ax_platform.h"
Ionel Popescu2649f2f2020-01-10 10:06:5937#include "ui/base/ui_base_features.h"
[email protected]93623c5d2009-12-10 21:40:3238
[email protected]dbf75012013-09-24 06:37:2539#if defined(TOOLKIT_VIEWS)
40#include "ui/views/controls/textfield/textfield.h"
[email protected]7bd129f2012-11-30 02:06:0141#endif
42
Tom Anderson4ee83742022-07-14 20:58:4143#if defined(USE_AURA) && BUILDFLAG(IS_LINUX)
[email protected]96514e72013-09-25 20:42:2444#include "chrome/browser/themes/theme_service.h"
45#include "chrome/browser/themes/theme_service_factory.h"
Tom Anderson4ee83742022-07-14 20:58:4146#include "ui/linux/linux_ui.h"
[email protected]96514e72013-09-25 20:42:2447#endif
48
guidouca055a542016-08-11 11:15:5349namespace {
50
51// Parses a string |range| with a port range in the form "<min>-<max>".
52// If |range| is not in the correct format or contains an invalid range, zero
53// is written to |min_port| and |max_port|.
54// TODO(guidou): Consider replacing with remoting/protocol/port_range.cc
55void ParsePortRange(const std::string& range,
56 uint16_t* min_port,
57 uint16_t* max_port) {
58 *min_port = 0;
59 *max_port = 0;
60
61 if (range.empty())
62 return;
63
64 size_t separator_index = range.find('-');
65 if (separator_index == std::string::npos)
66 return;
67
68 std::string min_port_string, max_port_string;
69 base::TrimWhitespaceASCII(range.substr(0, separator_index), base::TRIM_ALL,
70 &min_port_string);
71 base::TrimWhitespaceASCII(range.substr(separator_index + 1), base::TRIM_ALL,
72 &max_port_string);
73 unsigned min_port_uint, max_port_uint;
74 if (!base::StringToUint(min_port_string, &min_port_uint) ||
75 !base::StringToUint(max_port_string, &max_port_uint)) {
76 return;
77 }
78 if (min_port_uint == 0 || min_port_uint > max_port_uint ||
79 max_port_uint > UINT16_MAX) {
80 return;
81 }
82
83 *min_port = static_cast<uint16_t>(min_port_uint);
84 *max_port = static_cast<uint16_t>(max_port_uint);
85}
86
Qingsi Wang337dc7a2019-10-29 01:02:0487// Extracts the string representation of URLs allowed for local IP exposure.
88std::vector<std::string> GetLocalIpsAllowedUrls(
Roland Bock04381a572022-08-19 19:54:1389 const base::Value::List& allowed_urls) {
Qingsi Wang337dc7a2019-10-29 01:02:0490 std::vector<std::string> ret;
Roland Bock04381a572022-08-19 19:54:1391 for (const auto& url : allowed_urls)
92 ret.push_back(url.GetString());
Qingsi Wang337dc7a2019-10-29 01:02:0493 return ret;
94}
95
Yves Arrouye39b8ef62021-02-23 00:39:2996std::string GetLanguageListForProfile(Profile* profile,
97 const std::string& language_list) {
98 if (profile->IsOffTheRecord()) {
99 // In incognito mode return only the first language.
100 return language::GetFirstLanguage(language_list);
101 }
Yves Arrouye39b8ef62021-02-23 00:39:29102 return language_list;
103}
104
guidouca055a542016-08-11 11:15:53105} // namespace
106
[email protected]93623c5d2009-12-10 21:40:32107namespace renderer_preferences_util {
108
Mario Sanchez Prada0bd8b8c2020-10-21 17:49:23109void UpdateFromSystemSettings(blink::RendererPreferences* prefs,
Kenichi Ishibashif522ac42018-07-12 23:16:46110 Profile* profile) {
[email protected]f57f69012012-06-13 02:52:21111 const PrefService* pref_service = profile->GetPrefs();
Yves Arrouye39b8ef62021-02-23 00:39:29112 prefs->accept_languages = GetLanguageListForProfile(
113 profile, pref_service->GetString(language::prefs::kAcceptLanguages));
[email protected]f57f69012012-06-13 02:52:21114 prefs->enable_referrers = pref_service->GetBoolean(prefs::kEnableReferrers);
[email protected]498bc512012-09-21 21:50:08115 prefs->enable_do_not_track =
Fiona Macintoshafa01d22023-09-18 12:55:49116 TrackingProtectionSettingsFactory::GetForProfile(profile)
117 ->IsDoNotTrackEnabled();
Xiaohan Wang2ec4a6832017-11-15 00:55:51118 prefs->enable_encrypted_media =
119 pref_service->GetBoolean(prefs::kEnableEncryptedMedia);
Friedrich Horschig91c02102025-01-10 13:45:58120
121#if BUILDFLAG(IS_ANDROID)
122 prefs->uses_platform_autofill = pref_service->GetBoolean(
123 autofill::prefs::kAutofillUsingVirtualViewStructure);
124#else // if !BUILDFLAG(IS_ANDROID)
Dominic Mazzoni554089e2020-07-31 22:12:04125 prefs->caret_browsing_enabled =
126 pref_service->GetBoolean(prefs::kCaretBrowsingEnabled);
Jacques Newman58690f2c2024-04-30 20:47:03127 ui::AXPlatform::GetInstance().SetCaretBrowsingState(
Dominic Mazzoni554089e2020-07-31 22:12:04128 prefs->caret_browsing_enabled);
129#endif
130
Guido Urdanetad8bfc6f92024-12-11 14:01:22131 prefs->webrtc_ip_handling_policy = blink::ToWebRTCIPHandlingPolicy(
132 pref_service->GetString(prefs::kWebRTCIPHandlingPolicy));
Guido Urdanetafdaf45e2024-12-17 20:47:29133
134 const base::Value::List& webrtc_ip_handling_urls =
135 pref_service->GetList(prefs::kWebRTCIPHandlingUrl);
136 for (const base::Value& entry : webrtc_ip_handling_urls) {
137 const base::Value::Dict& dict = entry.GetDict();
138 const std::string* url = dict.FindString("url");
139 if (!url) {
140 DVLOG(1) << "Malformed WebRtcIPHandlingUrl entry: Missing 'url' value.";
141 continue;
142 }
143 ContentSettingsPattern pattern = ContentSettingsPattern::FromString(*url);
144 if (!pattern.IsValid()) {
145 DVLOG(1)
146 << "Malformed WebRtcIPHandlingUrl entry: Invalid pattern found: '"
147 << *url << "'.";
148 continue;
149 }
150 const std::string* handling = dict.FindString("handling");
151 if (!handling) {
152 DVLOG(1)
153 << "Malformed WebRtcIPHandlingUrl entry: Missing 'handling' value.";
154 continue;
155 }
156 prefs->webrtc_ip_handling_urls.push_back(
157 {pattern, blink::ToWebRTCIPHandlingPolicy(*handling)});
158 }
159
guidouca055a542016-08-11 11:15:53160 std::string webrtc_udp_port_range =
161 pref_service->GetString(prefs::kWebRTCUDPPortRange);
162 ParsePortRange(webrtc_udp_port_range, &prefs->webrtc_udp_min_port,
163 &prefs->webrtc_udp_max_port);
thestig2c8c41e2014-10-23 02:56:50164
Roland Bock04381a572022-08-19 19:54:13165 const base::Value::List& allowed_urls =
Roland Bock6edc4b72022-09-12 17:33:01166 pref_service->GetList(prefs::kWebRtcLocalIpsAllowedUrls);
Qingsi Wang337dc7a2019-10-29 01:02:04167 prefs->webrtc_local_ips_allowed_urls = GetLocalIpsAllowedUrls(allowed_urls);
Christopher Cameronadf36612019-05-14 17:19:42168#if defined(USE_AURA)
[email protected]38a85712013-01-02 22:45:02169 prefs->focus_ring_color = SkColorSetRGB(0x4D, 0x90, 0xFE);
Eric Willigers02f80aa2022-04-29 01:29:32170#if BUILDFLAG(IS_CHROMEOS)
[email protected]38a85712013-01-02 22:45:02171 // This color is 0x544d90fe modulated with 0xffffff.
172 prefs->active_selection_bg_color = SkColorSetRGB(0xCB, 0xE4, 0xFA);
173 prefs->active_selection_fg_color = SK_ColorBLACK;
174 prefs->inactive_selection_bg_color = SkColorSetRGB(0xEA, 0xEA, 0xEA);
175 prefs->inactive_selection_fg_color = SK_ColorBLACK;
[email protected]f2cb988d22013-07-01 22:07:04176#endif
[email protected]7bd129f2012-11-30 02:06:01177#endif
178
[email protected]dbf75012013-09-24 06:37:25179#if defined(TOOLKIT_VIEWS)
tzikd450061e2017-11-28 17:14:50180 prefs->caret_blink_interval = views::Textfield::GetCaretBlinkInterval();
[email protected]dbf75012013-09-24 06:37:25181#endif
182
Tom Anderson4ee83742022-07-14 20:58:41183#if defined(USE_AURA) && BUILDFLAG(IS_LINUX)
Tom Andersoncd61f3b22022-09-14 16:27:49184 auto* linux_ui_theme = ui::LinuxUiTheme::GetForProfile(profile);
185 if (linux_ui_theme) {
[email protected]448d7dc2014-05-13 03:22:55186 if (ThemeServiceFactory::GetForProfile(profile)->UsingSystemTheme()) {
Tom Anderson38f74732023-02-28 23:21:40187 linux_ui_theme->GetFocusRingColor(&prefs->focus_ring_color);
188 linux_ui_theme->GetActiveSelectionBgColor(
189 &prefs->active_selection_bg_color);
190 linux_ui_theme->GetActiveSelectionFgColor(
191 &prefs->active_selection_fg_color);
192 linux_ui_theme->GetInactiveSelectionBgColor(
193 &prefs->inactive_selection_bg_color);
194 linux_ui_theme->GetInactiveSelectionFgColor(
195 &prefs->inactive_selection_fg_color);
[email protected]96514e72013-09-25 20:42:24196 }
Tom Andersoncd61f3b22022-09-14 16:27:49197 }
[email protected]96514e72013-09-25 20:42:24198
199 // If we have a linux_ui object, set the caret blink interval regardless of
200 // whether we're in native theme mode.
Tom Andersoncd61f3b22022-09-14 16:27:49201 if (auto* linux_ui = ui::LinuxUi::instance())
[email protected]96514e72013-09-25 20:42:24202 prefs->caret_blink_interval = linux_ui->GetCursorBlinkInterval();
[email protected]96514e72013-09-25 20:42:24203#endif
204
Xiaohan Wang55ae2c012022-01-20 21:49:11205#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || \
206 BUILDFLAG(IS_WIN)
Sergey Ulanovfd5f85a2019-01-07 21:57:28207 content::UpdateFontRendererPreferencesFromSystemSettings(prefs);
[email protected]f57f69012012-06-13 02:52:21208#endif
[email protected]0509bc82013-09-20 20:42:59209
Xiaohan Wang55ae2c012022-01-20 21:49:11210#if !BUILDFLAG(IS_MAC)
[email protected]0509bc82013-09-20 20:42:59211 prefs->plugin_fullscreen_allowed =
212 pref_service->GetBoolean(prefs::kFullscreenAllowed);
213#endif
John Abd-El-Malek7d032b372019-05-07 18:01:31214
215 PrefService* local_state = g_browser_process->local_state();
216 if (local_state) {
217 prefs->allow_cross_origin_auth_prompt =
218 local_state->GetBoolean(prefs::kAllowCrossOriginAuthPrompt);
Adam Rice3d178a82021-04-12 13:49:53219
220 prefs->explicitly_allowed_network_ports =
221 ConvertExplicitlyAllowedNetworkPortsPref(local_state);
John Abd-El-Malek7d032b372019-05-07 18:01:31222 }
Ionel Popescu2649f2f2020-01-10 10:06:59223
Xiaohan Wang55ae2c012022-01-20 21:49:11224#if BUILDFLAG(IS_MAC)
Mason Freed972aab32021-05-21 19:11:46225 prefs->focus_ring_color = SkColorSetRGB(0x00, 0x5F, 0xCC);
Mason Freeda32930f2020-03-05 16:57:24226#else
Mason Freed972aab32021-05-21 19:11:46227 prefs->focus_ring_color = SkColorSetRGB(0x10, 0x10, 0x10);
Mason Freeda32930f2020-03-05 16:57:24228#endif
Topi Lassila782cdce2025-05-12 09:16:08229
230 prefs->view_source_line_wrap_enabled =
231 pref_service->GetBoolean(prefs::kViewSourceLineWrappingEnabled);
[email protected]93623c5d2009-12-10 21:40:32232}
233
[email protected]0509bc82013-09-20 20:42:59234} // namespace renderer_preferences_util