2023-09-05 17:45:21 +00:00
|
|
|
// Configuration
|
2022-10-23 13:08:06 +00:00
|
|
|
package firefox
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2023-02-18 23:13:23 +00:00
|
|
|
"git.blob42.xyz/gomark/gosuki/config"
|
|
|
|
"git.blob42.xyz/gomark/gosuki/database"
|
2023-09-05 17:45:21 +00:00
|
|
|
"git.blob42.xyz/gomark/gosuki/modules"
|
2023-02-18 23:13:23 +00:00
|
|
|
"git.blob42.xyz/gomark/gosuki/mozilla"
|
|
|
|
"git.blob42.xyz/gomark/gosuki/parsing"
|
|
|
|
"git.blob42.xyz/gomark/gosuki/tree"
|
|
|
|
"git.blob42.xyz/gomark/gosuki/utils"
|
2022-10-23 13:08:06 +00:00
|
|
|
|
|
|
|
"github.com/fatih/structs"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2023-09-05 17:45:21 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2022-10-23 13:08:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-11-07 20:58:35 +00:00
|
|
|
BrowserName = "firefox"
|
|
|
|
FirefoxConfigDir = "$HOME/.mozilla/firefox"
|
|
|
|
DefaultProfile = "default"
|
2022-10-23 13:08:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
2023-02-20 00:55:16 +00:00
|
|
|
// firefox global config state.
|
2022-10-27 22:33:20 +00:00
|
|
|
FFConfig = &FirefoxConfig{
|
2023-01-31 21:21:39 +00:00
|
|
|
BrowserConfig: &modules.BrowserConfig{
|
2022-11-07 20:58:35 +00:00
|
|
|
Name: BrowserName,
|
2023-01-31 21:21:39 +00:00
|
|
|
Type: modules.TFirefox,
|
2022-11-07 20:58:35 +00:00
|
|
|
BkDir: "",
|
|
|
|
BkFile: mozilla.PlacesFile,
|
|
|
|
WatchedPaths: []string{},
|
|
|
|
NodeTree: &tree.Node{
|
2022-12-15 21:15:11 +00:00
|
|
|
Name: mozilla.RootName,
|
2022-11-07 20:58:35 +00:00
|
|
|
Parent: nil,
|
|
|
|
Type: tree.RootNode,
|
|
|
|
},
|
2022-10-27 22:33:20 +00:00
|
|
|
Stats: &parsing.Stats{},
|
|
|
|
UseFileWatcher: true,
|
|
|
|
},
|
2022-10-23 13:08:06 +00:00
|
|
|
|
|
|
|
// Default data source name query options for `places.sqlite` db
|
|
|
|
PlacesDSN: database.DsnOptions{
|
|
|
|
"_journal_mode": "WAL",
|
|
|
|
},
|
|
|
|
|
2023-09-05 17:45:21 +00:00
|
|
|
// default profile to use, can be selected as cli argument
|
2022-11-07 19:07:13 +00:00
|
|
|
Profile: DefaultProfile,
|
2022-10-23 13:08:06 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-07 19:07:13 +00:00
|
|
|
ffProfileLoader = &mozilla.INIProfileLoader{
|
2022-10-23 13:08:06 +00:00
|
|
|
//BasePath to be set at runtime in init
|
|
|
|
ProfilesFile: mozilla.ProfilesFile,
|
|
|
|
}
|
|
|
|
|
|
|
|
FirefoxProfileManager = &mozilla.MozProfileManager{
|
|
|
|
BrowserName: BrowserName,
|
2022-11-07 19:07:13 +00:00
|
|
|
PathGetter: ffProfileLoader,
|
2022-10-23 13:08:06 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-10-27 22:33:20 +00:00
|
|
|
func init() {
|
|
|
|
config.RegisterConfigurator(BrowserName, FFConfig)
|
2022-11-01 22:17:46 +00:00
|
|
|
|
2022-11-07 20:58:35 +00:00
|
|
|
//BUG: initFirefoxConfig is is called too early
|
2022-10-27 22:33:20 +00:00
|
|
|
config.RegisterConfReadyHooks(initFirefoxConfig)
|
|
|
|
}
|
2023-02-20 00:55:16 +00:00
|
|
|
// FirefoxConfig implements the Configurator interface
|
|
|
|
// which allows it to register and set field through the Configurator.
|
|
|
|
//
|
|
|
|
// It is also used alongside cli_flags.go to dynamically register cli flags
|
|
|
|
// that can change this config (struct fields) from command line at runtime.
|
|
|
|
//
|
|
|
|
// The struct schema defines the parameters to pass on to firefox that can be
|
|
|
|
// overriden by users. Options defined here will automatically supported in the
|
|
|
|
// config.toml file as well as the command line flags. New command line flags or
|
|
|
|
// config file options will only be accepted if they are defined here.
|
2022-10-23 13:08:06 +00:00
|
|
|
type FirefoxConfig struct {
|
|
|
|
// Default data source name query options for `places.sqlite` db
|
2022-11-17 21:59:02 +00:00
|
|
|
PlacesDSN database.DsnOptions
|
|
|
|
Profile string
|
2022-10-27 22:33:20 +00:00
|
|
|
|
2023-09-05 17:45:21 +00:00
|
|
|
modules.BrowserProfilePrefs `toml:"profile_options"`
|
|
|
|
|
2023-01-31 21:21:39 +00:00
|
|
|
//TEST: ignore this field in config.Configurator interface
|
2022-10-27 22:33:20 +00:00
|
|
|
// Embed base browser config
|
2023-01-31 21:21:39 +00:00
|
|
|
*modules.BrowserConfig `toml:"-"`
|
2022-10-23 13:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fc *FirefoxConfig) Set(opt string, v interface{}) error {
|
2022-11-17 22:03:18 +00:00
|
|
|
// log.Debugf("setting option %s = %v", opt, v)
|
2022-10-23 13:08:06 +00:00
|
|
|
s := structs.New(fc)
|
|
|
|
f, ok := s.FieldOk(opt)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%s option not defined", opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.Set(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fc *FirefoxConfig) Get(opt string) (interface{}, error) {
|
|
|
|
s := structs.New(fc)
|
|
|
|
f, ok := s.FieldOk(opt)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("%s option not defined", opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.Value(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fc *FirefoxConfig) Dump() map[string]interface{} {
|
|
|
|
s := structs.New(fc)
|
|
|
|
return s.Map()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fc *FirefoxConfig) String() string {
|
|
|
|
s := structs.New(fc)
|
|
|
|
return fmt.Sprintf("%v", s.Map())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fc *FirefoxConfig) MapFrom(src interface{}) error {
|
|
|
|
return mapstructure.Decode(src, fc)
|
|
|
|
}
|
|
|
|
|
2023-09-05 17:45:21 +00:00
|
|
|
//REFACT:
|
|
|
|
// Hook called when the config is ready
|
|
|
|
func initFirefoxConfig(c *cli.Context) error {
|
2022-10-23 13:08:06 +00:00
|
|
|
log.Debugf("<firefox> initializing config")
|
|
|
|
|
2023-09-05 17:45:21 +00:00
|
|
|
// The following code is executed before the cli context is ready
|
|
|
|
// so we cannot use cli flags here
|
|
|
|
|
2022-11-07 20:58:35 +00:00
|
|
|
pm := FirefoxProfileManager
|
|
|
|
|
2023-09-05 17:45:21 +00:00
|
|
|
// expand path to config dir
|
2022-11-07 19:07:13 +00:00
|
|
|
pm.ConfigDir = filepath.Join(os.ExpandEnv(FirefoxConfigDir))
|
2022-10-23 13:08:06 +00:00
|
|
|
|
|
|
|
// Check if base folder exists
|
|
|
|
configFolderExists, err := utils.CheckDirExists(pm.ConfigDir)
|
|
|
|
if !configFolderExists {
|
|
|
|
log.Criticalf("the base firefox folder <%s> does not exist", pm.ConfigDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2023-09-05 17:45:21 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
return err
|
2022-10-23 13:08:06 +00:00
|
|
|
}
|
|
|
|
|
2023-09-05 17:45:21 +00:00
|
|
|
// The next part prepares the default profile using the profile manager
|
2022-11-07 19:07:13 +00:00
|
|
|
ffProfileLoader.BasePath = pm.ConfigDir
|
|
|
|
|
2022-10-27 22:33:20 +00:00
|
|
|
//FIX: bookmarkDir is used in created instance of FF before it is setup in config
|
2022-11-07 19:07:13 +00:00
|
|
|
|
2023-09-05 17:45:21 +00:00
|
|
|
// use default profile
|
2022-11-07 19:07:13 +00:00
|
|
|
bookmarkDir, err := FirefoxProfileManager.GetProfilePath(FFConfig.Profile)
|
2022-10-23 13:08:06 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-10-27 22:33:20 +00:00
|
|
|
// update bookmark dir in firefox config
|
2022-11-07 20:58:35 +00:00
|
|
|
//TEST: verify that bookmark dir is set before browser is started
|
2022-10-27 22:33:20 +00:00
|
|
|
FFConfig.BkDir = bookmarkDir
|
2023-09-05 17:45:21 +00:00
|
|
|
log.Debugf("Using profile %s", bookmarkDir)
|
|
|
|
return nil
|
2022-10-23 13:08:06 +00:00
|
|
|
}
|