Skip to content

Commit 7c4a050

Browse files
committed
[java] Avoid loading default Firefox profile preferences
This was used back when the embedded XPIDriver in Selenium was driving Firefox. Not needed anymore because GeckoDriver is doing this today. Hence, we do not need to load these preferences given that GeckoDriver takes care of that for us.
1 parent 12ac3de commit 7c4a050

3 files changed

Lines changed: 39 additions & 78 deletions

File tree

java/src/org/openqa/selenium/firefox/FirefoxProfile.java

Lines changed: 22 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
package org.openqa.selenium.firefox;
1919

20-
import com.google.common.io.Resources;
21-
22-
import org.openqa.selenium.Beta;
2320
import org.openqa.selenium.WebDriverException;
2421
import org.openqa.selenium.io.FileHandler;
2522
import org.openqa.selenium.io.TemporaryFilesystem;
@@ -29,31 +26,23 @@
2926
import java.io.File;
3027
import java.io.FileOutputStream;
3128
import java.io.IOException;
32-
import java.io.InputStreamReader;
3329
import java.io.OutputStreamWriter;
34-
import java.io.Reader;
3530
import java.io.StringReader;
3631
import java.io.Writer;
37-
import java.net.URL;
3832
import java.nio.charset.Charset;
3933
import java.util.HashMap;
4034
import java.util.Map;
4135

