Skip to content

Commit

Permalink
fix: fallback to hardcoded Edge/Chrome/Firefox Windows browser paths …
Browse files Browse the repository at this point in the history
…if user's default browser no longer exists

To reproduce:

- Use Windows
- Set your default browser to Firefox (Settings -> Apps -> Default Apps -> Web Browser)
- Rename your firefox.exe to something.else

The BrowserOpenURL before this commit silently fails.

The BrowserOpenURL after this commit tries to use a hardcoded fallback browser.
If successful, a warning message is logged.
If unsuccessful (default browser and fallback browsers fail), an error message is logged.
  • Loading branch information
Sean committed Feb 16, 2024
1 parent 9c3f91b commit cdeac67
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
4 changes: 3 additions & 1 deletion v2/internal/frontend/desktop/darwin/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ import (
// BrowserOpenURL Use the default browser to open the url
func (f *Frontend) BrowserOpenURL(url string) {
// Specific method implementation
_ = browser.OpenURL(url)
if err := browser.OpenURL(url); err != nil {
f.logger.Error("Unable to open default system browser")
}
}
4 changes: 3 additions & 1 deletion v2/internal/frontend/desktop/linux/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ import "github.com/pkg/browser"
// BrowserOpenURL Use the default browser to open the url
func (f *Frontend) BrowserOpenURL(url string) {
// Specific method implementation
_ = browser.OpenURL(url)
if err := browser.OpenURL(url); err != nil {
f.logger.Error("Unable to open default system browser")
}
}
23 changes: 22 additions & 1 deletion v2/internal/frontend/desktop/windows/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,31 @@ package windows

import (
"github.com/pkg/browser"
"golang.org/x/sys/windows"
)

var fallbackBrowserPaths = []string{
`\Program Files (x86)\Microsoft\Edge\Application\msedge.exe`,
`\Program Files\Google\Chrome\Application\chrome.exe`,
`\Program Files (x86)\Google\Chrome\Application\chrome.exe`,
`\Program Files\Mozilla Firefox\firefox.exe`,
}

// BrowserOpenURL Use the default browser to open the url
func (f *Frontend) BrowserOpenURL(url string) {
// Specific method implementation
_ = browser.OpenURL(url)
err := browser.OpenURL(url)
if err == nil {
return
}
for _, fallback := range fallbackBrowserPaths {
if err := openBrowser(fallback, url); err == nil {
return
}
}
f.logger.Error("Unable to open default system browser")
}

func openBrowser(path, url string) error {
return windows.ShellExecute(0, nil, windows.StringToUTF16Ptr(path), windows.StringToUTF16Ptr(url), nil, windows.SW_SHOWNORMAL)
}

0 comments on commit cdeac67

Please sign in to comment.