-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[dotnet] Strongly-typed handling for firefox profile's default preferences #14646
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
Closed
Closed
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| { | ||
|
|
@@ -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); | ||
| } | ||
|
|
@@ -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()); | ||
| 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); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 > |
||
| Debug.Fail("Above conversion should fail"); | ||
| } | ||
| } | ||
|
|
||
| throw new WebDriverException("Value must be string, int or boolean"); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
preferencesandimmutablePreferenceshave the exact same values (hence this error message). We can store just the keys of theimmutablePreferencesand use a hashset instead,