Skip to content

Commit bc4ce76

Browse files
authored
refactor(browser): deduplicate browser executable candidates (#129323)
1 parent 233bbda commit bc4ce76

2 files changed

Lines changed: 77 additions & 107 deletions

File tree

‎extensions/browser/src/browser/chrome.default-browser.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,52 @@ describe("browser default executable detection", () => {
232232
expect(exe?.path).toContain("Google Chrome.app/Contents/MacOS/Google Chrome");
233233
});
234234

235+
it("preserves vendor-first macOS browser discovery across system and user applications", () => {
236+
vi.mocked(fs.existsSync).mockReturnValue(false);
237+
238+
expect(
239+
resolveBrowserExecutableForPlatform(
240+
{} as Parameters<typeof resolveBrowserExecutableForPlatform>[0],
241+
"darwin",
242+
),
243+
).toBeNull();
244+
expect(
245+
vi
246+
.mocked(fs.existsSync)
247+
.mock.calls.map(([candidate]) => String(candidate))
248+
.filter((candidate) => candidate.includes(".app/Contents/MacOS/")),
249+
).toEqual([
250+
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
251+
"/Users/test/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
252+
"/Applications/Brave Browser.app/Contents/MacOS/Brave Browser",
253+
"/Users/test/Applications/Brave Browser.app/Contents/MacOS/Brave Browser",
254+
"/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
255+
"/Users/test/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
256+
"/Applications/Chromium.app/Contents/MacOS/Chromium",
257+
"/Users/test/Applications/Chromium.app/Contents/MacOS/Chromium",
258+
"/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary",
259+
"/Users/test/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary",
260+
]);
261+
});
262+
263+
it.each([
264+
["chrome", "Google Chrome"],
265+
["brave", "Brave Browser"],
266+
["edge", "Microsoft Edge"],
267+
["chromium", "Chromium"],
268+
["canary", "Google Chrome Canary"],
269+
])("preserves the %s kind for a user-installed macOS browser", (kind, appName) => {
270+
const expectedPath = `/Users/test/Applications/${appName}.app/Contents/MacOS/${appName}`;
271+
vi.mocked(fs.existsSync).mockImplementation((candidate) => String(candidate) === expectedPath);
272+
273+
expect(
274+
resolveBrowserExecutableForPlatform(
275+
{} as Parameters<typeof resolveBrowserExecutableForPlatform>[0],
276+
"darwin",
277+
),
278+
).toEqual({ kind, path: expectedPath });
279+
});
280+
235281
it("resolves an Opera default-browser launcher to the directly owned binary on Windows", () => {
236282
const installDir = "C:\\Users\\test\\AppData\\Local\\Programs\\Opera";
237283
const launcher = `${installDir}\\launcher.exe`;

‎extensions/browser/src/browser/chrome.executables.ts‎

Lines changed: 31 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -612,54 +612,20 @@ function readSortedDirNames(dir: string): string[] {
612612

613613
/** Find the best Chromium-family executable on macOS. */
614614
function findChromeExecutableMac(): BrowserExecutable | null {
615-
const candidates: Array<BrowserExecutable> = [
616-
{
617-
kind: "chrome",
618-
path: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
619-
},
620-
{
621-
kind: "chrome",
622-
path: path.join(os.homedir(), "Applications/Google Chrome.app/Contents/MacOS/Google Chrome"),
623-
},
624-
{
625-
kind: "brave",
626-
path: "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser",
627-
},
628-
{
629-
kind: "brave",
630-
path: path.join(os.homedir(), "Applications/Brave Browser.app/Contents/MacOS/Brave Browser"),
631-
},
632-
{
633-
kind: "edge",
634-
path: "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
635-
},
636-
{
637-
kind: "edge",
638-
path: path.join(
639-
os.homedir(),
640-
"Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
641-
),
642-
},
643-
{
644-
kind: "chromium",
645-
path: "/Applications/Chromium.app/Contents/MacOS/Chromium",
646-
},
647-
{
648-
kind: "chromium",
649-
path: path.join(os.homedir(), "Applications/Chromium.app/Contents/MacOS/Chromium"),
650-
},
651-
{
652-
kind: "canary",
653-
path: "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary",
654-
},
655-
{
656-
kind: "canary",
657-
path: path.join(
658-
os.homedir(),
659-
"Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary",
660-
),
661-
},
615+
const applications: Array<[BrowserExecutable["kind"], string]> = [
616+
["chrome", "Google Chrome"],
617+
["brave", "Brave Browser"],
618+
["edge", "Microsoft Edge"],
619+
["chromium", "Chromium"],
620+
["canary", "Google Chrome Canary"],
662621
];
622+
const roots = ["/Applications", path.join(os.homedir(), "Applications")];
623+
const candidates = applications.flatMap(([kind, name]) =>
624+
roots.map((root) => ({
625+
kind,
626+
path: path.join(root, `${name}.app`, "Contents", "MacOS", name),
627+
})),
628+
);
663629

664630
return findFirstExecutable(candidates, "darwin");
665631
}
@@ -721,68 +687,26 @@ function findGoogleChromeExecutableLinux(): BrowserExecutable | null {
721687
/** Find the best Chromium-family executable on Windows. */
722688
function findChromeExecutableWindows(): BrowserExecutable | null {
723689
const { localAppData, programFiles, programFilesX86 } = resolveWindowsBrowserInstallRoots();
724-
const joinWin = path.win32.join;
725-
const candidates: Array<BrowserExecutable> = [];
726-
727-
if (localAppData) {
728-
// Chrome (user install)
729-
candidates.push({
730-
kind: "chrome",
731-
path: joinWin(localAppData, "Google", "Chrome", "Application", "chrome.exe"),
732-
});
733-
// Brave (user install)
734-
candidates.push({
735-
kind: "brave",
736-
path: joinWin(localAppData, "BraveSoftware", "Brave-Browser", "Application", "brave.exe"),
737-
});
738-
// Edge (user install)
739-
candidates.push({
740-
kind: "edge",
741-
path: joinWin(localAppData, "Microsoft", "Edge", "Application", "msedge.exe"),
742-
});
743-
// Chromium (user install)
744-
candidates.push({
745-
kind: "chromium",
746-
path: joinWin(localAppData, "Chromium", "Application", "chrome.exe"),
747-
});
748-
// Chrome Canary (user install)
749-
candidates.push({
750-
kind: "canary",
751-
path: joinWin(localAppData, "Google", "Chrome SxS", "Application", "chrome.exe"),
752-
});
690+
const browsers: Array<[BrowserExecutable["kind"], ...string[]]> = [
691+
["chrome", "Google", "Chrome", "Application", "chrome.exe"],
692+
["brave", "BraveSoftware", "Brave-Browser", "Application", "brave.exe"],
693+
["edge", "Microsoft", "Edge", "Application", "msedge.exe"],
694+
["chromium", "Chromium", "Application", "chrome.exe"],
695+
["canary", "Google", "Chrome SxS", "Application", "chrome.exe"],
696+
];
697+
const candidates: BrowserExecutable[] = localAppData
698+
? browsers.map(([kind, ...segments]) => ({
699+
kind,
700+
path: path.win32.join(localAppData, ...segments),
701+
}))
702+
: [];
703+
704+
for (const [kind, ...segments] of browsers.slice(0, 3)) {
705+
for (const root of [programFiles, programFilesX86]) {
706+
candidates.push({ kind, path: path.win32.join(root, ...segments) });
707+
}
753708
}
754709

755-
// Chrome (system install, 64-bit)
756-
candidates.push({
757-
kind: "chrome",
758-
path: joinWin(programFiles, "Google", "Chrome", "Application", "chrome.exe"),
759-
});
760-
// Chrome (system install, 32-bit on 64-bit Windows)
761-
candidates.push({
762-
kind: "chrome",
763-
path: joinWin(programFilesX86, "Google", "Chrome", "Application", "chrome.exe"),
764-
});
765-
// Brave (system install, 64-bit)
766-
candidates.push({
767-
kind: "brave",
768-
path: joinWin(programFiles, "BraveSoftware", "Brave-Browser", "Application", "brave.exe"),
769-
});
770-
// Brave (system install, 32-bit on 64-bit Windows)
771-
candidates.push({
772-
kind: "brave",
773-
path: joinWin(programFilesX86, "BraveSoftware", "Brave-Browser", "Application", "brave.exe"),
774-
});
775-
// Edge (system install, 64-bit)
776-
candidates.push({
777-
kind: "edge",
778-
path: joinWin(programFiles, "Microsoft", "Edge", "Application", "msedge.exe"),
779-
});
780-
// Edge (system install, 32-bit on 64-bit Windows)
781-
candidates.push({
782-
kind: "edge",
783-
path: joinWin(programFilesX86, "Microsoft", "Edge", "Application", "msedge.exe"),
784-
});
785-
786710
return findFirstExecutable(candidates, "win32");
787711
}
788712

0 commit comments

Comments
 (0)