[M120][Tizen][Onscreen] Fix build errors for TV profile
[platform/framework/web/chromium-efl.git] / chrome / browser / renderer_preferences_util.cc
1 // Copyright 2012 The Chromium Authors
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/renderer_preferences_util.h"
6
7 #include <stdint.h>
8
9 #include <string>
10 #include <vector>
11
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h"
14 #include "build/build_config.h"
15 #include "build/chromeos_buildflags.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/net/convert_explicitly_allowed_network_ports_pref.h"
18 #include "chrome/browser/privacy_sandbox/tracking_protection_settings_factory.h"
19 #if BUILDFLAG(IS_CHROMEOS_ASH)
20 #include "chrome/browser/ash/login/demo_mode/demo_session.h"
21 #endif
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/common/pref_names.h"
24 #include "components/language/core/browser/language_prefs.h"
25 #include "components/language/core/browser/pref_names.h"
26 #include "components/prefs/pref_service.h"
27 #include "components/privacy_sandbox/tracking_protection_settings.h"
28 #include "content/public/browser/browser_accessibility_state.h"
29 #include "content/public/browser/renderer_preferences_util.h"
30 #include "media/media_buildflags.h"
31 #include "third_party/blink/public/common/peerconnection/webrtc_ip_handling_policy.h"
32 #include "third_party/blink/public/common/renderer_preferences/renderer_preferences.h"
33 #include "third_party/blink/public/public_buildflags.h"
34 #include "third_party/skia/include/core/SkColor.h"
35 #include "ui/base/ui_base_features.h"
36
37 #if defined(TOOLKIT_VIEWS)
38 #include "ui/views/controls/textfield/textfield.h"
39 #endif
40
41 #if defined(USE_AURA) && BUILDFLAG(IS_LINUX)
42 #include "chrome/browser/themes/theme_service.h"
43 #include "chrome/browser/themes/theme_service_factory.h"
44 #include "ui/linux/linux_ui.h"
45 #endif
46
47 namespace {
48
49 // Parses a string |range| with a port range in the form "<min>-<max>".
50 // If |range| is not in the correct format or contains an invalid range, zero
51 // is written to |min_port| and |max_port|.
52 // TODO(guidou): Consider replacing with remoting/protocol/port_range.cc
53 void ParsePortRange(const std::string& range,
54                     uint16_t* min_port,
55                     uint16_t* max_port) {
56   *min_port = 0;
57   *max_port = 0;
58
59   if (range.empty())
60     return;
61
62   size_t separator_index = range.find('-');
63   if (separator_index == std::string::npos)
64     return;
65
66   std::string min_port_string, max_port_string;
67   base::TrimWhitespaceASCII(range.substr(0, separator_index), base::TRIM_ALL,
68                             &min_port_string);
69   base::TrimWhitespaceASCII(range.substr(separator_index + 1), base::TRIM_ALL,
70                             &max_port_string);
71   unsigned min_port_uint, max_port_uint;
72   if (!base::StringToUint(min_port_string, &min_port_uint) ||
73       !base::StringToUint(max_port_string, &max_port_uint)) {
74     return;
75   }
76   if (min_port_uint == 0 || min_port_uint > max_port_uint ||
77       max_port_uint > UINT16_MAX) {
78     return;
79   }
80
81   *min_port = static_cast<uint16_t>(min_port_uint);
82   *max_port = static_cast<uint16_t>(max_port_uint);
83 }
84
85 // Extracts the string representation of URLs allowed for local IP exposure.
86 std::vector<std::string> GetLocalIpsAllowedUrls(
87     const base::Value::List& allowed_urls) {
88   std::vector<std::string> ret;
89   for (const auto& url : allowed_urls)
90     ret.push_back(url.GetString());
91   return ret;
92 }
93
94 std::string GetLanguageListForProfile(Profile* profile,
95                                       const std::string& language_list) {
96   if (profile->IsOffTheRecord()) {
97     // In incognito mode return only the first language.
98     return language::GetFirstLanguage(language_list);
99   }
100   return language_list;
101 }
102
103 }  // namespace
104
105 namespace renderer_preferences_util {
106
107 void UpdateFromSystemSettings(blink::RendererPreferences* prefs,
108                               Profile* profile) {
109   const PrefService* pref_service = profile->GetPrefs();
110   prefs->accept_languages = GetLanguageListForProfile(
111       profile, pref_service->GetString(language::prefs::kAcceptLanguages));
112   prefs->enable_referrers = pref_service->GetBoolean(prefs::kEnableReferrers);
113   prefs->enable_do_not_track =
114       TrackingProtectionSettingsFactory::GetForProfile(profile)
115           ->IsDoNotTrackEnabled();
116   prefs->enable_encrypted_media =
117       pref_service->GetBoolean(prefs::kEnableEncryptedMedia);
118   prefs->webrtc_ip_handling_policy = std::string();
119 #if !BUILDFLAG(IS_ANDROID)
120   prefs->caret_browsing_enabled =
121       pref_service->GetBoolean(prefs::kCaretBrowsingEnabled);
122   content::BrowserAccessibilityState::GetInstance()->SetCaretBrowsingState(
123       prefs->caret_browsing_enabled);
124 #endif
125
126   if (prefs->webrtc_ip_handling_policy.empty()) {
127     prefs->webrtc_ip_handling_policy =
128         pref_service->GetString(prefs::kWebRTCIPHandlingPolicy);
129   }
130   std::string webrtc_udp_port_range =
131       pref_service->GetString(prefs::kWebRTCUDPPortRange);
132   ParsePortRange(webrtc_udp_port_range, &prefs->webrtc_udp_min_port,
133                  &prefs->webrtc_udp_max_port);
134
135   const base::Value::List& allowed_urls =
136       pref_service->GetList(prefs::kWebRtcLocalIpsAllowedUrls);
137   prefs->webrtc_local_ips_allowed_urls = GetLocalIpsAllowedUrls(allowed_urls);
138   prefs->webrtc_allow_legacy_tls_protocols =
139       pref_service->GetBoolean(prefs::kWebRTCAllowLegacyTLSProtocols);
140 #if defined(USE_AURA)
141   prefs->focus_ring_color = SkColorSetRGB(0x4D, 0x90, 0xFE);
142 #if BUILDFLAG(IS_CHROMEOS)
143   // This color is 0x544d90fe modulated with 0xffffff.
144   prefs->active_selection_bg_color = SkColorSetRGB(0xCB, 0xE4, 0xFA);
145   prefs->active_selection_fg_color = SK_ColorBLACK;
146   prefs->inactive_selection_bg_color = SkColorSetRGB(0xEA, 0xEA, 0xEA);
147   prefs->inactive_selection_fg_color = SK_ColorBLACK;
148 #endif
149 #endif
150
151 #if defined(TOOLKIT_VIEWS)
152   prefs->caret_blink_interval = views::Textfield::GetCaretBlinkInterval();
153 #endif
154
155 #if defined(USE_AURA) && BUILDFLAG(IS_LINUX)
156   auto* linux_ui_theme = ui::LinuxUiTheme::GetForProfile(profile);
157   if (linux_ui_theme) {
158     if (ThemeServiceFactory::GetForProfile(profile)->UsingSystemTheme()) {
159       linux_ui_theme->GetFocusRingColor(&prefs->focus_ring_color);
160       linux_ui_theme->GetActiveSelectionBgColor(
161           &prefs->active_selection_bg_color);
162       linux_ui_theme->GetActiveSelectionFgColor(
163           &prefs->active_selection_fg_color);
164       linux_ui_theme->GetInactiveSelectionBgColor(
165           &prefs->inactive_selection_bg_color);
166       linux_ui_theme->GetInactiveSelectionFgColor(
167           &prefs->inactive_selection_fg_color);
168     }
169   }
170
171     // If we have a linux_ui object, set the caret blink interval regardless of
172     // whether we're in native theme mode.
173   if (auto* linux_ui = ui::LinuxUi::instance())
174     prefs->caret_blink_interval = linux_ui->GetCursorBlinkInterval();
175 #endif
176
177 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || \
178     BUILDFLAG(IS_WIN)
179   content::UpdateFontRendererPreferencesFromSystemSettings(prefs);
180 #endif
181
182 #if !BUILDFLAG(IS_MAC)
183   prefs->plugin_fullscreen_allowed =
184       pref_service->GetBoolean(prefs::kFullscreenAllowed);
185 #endif
186
187   PrefService* local_state = g_browser_process->local_state();
188   if (local_state) {
189     prefs->allow_cross_origin_auth_prompt =
190         local_state->GetBoolean(prefs::kAllowCrossOriginAuthPrompt);
191
192     prefs->explicitly_allowed_network_ports =
193         ConvertExplicitlyAllowedNetworkPortsPref(local_state);
194   }
195
196 #if BUILDFLAG(IS_MAC)
197   prefs->focus_ring_color = SkColorSetRGB(0x00, 0x5F, 0xCC);
198 #else
199   prefs->focus_ring_color = SkColorSetRGB(0x10, 0x10, 0x10);
200 #endif
201 }
202
203 }  // namespace renderer_preferences_util