forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreferences.cs
213 lines (193 loc) · 8.89 KB
/
Preferences.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// <copyright file="Preferences.cs" company="Selenium Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// </copyright>
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text.Json.Nodes;
#nullable enable
namespace OpenQA.Selenium.Firefox
{
/// <summary>
/// Represents the preferences used by a profile in Firefox.
/// </summary>
internal class Preferences
{
private readonly Dictionary<string, string> preferences = new Dictionary<string, string>();
private readonly HashSet<string> immutablePreferences = new HashSet<string>();
/// <summary>
/// Initializes a new instance of the <see cref="Preferences"/> class.
/// </summary>
/// <param name="defaultImmutablePreferences">A set of preferences that cannot be modified once set.</param>
/// <param name="defaultPreferences">A set of default preferences.</param>
public Preferences(JsonObject? defaultImmutablePreferences, JsonObject? defaultPreferences)
{
if (defaultImmutablePreferences != null)
{
foreach (KeyValuePair<string, JsonNode?> pref in defaultImmutablePreferences)
{
this.SetPreferenceValue(pref.Key, pref.Value);
this.immutablePreferences.Add(pref.Key);
}
}
if (defaultPreferences != null)
{
foreach (KeyValuePair<string, JsonNode?> pref in defaultPreferences)
{
this.SetPreferenceValue(pref.Key, pref.Value);
}
}
}
/// <summary>
/// Sets a preference.
/// </summary>
/// <param name="key">The name of the preference to set.</param>
/// <param name="value">A <see cref="string"/> value give the preference.</param>
/// <remarks>If the preference already exists in the currently-set list of preferences,
/// the value will be updated.</remarks>
/// <exception cref="ArgumentNullException">If <paramref name="key"/> is <see langword="null"/>.</exception>
internal void SetPreference(string key, string value)
{
this.SetPreferenceValue(key, value);
}
/// <summary>
/// Sets a preference.
/// </summary>
/// <param name="key">The name of the preference to set.</param>
/// <param name="value">A <see cref="int"/> value give the preference.</param>
/// <remarks>If the preference already exists in the currently-set list of preferences,
/// the value will be updated.</remarks>
/// <exception cref="ArgumentNullException">If <paramref name="key"/> is <see langword="null"/>.</exception>
internal void SetPreference(string key, int value)
{
this.SetPreferenceValue(key, value);
}
/// <summary>
/// Sets a preference.
/// </summary>
/// <param name="key">The name of the preference to set.</param>
/// <param name="value">A <see cref="bool"/> value give the preference.</param>
/// <remarks>If the preference already exists in the currently-set list of preferences,
/// the value will be updated.</remarks>
/// <exception cref="ArgumentNullException">If <paramref name="key"/> is <see langword="null"/>.</exception>
internal void SetPreference(string key, bool value)
{
this.SetPreferenceValue(key, value);
}
/// <summary>
/// Gets a preference from the list of preferences.
/// </summary>
/// <param name="preferenceName">The name of the preference to retrieve.</param>
/// <returns>The value of the preference, or an empty string if the preference is not set.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="preferenceName"/> is <see langword="null"/>.</exception>
internal string GetPreference(string preferenceName)
{
if (this.preferences.ContainsKey(preferenceName))
{
return this.preferences[preferenceName];
}
return string.Empty;
}
/// <summary>
/// Appends this set of preferences to the specified set of preferences.
/// </summary>
/// <param name="preferencesToAdd">A dictionary containing the preferences to which to
/// append these values.</param>
/// <remarks>If the preference already exists in <paramref name="preferencesToAdd"/>,
/// the value will be updated.</remarks>
internal void AppendPreferences(Dictionary<string, string> preferencesToAdd)
{
// This allows the user to add additional preferences, or update ones that already
// exist.
foreach (KeyValuePair<string, string> preferenceToAdd in preferencesToAdd)
{
if (this.IsSettablePreference(preferenceToAdd.Key))
{
this.preferences[preferenceToAdd.Key] = preferenceToAdd.Value;
}
}
}
/// <summary>
/// Writes the preferences to a file.
/// </summary>
/// <param name="filePath">The full path to the file to be written.</param>
internal void WriteToFile(string filePath)
{
using (TextWriter writer = File.CreateText(filePath))
{
foreach (KeyValuePair<string, string> preference in this.preferences)
{
string escapedValue = preference.Value.Replace(@"\", @"\\");
writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "user_pref(\"{0}\", {1});", preference.Key, escapedValue));
}
}
}
private static bool IsWrappedAsString(string value)
{
// Assume we a string is stringified (i.e. wrapped in " ") when
// the first character == " and the last character == "
return value.StartsWith("\"", StringComparison.OrdinalIgnoreCase) && value.EndsWith("\"", StringComparison.OrdinalIgnoreCase);
}
private bool IsSettablePreference(string preferenceName)
{
return !this.immutablePreferences.Contains(preferenceName);
}
private void SetPreferenceValue(string key, JsonNode? value)
{
if (!this.IsSettablePreference(key))
{
string message = string.Format(CultureInfo.InvariantCulture, "Preference {0} may not be overridden: frozen value={1}, requested value={2}", key, this.preferences[key], value?.ToString());
throw new ArgumentException(message);
}
if (value is JsonValue jValue)
{
if (jValue.TryGetValue(out string? stringValue))
{
if (IsWrappedAsString(stringValue))
{
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Preference values must be plain strings: {0}: {1}", key, value));
}
this.preferences[key] = string.Format(CultureInfo.InvariantCulture, "\"{0}\"", value);
return;
}
if (jValue.TryGetValue(out bool boolValue))
{
this.preferences[key] = boolValue ? "true" : "false";
return;
}
if (jValue.TryGetValue(out int intValue))
{
this.preferences[key] = intValue.ToString(CultureInfo.InvariantCulture);
return;
}
if (jValue.TryGetValue(out long longValue))
{
this.preferences[key] = longValue.ToString(CultureInfo.InvariantCulture);
return;
}
if (jValue.TryGetValue(out double doubleValue))
{
this.preferences[key] = doubleValue.ToString(CultureInfo.InvariantCulture);
return;
}
}
throw new WebDriverException("Value must be string, int or boolean");
}
}
}