Bug 1814798 - pt 1. Add bool to enable/disable PHC at runtime r=glandium
[gecko.git] / remote / shared / RecommendedPreferences.sys.mjs
blob915e0a2e273a070b3ec4f19f507886365593c34e
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
7 const lazy = {};
9 ChromeUtils.defineESModuleGetters(lazy, {
10   Preferences: "resource://gre/modules/Preferences.sys.mjs",
12   Log: "chrome://remote/content/shared/Log.sys.mjs",
13 });
15 XPCOMUtils.defineLazyPreferenceGetter(
16   lazy,
17   "useRecommendedPrefs",
18   "remote.prefs.recommended",
19   false
22 ChromeUtils.defineLazyGetter(lazy, "logger", () => lazy.Log.get());
24 // Ensure we are in the parent process.
25 if (Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) {
26   throw new Error(
27     "RecommendedPreferences should only be loaded in the parent process"
28   );
31 // ALL CHANGES TO THIS LIST MUST HAVE REVIEW FROM A WEBDRIVER PEER!
33 // Preferences are set for automation on startup, unless
34 // remote.prefs.recommended has been set to false.
36 // Note: Clients do not always use the latest version of the application. As
37 // such backward compatibility has to be ensured at least for the last three
38 // releases.
40 // INSTRUCTIONS TO ADD A NEW PREFERENCE
42 // Preferences for remote control and automation can be set from several entry
43 // points:
44 // - remote/shared/RecommendedPreferences.sys.mjs
45 // - remote/test/puppeteer/packages/browsers/src/browser-data/firefox.ts
46 // - testing/geckodriver/src/prefs.rs
47 // - testing/marionette/client/marionette_driver/geckoinstance.py
49 // The preferences in `firefox.ts`, `prefs.rs` and `geckoinstance.py`
50 // will be applied before the application starts, and should typically be used
51 // for preferences which cannot be updated during the lifetime of the application.
53 // The preferences in `RecommendedPreferences.sys.mjs` are applied after
54 // the application has started, which means that the application must apply this
55 // change dynamically and behave correctly. Note that you can also define
56 // protocol specific preferences (CDP, WebDriver, ...) which are merged with the
57 // COMMON_PREFERENCES from `RecommendedPreferences.sys.mjs`.
59 // Additionally, users relying on the Marionette Python client (ie. using
60 // geckoinstance.py) set `remote.prefs.recommended = false`. This means that
61 // preferences from `RecommendedPreferences.sys.mjs` are not applied and have to
62 // be added to the list of preferences in that Python file. Note that there are
63 // several lists of preferences, either common or specific to a given application
64 // (Firefox Desktop, Fennec, Thunderbird).
66 // Depending on how users interact with the Remote Agent, they will use different
67 // combinations of preferences. So it's important to update the preferences files
68 // so that all users have the proper preferences.
70 // When adding a new preference, follow this guide to decide where to add it:
71 // - Add the preference to `geckoinstance.py`
72 // - If the preference has to be set before startup:
73 //   - Add the preference to `prefs.rs`
74 //   - Add the preference `browser-data/firefox.ts` in the puppeteer folder
75 //   - Create a PR to upstream the change on `browser-data/firefox.ts` to puppeteer
76 // - Otherwise, if the preference can be set after startup:
77 //   - Add the preference to `RecommendedPreferences.sys.mjs`
78 const COMMON_PREFERENCES = new Map([
79   // Make sure Shield doesn't hit the network.
80   ["app.normandy.api_url", ""],
82   // Disable automatically upgrading Firefox
83   //
84   // Note: This preference should have already been set by the client when
85   // creating the profile. But if not and to absolutely make sure that updates
86   // of Firefox aren't downloaded and applied, enforce its presence.
87   ["app.update.disabledForTesting", true],
89   // Increase the APZ content response timeout in tests to 1 minute.
90   // This is to accommodate the fact that test environments tends to be
91   // slower than production environments (with the b2g emulator being
92   // the slowest of them all), resulting in the production timeout value
93   // sometimes being exceeded and causing false-positive test failures.
94   //
95   // (bug 1176798, bug 1177018, bug 1210465)
96   ["apz.content_response_timeout", 60000],
98   // Don't show the content blocking introduction panel.
99   // We use a larger number than the default 22 to have some buffer
100   // This can be removed once Firefox 69 and 68 ESR and are no longer supported.
101   ["browser.contentblocking.introCount", 99],
103   // Indicate that the download panel has been shown once so that
104   // whichever download test runs first doesn't show the popup
105   // inconsistently.
106   ["browser.download.panel.shown", true],
108   // Make sure Topsites doesn't hit the network to retrieve sponsored tiles.
109   ["browser.newtabpage.activity-stream.showSponsoredTopSites", false],
111   // Always display a blank page
112   ["browser.newtabpage.enabled", false],
114   // Background thumbnails in particular cause grief, and disabling
115   // thumbnails in general cannot hurt
116   ["browser.pagethumbnails.capturing_disabled", true],
118   // Disable geolocation ping(#1)
119   ["browser.region.network.url", ""],
121   // Disable safebrowsing components.
122   //
123   // These should also be set in the profile prior to starting Firefox,
124   // as it is picked up at runtime.
125   ["browser.safebrowsing.blockedURIs.enabled", false],
126   ["browser.safebrowsing.downloads.enabled", false],
127   ["browser.safebrowsing.malware.enabled", false],
128   ["browser.safebrowsing.passwords.enabled", false],
129   ["browser.safebrowsing.phishing.enabled", false],
131   // Disable updates to search engines.
132   //
133   // Should be set in profile.
134   ["browser.search.update", false],
136   // Do not restore the last open set of tabs if the browser has crashed
137   ["browser.sessionstore.resume_from_crash", false],
139   // Don't check for the default web browser during startup.
140   //
141   // These should also be set in the profile prior to starting Firefox,
142   // as it is picked up at runtime.
143   ["browser.shell.checkDefaultBrowser", false],
145   // Disable session restore infobar
146   ["browser.startup.couldRestoreSession.count", -1],
148   // Do not redirect user when a milstone upgrade of Firefox is detected
149   ["browser.startup.homepage_override.mstone", "ignore"],
151   // Do not close the window when the last tab gets closed
152   ["browser.tabs.closeWindowWithLastTab", false],
154   // Don't unload tabs when available memory is running low
155   ["browser.tabs.unloadOnLowMemory", false],
157   // Do not warn when closing all open tabs
158   ["browser.tabs.warnOnClose", false],
160   // Do not warn when closing all other open tabs
161   ["browser.tabs.warnOnCloseOtherTabs", false],
163   // Do not warn when multiple tabs will be opened
164   ["browser.tabs.warnOnOpen", false],
166   // Don't show the Bookmarks Toolbar on any tab (the above pref that
167   // disables the New Tab Page ends up showing the toolbar on about:blank).
168   ["browser.toolbars.bookmarks.visibility", "never"],
170   // Make sure Topsites doesn't hit the network to retrieve tiles from Contile.
171   ["browser.topsites.contile.enabled", false],
173   // Disable first run splash page on Windows 10
174   ["browser.usedOnWindows10.introURL", ""],
176   // Turn off Merino suggestions in the location bar so as not to trigger
177   // network connections.
178   ["browser.urlbar.merino.endpointURL", ""],
180   // Turn off search suggestions in the location bar so as not to trigger
181   // network connections.
182   ["browser.urlbar.suggest.searches", false],
184   // Do not warn on quitting Firefox
185   ["browser.warnOnQuit", false],
187   // Disable captive portal
188   ["captivedetect.canonicalURL", ""],
190   // Do not show datareporting policy notifications which can
191   // interfere with tests
192   [
193     "datareporting.healthreport.documentServerURI",
194     "http://%(server)s/dummy/healthreport/",
195   ],
196   ["datareporting.healthreport.logging.consoleEnabled", false],
197   ["datareporting.healthreport.service.enabled", false],
198   ["datareporting.healthreport.service.firstRun", false],
199   ["datareporting.healthreport.uploadEnabled", false],
200   ["datareporting.policy.dataSubmissionEnabled", false],
201   ["datareporting.policy.dataSubmissionPolicyAccepted", false],
202   ["datareporting.policy.dataSubmissionPolicyBypassNotification", true],
204   // Disable popup-blocker
205   ["dom.disable_open_during_load", false],
207   // Enabling the support for File object creation in the content process
208   ["dom.file.createInChild", true],
210   // Disable delayed user input event handling
211   ["dom.input_events.security.minNumTicks", 0],
212   ["dom.input_events.security.minTimeElapsedInMS", 0],
214   // Disable the ProcessHangMonitor
215   ["dom.ipc.reportProcessHangs", false],
217   // Disable slow script dialogues
218   ["dom.max_chrome_script_run_time", 0],
219   ["dom.max_script_run_time", 0],
221   // Disable location change rate limitation
222   ["dom.navigation.locationChangeRateLimit.count", 0],
224   // DOM Push
225   ["dom.push.connection.enabled", false],
227   // Screen Orientation API
228   ["dom.screenorientation.allow-lock", true],
230   // Disable dialog abuse if alerts are triggered too quickly.
231   ["dom.successive_dialog_time_limit", 0],
233   // Only load extensions from the application and user profile
234   // AddonManager.SCOPE_PROFILE + AddonManager.SCOPE_APPLICATION
235   //
236   // Should be set in profile.
237   ["extensions.autoDisableScopes", 0],
238   ["extensions.enabledScopes", 5],
240   // Disable metadata caching for installed add-ons by default
241   ["extensions.getAddons.cache.enabled", false],
243   // Disable installing any distribution extensions or add-ons.
244   // Should be set in profile.
245   ["extensions.installDistroAddons", false],
247   // Turn off extension updates so they do not bother tests
248   ["extensions.update.enabled", false],
249   ["extensions.update.notifyUser", false],
251   // Make sure opening about:addons will not hit the network
252   ["extensions.getAddons.discovery.api_url", "data:, "],
254   // Redirect various extension update URLs
255   [
256     "extensions.blocklist.detailsURL",
257     "http://%(server)s/extensions-dummy/blocklistDetailsURL",
258   ],
259   [
260     "extensions.blocklist.itemURL",
261     "http://%(server)s/extensions-dummy/blocklistItemURL",
262   ],
263   ["extensions.hotfix.url", "http://%(server)s/extensions-dummy/hotfixURL"],
264   [
265     "extensions.systemAddon.update.url",
266     "http://%(server)s/dummy-system-addons.xml",
267   ],
268   [
269     "extensions.update.background.url",
270     "http://%(server)s/extensions-dummy/updateBackgroundURL",
271   ],
272   ["extensions.update.url", "http://%(server)s/extensions-dummy/updateURL"],
274   // Make sure opening about: addons won't hit the network
275   ["extensions.getAddons.discovery.api_url", "data:, "],
276   [
277     "extensions.getAddons.get.url",
278     "http://%(server)s/extensions-dummy/repositoryGetURL",
279   ],
280   [
281     "extensions.getAddons.search.browseURL",
282     "http://%(server)s/extensions-dummy/repositoryBrowseURL",
283   ],
285   // Allow the application to have focus even it runs in the background
286   ["focusmanager.testmode", true],
288   // Disable useragent updates
289   ["general.useragent.updates.enabled", false],
291   // Disable geolocation ping(#2)
292   ["geo.provider.network.url", ""],
294   // Always use network provider for geolocation tests so we bypass the
295   // macOS dialog raised by the corelocation provider
296   ["geo.provider.testing", true],
298   // Do not scan Wifi
299   ["geo.wifi.scan", false],
301   // Disable Firefox accounts ping
302   ["identity.fxaccounts.auth.uri", "https://{server}/dummy/fxa"],
304   // Disable connectivity service pings
305   ["network.connectivity-service.enabled", false],
307   // Do not prompt with long usernames or passwords in URLs
308   ["network.http.phishy-userpass-length", 255],
310   // Do not prompt for temporary redirects
311   ["network.http.prompt-temp-redirect", false],
313   // Do not automatically switch between offline and online
314   ["network.manage-offline-status", false],
316   // Make sure SNTP requests do not hit the network
317   ["network.sntp.pools", "%(server)s"],
319   // Privacy and Tracking Protection
320   ["privacy.trackingprotection.enabled", false],
322   // Don't do network connections for mitm priming
323   ["security.certerrors.mitm.priming.enabled", false],
325   // Local documents have access to all other local documents,
326   // including directory listings
327   ["security.fileuri.strict_origin_policy", false],
329   // Tests do not wait for the notification button security delay
330   ["security.notification_enable_delay", 0],
332   // Do not download intermediate certificates
333   ["security.remote_settings.intermediates.enabled", false],
335   // Ensure blocklist updates do not hit the network
336   ["services.settings.server", "http://%(server)s/dummy/blocklist/"],
338   // Do not automatically fill sign-in forms with known usernames and
339   // passwords
340   ["signon.autofillForms", false],
342   // Disable password capture, so that tests that include forms are not
343   // influenced by the presence of the persistent doorhanger notification
344   ["signon.rememberSignons", false],
346   // Disable first-run welcome page
347   ["startup.homepage_welcome_url", "about:blank"],
348   ["startup.homepage_welcome_url.additional", ""],
350   // Prevent starting into safe mode after application crashes
351   ["toolkit.startup.max_resumed_crashes", -1],
353   // Disable all telemetry pings
354   ["toolkit.telemetry.server", "https://%(server)s/telemetry-dummy/"],
356   // Disable window occlusion on Windows, which can prevent webdriver commands
357   // such as WebDriver:FindElements from working properly (Bug 1802473).
358   ["widget.windows.window_occlusion_tracking.enabled", false],
361 export const RecommendedPreferences = {
362   alteredPrefs: new Set(),
364   isInitialized: false,
366   /**
367    * Apply the provided map of preferences.
368    * They will be automatically reset on application shutdown.
369    *
370    * @param {Map} preferences
371    *     Map of preference key to preference value.
372    */
373   applyPreferences(preferences) {
374     if (!lazy.useRecommendedPrefs) {
375       // If remote.prefs.recommended is set to false, do not set any preference
376       // here. Needed for our Firefox CI.
377       return;
378     }
380     // Only apply common recommended preferences on first call to
381     // applyPreferences.
382     if (!this.isInitialized) {
383       // Merge common preferences and provided preferences in a single map.
384       preferences = new Map([...COMMON_PREFERENCES, ...preferences]);
385       Services.obs.addObserver(this, "quit-application");
386       this.isInitialized = true;
387     }
389     for (const [k, v] of preferences) {
390       if (!lazy.Preferences.isSet(k)) {
391         lazy.logger.debug(`Setting recommended pref ${k} to ${v}`);
392         lazy.Preferences.set(k, v);
394         // Keep track all the altered preferences to restore them on
395         // quit-application.
396         this.alteredPrefs.add(k);
397       }
398     }
399   },
401   observe(subject, topic) {
402     if (topic === "quit-application") {
403       Services.obs.removeObserver(this, "quit-application");
404       this.restoreAllPreferences();
405     }
406   },
408   /**
409    * Restore all the altered preferences.
410    */
411   restoreAllPreferences() {
412     this.restorePreferences(this.alteredPrefs);
413     this.isInitialized = false;
414   },
416   /**
417    * Restore provided preferences.
418    *
419    * @param {Map} preferences
420    *     Map of preferences that should be restored.
421    */
422   restorePreferences(preferences) {
423     for (const k of preferences.keys()) {
424       lazy.logger.debug(`Resetting recommended pref ${k}`);
425       lazy.Preferences.reset(k);
426       this.alteredPrefs.delete(k);
427     }
428   },