Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
16 changes: 4 additions & 12 deletions dotnet/src/webdriver/Firefox/FirefoxProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,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 @@ -295,22 +295,14 @@ 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)?.AsObject() ?? throw new WebDriverException("webdriver_prefs.json contents was not an object");
JsonObject immutableDefaultPreferences = deserializedPreferences["frozen"] as JsonObject;
JsonObject editableDefaultPreferences = deserializedPreferences["mutable"] as JsonObject;
this.profilePreferences = new Preferences(immutableDefaultPreferences, editableDefaultPreferences);
}
}
Expand Down
62 changes: 37 additions & 25 deletions dotnet/src/webdriver/Firefox/Preferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text.Json.Nodes;

#nullable enable

namespace OpenQA.Selenium.Firefox
{
Expand All @@ -28,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 Down Expand Up @@ -152,39 +156,47 @@ 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());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can take advantage of the fact that preferences and immutablePreferences have the exact same values (hence this error message). We can store just the keys of the immutablePreferences and use a hashset instead,

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 (jValue.TryGetValue(out bool boolValue))
{
this.preferences[key] = boolValue ? "true" : "false";
return;
}

if (value is bool)
{
this.preferences[key] = Convert.ToBoolean(value, CultureInfo.InvariantCulture).ToString().ToLowerInvariant();
return;
}
if (jValue.TryGetValue(out int intValue))
{
this.preferences[key] = intValue.ToString(CultureInfo.InvariantCulture);
return;
}

if (value is int || value is long)
{
this.preferences[key] = Convert.ToInt32(value, CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture);
return;
if (jValue.TryGetValue(out long longValue))
{
this.preferences[key] = Convert.ToInt32(longValue, CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just to maintain the same exception-for-exception handling as before. Let me know if JSON numbers > int.MaxValue should be handled differently.

Debug.Fail("Above conversion should fail");
}
}

throw new WebDriverException("Value must be string, int or boolean");
Expand Down