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" "time"
"github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/cdp"
"github.com/chromedp/chromedp/client" "github.com/chromedp/chromedp/client"
"github.com/chromedp/chromedp/runner" "github.com/chromedp/chromedp/runner"
) )
@ -56,7 +57,7 @@ type CDP struct {
handlerMap map[string]int handlerMap map[string]int
// logging funcs // logging funcs
logf, debugf, errorf LogFunc logf, debugf, errf func(string, ...interface{})
sync.RWMutex sync.RWMutex
} }
@ -68,7 +69,7 @@ func New(ctxt context.Context, opts ...Option) (*CDP, error) {
handlerMap: make(map[string]int), handlerMap: make(map[string]int),
logf: log.Printf, logf: log.Printf,
debugf: func(string, ...interface{}) {}, 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 // apply options
@ -133,15 +134,15 @@ func (c *CDP) AddTarget(ctxt context.Context, t client.Target) {
defer c.Unlock() defer c.Unlock()
// create target manager // 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 { 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 return
} }
// run // run
if err := h.Run(ctxt); err != nil { 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 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. // 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 { return func(c *CDP) error {
c.logf = f c.logf = f
return nil 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, // WithDebugf is a CDP option to specify a func to receive debug logging (ie,
// protocol information). // protocol information).
func WithDebugf(f LogFunc) Option { func WithDebugf(f func(string, ...interface{})) Option {
return func(c *CDP) error { return func(c *CDP) error {
c.debugf = f c.debugf = f
return nil return nil
@ -389,20 +387,18 @@ func WithDebugf(f LogFunc) Option {
} }
// WithErrorf is a CDP option to specify a func to receive error logging. // 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 { return func(c *CDP) error {
c.errorf = f c.errf = f
return nil return nil
} }
} }
// WithLog is a CDP option that sets the logging, debugging, and error funcs to // WithLog is a CDP option that sets the logging, debugging, and error funcs to
// f. // f.
func WithLog(f LogFunc) Option { func WithLog(f func(string, ...interface{})) Option {
return func(c *CDP) error { return func(c *CDP) error {
c.logf = f c.logf, c.debugf, c.errf = f, f, f
c.debugf = f
c.errorf = f
return nil 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. // WithConsolef is a CDP option to specify a func to receive chrome log events.
// //
// Note: NOT YET IMPLEMENTED. // Note: NOT YET IMPLEMENTED.
func WithConsolef(f LogFunc) Option { func WithConsolef(f func(string, ...interface{})) Option {
return func(c *CDP) error { return func(c *CDP) error {
return nil return nil
} }

View File

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

View File

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

18
eval.go
View File

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

View File

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

View File

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

23
pool.go
View File

@ -21,7 +21,7 @@ type Pool struct {
res map[int]*Res res map[int]*Res
// logging funcs // logging funcs
logf, debugf, errorf LogFunc logf, debugf, errf func(string, ...interface{})
rw sync.RWMutex rw sync.RWMutex
} }
@ -34,7 +34,6 @@ func NewPool(opts ...PoolOption) (*Pool, error) {
res: make(map[int]*Res), res: make(map[int]*Res),
logf: log.Printf, logf: log.Printf,
debugf: func(string, ...interface{}) {}, debugf: func(string, ...interface{}) {},
errorf: func(s string, v ...interface{}) { log.Printf("error: "+s, v...) },
} }
// apply opts // 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 return p, nil
} }
@ -73,7 +78,7 @@ func (p *Pool) Allocate(ctxt context.Context, opts ...runner.CommandLineOption)
}, opts...)...) }, opts...)...)
if err != nil { if err != nil {
defer r.Release() 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 return nil, err
} }
@ -81,18 +86,18 @@ func (p *Pool) Allocate(ctxt context.Context, opts ...runner.CommandLineOption)
err = r.r.Start(r.ctxt) err = r.r.Start(r.ctxt)
if err != nil { if err != nil {
defer r.Release() 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 return nil, err
} }
// setup cdp // setup cdp
r.c, err = New( r.c, err = New(
r.ctxt, WithRunner(r.r), 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 { if err != nil {
defer r.Release() 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 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. // 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 { return func(p *Pool) error {
p.logf = logf p.logf, p.debugf, p.errf = logf, debugf, errf
p.debugf = debugf
p.errorf = errorf
return nil return nil
} }
} }

View File

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