vendor/github.com/go-rod/rod/states.go

119 lines · 3342 bytes

  1package rod
  2
  3import (
  4	"reflect"
  5
  6	"github.com/go-rod/rod/lib/proto"
  7)
  8
  9type stateKey struct {
 10	browserContextID proto.BrowserBrowserContextID
 11	sessionID        proto.TargetSessionID
 12	methodName       string
 13}
 14
 15func (b *Browser) key(sessionID proto.TargetSessionID, methodName string) stateKey {
 16	return stateKey{
 17		browserContextID: b.BrowserContextID,
 18		sessionID:        sessionID,
 19		methodName:       methodName,
 20	}
 21}
 22
 23func (b *Browser) set(sessionID proto.TargetSessionID, methodName string, params interface{}) {
 24	b.states.Store(b.key(sessionID, methodName), params)
 25
 26	key := ""
 27	switch methodName {
 28	case (proto.EmulationClearDeviceMetricsOverride{}).ProtoReq():
 29		key = (proto.EmulationSetDeviceMetricsOverride{}).ProtoReq()
 30	case (proto.EmulationClearGeolocationOverride{}).ProtoReq():
 31		key = (proto.EmulationSetGeolocationOverride{}).ProtoReq()
 32	default:
 33		domain, name := proto.ParseMethodName(methodName)
 34		if name == "disable" {
 35			key = domain + ".enable"
 36		}
 37	}
 38	if key != "" {
 39		b.states.Delete(b.key(sessionID, key))
 40	}
 41}
 42
 43// LoadState into the method, sessionID can be empty.
 44func (b *Browser) LoadState(sessionID proto.TargetSessionID, method proto.Request) (has bool) {
 45	data, has := b.states.Load(b.key(sessionID, method.ProtoReq()))
 46	if has {
 47		reflect.Indirect(reflect.ValueOf(method)).Set(
 48			reflect.Indirect(reflect.ValueOf(data)),
 49		)
 50	}
 51	return
 52}
 53
 54// RemoveState a state.
 55func (b *Browser) RemoveState(key interface{}) {
 56	b.states.Delete(key)
 57}
 58
 59// EnableDomain and returns a restore function to restore previous state.
 60func (b *Browser) EnableDomain(sessionID proto.TargetSessionID, req proto.Request) (restore func()) {
 61	_, enabled := b.states.Load(b.key(sessionID, req.ProtoReq()))
 62
 63	if !enabled {
 64		_, _ = b.Call(b.ctx, string(sessionID), req.ProtoReq(), req)
 65	}
 66
 67	return func() {
 68		if !enabled {
 69			domain, _ := proto.ParseMethodName(req.ProtoReq())
 70			_, _ = b.Call(b.ctx, string(sessionID), domain+".disable", nil)
 71		}
 72	}
 73}
 74
 75// DisableDomain and returns a restore function to restore previous state.
 76func (b *Browser) DisableDomain(sessionID proto.TargetSessionID, req proto.Request) (restore func()) {
 77	_, enabled := b.states.Load(b.key(sessionID, req.ProtoReq()))
 78	domain, _ := proto.ParseMethodName(req.ProtoReq())
 79
 80	if enabled {
 81		_, _ = b.Call(b.ctx, string(sessionID), domain+".disable", nil)
 82	}
 83
 84	return func() {
 85		if enabled {
 86			_, _ = b.Call(b.ctx, string(sessionID), req.ProtoReq(), req)
 87		}
 88	}
 89}
 90
 91func (b *Browser) cachePage(page *Page) {
 92	b.states.Store(page.TargetID, page)
 93}
 94
 95func (b *Browser) loadCachedPage(id proto.TargetTargetID) *Page {
 96	if cache, ok := b.states.Load(id); ok {
 97		return cache.(*Page) //nolint: forcetypeassert
 98	}
 99	return nil
100}
101
102// LoadState into the method.
103func (p *Page) LoadState(method proto.Request) (has bool) {
104	return p.browser.LoadState(p.SessionID, method)
105}
106
107// EnableDomain and returns a restore function to restore previous state.
108func (p *Page) EnableDomain(method proto.Request) (restore func()) {
109	return p.browser.Context(p.ctx).EnableDomain(p.SessionID, method)
110}
111
112// DisableDomain and returns a restore function to restore previous state.
113func (p *Page) DisableDomain(method proto.Request) (restore func()) {
114	return p.browser.Context(p.ctx).DisableDomain(p.SessionID, method)
115}
116
117func (p *Page) cleanupStates() {
118	p.browser.RemoveState(p.TargetID)
119}