Skip to content

Commit

Permalink
Fix addons startup (portapps/phyrox-portable#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazy-max committed May 29, 2020
1 parent b583e80 commit bcdd25e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 69 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 68.8.0-37 (2020/05/29)

* Fix addons startup (portapps/phyrox-portable#3)
* Portapps 2.4.4

## 68.8.0-36 (2020/05/10)

* Firefox ESR 68.8.0
Expand Down
111 changes: 42 additions & 69 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"fmt"
"html/template"
"io/ioutil"
"net/url"
"os"
"path"
"strings"

"github.com/Jeffail/gabs"
"github.com/pkg/errors"
Expand Down Expand Up @@ -66,6 +66,36 @@ func main() {
profileFolder,
}

// Set env vars
crashreporterFolder := utl.CreateFolder(app.DataPath, "crashreporter")
pluginsFolder := utl.CreateFolder(app.DataPath, "plugins")
utl.OverrideEnv("MOZ_CRASHREPORTER", "0")
utl.OverrideEnv("MOZ_CRASHREPORTER_DATA_DIRECTORY", crashreporterFolder)
utl.OverrideEnv("MOZ_CRASHREPORTER_DISABLE", "1")
utl.OverrideEnv("MOZ_CRASHREPORTER_NO_REPORT", "1")
utl.OverrideEnv("MOZ_DATA_REPORTING", "0")
utl.OverrideEnv("MOZ_MAINTENANCE_SERVICE", "0")
utl.OverrideEnv("MOZ_PLUGIN_PATH", pluginsFolder)
utl.OverrideEnv("MOZ_UPDATER", "0")

// Create and check mutex
mu, err := mutex.New(app.ID)
defer mu.Release()
if err != nil {
if !cfg.MultipleInstances {
log.Error().Msg("You have to enable multiple instances in your configuration if you want to launch another instance")
if _, err = dialog.MsgBox(
fmt.Sprintf("%s portable", app.Name),
"Other instance detected. You have to enable multiple instances in your configuration if you want to launch another instance.",
dialog.MsgBoxBtnOk|dialog.MsgBoxIconError); err != nil {
log.Error().Err(err).Msg("Cannot create dialog box")
}
return
} else {
log.Warn().Msg("Another instance is already running")
}
}

// Cleanup on exit
if cfg.Cleanup {
defer func() {
Expand Down Expand Up @@ -136,36 +166,6 @@ pref("browser.startup.homepage_override.mstone", "ignore");
log.Error().Err(err).Msg("Cannot fix extensions path")
}

// Set env vars
crashreporterFolder := utl.CreateFolder(app.DataPath, "crashreporter")
pluginsFolder := utl.CreateFolder(app.DataPath, "plugins")
utl.OverrideEnv("MOZ_CRASHREPORTER", "0")
utl.OverrideEnv("MOZ_CRASHREPORTER_DATA_DIRECTORY", crashreporterFolder)
utl.OverrideEnv("MOZ_CRASHREPORTER_DISABLE", "1")
utl.OverrideEnv("MOZ_CRASHREPORTER_NO_REPORT", "1")
utl.OverrideEnv("MOZ_DATA_REPORTING", "0")
utl.OverrideEnv("MOZ_MAINTENANCE_SERVICE", "0")
utl.OverrideEnv("MOZ_PLUGIN_PATH", pluginsFolder)
utl.OverrideEnv("MOZ_UPDATER", "0")

// Create and check mutex
mu, err := mutex.New(app.ID)
defer mu.Release()
if err != nil {
if !cfg.MultipleInstances {
log.Error().Msg("You have to enable multiple instances in your configuration if you want to launch another instance")
if _, err = dialog.MsgBox(
fmt.Sprintf("%s portable", app.Name),
"Other instance detected. You have to enable multiple instances in your configuration if you want to launch another instance.",
dialog.MsgBoxBtnOk|dialog.MsgBoxIconError); err != nil {
log.Error().Err(err).Msg("Cannot create dialog box")
}
return
} else {
log.Warn().Msg("Another instance is already running")
}
}

// Copy default shortcut
shortcutPath := path.Join(os.Getenv("APPDATA"), "Microsoft", "Windows", "Start Menu", "Programs", "Phyrox ESR Portable.lnk")
defaultShortcut, err := assets.Asset("FirefoxESR.lnk")
Expand Down Expand Up @@ -267,55 +267,28 @@ func checkLocale() (string, error) {
}

func updateAddonStartup(profileFolder string) error {
asLz4 := path.Join(profileFolder, "addonStartup.json.lz4")
if !utl.Exists(asLz4) {
lz4File := path.Join(profileFolder, "addonStartup.json.lz4")
if !utl.Exists(lz4File) || app.Prev.RootPath == "" {
return nil
}

decAsLz4, err := mozLz4Decompress(asLz4)
lz4Raw, err := mozLz4Decompress(lz4File)
if err != nil {
return err
}

jsonAs, err := gabs.ParseJSON(decAsLz4)
if err != nil {
return err
}
prevPathLin := strings.Replace(utl.FormatUnixPath(app.Prev.RootPath), ` `, `%20`, -1)
currPathLin := strings.Replace(utl.FormatUnixPath(app.RootPath), ` `, `%20`, -1)
lz4Str := strings.Replace(string(lz4Raw), prevPathLin, currPathLin, -1)

if err := updateAddons("app-global", utl.PathJoin(profileFolder, "extensions"), jsonAs); err != nil {
return err
}
if err := updateAddons("app-profile", utl.PathJoin(profileFolder, "extensions"), jsonAs); err != nil {
return err
}
if err := updateAddons("app-system-defaults", utl.PathJoin(app.AppPath, "browser", "features"), jsonAs); err != nil {
return err
}
log.Debug().Msgf("Updated addonStartup.json: %s", jsonAs.String())
prevPathWin := strings.Replace(strings.Replace(utl.FormatWindowsPath(app.Prev.RootPath), `\`, `\\`, -1), ` `, `%20`, -1)
currPathWin := strings.Replace(strings.Replace(utl.FormatWindowsPath(app.RootPath), `\`, `\\`, -1), ` `, `%20`, -1)
lz4Str = strings.Replace(lz4Str, prevPathWin, currPathWin, -1)

encAsLz4, err := mozLz4Compress(jsonAs.Bytes())
lz4Enc, err := mozLz4Compress([]byte(lz4Str))
if err != nil {
return err
}

return ioutil.WriteFile(asLz4, encAsLz4, 0644)
}

func updateAddons(field string, basePath string, container *gabs.Container) error {
if _, ok := container.Search(field, "path").Data().(string); !ok {
return nil
}
if _, err := container.Set(basePath, field, "path"); err != nil {
return errors.Wrap(err, fmt.Sprintf("couldn't set %s.path", field))
}

addons, _ := container.S(field, "addons").ChildrenMap()
for key, addon := range addons {
_, err := addon.Set(fmt.Sprintf("jar:file:///%s/%s.xpi!/", utl.FormatUnixPath(basePath), url.PathEscape(key)), "rootURI")
if err != nil {
return errors.Wrap(err, fmt.Sprintf("couldn't set %s %s.rootURI", field, key))
}
}

return nil
return ioutil.WriteFile(lz4File, lz4Enc, 0644)
}

0 comments on commit bcdd25e

Please sign in to comment.