blob: 2ed4404e2fcd9b90ca0d187cf8678e9135af944f [file] [log] [blame]
[email protected]37cc8042012-02-27 19:32:581// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[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
[email protected]516b2c732014-07-25 00:10:3112#include "base/macros.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"
Yuta Hijikata235fc62b2020-12-08 03:48:3216#include "build/chromeos_buildflags.h"
John Abd-El-Malek7d032b372019-05-07 18:01:3117#include "chrome/browser/browser_process.h"
Adam Rice3d178a82021-04-12 13:49:5318#include "chrome/browser/net/convert_explicitly_allowed_network_ports_pref.h"
Yves Arrouye39b8ef62021-02-23 00:39:2919#if BUILDFLAG(IS_CHROMEOS_ASH)
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"
Dominic Mazzoni554089e2020-07-31 22:12:0423#include "chrome/browser/profiles/profile_manager.h"
[email protected]0a8db0d2011-04-13 15:15:4024#include "chrome/common/pref_names.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"
Dominic Mazzoni554089e2020-07-31 22:12:0428#include "content/public/browser/browser_accessibility_state.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"
Blink Reformata30d4232018-04-07 15:31:0633#include "third_party/blink/public/public_buildflags.h"
[email protected]38a85712013-01-02 22:45:0234#include "third_party/skia/include/core/SkColor.h"
Ionel Popescu2649f2f2020-01-10 10:06:5935#include "ui/base/ui_base_features.h"
[email protected]93623c5d2009-12-10 21:40:3236
[email protected]dbf75012013-09-24 06:37:2537#if defined(TOOLKIT_VIEWS)
38#include "ui/views/controls/textfield/textfield.h"
[email protected]7bd129f2012-11-30 02:06:0139#endif
40
Avi Drissman2e458df2020-07-29 16:24:3141#if defined(OS_MAC)
ellyjonesd0fd7b12016-08-26 18:26:3642#include "ui/base/cocoa/defaults_utils.h"
43#endif
44
Yuta Hijikata235fc62b2020-12-08 03:48:3245#if defined(USE_AURA) && (defined(OS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS))
[email protected]96514e72013-09-25 20:42:2446#include "chrome/browser/themes/theme_service.h"
47#include "chrome/browser/themes/theme_service_factory.h"
48#include "ui/views/linux_ui/linux_ui.h"
49#endif
50
guidouca055a542016-08-11 11:15:5351namespace {
52
53// Parses a string |range| with a port range in the form "<min>-<max>".
54// If |range| is not in the correct format or contains an invalid range, zero
55// is written to |min_port| and |max_port|.
56// TODO(guidou): Consider replacing with remoting/protocol/port_range.cc
57void ParsePortRange(const std::string& range,
58 uint16_t* min_port,
59 uint16_t* max_port) {
60 *min_port = 0;
61 *max_port = 0;
62
63 if (range.empty())
64 return;
65
66 size_t separator_index = range.find('-');
67 if (separator_index == std::string::npos)
68 return;
69
70 std::string min_port_string, max_port_string;
71 base::TrimWhitespaceASCII(range.substr(0, separator_index), base::TRIM_ALL,
72 &min_port_string);
73 base::TrimWhitespaceASCII(range.substr(separator_index + 1), base::TRIM_ALL,
74 &max_port_string);
75 unsigned min_port_uint, max_port_uint;
76 if (!base::StringToUint(min_port_string, &min_port_uint) ||
77 !base::StringToUint(max_port_string, &max_port_uint)) {
78 return;
79 }
80 if (min_port_uint == 0 || min_port_uint > max_port_uint ||
81 max_port_uint > UINT16_MAX) {
82 return;
83 }
84
85 *min_port = static_cast<uint16_t>(min_port_uint);
86 *max_port = static_cast<uint16_t>(max_port_uint);
87}
88
Qingsi Wang337dc7a2019-10-29 01:02:0489// Extracts the string representation of URLs allowed for local IP exposure.
90std::vector<std::string> GetLocalIpsAllowedUrls(
91 const base::ListValue* allowed_urls) {
92 std::vector<std::string> ret;
93 if (allowed_urls) {
94 const auto& urls = allowed_urls->GetList();
95 for (const auto& url : urls)
96 ret.push_back(url.GetString());
97 }
98 return ret;
99}
100
Yves Arrouye39b8ef62021-02-23 00:39:29101std::string GetLanguageListForProfile(Profile* profile,
102 const std::string& language_list) {
103 if (profile->IsOffTheRecord()) {
104 // In incognito mode return only the first language.
105 return language::GetFirstLanguage(language_list);
106 }
107#if BUILDFLAG(IS_CHROMEOS_ASH)
108 // On Chrome OS, if in demo mode, add the demo mode private language list.
109 if (chromeos::DemoSession::IsDeviceInDemoMode()) {
110 return language_list + "," +
111 chromeos::DemoSession::GetAdditionalLanguageList();
112 }
113#endif
114 return language_list;
115}
116
guidouca055a542016-08-11 11:15:53117} // namespace
118
[email protected]93623c5d2009-12-10 21:40:32119namespace renderer_preferences_util {
120
Mario Sanchez Prada0bd8b8c2020-10-21 17:49:23121void UpdateFromSystemSettings(blink::RendererPreferences* prefs,
Kenichi Ishibashif522ac42018-07-12 23:16:46122 Profile* profile) {
[email protected]f57f69012012-06-13 02:52:21123 const PrefService* pref_service = profile->GetPrefs();
Yves Arrouye39b8ef62021-02-23 00:39:29124 prefs->accept_languages = GetLanguageListForProfile(
125 profile, pref_service->GetString(language::prefs::kAcceptLanguages));
[email protected]f57f69012012-06-13 02:52:21126 prefs->enable_referrers = pref_service->GetBoolean(prefs::kEnableReferrers);
[email protected]498bc512012-09-21 21:50:08127 prefs->enable_do_not_track =
128 pref_service->GetBoolean(prefs::kEnableDoNotTrack);
Xiaohan Wang2ec4a6832017-11-15 00:55:51129 prefs->enable_encrypted_media =
130 pref_service->GetBoolean(prefs::kEnableEncryptedMedia);
guoweisc537ba22015-11-06 21:20:31131 prefs->webrtc_ip_handling_policy = std::string();
Dominic Mazzoni554089e2020-07-31 22:12:04132#if !defined(OS_ANDROID)
133 prefs->caret_browsing_enabled =
134 pref_service->GetBoolean(prefs::kCaretBrowsingEnabled);
135 content::BrowserAccessibilityState::GetInstance()->SetCaretBrowsingState(
136 prefs->caret_browsing_enabled);
137#endif
138
guoweisc537ba22015-11-06 21:20:31139 if (prefs->webrtc_ip_handling_policy.empty()) {
140 prefs->webrtc_ip_handling_policy =
141 pref_service->GetString(prefs::kWebRTCIPHandlingPolicy);
142 }
guidouca055a542016-08-11 11:15:53143 std::string webrtc_udp_port_range =
144 pref_service->GetString(prefs::kWebRTCUDPPortRange);
145 ParsePortRange(webrtc_udp_port_range, &prefs->webrtc_udp_min_port,
146 &prefs->webrtc_udp_max_port);
thestig2c8c41e2014-10-23 02:56:50147
Qingsi Wang337dc7a2019-10-29 01:02:04148 const base::ListValue* allowed_urls =
149 pref_service->GetList(prefs::kWebRtcLocalIpsAllowedUrls);
150 prefs->webrtc_local_ips_allowed_urls = GetLocalIpsAllowedUrls(allowed_urls);
Guido Urdaneta7f9f9df2020-09-29 15:19:17151 prefs->webrtc_allow_legacy_tls_protocols =
152 pref_service->GetBoolean(prefs::kWebRTCAllowLegacyTLSProtocols);
Christopher Cameronadf36612019-05-14 17:19:42153#if defined(USE_AURA)
[email protected]38a85712013-01-02 22:45:02154 prefs->focus_ring_color = SkColorSetRGB(0x4D, 0x90, 0xFE);
Darin Fisher2e8ce852020-12-16 21:04:32155#if BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS)
[email protected]38a85712013-01-02 22:45:02156 // This color is 0x544d90fe modulated with 0xffffff.
157 prefs->active_selection_bg_color = SkColorSetRGB(0xCB, 0xE4, 0xFA);
158 prefs->active_selection_fg_color = SK_ColorBLACK;
159 prefs->inactive_selection_bg_color = SkColorSetRGB(0xEA, 0xEA, 0xEA);
160 prefs->inactive_selection_fg_color = SK_ColorBLACK;
[email protected]f2cb988d22013-07-01 22:07:04161#endif
[email protected]7bd129f2012-11-30 02:06:01162#endif
163
[email protected]dbf75012013-09-24 06:37:25164#if defined(TOOLKIT_VIEWS)
tzikd450061e2017-11-28 17:14:50165 prefs->caret_blink_interval = views::Textfield::GetCaretBlinkInterval();
[email protected]dbf75012013-09-24 06:37:25166#endif
167
Avi Drissman2e458df2020-07-29 16:24:31168#if defined(OS_MAC)
ellyjonesd0fd7b12016-08-26 18:26:36169 base::TimeDelta interval;
170 if (ui::TextInsertionCaretBlinkPeriod(&interval))
tzikd450061e2017-11-28 17:14:50171 prefs->caret_blink_interval = interval;
ellyjonesd0fd7b12016-08-26 18:26:36172#endif
173
Yuta Hijikata235fc62b2020-12-08 03:48:32174#if defined(USE_AURA) && (defined(OS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS))
[email protected]96514e72013-09-25 20:42:24175 views::LinuxUI* linux_ui = views::LinuxUI::instance();
176 if (linux_ui) {
[email protected]448d7dc2014-05-13 03:22:55177 if (ThemeServiceFactory::GetForProfile(profile)->UsingSystemTheme()) {
[email protected]96514e72013-09-25 20:42:24178 prefs->focus_ring_color = linux_ui->GetFocusRingColor();
[email protected]96514e72013-09-25 20:42:24179 prefs->active_selection_bg_color = linux_ui->GetActiveSelectionBgColor();
180 prefs->active_selection_fg_color = linux_ui->GetActiveSelectionFgColor();
181 prefs->inactive_selection_bg_color =
182 linux_ui->GetInactiveSelectionBgColor();
183 prefs->inactive_selection_fg_color =
184 linux_ui->GetInactiveSelectionFgColor();
185 }
186
187 // If we have a linux_ui object, set the caret blink interval regardless of
188 // whether we're in native theme mode.
189 prefs->caret_blink_interval = linux_ui->GetCursorBlinkInterval();
190 }
191#endif
192
Sean McAllisterdf807712020-08-13 23:19:09193#if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_ANDROID) || \
194 defined(OS_WIN)
Sergey Ulanovfd5f85a2019-01-07 21:57:28195 content::UpdateFontRendererPreferencesFromSystemSettings(prefs);
[email protected]f57f69012012-06-13 02:52:21196#endif
[email protected]0509bc82013-09-20 20:42:59197
Avi Drissman2e458df2020-07-29 16:24:31198#if !defined(OS_MAC)
[email protected]0509bc82013-09-20 20:42:59199 prefs->plugin_fullscreen_allowed =
200 pref_service->GetBoolean(prefs::kFullscreenAllowed);
201#endif
John Abd-El-Malek7d032b372019-05-07 18:01:31202
203 PrefService* local_state = g_browser_process->local_state();
204 if (local_state) {
205 prefs->allow_cross_origin_auth_prompt =
206 local_state->GetBoolean(prefs::kAllowCrossOriginAuthPrompt);
Adam Rice3d178a82021-04-12 13:49:53207
208 prefs->explicitly_allowed_network_ports =
209 ConvertExplicitlyAllowedNetworkPortsPref(local_state);
John Abd-El-Malek7d032b372019-05-07 18:01:31210 }
Ionel Popescu2649f2f2020-01-10 10:06:59211
212 if (::features::IsFormControlsRefreshEnabled()) {
Avi Drissman2e458df2020-07-29 16:24:31213#if defined(OS_MAC)
Mason Freede31aceb2020-03-09 17:04:46214 prefs->focus_ring_color = SkColorSetRGB(0x00, 0x5F, 0xCC);
Mason Freeda32930f2020-03-05 16:57:24215#else
Ionel Popescu2649f2f2020-01-10 10:06:59216 prefs->focus_ring_color = SkColorSetRGB(0x10, 0x10, 0x10);
Mason Freeda32930f2020-03-05 16:57:24217#endif
Ionel Popescu2649f2f2020-01-10 10:06:59218 }
[email protected]93623c5d2009-12-10 21:40:32219}
220
[email protected]0509bc82013-09-20 20:42:59221} // namespace renderer_preferences_util