chromedp/target.go

322 lines
7.1 KiB
Go
Raw Normal View History

2017-01-24 15:09:23 +00:00
package chromedp
import (
"context"
2017-12-27 02:30:28 +00:00
"encoding/json"
"sync/atomic"
2017-01-24 15:09:23 +00:00
"time"
"github.com/mailru/easyjson"
2017-12-27 02:30:28 +00:00
"github.com/chromedp/cdproto"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/dom"
"github.com/chromedp/cdproto/inspector"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/cdproto/target"
2017-01-24 15:09:23 +00:00
)
// Target manages a Chrome DevTools Protocol target.
type Target struct {
browser *Browser
SessionID target.SessionID
2017-01-24 15:09:23 +00:00
waitQueue chan func(cur *cdp.Frame) bool
eventQueue chan *cdproto.Message
2017-01-24 15:09:23 +00:00
// below are the old TargetHandler fields.
2017-01-24 15:09:23 +00:00
// frames is the set of encountered frames.
frames map[cdp.FrameID]*cdp.Frame
2017-01-24 15:09:23 +00:00
// cur is the current top level frame.
cur *cdp.Frame
2017-01-24 15:09:23 +00:00
// logging funcs
logf, errf func(string, ...interface{})
2017-01-24 15:09:23 +00:00
}
func (t *Target) run(ctx context.Context) {
2017-01-24 15:09:23 +00:00
for {
select {
case <-ctx.Done():
return
case msg := <-t.eventQueue:
if err := t.processEvent(ctx, msg); err != nil {
t.errf("could not process event: %v", err)
continue
2017-01-24 15:09:23 +00:00
}
default:
// prevent busy spinning. TODO: do better
time.Sleep(5 * time.Millisecond)
n := len(t.waitQueue)
if n == 0 {
continue
2017-01-24 15:09:23 +00:00
}
if t.cur == nil {
continue
2017-01-24 15:09:23 +00:00
}
for i := 0; i < n; i++ {
fn := <-t.waitQueue
if !fn(t.cur) {
// try again later.
t.waitQueue <- fn
}
}
2017-01-24 15:09:23 +00:00
}
}
}
func (t *Target) Execute(ctx context.Context, method string, params json.Marshaler, res json.Unmarshaler) error {
paramsMsg := emptyObj
if params != nil {
var err error
if paramsMsg, err = json.Marshal(params); err != nil {
return err
}
}
innerID := atomic.AddInt64(&t.browser.next, 1)
msg := &cdproto.Message{
ID: innerID,
Method: cdproto.MethodType(method),
Params: paramsMsg,
}
msgJSON, err := json.Marshal(msg)
2017-01-24 15:09:23 +00:00
if err != nil {
return err
2017-01-24 15:09:23 +00:00
}
sendParams := target.SendMessageToTarget(string(msgJSON)).
WithSessionID(t.SessionID)
sendParamsJSON, _ := json.Marshal(sendParams)
2017-01-24 15:09:23 +00:00
// We want to grab the response from the inner message.
ch := make(chan *cdproto.Message, 1)
t.browser.cmdQueue <- cmdJob{
msg: &cdproto.Message{ID: innerID},
resp: ch,
}
2017-01-24 15:09:23 +00:00
// The response from the outer message is uninteresting; pass a nil
// resp channel.
outerID := atomic.AddInt64(&t.browser.next, 1)
t.browser.cmdQueue <- cmdJob{
msg: &cdproto.Message{
ID: outerID,
Method: target.CommandSendMessageToTarget,
Params: sendParamsJSON,
},
2017-01-24 15:09:23 +00:00
}
select {
case msg := <-ch:
switch {
case msg == nil:
return ErrChannelClosed
case msg.Error != nil:
return msg.Error
case res != nil:
return json.Unmarshal(msg.Result, res)
}
case <-ctx.Done():
return ctx.Err()
}
return nil
2017-01-24 15:09:23 +00:00
}
// below are the old TargetHandler methods.
2017-01-24 15:09:23 +00:00
// processEvent processes an incoming event.
func (t *Target) processEvent(ctx context.Context, msg *cdproto.Message) error {
2017-01-24 15:09:23 +00:00
if msg == nil {
2017-12-27 02:30:28 +00:00
return ErrChannelClosed
2017-01-24 15:09:23 +00:00
}
switch msg.Method {
case "Page.frameClearedScheduledNavigation",
"Page.frameScheduledNavigation":
// These events are now deprecated, and UnmarshalMessage panics
// when they are received from Chrome. For now, to avoid panics
// and compile errors, and to fix chromedp v0 when installed via
// 'go get -u', skip the events here.
return nil
}
2017-01-24 15:09:23 +00:00
// unmarshal
2017-12-27 02:30:28 +00:00
ev, err := cdproto.UnmarshalMessage(msg)
2017-01-24 15:09:23 +00:00
if err != nil {
return err
}
switch ev.(type) {
2017-01-24 15:09:23 +00:00
case *inspector.EventDetached:
return nil
case *dom.EventDocumentUpdated:
t.documentUpdated(ctx)
2017-01-24 15:09:23 +00:00
return nil
}
switch msg.Method.Domain() {
2017-01-24 15:09:23 +00:00
case "Page":
2019-03-20 12:06:52 +00:00
t.pageEvent(ev)
2017-01-24 15:09:23 +00:00
case "DOM":
2019-03-20 12:06:52 +00:00
t.domEvent(ev)
2017-01-24 15:09:23 +00:00
}
return nil
}
// documentUpdated handles the document updated event, retrieving the document
// root for the root frame.
func (t *Target) documentUpdated(ctx context.Context) {
f := t.cur
2017-01-24 15:09:23 +00:00
f.Lock()
defer f.Unlock()
// invalidate nodes
if f.Root != nil {
close(f.Root.Invalidated)
}
2017-01-26 07:28:34 +00:00
f.Nodes = make(map[cdp.NodeID]*cdp.Node)
var err error
f.Root, err = dom.GetDocument().WithPierce(true).Do(ctx, t)
if err == context.Canceled {
return // TODO: perhaps not necessary, but useful to keep the tests less noisy
}
2017-01-24 15:09:23 +00:00
if err != nil {
t.errf("could not retrieve document root for %s: %v", f.ID, err)
2017-01-24 15:09:23 +00:00
return
}
f.Root.Invalidated = make(chan struct{})
walk(f.Nodes, f.Root)
}
// emptyObj is an empty JSON object message.
var emptyObj = easyjson.RawMessage([]byte(`{}`))
2017-01-24 15:09:23 +00:00
// pageEvent handles incoming page events.
2019-03-20 12:06:52 +00:00
func (t *Target) pageEvent(ev interface{}) {
2017-01-26 07:28:34 +00:00
var id cdp.FrameID
var op frameOp
2017-01-24 15:09:23 +00:00
switch e := ev.(type) {
case *page.EventFrameNavigated:
t.frames[e.Frame.ID] = e.Frame
t.cur = e.Frame
2017-01-24 15:09:23 +00:00
return
case *page.EventFrameAttached:
id, op = e.FrameID, frameAttached(e.ParentFrameID)
case *page.EventFrameDetached:
id, op = e.FrameID, frameDetached
case *page.EventFrameStartedLoading:
id, op = e.FrameID, frameStartedLoading
2017-01-24 15:09:23 +00:00
case *page.EventFrameStoppedLoading:
id, op = e.FrameID, frameStoppedLoading
// ignored events
case *page.EventFrameRequestedNavigation:
return
2017-01-24 15:09:23 +00:00
case *page.EventDomContentEventFired:
return
case *page.EventLoadEventFired:
return
case *page.EventFrameResized:
return
case *page.EventLifecycleEvent:
return
case *page.EventNavigatedWithinDocument:
return
2017-01-24 15:09:23 +00:00
default:
t.errf("unhandled page event %T", ev)
return
2017-01-24 15:09:23 +00:00
}
f := t.frames[id]
if f == nil {
// This can happen if a frame is attached or starts loading
// before it's ever navigated to. We won't have all the frame
// details just yet, but that's okay.
f = &cdp.Frame{ID: id}
t.frames[id] = f
}
2017-01-24 15:09:23 +00:00
f.Lock()
defer f.Unlock()
op(f)
}
// domEvent handles incoming DOM events.
2019-03-20 12:06:52 +00:00
func (t *Target) domEvent(ev interface{}) {
f := t.cur
2017-01-24 15:09:23 +00:00
2017-01-26 07:28:34 +00:00
var id cdp.NodeID
var op nodeOp
2017-01-24 15:09:23 +00:00
switch e := ev.(type) {
case *dom.EventSetChildNodes:
id, op = e.ParentID, setChildNodes(f.Nodes, e.Nodes)
case *dom.EventAttributeModified:
id, op = e.NodeID, attributeModified(e.Name, e.Value)
case *dom.EventAttributeRemoved:
id, op = e.NodeID, attributeRemoved(e.Name)
case *dom.EventInlineStyleInvalidated:
2017-03-28 00:42:47 +00:00
if len(e.NodeIds) == 0 {
return
}
2017-01-24 15:09:23 +00:00
id, op = e.NodeIds[0], inlineStyleInvalidated(e.NodeIds[1:])
case *dom.EventCharacterDataModified:
id, op = e.NodeID, characterDataModified(e.CharacterData)
case *dom.EventChildNodeCountUpdated:
id, op = e.NodeID, childNodeCountUpdated(e.ChildNodeCount)
case *dom.EventChildNodeInserted:
id, op = e.ParentNodeID, childNodeInserted(f.Nodes, e.PreviousNodeID, e.Node)
case *dom.EventChildNodeRemoved:
id, op = e.ParentNodeID, childNodeRemoved(f.Nodes, e.NodeID)
case *dom.EventShadowRootPushed:
id, op = e.HostID, shadowRootPushed(f.Nodes, e.Root)
case *dom.EventShadowRootPopped:
id, op = e.HostID, shadowRootPopped(f.Nodes, e.RootID)
case *dom.EventPseudoElementAdded:
id, op = e.ParentID, pseudoElementAdded(f.Nodes, e.PseudoElement)
case *dom.EventPseudoElementRemoved:
id, op = e.ParentID, pseudoElementRemoved(f.Nodes, e.PseudoElementID)
case *dom.EventDistributedNodesUpdated:
id, op = e.InsertionPointID, distributedNodesUpdated(e.DistributedNodes)
default:
t.errf("unhandled node event %T", ev)
return
2017-01-24 15:09:23 +00:00
}
n, ok := f.Nodes[id]
if !ok {
// Node ID has been invalidated. Nothing to do.
2017-01-24 15:09:23 +00:00
return
}
f.Lock()
defer f.Unlock()
op(n)
}
type TargetOption func(*Target)