remove top level frame mutex from Target

Now that it handles events synchronously, there's no need to worry about
concurrent field accesses.
This commit is contained in:
Daniel Martí 2019-03-20 11:23:15 +00:00
parent 81a48280ef
commit 24decf54d3

View File

@ -3,7 +3,6 @@ package chromedp
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"sync"
"sync/atomic" "sync/atomic"
"time" "time"
@ -30,8 +29,7 @@ type Target struct {
// frames is the set of encountered frames. // frames is the set of encountered frames.
frames map[cdp.FrameID]*cdp.Frame frames map[cdp.FrameID]*cdp.Frame
// cur is the current top level frame. TODO: delete mutex // cur is the current top level frame.
curMu sync.RWMutex
cur *cdp.Frame cur *cdp.Frame
// logging funcs // logging funcs
@ -44,7 +42,6 @@ func (t *Target) run(ctx context.Context) {
case <-ctx.Done(): case <-ctx.Done():
return return
case msg := <-t.eventQueue: case msg := <-t.eventQueue:
//fmt.Printf("%d %s: %s\n", msg.ID, msg.Method, msg.Params)
if err := t.processEvent(ctx, msg); err != nil { if err := t.processEvent(ctx, msg); err != nil {
t.errf("could not process event: %v", err) t.errf("could not process event: %v", err)
continue continue
@ -56,17 +53,13 @@ func (t *Target) run(ctx context.Context) {
if n == 0 { if n == 0 {
continue continue
} }
if t.cur == nil {
t.curMu.RLock()
cur := t.cur
t.curMu.RUnlock()
if cur == nil {
continue continue
} }
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
fn := <-t.waitQueue fn := <-t.waitQueue
if !fn(cur) { if !fn(t.cur) {
// try again later. // try again later.
t.waitQueue <- fn t.waitQueue <- fn
} }
@ -174,9 +167,7 @@ func (t *Target) processEvent(ctxt context.Context, msg *cdproto.Message) error
// documentUpdated handles the document updated event, retrieving the document // documentUpdated handles the document updated event, retrieving the document
// root for the root frame. // root for the root frame.
func (t *Target) documentUpdated(ctxt context.Context) { func (t *Target) documentUpdated(ctxt context.Context) {
t.curMu.RLock()
f := t.cur f := t.cur
t.curMu.RUnlock()
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
@ -210,9 +201,7 @@ func (t *Target) pageEvent(ctxt context.Context, ev interface{}) {
switch e := ev.(type) { switch e := ev.(type) {
case *page.EventFrameNavigated: case *page.EventFrameNavigated:
t.frames[e.Frame.ID] = e.Frame t.frames[e.Frame.ID] = e.Frame
t.curMu.Lock()
t.cur = e.Frame t.cur = e.Frame
t.curMu.Unlock()
return return
case *page.EventFrameAttached: case *page.EventFrameAttached:
@ -257,9 +246,7 @@ func (t *Target) pageEvent(ctxt context.Context, ev interface{}) {
// domEvent handles incoming DOM events. // domEvent handles incoming DOM events.
func (t *Target) domEvent(ctxt context.Context, ev interface{}) { func (t *Target) domEvent(ctxt context.Context, ev interface{}) {
t.curMu.RLock()
f := t.cur f := t.cur
t.curMu.RUnlock()
var id cdp.NodeID var id cdp.NodeID
var op nodeOp var op nodeOp