File size: 2,168 Bytes
6a7089a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package engine

// RouteRule inspects an incoming operation and returns a routing decision.
// Rules are evaluated in order; the first non-Undecided verdict wins.
type RouteRule interface {
	// Name returns a short human-readable identifier for logging.
	Name() string
	// Decide returns UseLite, UseChrome, or Undecided.
	Decide(op Capability, url string) Decision
}

// ---------- built-in rules ----------

// CapabilityRule routes chrome-only operations (screenshot, pdf, evaluate,
// cookies) to Chrome unconditionally.
type CapabilityRule struct{}

func (CapabilityRule) Name() string { return "capability" }

func (CapabilityRule) Decide(op Capability, _ string) Decision {
	switch op {
	case CapScreenshot, CapPDF, CapEvaluate, CapCookies:
		return UseChrome
	}
	return Undecided
}

// ContentHintRule uses a simple heuristic: if a URL path ends with common
// static-content extensions it is a good candidate for the lite engine.
type ContentHintRule struct{}

func (ContentHintRule) Name() string { return "content-hint" }

func (ContentHintRule) Decide(op Capability, url string) Decision {
	if op != CapNavigate && op != CapSnapshot && op != CapText {
		return Undecided
	}
	// Static content is well-suited for Gost-DOM because it does not
	// rely on JavaScript rendering.
	for _, ext := range []string{".html", ".htm", ".xml", ".txt", ".md"} {
		if len(url) > len(ext) && url[len(url)-len(ext):] == ext {
			return UseLite
		}
	}
	return Undecided
}

// DefaultLiteRule is a catch-all that sends every remaining DOM operation
// to the lite engine.  Used when Mode == ModeLite.
type DefaultLiteRule struct{}

func (DefaultLiteRule) Name() string { return "default-lite" }

func (DefaultLiteRule) Decide(op Capability, _ string) Decision {
	switch op {
	case CapNavigate, CapSnapshot, CapText, CapClick, CapType:
		return UseLite
	}
	return Undecided
}

// DefaultChromeRule is a catch-all that sends everything to Chrome.
// Used as the final fallback in ModeAuto.
type DefaultChromeRule struct{}

func (DefaultChromeRule) Name() string { return "default-chrome" }

func (DefaultChromeRule) Decide(_ Capability, _ string) Decision {
	return UseChrome
}