2017-01-24 15:09:23 +00:00
|
|
|
package chromedp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2017-12-27 02:30:28 +00:00
|
|
|
"github.com/chromedp/cdproto/cdp"
|
2017-01-24 15:09:23 +00:00
|
|
|
)
|
|
|
|
|
2017-02-12 04:59:33 +00:00
|
|
|
// Action is the common interface for an action that will be executed against a
|
|
|
|
// context and frame handler.
|
2017-01-24 15:09:23 +00:00
|
|
|
type Action interface {
|
2017-02-12 04:59:33 +00:00
|
|
|
// Do executes the action using the provided context and frame handler.
|
2017-12-27 02:30:28 +00:00
|
|
|
Do(context.Context, cdp.Executor) error
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 04:59:33 +00:00
|
|
|
// ActionFunc is a adapter to allow the use of ordinary func's as an Action.
|
2017-12-27 02:30:28 +00:00
|
|
|
type ActionFunc func(context.Context, cdp.Executor) error
|
2017-01-24 15:09:23 +00:00
|
|
|
|
2017-02-12 04:59:33 +00:00
|
|
|
// Do executes the func f using the provided context and frame handler.
|
2019-03-20 12:08:50 +00:00
|
|
|
func (f ActionFunc) Do(ctx context.Context, h cdp.Executor) error {
|
|
|
|
return f(ctx, h)
|
2017-01-24 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 04:59:33 +00:00
|
|
|
// Tasks is a sequential list of Actions that can be used as a single Action.
|
2017-01-24 15:09:23 +00:00
|
|
|
type Tasks []Action
|
|
|
|
|
2017-02-12 04:59:33 +00:00
|
|
|
// Do executes the list of Actions sequentially, using the provided context and
|
|
|
|
// frame handler.
|
2019-03-20 12:08:50 +00:00
|
|
|
func (t Tasks) Do(ctx context.Context, h cdp.Executor) error {
|
2017-01-24 15:09:23 +00:00
|
|
|
// TODO: put individual task timeouts from context here
|
|
|
|
for _, a := range t {
|
2019-03-20 12:08:50 +00:00
|
|
|
// ctx, cancel = context.WithTimeout(ctx, timeout)
|
2017-01-24 15:09:23 +00:00
|
|
|
// defer cancel()
|
2019-03-20 12:08:50 +00:00
|
|
|
if err := a.Do(ctx, h); err != nil {
|
2017-01-24 15:09:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sleep is an empty action that calls time.Sleep with the specified duration.
|
2017-02-12 04:59:33 +00:00
|
|
|
//
|
|
|
|
// Note: this is a temporary action definition for convenience, and will likely
|
|
|
|
// be marked for deprecation in the future, after the remaining Actions have
|
|
|
|
// been able to be written/tested.
|
2017-01-24 15:09:23 +00:00
|
|
|
func Sleep(d time.Duration) Action {
|
2019-03-20 12:08:50 +00:00
|
|
|
return ActionFunc(func(ctx context.Context, h cdp.Executor) error {
|
2019-04-01 16:02:40 +00:00
|
|
|
// Don't use time.After, to avoid a temporary goroutine leak if
|
|
|
|
// ctx is cancelled before the timer fires.
|
|
|
|
t := time.NewTimer(d)
|
2017-02-12 04:59:33 +00:00
|
|
|
select {
|
2019-04-01 16:02:40 +00:00
|
|
|
case <-t.C:
|
2019-03-20 12:08:50 +00:00
|
|
|
case <-ctx.Done():
|
2019-04-01 16:02:40 +00:00
|
|
|
t.Stop()
|
2019-03-20 12:08:50 +00:00
|
|
|
return ctx.Err()
|
2017-02-12 04:59:33 +00:00
|
|
|
}
|
2017-01-24 15:09:23 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|