General code cleanup

This commit is contained in:
Kenneth Shaw 2018-05-19 05:03:47 +07:00
parent 310d213f6f
commit 1e1a3ace12
8 changed files with 56 additions and 56 deletions

View File

@ -15,6 +15,7 @@ import (
"time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/chromedp/client"
"github.com/chromedp/chromedp/runner"
)
@ -56,7 +57,7 @@ type CDP struct {
handlerMap map[string]int
// logging funcs
logf, debugf, errorf LogFunc
logf, debugf, errf func(string, ...interface{})
sync.RWMutex
}
@ -68,7 +69,7 @@ func New(ctxt context.Context, opts ...Option) (*CDP, error) {
handlerMap: make(map[string]int),
logf: log.Printf,
debugf: func(string, ...interface{}) {},
errorf: func(s string, v ...interface{}) { log.Printf("error: "+s, v...) },
errf: func(s string, v ...interface{}) { log.Printf("error: "+s, v...) },
}
// apply options
@ -133,15 +134,15 @@ func (c *CDP) AddTarget(ctxt context.Context, t client.Target) {
defer c.Unlock()
// create target manager
h, err := NewTargetHandler(t, c.logf, c.debugf, c.errorf)
h, err := NewTargetHandler(t, c.logf, c.debugf, c.errf)
if err != nil {
c.errorf("could not create handler for %s: %v", t, err)
c.errf("could not create handler for %s: %v", t, err)
return
}
// run
if err := h.Run(ctxt); err != nil {
c.errorf("could not start handler for %s: %v", t, err)
c.errf("could not start handler for %s: %v", t, err)
return
}
@ -368,11 +369,8 @@ func WithRunnerOptions(opts ...runner.CommandLineOption) Option {
}
}
// LogFunc is the common logging func type.
type LogFunc func(string, ...interface{})
// WithLogf is a CDP option to specify a func to receive general logging.
func WithLogf(f LogFunc) Option {
func WithLogf(f func(string, ...interface{})) Option {
return func(c *CDP) error {
c.logf = f
return nil
@ -381,7 +379,7 @@ func WithLogf(f LogFunc) Option {
// WithDebugf is a CDP option to specify a func to receive debug logging (ie,
// protocol information).
func WithDebugf(f LogFunc) Option {
func WithDebugf(f func(string, ...interface{})) Option {
return func(c *CDP) error {
c.debugf = f
return nil
@ -389,20 +387,18 @@ func WithDebugf(f LogFunc) Option {
}
// WithErrorf is a CDP option to specify a func to receive error logging.
func WithErrorf(f LogFunc) Option {
func WithErrorf(f func(string, ...interface{})) Option {
return func(c *CDP) error {
c.errorf = f
c.errf = f
return nil
}
}
// WithLog is a CDP option that sets the logging, debugging, and error funcs to
// f.
func WithLog(f LogFunc) Option {
func WithLog(f func(string, ...interface{})) Option {
return func(c *CDP) error {
c.logf = f
c.debugf = f
c.errorf = f
c.logf, c.debugf, c.errf = f, f, f
return nil
}
}
@ -410,7 +406,7 @@ func WithLog(f LogFunc) Option {
// WithConsolef is a CDP option to specify a func to receive chrome log events.
//
// Note: NOT YET IMPLEMENTED.
func WithConsolef(f LogFunc) Option {
func WithConsolef(f func(string, ...interface{})) Option {
return func(c *CDP) error {
return nil
}

View File

@ -51,10 +51,9 @@ func testAllocate(t *testing.T, path string) *Res {
t.Fatalf("handler is invalid type")
}
th.logf = t.Logf
th.debugf = t.Logf
th.errorf = func(s string, v ...interface{}) {
t.Logf("target handler error: "+s, v...)
th.logf, th.debugf = t.Logf, t.Logf
th.errf = func(s string, v ...interface{}) {
t.Logf("TARGET HANDLER ERROR: "+s, v...)
}
if path != "" {

View File

@ -4,8 +4,8 @@ package chromedp
type Error string
// Error satisfies the error interface.
func (e Error) Error() string {
return string(e)
func (err Error) Error() string {
return string(err)
}
// Error types.

18
eval.go
View File

@ -5,7 +5,7 @@ import (
"encoding/json"
"github.com/chromedp/cdproto/cdp"
rundom "github.com/chromedp/cdproto/runtime"
"github.com/chromedp/cdproto/runtime"
)
// Evaluate is an action to evaluate the Javascript expression, unmarshaling
@ -29,9 +29,9 @@ func Evaluate(expression string, res interface{}, opts ...EvaluateOption) Action
return ActionFunc(func(ctxt context.Context, h cdp.Executor) error {
// set up parameters
p := rundom.Evaluate(expression)
p := runtime.Evaluate(expression)
switch res.(type) {
case **rundom.RemoteObject:
case **runtime.RemoteObject:
default:
p = p.WithReturnByValue(true)
}
@ -51,7 +51,7 @@ func Evaluate(expression string, res interface{}, opts ...EvaluateOption) Action
}
switch x := res.(type) {
case **rundom.RemoteObject:
case **runtime.RemoteObject:
*x = v
return nil
@ -75,11 +75,11 @@ func EvaluateAsDevTools(expression string, res interface{}, opts ...EvaluateOpti
}
// EvaluateOption is the type for script evaluation options.
type EvaluateOption func(*rundom.EvaluateParams) *rundom.EvaluateParams
type EvaluateOption func(*runtime.EvaluateParams) *runtime.EvaluateParams
// EvalObjectGroup is a evaluate option to set the object group.
func EvalObjectGroup(objectGroup string) EvaluateOption {
return func(p *rundom.EvaluateParams) *rundom.EvaluateParams {
return func(p *runtime.EvaluateParams) *runtime.EvaluateParams {
return p.WithObjectGroup(objectGroup)
}
}
@ -88,18 +88,18 @@ func EvalObjectGroup(objectGroup string) EvaluateOption {
// Line API available to the evaluated script.
//
// Note: this should not be used with untrusted Javascript.
func EvalWithCommandLineAPI(p *rundom.EvaluateParams) *rundom.EvaluateParams {
func EvalWithCommandLineAPI(p *runtime.EvaluateParams) *runtime.EvaluateParams {
return p.WithIncludeCommandLineAPI(true)
}
// EvalIgnoreExceptions is a evaluate option that will cause script evaluation
// to ignore exceptions.
func EvalIgnoreExceptions(p *rundom.EvaluateParams) *rundom.EvaluateParams {
func EvalIgnoreExceptions(p *runtime.EvaluateParams) *runtime.EvaluateParams {
return p.WithSilent(true)
}
// EvalAsValue is a evaluate option that will cause the evaluated script to
// encode the result of the expression as a JSON-encoded value.
func EvalAsValue(p *rundom.EvaluateParams) *rundom.EvaluateParams {
func EvalAsValue(p *runtime.EvaluateParams) *runtime.EvaluateParams {
return p.WithReturnByValue(true)
}

View File

@ -57,13 +57,13 @@ type TargetHandler struct {
resrw sync.RWMutex
// logging funcs
logf, debugf, errorf LogFunc
logf, debugf, errf func(string, ...interface{})
sync.RWMutex
}
// NewTargetHandler creates a new handler for the specified client target.
func NewTargetHandler(t client.Target, logf, debugf, errorf LogFunc) (*TargetHandler, error) {
func NewTargetHandler(t client.Target, logf, debugf, errf func(string, ...interface{})) (*TargetHandler, error) {
conn, err := client.Dial(t)
if err != nil {
return nil, err
@ -73,7 +73,7 @@ func NewTargetHandler(t client.Target, logf, debugf, errorf LogFunc) (*TargetHan
conn: conn,
logf: logf,
debugf: debugf,
errorf: errorf,
errf: errf,
}, nil
}
@ -161,7 +161,7 @@ func (h *TargetHandler) run(ctxt context.Context) {
h.qres <- msg
default:
h.errorf("ignoring malformed incoming message (missing id or method): %#v", msg)
h.errf("ignoring malformed incoming message (missing id or method): %#v", msg)
}
case <-h.detached:
@ -180,19 +180,19 @@ func (h *TargetHandler) run(ctxt context.Context) {
case ev := <-h.qevents:
err := h.processEvent(ctxt, ev)
if err != nil {
h.errorf("could not process event %s: %v", ev.Method, err)
h.errf("could not process event %s: %v", ev.Method, err)
}
case res := <-h.qres:
err := h.processResult(res)
if err != nil {
h.errorf("could not process result for message %d: %v", res.ID, err)
h.errf("could not process result for message %d: %v", res.ID, err)
}
case cmd := <-h.qcmd:
err := h.processCommand(cmd)
if err != nil {
h.errorf("could not process command message %d: %v", cmd.ID, err)
h.errf("could not process command message %d: %v", cmd.ID, err)
}
case <-ctxt.Done():
@ -269,7 +269,7 @@ func (h *TargetHandler) processEvent(ctxt context.Context, msg *cdproto.Message)
func (h *TargetHandler) documentUpdated(ctxt context.Context) {
f, err := h.WaitFrame(ctxt, cdp.EmptyFrameID)
if err != nil {
h.errorf("could not get current frame: %v", err)
h.errf("could not get current frame: %v", err)
return
}
@ -284,7 +284,7 @@ func (h *TargetHandler) documentUpdated(ctxt context.Context) {
f.Nodes = make(map[cdp.NodeID]*cdp.Node)
f.Root, err = dom.GetDocument().WithPierce(true).Do(ctxt, h)
if err != nil {
h.errorf("could not retrieve document root for %s: %v", f.ID, err)
h.errf("could not retrieve document root for %s: %v", f.ID, err)
return
}
f.Root.Invalidated = make(chan struct{})
@ -547,13 +547,13 @@ func (h *TargetHandler) pageEvent(ctxt context.Context, ev interface{}) {
return
default:
h.errorf("unhandled page event %s", reflect.TypeOf(ev))
h.errf("unhandled page event %s", reflect.TypeOf(ev))
return
}
f, err := h.WaitFrame(ctxt, id)
if err != nil {
h.errorf("could not get frame %s: %v", id, err)
h.errf("could not get frame %s: %v", id, err)
return
}
@ -573,7 +573,7 @@ func (h *TargetHandler) domEvent(ctxt context.Context, ev interface{}) {
// wait current frame
f, err := h.WaitFrame(ctxt, cdp.EmptyFrameID)
if err != nil {
h.errorf("could not process DOM event %s: %v", reflect.TypeOf(ev), err)
h.errf("could not process DOM event %s: %v", reflect.TypeOf(ev), err)
return
}
@ -631,7 +631,7 @@ func (h *TargetHandler) domEvent(ctxt context.Context, ev interface{}) {
id, op = e.InsertionPointID, distributedNodesUpdated(e.DistributedNodes)
default:
h.errorf("unhandled node event %s", reflect.TypeOf(ev))
h.errf("unhandled node event %s", reflect.TypeOf(ev))
return
}
@ -643,7 +643,7 @@ func (h *TargetHandler) domEvent(ctxt context.Context, ev interface{}) {
if i != -1 {
s = s[i+1:]
}
h.errorf("could not perform (%s) operation on node %d (wait node): %v", s, id, err)
h.errf("could not perform (%s) operation on node %d (wait node): %v", s, id, err)
return
}

View File

@ -8,6 +8,7 @@ import (
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/dom"
"github.com/chromedp/cdproto/input"
"github.com/chromedp/chromedp/kb"
)

23
pool.go
View File

@ -21,7 +21,7 @@ type Pool struct {
res map[int]*Res
// logging funcs
logf, debugf, errorf LogFunc
logf, debugf, errf func(string, ...interface{})
rw sync.RWMutex
}
@ -34,7 +34,6 @@ func NewPool(opts ...PoolOption) (*Pool, error) {
res: make(map[int]*Res),
logf: log.Printf,
debugf: func(string, ...interface{}) {},
errorf: func(s string, v ...interface{}) { log.Printf("error: "+s, v...) },
}
// apply opts
@ -44,6 +43,12 @@ func NewPool(opts ...PoolOption) (*Pool, error) {
}
}
if p.errf == nil {
p.errf = func(s string, v ...interface{}) {
p.logf("ERROR: "+s, v...)
}
}
return p, nil
}
@ -73,7 +78,7 @@ func (p *Pool) Allocate(ctxt context.Context, opts ...runner.CommandLineOption)
}, opts...)...)
if err != nil {
defer r.Release()
p.errorf("pool could not allocate runner on port %d: %v", r.port, err)
p.errf("pool could not allocate runner on port %d: %v", r.port, err)
return nil, err
}
@ -81,18 +86,18 @@ func (p *Pool) Allocate(ctxt context.Context, opts ...runner.CommandLineOption)
err = r.r.Start(r.ctxt)
if err != nil {
defer r.Release()
p.errorf("pool could not start runner on port %d: %v", r.port, err)
p.errf("pool could not start runner on port %d: %v", r.port, err)
return nil, err
}
// setup cdp
r.c, err = New(
r.ctxt, WithRunner(r.r),
WithLogf(p.logf), WithDebugf(p.debugf), WithErrorf(p.errorf),
WithLogf(p.logf), WithDebugf(p.debugf), WithErrorf(p.errf),
)
if err != nil {
defer r.Release()
p.errorf("pool could not connect to %d: %v", r.port, err)
p.errf("pool could not connect to %d: %v", r.port, err)
return nil, err
}
@ -189,11 +194,9 @@ func PortRange(start, end int) PoolOption {
}
// PoolLog is a pool option to set the logging to use for the pool.
func PoolLog(logf, debugf, errorf LogFunc) PoolOption {
func PoolLog(logf, debugf, errf func(string, ...interface{})) PoolOption {
return func(p *Pool) error {
p.logf = logf
p.debugf = debugf
p.errorf = errorf
p.logf, p.debugf, p.errf = logf, debugf, errf
return nil
}
}

View File

@ -13,6 +13,7 @@ import (
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/css"
"github.com/chromedp/cdproto/dom"
"github.com/chromedp/chromedp/kb"
)