4236
public class FirefoxProfile {
43-
public static final String PORT_PREFERENCE = "webdriver_firefox_port";
44-
public static final String ALLOWED_HOSTS_PREFERENCE = "webdriver_firefox_allowed_hosts";
45-
46-
private static final String defaultPrefs = "/org/openqa/selenium/firefox/webdriver_prefs.json";
47-
48-
private Preferences additionalPrefs;
4937

50-
private Map<String, Extension> extensions = new HashMap<>();
38+
private static final String ACCEPT_UNTRUSTED_CERTS_PREF = "webdriver_accept_untrusted_certs";
39+
private static final String ASSUME_UNTRUSTED_ISSUER_PREF = "webdriver_assume_untrusted_issuer";
40+
private final Preferences additionalPrefs;
41+
private final Map<String, Extension> extensions = new HashMap<>();
42+
private final File model;
5143
private boolean loadNoFocusLib;
5244
private boolean acceptUntrustedCerts;
5345
private boolean untrustedCertIssuer;
54-
private File model;
55-
private static final String ACCEPT_UNTRUSTED_CERTS_PREF = "webdriver_accept_untrusted_certs";
56-
private static final String ASSUME_UNTRUSTED_ISSUER_PREF = "webdriver_assume_untrusted_issuer";
5746

5847
public FirefoxProfile() {
5948
this(null);
@@ -67,17 +56,7 @@ public FirefoxProfile() {
6756
* @param profileDir The profile directory to use as a model.
6857
*/
6958
public FirefoxProfile(File profileDir) {
70-
this(null, profileDir);
71-
}
72-
73-
@Beta
74-
protected FirefoxProfile(Reader defaultsReader, File profileDir) {
75-
if (defaultsReader == null) {
76-
defaultsReader = onlyOverrideThisIfYouKnowWhatYouAreDoing();
77-
}
78-
79-
additionalPrefs = new Preferences(defaultsReader);
80-
59+
additionalPrefs = new Preferences();
8160
model = profileDir;
8261
verifyModel(model);
8362

@@ -87,7 +66,6 @@ protected FirefoxProfile(Reader defaultsReader, File profileDir) {
8766
Preferences existingPrefs = new Preferences(reader, prefsInModel);
8867
acceptUntrustedCerts = getBooleanPreference(existingPrefs, ACCEPT_UNTRUSTED_CERTS_PREF, true);
8968
untrustedCertIssuer = getBooleanPreference(existingPrefs, ASSUME_UNTRUSTED_ISSUER_PREF, true);
90-
existingPrefs.addTo(additionalPrefs);
9169
} else {
9270
acceptUntrustedCerts = true;
9371
untrustedCertIssuer = true;
@@ -96,27 +74,20 @@ protected FirefoxProfile(Reader defaultsReader, File profileDir) {
9674
// This is not entirely correct but this is not stored in the profile
9775
// so for now will always be set to false.
9876
loadNoFocusLib = false;
99-
100-
try {
101-
defaultsReader.close();
102-
} catch (IOException e) {
103-
throw new WebDriverException(e);
104-
}
10577
}
10678

107-
/**
108-
* <strong>Internal method. This is liable to change at a moment's notice.</strong>
109-
*
110-
* @return InputStreamReader of the default firefox profile preferences
111-
*/
112-
@Beta
113-
protected Reader onlyOverrideThisIfYouKnowWhatYouAreDoing() {
114-
URL resource = Resources.getResource(FirefoxProfile.class, defaultPrefs);
115-
try {
116-
return new InputStreamReader(resource.openStream(), Charset.defaultCharset());
117-
} catch (IOException e) {
118-
throw new WebDriverException(e);
79+
public static FirefoxProfile fromJson(String json) throws IOException {
80+
// We used to just pass in the entire string without quotes. If we see that, we're good.
81+
// Otherwise, parse the json.
82+
83+
if (json.trim().startsWith("\"")) {
84+
json = new Json().toType(json, String.class);
11985
}
86+
87+
return new FirefoxProfile(Zip.unzipToTempDir(
88+
json,
89+
"webdriver",
90+
"duplicated"));
12091
}
12192

12293
private boolean getBooleanPreference(Preferences prefs, String key, boolean defaultValue) {
@@ -218,7 +189,7 @@ protected Preferences getAdditionalPreferences() {
218189
}
219190

220191
public void updateUserPrefs(File userPrefs) {
221-
Preferences prefs = new Preferences(onlyOverrideThisIfYouKnowWhatYouAreDoing());
192+
Preferences prefs = new Preferences();
222193

223194
// Allow users to override these settings
224195
prefs.setPreference("browser.startup.homepage", "about:blank");
@@ -227,7 +198,7 @@ public void updateUserPrefs(File userPrefs) {
227198
prefs.setPreference("browser.startup.page", 0);
228199

229200
if (userPrefs.exists()) {
230-
prefs = new Preferences(onlyOverrideThisIfYouKnowWhatYouAreDoing(), userPrefs);
201+
prefs = new Preferences(userPrefs);
231202
if (!userPrefs.delete()) {
232203
throw new WebDriverException("Cannot delete existing user preferences");
233204
}
@@ -334,24 +305,13 @@ String toJson() throws IOException {
334305
}
335306
}
336307

337-
public static FirefoxProfile fromJson(String json) throws IOException {
338-
// We used to just pass in the entire string without quotes. If we see that, we're good.
339-
// Otherwise, parse the json.
340-
341-
if (json.trim().startsWith("\"")) {
342-
json = new Json().toType(json, String.class);
343-
}
344-
345-
return new FirefoxProfile(Zip.unzipToTempDir(
346-
json,
347-
"webdriver",
348-
"duplicated"));
349-
}
350-
351308
public void cleanTemporaryModel() {
352309
clean(model);
353310
}
354311

312+
/**
313+
* @deprecated This method will not be replaced as no default preferences are loaded anymore.
314+
*/
355315
public void checkForChangesInFrozenPreferences() {
356316
additionalPrefs.checkForChangesInFrozenPreferences();
357317
}

java/src/org/openqa/selenium/firefox/Preferences.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,14 @@ class Preferences {
5757
* not match that line because Firefox never generates lines like that.
5858
*/
5959
private static final Pattern PREFERENCE_PATTERN =
60-
Pattern.compile("user_pref\\(\"([^\"]+)\", (\"?.+?\"?)\\);");
60+
Pattern.compile("user_pref\\(\"([^\"]+)\", (\"?.+?\"?)\\);");
6161

6262
private Map<String, Object> immutablePrefs = new HashMap<>();
6363
private Map<String, Object> allPrefs = new HashMap<>();
6464

65+
public Preferences() {
66+
}
67+
6568
public Preferences(Reader defaults) {
6669
readDefaultPreferences(defaults);
6770
}
@@ -75,6 +78,10 @@ public Preferences(Reader defaults, File userPrefs) {
7578
}
7679
}
7780

81+
public Preferences(File userPrefs) {
82+
readUserPrefs(userPrefs);
83+
}
84+
7885
public Preferences(Reader defaults, Reader reader) {
7986
readDefaultPreferences(defaults);
8087
try {
@@ -89,6 +96,14 @@ public Preferences(Reader defaults, Reader reader) {
8996
}
9097
}
9198

99+
private void readUserPrefs(File userPrefs) {
100+
try (Reader reader = Files.newBufferedReader(userPrefs.toPath(), Charset.defaultCharset())) {
101+
readPreferences(reader);
102+
} catch (IOException e) {
103+
throw new WebDriverException(e);
104+
}
105+
}
106+
92107
private void readDefaultPreferences(Reader defaultsReader) {
93108
try {
94109
String rawJson = CharStreams.toString(defaultsReader);

java/test/org/openqa/selenium/firefox/FirefoxProfileTest.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818
package org.openqa.selenium.firefox;
1919

2020
import static org.assertj.core.api.Assertions.assertThat;
21-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2221

2322
import org.junit.Before;
2423
import org.junit.Ignore;
2524
import org.junit.Test;
2625
import org.junit.experimental.categories.Category;
26+
import org.openqa.selenium.build.InProject;
2727
import org.openqa.selenium.io.FileHandler;
2828
import org.openqa.selenium.io.Zip;
29-
import org.openqa.selenium.build.InProject;
3029
import org.openqa.selenium.testing.UnitTests;
3130

3231
import java.io.BufferedReader;
@@ -132,25 +131,12 @@ public void getBooleanPreferenceShouldReturnDefaultValueWhenSet() {
132131
assertThat(profile.getBooleanPreference(key, defaultValue)).isEqualTo(defaultValue);
133132
}
134133

135-
@Test
136-
public void shouldSetDefaultPreferences() throws Exception {
137-
assertPreferenceValueEquals("network.http.phishy-userpass-length", 255);
138-
}
139-
140134
@Test
141135
public void shouldAllowSettingFrozenPreferences() throws Exception {
142136
profile.setPreference("network.http.phishy-userpass-length", 1024);
143137
assertPreferenceValueEquals("network.http.phishy-userpass-length", 1024);
144138
}
145139

146-
@Test
147-
public void shouldAllowCheckingForChangesInFrozenPreferences() {
148-
profile.setPreference("network.http.phishy-userpass-length", 1024);
149-
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(
150-
() -> profile.checkForChangesInFrozenPreferences()
151-
).withMessageContaining("network.http.phishy-userpass-length");
152-
}
153-
154140
@Test
155141
@Ignore("Need to replace the extension")
156142
public void shouldInstallExtensionFromZip() {

0 commit comments

Comments
 (0)