Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] Make FirefoxProfile AOT-safe #14742

Merged
merged 13 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions dotnet/src/webdriver/Firefox/FirefoxProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace OpenQA.Selenium.Firefox
{
Expand Down Expand Up @@ -296,22 +296,16 @@ private void UpdateUserPreferences()

private void ReadDefaultPreferences()
{
var jsonSerializerOptions = new JsonSerializerOptions
{
Converters =
{
new ResponseValueJsonConverter()
}
};

using (Stream defaultPrefsStream = ResourceUtilities.GetResourceStream("webdriver_prefs.json", "webdriver_prefs.json"))
{
using (StreamReader reader = new StreamReader(defaultPrefsStream))
{
string defaultPreferences = reader.ReadToEnd();
Dictionary<string, object> deserializedPreferences = JsonSerializer.Deserialize<Dictionary<string, object>>(defaultPreferences, jsonSerializerOptions);
Dictionary<string, object> immutableDefaultPreferences = deserializedPreferences["frozen"] as Dictionary<string, object>;
Dictionary<string, object> editableDefaultPreferences = deserializedPreferences["mutable"] as Dictionary<string, object>;
JsonObject deserializedPreferences = JsonNode.Parse(defaultPreferences) as JsonObject
RenderMichael marked this conversation as resolved.
Show resolved Hide resolved
?? throw new WebDriverException("webdriver_prefs.json was not a JSON object");

JsonObject immutableDefaultPreferences = deserializedPreferences["frozen"] as JsonObject;
JsonObject editableDefaultPreferences = deserializedPreferences["mutable"] as JsonObject;
this.profilePreferences = new Preferences(immutableDefaultPreferences, editableDefaultPreferences);
}
}
Expand Down
66 changes: 40 additions & 26 deletions dotnet/src/webdriver/Firefox/Preferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text.Json.Nodes;

#nullable enable

namespace OpenQA.Selenium.Firefox
{
Expand All @@ -29,28 +32,28 @@ namespace OpenQA.Selenium.Firefox
/// </summary>
internal class Preferences
{
private Dictionary<string, string> preferences = new Dictionary<string, string>();
private Dictionary<string, string> immutablePreferences = new Dictionary<string, string>();
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(Dictionary<string, object> defaultImmutablePreferences, Dictionary<string, object> defaultPreferences)
public Preferences(JsonObject? defaultImmutablePreferences, JsonObject? defaultPreferences)
{
if (defaultImmutablePreferences != null)
{
foreach (KeyValuePair<string, object> pref in defaultImmutablePreferences)
foreach (KeyValuePair<string, JsonNode?> pref in defaultImmutablePreferences)
{
this.SetPreferenceValue(pref.Key, pref.Value);
this.immutablePreferences.Add(pref.Key, pref.Value.ToString());
this.immutablePreferences.Add(pref.Key);
}
}

if (defaultPreferences != null)
{
foreach (KeyValuePair<string, object> pref in defaultPreferences)
foreach (KeyValuePair<string, JsonNode?> pref in defaultPreferences)
{
this.SetPreferenceValue(pref.Key, pref.Value);
}
Expand All @@ -64,6 +67,7 @@ public Preferences(Dictionary<string, object> defaultImmutablePreferences, Dicti
/// <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 null.</exception>
RenderMichael marked this conversation as resolved.
Show resolved Hide resolved
internal void SetPreference(string key, string value)
{
this.SetPreferenceValue(key, value);
Expand All @@ -76,6 +80,7 @@ internal void SetPreference(string key, string value)
/// <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 null.</exception>
internal void SetPreference(string key, int value)
{
this.SetPreferenceValue(key, value);
Expand All @@ -88,6 +93,7 @@ internal void SetPreference(string key, int value)
/// <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 null.</exception>
internal void SetPreference(string key, bool value)
{
this.SetPreferenceValue(key, value);
Expand All @@ -98,6 +104,7 @@ internal void SetPreference(string key, bool value)
/// </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 null.</exception>
internal string GetPreference(string preferenceName)
{
if (this.preferences.ContainsKey(preferenceName))
Expand Down Expand Up @@ -153,39 +160,46 @@ private static bool IsWrappedAsString(string value)

private bool IsSettablePreference(string preferenceName)
{
return !this.immutablePreferences.ContainsKey(preferenceName);
return !this.immutablePreferences.Contains(preferenceName);
}

private void SetPreferenceValue(string key, object value)
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.immutablePreferences[key], value.ToString());
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);
}

string stringValue = value as string;
if (stringValue != null)
if (value is JsonValue jValue)
{
if (IsWrappedAsString(stringValue))
if (jValue.TryGetValue(out string? stringValue))
{
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Preference values must be plain strings: {0}: {1}", key, value));
}
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;
}
this.preferences[key] = string.Format(CultureInfo.InvariantCulture, "\"{0}\"", value);
return;
}

if (value is bool)
{
this.preferences[key] = Convert.ToBoolean(value, CultureInfo.InvariantCulture).ToString().ToLowerInvariant();
return;
}
if (jValue.TryGetValue(out bool boolValue))
{
this.preferences[key] = boolValue ? "true" : "false";
return;
}

if (value is int || value is long)
{
this.preferences[key] = Convert.ToInt32(value, CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture);
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;
}
}

throw new WebDriverException("Value must be string, int or boolean");
RenderMichael marked this conversation as resolved.
Show resolved Hide resolved
RenderMichael marked this conversation as resolved.
Show resolved Hide resolved
Expand Down