package proxy import ( "sync" "forge.cadoles.com/arcad/arcast/pkg/browser" "github.com/pkg/errors" ) var ( ErrNotInitialized = errors.New("not initialized") ) type Browser struct { loadURL func(url string) getURL func() string getTitle func() string status browser.Status mutex sync.RWMutex } // Load implements browser.Browser. func (b *Browser) Load(url string) error { b.mutex.Lock() defer b.mutex.Unlock() if b.loadURL == nil { return errors.WithStack(ErrNotInitialized) } b.loadURL(url) b.status = browser.StatusCasting return nil } // Reset implements browser.Browser. func (b *Browser) Reset(url string) error { b.mutex.Lock() defer b.mutex.Unlock() if b.loadURL == nil { return errors.WithStack(ErrNotInitialized) } b.loadURL(url) b.status = browser.StatusIdle return nil } // Status implements browser.Browser. func (b *Browser) Status() (browser.Status, error) { b.mutex.RLock() defer b.mutex.RUnlock() return b.status, nil } // Title implements browser.Browser. func (b *Browser) Title() (string, error) { b.mutex.RLock() defer b.mutex.RUnlock() if b.getTitle == nil { return "", errors.WithStack(ErrNotInitialized) } return b.getTitle(), nil } // URL implements browser.Browser. func (b *Browser) URL() (string, error) { b.mutex.RLock() defer b.mutex.RUnlock() if b.getURL == nil { return "", errors.WithStack(ErrNotInitialized) } return b.getURL(), nil } func (b *Browser) SetLoadURL(fn func(url string)) { b.mutex.Lock() defer b.mutex.Unlock() b.loadURL = fn } func (b *Browser) SetGetTitle(fn func() string) { b.mutex.Lock() defer b.mutex.Unlock() b.getTitle = fn } func (b *Browser) SetGetURL(fn func() string) { b.mutex.Lock() defer b.mutex.Unlock() b.getURL = fn } func NewBrowser() *Browser { return &Browser{} } var _ browser.Browser = &Browser{}