firefox.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * Copyright 2023 Google Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import fs from 'fs';
  17. import path from 'path';
  18. import { getJSON } from '../httpUtil.js';
  19. import { BrowserPlatform } from './types.js';
  20. function archive(platform, buildId) {
  21. switch (platform) {
  22. case BrowserPlatform.LINUX:
  23. return `firefox-${buildId}.en-US.${platform}-x86_64.tar.bz2`;
  24. case BrowserPlatform.MAC_ARM:
  25. case BrowserPlatform.MAC:
  26. return `firefox-${buildId}.en-US.mac.dmg`;
  27. case BrowserPlatform.WIN32:
  28. case BrowserPlatform.WIN64:
  29. return `firefox-${buildId}.en-US.${platform}.zip`;
  30. }
  31. }
  32. export function resolveDownloadUrl(platform, buildId, baseUrl = 'https://archive.mozilla.org/pub/firefox/nightly/latest-mozilla-central') {
  33. return `${baseUrl}/${resolveDownloadPath(platform, buildId).join('/')}`;
  34. }
  35. export function resolveDownloadPath(platform, buildId) {
  36. return [archive(platform, buildId)];
  37. }
  38. export function relativeExecutablePath(platform, _buildId) {
  39. switch (platform) {
  40. case BrowserPlatform.MAC_ARM:
  41. case BrowserPlatform.MAC:
  42. return path.join('Firefox Nightly.app', 'Contents', 'MacOS', 'firefox');
  43. case BrowserPlatform.LINUX:
  44. return path.join('firefox', 'firefox');
  45. case BrowserPlatform.WIN32:
  46. case BrowserPlatform.WIN64:
  47. return path.join('firefox', 'firefox.exe');
  48. }
  49. }
  50. export async function resolveBuildId(channel = 'FIREFOX_NIGHTLY') {
  51. const versions = (await getJSON(new URL('https://product-details.mozilla.org/1.0/firefox_versions.json')));
  52. const version = versions[channel];
  53. if (!version) {
  54. throw new Error(`Channel ${channel} is not found.`);
  55. }
  56. return version;
  57. }
  58. export async function createProfile(options) {
  59. if (!fs.existsSync(options.path)) {
  60. await fs.promises.mkdir(options.path, {
  61. recursive: true,
  62. });
  63. }
  64. await writePreferences({
  65. preferences: {
  66. ...defaultProfilePreferences(options.preferences),
  67. ...options.preferences,
  68. },
  69. path: options.path,
  70. });
  71. }
  72. function defaultProfilePreferences(extraPrefs) {
  73. const server = 'dummy.test';
  74. const defaultPrefs = {
  75. // Make sure Shield doesn't hit the network.
  76. 'app.normandy.api_url': '',
  77. // Disable Firefox old build background check
  78. 'app.update.checkInstallTime': false,
  79. // Disable automatically upgrading Firefox
  80. 'app.update.disabledForTesting': true,
  81. // Increase the APZ content response timeout to 1 minute
  82. 'apz.content_response_timeout': 60000,
  83. // Prevent various error message on the console
  84. // jest-puppeteer asserts that no error message is emitted by the console
  85. 'browser.contentblocking.features.standard': '-tp,tpPrivate,cookieBehavior0,-cm,-fp',
  86. // Enable the dump function: which sends messages to the system
  87. // console
  88. // https://bugzilla.mozilla.org/show_bug.cgi?id=1543115
  89. 'browser.dom.window.dump.enabled': true,
  90. // Disable topstories
  91. 'browser.newtabpage.activity-stream.feeds.system.topstories': false,
  92. // Always display a blank page
  93. 'browser.newtabpage.enabled': false,
  94. // Background thumbnails in particular cause grief: and disabling
  95. // thumbnails in general cannot hurt
  96. 'browser.pagethumbnails.capturing_disabled': true,
  97. // Disable safebrowsing components.
  98. 'browser.safebrowsing.blockedURIs.enabled': false,
  99. 'browser.safebrowsing.downloads.enabled': false,
  100. 'browser.safebrowsing.malware.enabled': false,
  101. 'browser.safebrowsing.passwords.enabled': false,
  102. 'browser.safebrowsing.phishing.enabled': false,
  103. // Disable updates to search engines.
  104. 'browser.search.update': false,
  105. // Do not restore the last open set of tabs if the browser has crashed
  106. 'browser.sessionstore.resume_from_crash': false,
  107. // Skip check for default browser on startup
  108. 'browser.shell.checkDefaultBrowser': false,
  109. // Disable newtabpage
  110. 'browser.startup.homepage': 'about:blank',
  111. // Do not redirect user when a milstone upgrade of Firefox is detected
  112. 'browser.startup.homepage_override.mstone': 'ignore',
  113. // Start with a blank page about:blank
  114. 'browser.startup.page': 0,
  115. // Do not allow background tabs to be zombified on Android: otherwise for
  116. // tests that open additional tabs: the test harness tab itself might get
  117. // unloaded
  118. 'browser.tabs.disableBackgroundZombification': false,
  119. // Do not warn when closing all other open tabs
  120. 'browser.tabs.warnOnCloseOtherTabs': false,
  121. // Do not warn when multiple tabs will be opened
  122. 'browser.tabs.warnOnOpen': false,
  123. // Disable page translations, which can cause issues with tests.
  124. // See https://bugzilla.mozilla.org/show_bug.cgi?id=1836093.
  125. 'browser.translations.enable': false,
  126. // Disable the UI tour.
  127. 'browser.uitour.enabled': false,
  128. // Turn off search suggestions in the location bar so as not to trigger
  129. // network connections.
  130. 'browser.urlbar.suggest.searches': false,
  131. // Disable first run splash page on Windows 10
  132. 'browser.usedOnWindows10.introURL': '',
  133. // Do not warn on quitting Firefox
  134. 'browser.warnOnQuit': false,
  135. // Defensively disable data reporting systems
  136. 'datareporting.healthreport.documentServerURI': `http://${server}/dummy/healthreport/`,
  137. 'datareporting.healthreport.logging.consoleEnabled': false,
  138. 'datareporting.healthreport.service.enabled': false,
  139. 'datareporting.healthreport.service.firstRun': false,
  140. 'datareporting.healthreport.uploadEnabled': false,
  141. // Do not show datareporting policy notifications which can interfere with tests
  142. 'datareporting.policy.dataSubmissionEnabled': false,
  143. 'datareporting.policy.dataSubmissionPolicyBypassNotification': true,
  144. // DevTools JSONViewer sometimes fails to load dependencies with its require.js.
  145. // This doesn't affect Puppeteer but spams console (Bug 1424372)
  146. 'devtools.jsonview.enabled': false,
  147. // Disable popup-blocker
  148. 'dom.disable_open_during_load': false,
  149. // Enable the support for File object creation in the content process
  150. // Required for |Page.setFileInputFiles| protocol method.
  151. 'dom.file.createInChild': true,
  152. // Disable the ProcessHangMonitor
  153. 'dom.ipc.reportProcessHangs': false,
  154. // Disable slow script dialogues
  155. 'dom.max_chrome_script_run_time': 0,
  156. 'dom.max_script_run_time': 0,
  157. // Only load extensions from the application and user profile
  158. // AddonManager.SCOPE_PROFILE + AddonManager.SCOPE_APPLICATION
  159. 'extensions.autoDisableScopes': 0,
  160. 'extensions.enabledScopes': 5,
  161. // Disable metadata caching for installed add-ons by default
  162. 'extensions.getAddons.cache.enabled': false,
  163. // Disable installing any distribution extensions or add-ons.
  164. 'extensions.installDistroAddons': false,
  165. // Disabled screenshots extension
  166. 'extensions.screenshots.disabled': true,
  167. // Turn off extension updates so they do not bother tests
  168. 'extensions.update.enabled': false,
  169. // Turn off extension updates so they do not bother tests
  170. 'extensions.update.notifyUser': false,
  171. // Make sure opening about:addons will not hit the network
  172. 'extensions.webservice.discoverURL': `http://${server}/dummy/discoveryURL`,
  173. // Temporarily force disable BFCache in parent (https://bit.ly/bug-1732263)
  174. 'fission.bfcacheInParent': false,
  175. // Force all web content to use a single content process
  176. 'fission.webContentIsolationStrategy': 0,
  177. // Allow the application to have focus even it runs in the background
  178. 'focusmanager.testmode': true,
  179. // Disable useragent updates
  180. 'general.useragent.updates.enabled': false,
  181. // Always use network provider for geolocation tests so we bypass the
  182. // macOS dialog raised by the corelocation provider
  183. 'geo.provider.testing': true,
  184. // Do not scan Wifi
  185. 'geo.wifi.scan': false,
  186. // No hang monitor
  187. 'hangmonitor.timeout': 0,
  188. // Show chrome errors and warnings in the error console
  189. 'javascript.options.showInConsole': true,
  190. // Disable download and usage of OpenH264: and Widevine plugins
  191. 'media.gmp-manager.updateEnabled': false,
  192. // Prevent various error message on the console
  193. // jest-puppeteer asserts that no error message is emitted by the console
  194. 'network.cookie.cookieBehavior': 0,
  195. // Disable experimental feature that is only available in Nightly
  196. 'network.cookie.sameSite.laxByDefault': false,
  197. // Do not prompt for temporary redirects
  198. 'network.http.prompt-temp-redirect': false,
  199. // Disable speculative connections so they are not reported as leaking
  200. // when they are hanging around
  201. 'network.http.speculative-parallel-limit': 0,
  202. // Do not automatically switch between offline and online
  203. 'network.manage-offline-status': false,
  204. // Make sure SNTP requests do not hit the network
  205. 'network.sntp.pools': server,
  206. // Disable Flash.
  207. 'plugin.state.flash': 0,
  208. 'privacy.trackingprotection.enabled': false,
  209. // Can be removed once Firefox 89 is no longer supported
  210. // https://bugzilla.mozilla.org/show_bug.cgi?id=1710839
  211. 'remote.enabled': true,
  212. // Don't do network connections for mitm priming
  213. 'security.certerrors.mitm.priming.enabled': false,
  214. // Local documents have access to all other local documents,
  215. // including directory listings
  216. 'security.fileuri.strict_origin_policy': false,
  217. // Do not wait for the notification button security delay
  218. 'security.notification_enable_delay': 0,
  219. // Ensure blocklist updates do not hit the network
  220. 'services.settings.server': `http://${server}/dummy/blocklist/`,
  221. // Do not automatically fill sign-in forms with known usernames and
  222. // passwords
  223. 'signon.autofillForms': false,
  224. // Disable password capture, so that tests that include forms are not
  225. // influenced by the presence of the persistent doorhanger notification
  226. 'signon.rememberSignons': false,
  227. // Disable first-run welcome page
  228. 'startup.homepage_welcome_url': 'about:blank',
  229. // Disable first-run welcome page
  230. 'startup.homepage_welcome_url.additional': '',
  231. // Disable browser animations (tabs, fullscreen, sliding alerts)
  232. 'toolkit.cosmeticAnimations.enabled': false,
  233. // Prevent starting into safe mode after application crashes
  234. 'toolkit.startup.max_resumed_crashes': -1,
  235. };
  236. return Object.assign(defaultPrefs, extraPrefs);
  237. }
  238. /**
  239. * Populates the user.js file with custom preferences as needed to allow
  240. * Firefox's CDP support to properly function. These preferences will be
  241. * automatically copied over to prefs.js during startup of Firefox. To be
  242. * able to restore the original values of preferences a backup of prefs.js
  243. * will be created.
  244. *
  245. * @param prefs - List of preferences to add.
  246. * @param profilePath - Firefox profile to write the preferences to.
  247. */
  248. async function writePreferences(options) {
  249. const lines = Object.entries(options.preferences).map(([key, value]) => {
  250. return `user_pref(${JSON.stringify(key)}, ${JSON.stringify(value)});`;
  251. });
  252. await fs.promises.writeFile(path.join(options.path, 'user.js'), lines.join('\n'));
  253. // Create a backup of the preferences file if it already exitsts.
  254. const prefsPath = path.join(options.path, 'prefs.js');
  255. if (fs.existsSync(prefsPath)) {
  256. const prefsBackupPath = path.join(options.path, 'prefs.js.puppeteer');
  257. await fs.promises.copyFile(prefsPath, prefsBackupPath);
  258. }
  259. }
  260. //# sourceMappingURL=firefox.js.map