firefox.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2023 Google Inc.
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. var __importDefault = (this && this.__importDefault) || function (mod) {
  8. return (mod && mod.__esModule) ? mod : { "default": mod };
  9. };
  10. Object.defineProperty(exports, "__esModule", { value: true });
  11. exports.compareVersions = exports.createProfile = exports.resolveBuildId = exports.FirefoxChannel = exports.relativeExecutablePath = exports.resolveDownloadPath = exports.resolveDownloadUrl = void 0;
  12. const fs_1 = __importDefault(require("fs"));
  13. const path_1 = __importDefault(require("path"));
  14. const httpUtil_js_1 = require("../httpUtil.js");
  15. const types_js_1 = require("./types.js");
  16. function archiveNightly(platform, buildId) {
  17. switch (platform) {
  18. case types_js_1.BrowserPlatform.LINUX:
  19. return `firefox-${buildId}.en-US.${platform}-x86_64.tar.bz2`;
  20. case types_js_1.BrowserPlatform.MAC_ARM:
  21. case types_js_1.BrowserPlatform.MAC:
  22. return `firefox-${buildId}.en-US.mac.dmg`;
  23. case types_js_1.BrowserPlatform.WIN32:
  24. case types_js_1.BrowserPlatform.WIN64:
  25. return `firefox-${buildId}.en-US.${platform}.zip`;
  26. }
  27. }
  28. function archive(platform, buildId) {
  29. switch (platform) {
  30. case types_js_1.BrowserPlatform.LINUX:
  31. return `firefox-${buildId}.tar.bz2`;
  32. case types_js_1.BrowserPlatform.MAC_ARM:
  33. case types_js_1.BrowserPlatform.MAC:
  34. return `Firefox ${buildId}.dmg`;
  35. case types_js_1.BrowserPlatform.WIN32:
  36. case types_js_1.BrowserPlatform.WIN64:
  37. return `Firefox Setup ${buildId}.exe`;
  38. }
  39. }
  40. function platformName(platform) {
  41. switch (platform) {
  42. case types_js_1.BrowserPlatform.LINUX:
  43. return `linux-x86_64`;
  44. case types_js_1.BrowserPlatform.MAC_ARM:
  45. case types_js_1.BrowserPlatform.MAC:
  46. return `mac`;
  47. case types_js_1.BrowserPlatform.WIN32:
  48. case types_js_1.BrowserPlatform.WIN64:
  49. return platform;
  50. }
  51. }
  52. function parseBuildId(buildId) {
  53. for (const value of Object.values(FirefoxChannel)) {
  54. if (buildId.startsWith(value + '_')) {
  55. buildId = buildId.substring(value.length + 1);
  56. return [value, buildId];
  57. }
  58. }
  59. // Older versions do not have channel as the prefix.«
  60. return [FirefoxChannel.NIGHTLY, buildId];
  61. }
  62. function resolveDownloadUrl(platform, buildId, baseUrl) {
  63. const [channel, resolvedBuildId] = parseBuildId(buildId);
  64. switch (channel) {
  65. case FirefoxChannel.NIGHTLY:
  66. baseUrl ??=
  67. 'https://archive.mozilla.org/pub/firefox/nightly/latest-mozilla-central';
  68. break;
  69. case FirefoxChannel.DEVEDITION:
  70. baseUrl ??= 'https://archive.mozilla.org/pub/devedition/releases';
  71. break;
  72. case FirefoxChannel.BETA:
  73. case FirefoxChannel.STABLE:
  74. case FirefoxChannel.ESR:
  75. baseUrl ??= 'https://archive.mozilla.org/pub/firefox/releases';
  76. break;
  77. }
  78. switch (channel) {
  79. case FirefoxChannel.NIGHTLY:
  80. return `${baseUrl}/${resolveDownloadPath(platform, resolvedBuildId).join('/')}`;
  81. case FirefoxChannel.DEVEDITION:
  82. case FirefoxChannel.BETA:
  83. case FirefoxChannel.STABLE:
  84. case FirefoxChannel.ESR:
  85. return `${baseUrl}/${resolvedBuildId}/${platformName(platform)}/en-US/${archive(platform, resolvedBuildId)}`;
  86. }
  87. }
  88. exports.resolveDownloadUrl = resolveDownloadUrl;
  89. function resolveDownloadPath(platform, buildId) {
  90. return [archiveNightly(platform, buildId)];
  91. }
  92. exports.resolveDownloadPath = resolveDownloadPath;
  93. function relativeExecutablePath(platform, buildId) {
  94. const [channel] = parseBuildId(buildId);
  95. switch (channel) {
  96. case FirefoxChannel.NIGHTLY:
  97. switch (platform) {
  98. case types_js_1.BrowserPlatform.MAC_ARM:
  99. case types_js_1.BrowserPlatform.MAC:
  100. return path_1.default.join('Firefox Nightly.app', 'Contents', 'MacOS', 'firefox');
  101. case types_js_1.BrowserPlatform.LINUX:
  102. return path_1.default.join('firefox', 'firefox');
  103. case types_js_1.BrowserPlatform.WIN32:
  104. case types_js_1.BrowserPlatform.WIN64:
  105. return path_1.default.join('firefox', 'firefox.exe');
  106. }
  107. case FirefoxChannel.BETA:
  108. case FirefoxChannel.DEVEDITION:
  109. case FirefoxChannel.ESR:
  110. case FirefoxChannel.STABLE:
  111. switch (platform) {
  112. case types_js_1.BrowserPlatform.MAC_ARM:
  113. case types_js_1.BrowserPlatform.MAC:
  114. return path_1.default.join('Firefox.app', 'Contents', 'MacOS', 'firefox');
  115. case types_js_1.BrowserPlatform.LINUX:
  116. return path_1.default.join('firefox', 'firefox');
  117. case types_js_1.BrowserPlatform.WIN32:
  118. case types_js_1.BrowserPlatform.WIN64:
  119. return path_1.default.join('core', 'firefox.exe');
  120. }
  121. }
  122. }
  123. exports.relativeExecutablePath = relativeExecutablePath;
  124. var FirefoxChannel;
  125. (function (FirefoxChannel) {
  126. FirefoxChannel["STABLE"] = "stable";
  127. FirefoxChannel["ESR"] = "esr";
  128. FirefoxChannel["DEVEDITION"] = "devedition";
  129. FirefoxChannel["BETA"] = "beta";
  130. FirefoxChannel["NIGHTLY"] = "nightly";
  131. })(FirefoxChannel || (exports.FirefoxChannel = FirefoxChannel = {}));
  132. async function resolveBuildId(channel = FirefoxChannel.NIGHTLY) {
  133. const channelToVersionKey = {
  134. [FirefoxChannel.ESR]: 'FIREFOX_ESR',
  135. [FirefoxChannel.STABLE]: 'LATEST_FIREFOX_VERSION',
  136. [FirefoxChannel.DEVEDITION]: 'FIREFOX_DEVEDITION',
  137. [FirefoxChannel.BETA]: 'FIREFOX_DEVEDITION',
  138. [FirefoxChannel.NIGHTLY]: 'FIREFOX_NIGHTLY',
  139. };
  140. const versions = (await (0, httpUtil_js_1.getJSON)(new URL('https://product-details.mozilla.org/1.0/firefox_versions.json')));
  141. const version = versions[channelToVersionKey[channel]];
  142. if (!version) {
  143. throw new Error(`Channel ${channel} is not found.`);
  144. }
  145. return channel + '_' + version;
  146. }
  147. exports.resolveBuildId = resolveBuildId;
  148. async function createProfile(options) {
  149. if (!fs_1.default.existsSync(options.path)) {
  150. await fs_1.default.promises.mkdir(options.path, {
  151. recursive: true,
  152. });
  153. }
  154. await writePreferences({
  155. preferences: {
  156. ...defaultProfilePreferences(options.preferences),
  157. ...options.preferences,
  158. },
  159. path: options.path,
  160. });
  161. }
  162. exports.createProfile = createProfile;
  163. function defaultProfilePreferences(extraPrefs) {
  164. const server = 'dummy.test';
  165. const defaultPrefs = {
  166. // Make sure Shield doesn't hit the network.
  167. 'app.normandy.api_url': '',
  168. // Disable Firefox old build background check
  169. 'app.update.checkInstallTime': false,
  170. // Disable automatically upgrading Firefox
  171. 'app.update.disabledForTesting': true,
  172. // Increase the APZ content response timeout to 1 minute
  173. 'apz.content_response_timeout': 60000,
  174. // Prevent various error message on the console
  175. // jest-puppeteer asserts that no error message is emitted by the console
  176. 'browser.contentblocking.features.standard': '-tp,tpPrivate,cookieBehavior0,-cm,-fp',
  177. // Enable the dump function: which sends messages to the system
  178. // console
  179. // https://bugzilla.mozilla.org/show_bug.cgi?id=1543115
  180. 'browser.dom.window.dump.enabled': true,
  181. // Disable topstories
  182. 'browser.newtabpage.activity-stream.feeds.system.topstories': false,
  183. // Always display a blank page
  184. 'browser.newtabpage.enabled': false,
  185. // Background thumbnails in particular cause grief: and disabling
  186. // thumbnails in general cannot hurt
  187. 'browser.pagethumbnails.capturing_disabled': true,
  188. // Disable safebrowsing components.
  189. 'browser.safebrowsing.blockedURIs.enabled': false,
  190. 'browser.safebrowsing.downloads.enabled': false,
  191. 'browser.safebrowsing.malware.enabled': false,
  192. 'browser.safebrowsing.phishing.enabled': false,
  193. // Disable updates to search engines.
  194. 'browser.search.update': false,
  195. // Do not restore the last open set of tabs if the browser has crashed
  196. 'browser.sessionstore.resume_from_crash': false,
  197. // Skip check for default browser on startup
  198. 'browser.shell.checkDefaultBrowser': false,
  199. // Disable newtabpage
  200. 'browser.startup.homepage': 'about:blank',
  201. // Do not redirect user when a milstone upgrade of Firefox is detected
  202. 'browser.startup.homepage_override.mstone': 'ignore',
  203. // Start with a blank page about:blank
  204. 'browser.startup.page': 0,
  205. // Do not allow background tabs to be zombified on Android: otherwise for
  206. // tests that open additional tabs: the test harness tab itself might get
  207. // unloaded
  208. 'browser.tabs.disableBackgroundZombification': false,
  209. // Do not warn when closing all other open tabs
  210. 'browser.tabs.warnOnCloseOtherTabs': false,
  211. // Do not warn when multiple tabs will be opened
  212. 'browser.tabs.warnOnOpen': false,
  213. // Do not automatically offer translations, as tests do not expect this.
  214. 'browser.translations.automaticallyPopup': false,
  215. // Disable the UI tour.
  216. 'browser.uitour.enabled': false,
  217. // Turn off search suggestions in the location bar so as not to trigger
  218. // network connections.
  219. 'browser.urlbar.suggest.searches': false,
  220. // Disable first run splash page on Windows 10
  221. 'browser.usedOnWindows10.introURL': '',
  222. // Do not warn on quitting Firefox
  223. 'browser.warnOnQuit': false,
  224. // Defensively disable data reporting systems
  225. 'datareporting.healthreport.documentServerURI': `http://${server}/dummy/healthreport/`,
  226. 'datareporting.healthreport.logging.consoleEnabled': false,
  227. 'datareporting.healthreport.service.enabled': false,
  228. 'datareporting.healthreport.service.firstRun': false,
  229. 'datareporting.healthreport.uploadEnabled': false,
  230. // Do not show datareporting policy notifications which can interfere with tests
  231. 'datareporting.policy.dataSubmissionEnabled': false,
  232. 'datareporting.policy.dataSubmissionPolicyBypassNotification': true,
  233. // DevTools JSONViewer sometimes fails to load dependencies with its require.js.
  234. // This doesn't affect Puppeteer but spams console (Bug 1424372)
  235. 'devtools.jsonview.enabled': false,
  236. // Disable popup-blocker
  237. 'dom.disable_open_during_load': false,
  238. // Enable the support for File object creation in the content process
  239. // Required for |Page.setFileInputFiles| protocol method.
  240. 'dom.file.createInChild': true,
  241. // Disable the ProcessHangMonitor
  242. 'dom.ipc.reportProcessHangs': false,
  243. // Disable slow script dialogues
  244. 'dom.max_chrome_script_run_time': 0,
  245. 'dom.max_script_run_time': 0,
  246. // Only load extensions from the application and user profile
  247. // AddonManager.SCOPE_PROFILE + AddonManager.SCOPE_APPLICATION
  248. 'extensions.autoDisableScopes': 0,
  249. 'extensions.enabledScopes': 5,
  250. // Disable metadata caching for installed add-ons by default
  251. 'extensions.getAddons.cache.enabled': false,
  252. // Disable installing any distribution extensions or add-ons.
  253. 'extensions.installDistroAddons': false,
  254. // Disabled screenshots extension
  255. 'extensions.screenshots.disabled': true,
  256. // Turn off extension updates so they do not bother tests
  257. 'extensions.update.enabled': false,
  258. // Turn off extension updates so they do not bother tests
  259. 'extensions.update.notifyUser': false,
  260. // Make sure opening about:addons will not hit the network
  261. 'extensions.webservice.discoverURL': `http://${server}/dummy/discoveryURL`,
  262. // Allow the application to have focus even it runs in the background
  263. 'focusmanager.testmode': true,
  264. // Disable useragent updates
  265. 'general.useragent.updates.enabled': false,
  266. // Always use network provider for geolocation tests so we bypass the
  267. // macOS dialog raised by the corelocation provider
  268. 'geo.provider.testing': true,
  269. // Do not scan Wifi
  270. 'geo.wifi.scan': false,
  271. // No hang monitor
  272. 'hangmonitor.timeout': 0,
  273. // Show chrome errors and warnings in the error console
  274. 'javascript.options.showInConsole': true,
  275. // Disable download and usage of OpenH264: and Widevine plugins
  276. 'media.gmp-manager.updateEnabled': false,
  277. // Disable the GFX sanity window
  278. 'media.sanity-test.disabled': true,
  279. // Disable experimental feature that is only available in Nightly
  280. 'network.cookie.sameSite.laxByDefault': false,
  281. // Do not prompt for temporary redirects
  282. 'network.http.prompt-temp-redirect': false,
  283. // Disable speculative connections so they are not reported as leaking
  284. // when they are hanging around
  285. 'network.http.speculative-parallel-limit': 0,
  286. // Do not automatically switch between offline and online
  287. 'network.manage-offline-status': false,
  288. // Make sure SNTP requests do not hit the network
  289. 'network.sntp.pools': server,
  290. // Disable Flash.
  291. 'plugin.state.flash': 0,
  292. 'privacy.trackingprotection.enabled': false,
  293. // Can be removed once Firefox 89 is no longer supported
  294. // https://bugzilla.mozilla.org/show_bug.cgi?id=1710839
  295. 'remote.enabled': true,
  296. // Don't do network connections for mitm priming
  297. 'security.certerrors.mitm.priming.enabled': false,
  298. // Local documents have access to all other local documents,
  299. // including directory listings
  300. 'security.fileuri.strict_origin_policy': false,
  301. // Do not wait for the notification button security delay
  302. 'security.notification_enable_delay': 0,
  303. // Ensure blocklist updates do not hit the network
  304. 'services.settings.server': `http://${server}/dummy/blocklist/`,
  305. // Do not automatically fill sign-in forms with known usernames and
  306. // passwords
  307. 'signon.autofillForms': false,
  308. // Disable password capture, so that tests that include forms are not
  309. // influenced by the presence of the persistent doorhanger notification
  310. 'signon.rememberSignons': false,
  311. // Disable first-run welcome page
  312. 'startup.homepage_welcome_url': 'about:blank',
  313. // Disable first-run welcome page
  314. 'startup.homepage_welcome_url.additional': '',
  315. // Disable browser animations (tabs, fullscreen, sliding alerts)
  316. 'toolkit.cosmeticAnimations.enabled': false,
  317. // Prevent starting into safe mode after application crashes
  318. 'toolkit.startup.max_resumed_crashes': -1,
  319. };
  320. return Object.assign(defaultPrefs, extraPrefs);
  321. }
  322. /**
  323. * Populates the user.js file with custom preferences as needed to allow
  324. * Firefox's CDP support to properly function. These preferences will be
  325. * automatically copied over to prefs.js during startup of Firefox. To be
  326. * able to restore the original values of preferences a backup of prefs.js
  327. * will be created.
  328. *
  329. * @param prefs - List of preferences to add.
  330. * @param profilePath - Firefox profile to write the preferences to.
  331. */
  332. async function writePreferences(options) {
  333. const lines = Object.entries(options.preferences).map(([key, value]) => {
  334. return `user_pref(${JSON.stringify(key)}, ${JSON.stringify(value)});`;
  335. });
  336. await fs_1.default.promises.writeFile(path_1.default.join(options.path, 'user.js'), lines.join('\n'));
  337. // Create a backup of the preferences file if it already exitsts.
  338. const prefsPath = path_1.default.join(options.path, 'prefs.js');
  339. if (fs_1.default.existsSync(prefsPath)) {
  340. const prefsBackupPath = path_1.default.join(options.path, 'prefs.js.puppeteer');
  341. await fs_1.default.promises.copyFile(prefsPath, prefsBackupPath);
  342. }
  343. }
  344. function compareVersions(a, b) {
  345. // TODO: this is a not very reliable check.
  346. return parseInt(a.replace('.', ''), 16) - parseInt(b.replace('.', ''), 16);
  347. }
  348. exports.compareVersions = compareVersions;
  349. //# sourceMappingURL=firefox.js.map