|
2 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
3 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */
|
4 | 4 |
|
5 |
| -const electron = require('electron') |
6 |
| -const path = require('path') |
7 |
| -const childProcess = require('child_process') |
8 |
| -const spawn = childProcess.spawn |
9 |
| -const spawnSync = childProcess.spawnSync |
10 |
| -const execSync = childProcess.execSync |
11 |
| -const app = electron.app |
12 |
| -const Channel = require('./channel') |
13 |
| -const cmdLine = require('./cmdLine') |
14 |
| -const promoCodeFirstRunStorage = require('./promoCodeFirstRunStorage') |
15 |
| - |
16 |
| -let appUserModelId = 'com.squirrel.brave.Brave' |
17 |
| -switch (Channel.channel()) { |
18 |
| - case 'nightly': |
19 |
| - appUserModelId = 'com.squirrel.BraveNightly.BraveNightly' |
20 |
| - break |
21 |
| - case 'developer': |
22 |
| - appUserModelId = 'com.squirrel.BraveDeveloper.BraveDeveloper' |
23 |
| - break |
24 |
| - case 'beta': |
25 |
| - appUserModelId = 'com.squirrel.BraveBeta.BraveBeta' |
26 |
| - break |
27 |
| - case 'dev': |
28 |
| - appUserModelId = 'com.squirrel.brave.Brave' |
29 |
| - break |
30 |
| - default: |
31 |
| - appUserModelId = 'com.squirrel.brave.Brave' |
32 |
| - break |
33 |
| -} |
34 |
| - |
35 |
| -const getBraveBinPath = () => { |
36 |
| - const appPath = app.getPath('exe') |
37 |
| - return path.dirname(appPath) |
38 |
| -} |
| 5 | +if (process.platform === 'win32') { |
| 6 | + const electron = require('electron') |
| 7 | + const path = require('path') |
| 8 | + const childProcess = require('child_process') |
| 9 | + const spawn = childProcess.spawn |
| 10 | + const spawnSync = childProcess.spawnSync |
| 11 | + const execSync = childProcess.execSync |
| 12 | + const app = electron.app |
| 13 | + const fs = require('fs') |
| 14 | + const Channel = require('./channel') |
| 15 | + const cmdLine = require('./cmdLine') |
| 16 | + const promoCodeFirstRunStorage = require('./promoCodeFirstRunStorage') |
39 | 17 |
|
40 |
| -const getBraveDefaultsBinPath = () => { |
41 |
| - const appDir = getBraveBinPath() |
42 |
| - return path.join(appDir, 'resources', 'braveDefaults.exe') |
43 |
| -} |
| 18 | + let appUserModelId = 'com.squirrel.brave.Brave' |
| 19 | + switch (Channel.channel()) { |
| 20 | + case 'nightly': |
| 21 | + appUserModelId = 'com.squirrel.BraveNightly.BraveNightly' |
| 22 | + break |
| 23 | + case 'developer': |
| 24 | + appUserModelId = 'com.squirrel.BraveDeveloper.BraveDeveloper' |
| 25 | + break |
| 26 | + case 'beta': |
| 27 | + appUserModelId = 'com.squirrel.BraveBeta.BraveBeta' |
| 28 | + break |
| 29 | + case 'dev': |
| 30 | + appUserModelId = 'com.squirrel.brave.Brave' |
| 31 | + break |
| 32 | + default: |
| 33 | + appUserModelId = 'com.squirrel.brave.Brave' |
| 34 | + break |
| 35 | + } |
44 | 36 |
|
45 |
| -const getVisualElementsManifestPath = () => { |
46 |
| - const appDir = getBraveBinPath() |
47 |
| - return path.join(appDir, 'resources', 'Update.VisualElementsManifest.xml') |
48 |
| -} |
| 37 | + const getBraveBinPath = () => { |
| 38 | + const appPath = app.getPath('exe') |
| 39 | + return path.dirname(appPath) |
| 40 | + } |
49 | 41 |
|
50 |
| -function CopyManifestFile () { |
51 |
| - const versionedRoot = getBraveBinPath() |
52 |
| - let updateRoot = versionedRoot.split('\\') |
53 |
| - updateRoot.pop() |
54 |
| - updateRoot = updateRoot.join('\\') |
55 |
| - const cmd = 'copy "' + getVisualElementsManifestPath() + '" "' + updateRoot + '"' |
56 |
| - execSync(cmd) |
57 |
| -} |
| 42 | + const getBraveDefaultsBinPath = () => { |
| 43 | + const appDir = getBraveBinPath() |
| 44 | + return path.join(appDir, 'resources', 'braveDefaults.exe') |
| 45 | + } |
58 | 46 |
|
59 |
| -// windows installation events etc... |
60 |
| -if (process.platform === 'win32') { |
61 |
| - const shouldQuit = require('electron-squirrel-startup') |
62 |
| - const channel = Channel.channel() |
63 |
| - const isSquirrelInstall = process.argv.includes('--squirrel-install') |
64 |
| - const isSquirrelUpdate = process.argv.includes('--squirrel-updated') |
65 |
| - const isSquirrelUninstall = process.argv.includes('--squirrel-uninstall') |
66 |
| - const isSquirrelFirstRun = process.argv.includes('--squirrel-firstrun') |
67 |
| - // handle running as part of install process |
68 |
| - if (isSquirrelInstall) { |
69 |
| - // determine if promo code was provided by setup.exe |
70 |
| - const promoCode = cmdLine.getFirstRunPromoCode() |
71 |
| - if (promoCode) { |
72 |
| - // write promo code so state can access it |
73 |
| - promoCodeFirstRunStorage.writeFirstRunPromoCodeSync(promoCode) |
74 |
| - } |
| 47 | + const getVisualElementsManifestPath = () => { |
| 48 | + const appDir = getBraveBinPath() |
| 49 | + return path.join(appDir, 'resources', 'Update.VisualElementsManifest.xml') |
75 | 50 | }
|
76 |
| - // first-run after install / update |
77 |
| - if (isSquirrelInstall || isSquirrelUpdate) { |
78 |
| - // The manifest file is used to customize the look of the Start menu tile. |
79 |
| - // This function copies it from the versioned folder to the parent folder |
80 |
| - // (where the auto-update executable lives) |
81 |
| - CopyManifestFile() |
82 |
| - // Launch defaults helper to add defaults on install |
83 |
| - spawn(getBraveDefaultsBinPath(), [], { detached: true }) |
84 |
| - } else if (isSquirrelUninstall) { |
85 |
| - // Launch defaults helper to remove defaults on uninstall |
86 |
| - // Sync to avoid file path in use on uninstall |
87 |
| - spawnSync(getBraveDefaultsBinPath(), ['-uninstall']) |
| 51 | + |
| 52 | + const copyManifestFile = () => { |
| 53 | + const versionedRoot = getBraveBinPath() |
| 54 | + let updateRoot = versionedRoot.split('\\') |
| 55 | + updateRoot.pop() |
| 56 | + updateRoot = updateRoot.join('\\') |
| 57 | + const cmd = 'copy "' + getVisualElementsManifestPath() + '" "' + updateRoot + '"' |
| 58 | + execSync(cmd) |
88 | 59 | }
|
89 | 60 |
|
90 |
| - if (shouldQuit(channel)) { |
91 |
| - process.exit(0) |
| 61 | + const getBraveCoreInstallPath = () => { |
| 62 | + const braveCoreInstallLocations = [ |
| 63 | + '%USERPROFILE%\\AppData\\Local\\BraveSoftware\\Brave-Browser\\Application', |
| 64 | + '%ProgramFiles(x86)%\\BraveSoftware\\Brave-Browser\\Application', |
| 65 | + '%ProgramFiles%\\BraveSoftware\\Brave-Browser\\Application' |
| 66 | + ] |
| 67 | + |
| 68 | + // check for existing installations |
| 69 | + for (let i = 0; i < braveCoreInstallLocations.length; i++) { |
| 70 | + const path = braveCoreInstallLocations[i] |
| 71 | + const resolvedPath = path.replace(/%([^%]+)%/g, function (_, variableToResolve) { |
| 72 | + return process.env[variableToResolve] |
| 73 | + }) |
| 74 | + if (fs.existsSync(resolvedPath)) { |
| 75 | + console.log(`brave-core already installed at "${resolvedPath}"`) |
| 76 | + return resolvedPath |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return false |
92 | 81 | }
|
93 | 82 |
|
94 |
| - const userDataDirSwitch = '--user-data-dir-name=brave-' + channel |
95 |
| - if (channel !== 'dev' && !process.argv.includes(userDataDirSwitch) && |
96 |
| - !process.argv.includes('--relaunch') && |
97 |
| - !process.argv.includes('--user-data-dir-name=brave-development')) { |
98 |
| - delete process.env.CHROME_USER_DATA_DIR |
99 |
| - if (isSquirrelFirstRun) { |
100 |
| - app.relaunch({args: [userDataDirSwitch, '--relaunch']}) |
101 |
| - } else { |
102 |
| - app.relaunch({args: process.argv.slice(1).concat([userDataDirSwitch, '--relaunch'])}) |
| 83 | + module.exports = function () { |
| 84 | + const shouldQuit = require('electron-squirrel-startup') |
| 85 | + const channel = Channel.channel() |
| 86 | + const isSquirrelInstall = process.argv.includes('--squirrel-install') |
| 87 | + const isSquirrelUpdate = process.argv.includes('--squirrel-updated') |
| 88 | + const isSquirrelUninstall = process.argv.includes('--squirrel-uninstall') |
| 89 | + const isSquirrelFirstRun = process.argv.includes('--squirrel-firstrun') |
| 90 | + |
| 91 | + // Events like `--squirrel-install` and `--squirrel-updated` |
| 92 | + // are fired by Update.exe DURING the install/upgrade. Since |
| 93 | + // we don't intend to actually launch the executable, we need |
| 94 | + // to exit after performing housekeeping tasks. |
| 95 | + if (isSquirrelInstall || isSquirrelUpdate) { |
| 96 | + // Detect promoCode (via CLI) and write to disk for later use |
| 97 | + const promoCode = isSquirrelInstall && cmdLine.getFirstRunPromoCode() |
| 98 | + if (promoCode) { |
| 99 | + promoCodeFirstRunStorage.writeFirstRunPromoCodeSync(promoCode) |
| 100 | + } |
| 101 | + // The manifest file is used to customize the look of the Start menu tile. |
| 102 | + // This function copies it from the versioned folder to the parent folder |
| 103 | + // (where the auto-update executable lives) |
| 104 | + copyManifestFile() |
| 105 | + // Launch defaults helper to add defaults on install |
| 106 | + spawn(getBraveDefaultsBinPath(), [], { detached: true }) |
| 107 | + } else if (isSquirrelUninstall) { |
| 108 | + // Launch defaults helper to remove defaults on uninstall |
| 109 | + // Sync to avoid file path in use on uninstall |
| 110 | + spawnSync(getBraveDefaultsBinPath(), ['-uninstall']) |
| 111 | + } |
| 112 | + |
| 113 | + // Quit if this is only an install or update event. |
| 114 | + // This logic also creates the shortcuts (desktop, etc) |
| 115 | + if (shouldQuit(channel)) { |
| 116 | + process.exit(0) |
| 117 | + } |
| 118 | + |
| 119 | + // If executable is on BETA/DEVELOPER/NIGHTLY channels, there |
| 120 | + // is a different `user-data-dir-name`. This logic will |
| 121 | + // relaunch using the proper profile if it was omitted for |
| 122 | + // some reason. |
| 123 | + const userDataDirSwitch = '--user-data-dir-name=brave-' + channel |
| 124 | + if (channel !== 'dev' && !process.argv.includes(userDataDirSwitch) && |
| 125 | + !process.argv.includes('--relaunch') && |
| 126 | + !process.argv.includes('--user-data-dir-name=brave-development')) { |
| 127 | + delete process.env.CHROME_USER_DATA_DIR |
| 128 | + if (isSquirrelFirstRun) { |
| 129 | + app.relaunch({args: [userDataDirSwitch, '--relaunch']}) |
| 130 | + } else { |
| 131 | + app.relaunch({args: process.argv.slice(1).concat([userDataDirSwitch, '--relaunch'])}) |
| 132 | + } |
| 133 | + app.exit() |
| 134 | + return |
103 | 135 | }
|
104 |
| - app.exit() |
| 136 | + |
| 137 | + app.on('will-finish-launching', () => { |
| 138 | + app.setAppUserModelId(appUserModelId) |
| 139 | + }) |
| 140 | + |
| 141 | + // If brave-core is installed, find the path and version |
| 142 | + const braveCoreInstallPath = getBraveCoreInstallPath() |
| 143 | + if (braveCoreInstallPath) { |
| 144 | + const getVersionCmd = `wmic datafile where name='${braveCoreInstallPath.replace(/\\/g, '\\\\')}\\\\brave.exe' get Version /value` |
| 145 | + let braveCoreVersion |
| 146 | + try { |
| 147 | + // format will be like `Version=70.0.56.8` |
| 148 | + braveCoreVersion = execSync(getVersionCmd).toString().trim() |
| 149 | + const keyValue = braveCoreVersion.split('=') |
| 150 | + if (keyValue.length === 2) { |
| 151 | + // remove the Chromium version from the string |
| 152 | + const versionAsArray = keyValue[1].split('.') |
| 153 | + if (versionAsArray.length === 4) { |
| 154 | + braveCoreVersion = versionAsArray.slice(1).join('.') |
| 155 | + } |
| 156 | + } |
| 157 | + } catch (e) {} |
| 158 | + |
| 159 | + return {braveCoreInstalled: true, braveCoreInstallPath, braveCoreVersion} |
| 160 | + } |
| 161 | + |
| 162 | + return {braveCoreInstalled: false} |
105 | 163 | }
|
106 | 164 | }
|
107 |
| - |
108 |
| -app.on('will-finish-launching', () => { |
109 |
| - app.setAppUserModelId(appUserModelId) |
110 |
| -}) |
|
0 commit comments