blob: 0162bd7c17b752889bdcd7257bc2085c78af83c9 [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
guidouca055a542016-08-11 11:15:5312#include "base/strings/string_number_conversions.h"
13#include "base/strings/string_util.h"
avib896c712015-12-26 02:10:4314#include "build/build_config.h"
Yuta Hijikata235fc62b2020-12-08 03:48:3215#include "build/chromeos_buildflags.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"
Yves Arrouye39b8ef62021-02-23 00:39:2918#if BUILDFLAG(IS_CHROMEOS_ASH)
Henrique Ferreiro606669a2021-02-24 13:36:5519#include "chrome/browser/ash/login/demo_mode/demo_session.h"
Yves Arrouye39b8ef62021-02-23 00:39:2920#endif
[email protected]8ecad5e2010-12-02 21:18:3321#include "chrome/browser/profiles/profile.h"
Dominic Mazzoni554089e2020-07-31 22:12:0422#include "chrome/browser/profiles/profile_manager.h"
[email protected]0a8db0d2011-04-13 15:15:4023#include "chrome/common/pref_names.h"
Trevor Perrierfa7339d2020-09-11 00:42:4024#include "components/language/core/browser/language_prefs.h"
Alexandre Frechette572755b2019-02-13 22:30:2025#include "components/language/core/browser/pref_names.h"
brettwb1fc1b82016-02-02 00:19:0826#include "components/prefs/pref_service.h"
Dominic Mazzoni554089e2020-07-31 22:12:0427#include "content/public/browser/browser_accessibility_state.h"
Ian Vollick0c34a072019-02-15 03:40:2728#include "content/public/browser/renderer_preferences_util.h"
Scott Violeta35f9a42018-03-22 22:00:4429#include "media/media_buildflags.h"
Antonio Gomesb5bf548f2019-09-12 17:40:1530#include "third_party/blink/public/common/peerconnection/webrtc_ip_handling_policy.h"
Mario Sanchez Prada0bd8b8c2020-10-21 17:49:2331#include "third_party/blink/public/common/renderer_preferences/renderer_preferences.h"
Blink Reformata30d4232018-04-07 15:31:0632#include "third_party/blink/public/public_buildflags.h"
[email protected]38a85712013-01-02 22:45:0233#include "third_party/skia/include/core/SkColor.h"
Ionel Popescu2649f2f2020-01-10 10:06:5934#include "ui/base/ui_base_features.h"
[email protected]93623c5d2009-12-10 21:40:3235
[email protected]dbf75012013-09-24 06:37:2536#if defined(TOOLKIT_VIEWS)
37#include "ui/views/controls/textfield/textfield.h"
[email protected]7bd129f2012-11-30 02:06:0138#endif
39
Xiaohan Wang55ae2c012022-01-20 21:49:1140#if BUILDFLAG(IS_MAC)
ellyjonesd0fd7b12016-08-26 18:26:3641#include "ui/base/cocoa/defaults_utils.h"
42#endif
43
Xiaohan Wang55ae2c012022-01-20 21:49:1144#if defined(USE_AURA) && (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS))
[email protected]96514e72013-09-25 20:42:2445#include "chrome/browser/themes/theme_service.h"
46#include "chrome/browser/themes/theme_service_factory.h"
47#include "ui/views/linux_ui/linux_ui.h"
48#endif
49
guidouca055a542016-08-11 11:15:5350namespace {
51
52// Parses a string |range| with a port range in the form "<min>-<max>".
53// If |range| is not in the correct format or contains an invalid range, zero
54// is written to |min_port| and |max_port|.
55// TODO(guidou): Consider replacing with remoting/protocol/port_range.cc
56void ParsePortRange(const std::string& range,
57 uint16_t* min_port,
58 uint16_t* max_port) {
59 *min_port = 0;
60 *max_port = 0;
61
62 if (range.empty())
63 return;
64
65 size_t separator_index = range.find('-');
66 if (separator_index == std::string::npos)
67 return;
68
69 std::string min_port_string, max_port_string;
70 base::TrimWhitespaceASCII(range.substr(0, separator_index), base::TRIM_ALL,
71 &min_port_string);
72 base::TrimWhitespaceASCII(range.substr(separator_index + 1), base::TRIM_ALL,
73 &max_port_string);
74 unsigned min_port_uint, max_port_uint;
75 if (!base::StringToUint(min_port_string, &min_port_uint) ||
76 !base::StringToUint(max_port_string, &max_port_uint)) {
77 return;
78 }
79 if (min_port_uint == 0 || min_port_uint > max_port_uint ||
80 max_port_uint > UINT16_MAX) {
81 return;
82 }
83
84 *min_port = static_cast<uint16_t>(min_port_uint);
85 *max_port = static_cast<uint16_t>(max_port_uint);
86}
87
Qingsi Wang337dc7a2019-10-29 01:02:0488// Extracts the string representation of URLs allowed for local IP exposure.
89std::vector<std::string> GetLocalIpsAllowedUrls(
Austin Sullivan7d219a252021-12-20 14:55:3190 const base::Value* allowed_urls) {
Qingsi Wang337dc7a2019-10-29 01:02:0491 std::vector<std::string> ret;
92 if (allowed_urls) {
93 const auto& urls = allowed_urls->GetList();
94 for (const auto& url : urls)
95 ret.push_back(url.GetString());
96 }
97 return ret;
98}
99
Yves Arrouye39b8ef62021-02-23 00:39:29100std::string GetLanguageListForProfile(Profile* profile,
101 const std::string& language_list) {
102 if (profile->IsOffTheRecord()) {
103 // In incognito mode return only the first language.
104 return language::GetFirstLanguage(language_list);
105 }
106#if BUILDFLAG(IS_CHROMEOS_ASH)
107 // On Chrome OS, if in demo mode, add the demo mode private language list.
Henrique Ferreirod71de902021-05-10 18:57:11108 if (ash::DemoSession::IsDeviceInDemoMode()) {
109 return language_list + "," + ash::DemoSession::GetAdditionalLanguageList();
Yves Arrouye39b8ef62021-02-23 00:39:29110 }
111#endif
112 return language_list;
113}
114
guidouca055a542016-08-11 11:15:53115} // namespace
116
[email protected]93623c5d2009-12-10 21:40:32117namespace renderer_preferences_util {
118
Mario Sanchez Prada0bd8b8c2020-10-21 17:49:23119void UpdateFromSystemSettings(blink::RendererPreferences* prefs,
Kenichi Ishibashif522ac42018-07-12 23:16:46120 Profile* profile) {
[email protected]f57f69012012-06-13 02:52:21121 const PrefService* pref_service = profile->GetPrefs();
Yves Arrouye39b8ef62021-02-23 00:39:29122 prefs->accept_languages = GetLanguageListForProfile(
123 profile, pref_service->GetString(language::prefs::kAcceptLanguages));
[email protected]f57f69012012-06-13 02:52:21124 prefs->enable_referrers = pref_service->GetBoolean(prefs::kEnableReferrers);
[email protected]498bc512012-09-21 21:50:08125 prefs->enable_do_not_track =
126 pref_service->GetBoolean(prefs::kEnableDoNotTrack);
Xiaohan Wang2ec4a6832017-11-15 00:55:51127 prefs->enable_encrypted_media =
128 pref_service->GetBoolean(prefs::kEnableEncryptedMedia);
guoweisc537ba22015-11-06 21:20:31129 prefs->webrtc_ip_handling_policy = std::string();
Xiaohan Wang55ae2c012022-01-20 21:49:11130#if !BUILDFLAG(IS_ANDROID)
Dominic Mazzoni554089e2020-07-31 22:12:04131 prefs->caret_browsing_enabled =
132 pref_service->GetBoolean(prefs::kCaretBrowsingEnabled);
133 content::BrowserAccessibilityState::GetInstance()->SetCaretBrowsingState(
134 prefs->caret_browsing_enabled);
135#endif
136
guoweisc537ba22015-11-06 21:20:31137 if (prefs->webrtc_ip_handling_policy.empty()) {
138 prefs->webrtc_ip_handling_policy =
139 pref_service->GetString(prefs::kWebRTCIPHandlingPolicy);
140 }
guidouca055a542016-08-11 11:15:53141 std::string webrtc_udp_port_range =
142 pref_service->GetString(prefs::kWebRTCUDPPortRange);
143 ParsePortRange(webrtc_udp_port_range, &prefs->webrtc_udp_min_port,
144 &prefs->webrtc_udp_max_port);
thestig2c8c41e2014-10-23 02:56:50145
Austin Sullivan7d219a252021-12-20 14:55:31146 const base::Value* allowed_urls =
Qingsi Wang337dc7a2019-10-29 01:02:04147 pref_service->GetList(prefs::kWebRtcLocalIpsAllowedUrls);
148 prefs->webrtc_local_ips_allowed_urls = GetLocalIpsAllowedUrls(allowed_urls);
Guido Urdaneta7f9f9df2020-09-29 15:19:17149 prefs->webrtc_allow_legacy_tls_protocols =
150 pref_service->GetBoolean(prefs::kWebRTCAllowLegacyTLSProtocols);
Christopher Cameronadf36612019-05-14 17:19:42151#if defined(USE_AURA)
[email protected]38a85712013-01-02 22:45:02152 prefs->focus_ring_color = SkColorSetRGB(0x4D, 0x90, 0xFE);
Darin Fisher2e8ce852020-12-16 21:04:32153#if BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS)
[email protected]38a85712013-01-02 22:45:02154 // This color is 0x544d90fe modulated with 0xffffff.
155 prefs->active_selection_bg_color = SkColorSetRGB(0xCB, 0xE4, 0xFA);
156 prefs->active_selection_fg_color = SK_ColorBLACK;
157 prefs->inactive_selection_bg_color = SkColorSetRGB(0xEA, 0xEA, 0xEA);
158 prefs->inactive_selection_fg_color = SK_ColorBLACK;
[email protected]f2cb988d22013-07-01 22:07:04159#endif
[email protected]7bd129f2012-11-30 02:06:01160#endif
161
[email protected]dbf75012013-09-24 06:37:25162#if defined(TOOLKIT_VIEWS)
tzikd450061e2017-11-28 17:14:50163 prefs->caret_blink_interval = views::Textfield::GetCaretBlinkInterval();
[email protected]dbf75012013-09-24 06:37:25164#endif
165
Xiaohan Wang55ae2c012022-01-20 21:49:11166#if BUILDFLAG(IS_MAC)
ellyjonesd0fd7b12016-08-26 18:26:36167 base::TimeDelta interval;
168 if (ui::TextInsertionCaretBlinkPeriod(&interval))
tzikd450061e2017-11-28 17:14:50169 prefs->caret_blink_interval = interval;
ellyjonesd0fd7b12016-08-26 18:26:36170#endif
171
Xiaohan Wang55ae2c012022-01-20 21:49:11172#if defined(USE_AURA) && (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS))
[email protected]96514e72013-09-25 20:42:24173 views::LinuxUI* linux_ui = views::LinuxUI::instance();
174 if (linux_ui) {
[email protected]448d7dc2014-05-13 03:22:55175 if (ThemeServiceFactory::GetForProfile(profile)->UsingSystemTheme()) {
[email protected]96514e72013-09-25 20:42:24176 prefs->focus_ring_color = linux_ui->GetFocusRingColor();
[email protected]96514e72013-09-25 20:42:24177 prefs->active_selection_bg_color = linux_ui->GetActiveSelectionBgColor();
178 prefs->active_selection_fg_color = linux_ui->GetActiveSelectionFgColor();
179 prefs->inactive_selection_bg_color =
180 linux_ui->GetInactiveSelectionBgColor();
181 prefs->inactive_selection_fg_color =
182 linux_ui->GetInactiveSelectionFgColor();
183 }
184
185 // If we have a linux_ui object, set the caret blink interval regardless of
186 // whether we're in native theme mode.
187 prefs->caret_blink_interval = linux_ui->GetCursorBlinkInterval();
188 }
189#endif
190
Xiaohan Wang55ae2c012022-01-20 21:49:11191#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || \
192 BUILDFLAG(IS_WIN)
Sergey Ulanovfd5f85a2019-01-07 21:57:28193 content::UpdateFontRendererPreferencesFromSystemSettings(prefs);
[email protected]f57f69012012-06-13 02:52:21194#endif
[email protected]0509bc82013-09-20 20:42:59195
Xiaohan Wang55ae2c012022-01-20 21:49:11196#if !BUILDFLAG(IS_MAC)
[email protected]0509bc82013-09-20 20:42:59197 prefs->plugin_fullscreen_allowed =
198 pref_service->GetBoolean(prefs::kFullscreenAllowed);
199#endif
John Abd-El-Malek7d032b372019-05-07 18:01:31200
201 PrefService* local_state = g_browser_process->local_state();
202 if (local_state) {
203 prefs->allow_cross_origin_auth_prompt =
204 local_state->GetBoolean(prefs::kAllowCrossOriginAuthPrompt);
Adam Rice3d178a82021-04-12 13:49:53205
206 prefs->explicitly_allowed_network_ports =
207 ConvertExplicitlyAllowedNetworkPortsPref(local_state);
John Abd-El-Malek7d032b372019-05-07 18:01:31208 }
Ionel Popescu2649f2f2020-01-10 10:06:59209
Xiaohan Wang55ae2c012022-01-20 21:49:11210#if BUILDFLAG(IS_MAC)
Mason Freed972aab32021-05-21 19:11:46211 prefs->focus_ring_color = SkColorSetRGB(0x00, 0x5F, 0xCC);
Mason Freeda32930f2020-03-05 16:57:24212#else
Mason Freed972aab32021-05-21 19:11:46213 prefs->focus_ring_color = SkColorSetRGB(0x10, 0x10, 0x10);
Mason Freeda32930f2020-03-05 16:57:24214#endif
[email protected]93623c5d2009-12-10 21:40:32215}
216
[email protected]0509bc82013-09-20 20:42:59217} // namespace renderer_preferences_util