Repository navigation
Expand file tree
/
Copy pathadblock.go
More file actions
114 lines (105 loc) · 2.95 KB
/
Copy pathadblock.go
File metadata and controls
114 lines (105 loc) · 2.95 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"context"
"fmt"
"log"
"path/filepath"
"slices"
"sync"
"github.com/egoist/mygo"
"panenest/internal/adblock"
"panenest/internal/proto"
)
// adBlock blocks ads and trackers in browser panes (browser-block-ads):
// the first time a pane shows a page with the setting on, it downloads
// the filter lists the settings name (browser-filter-list), if it has
// to, and compiles them off the main thread, and again when the user
// chooses other lists. Pages made before that is done get the rules when
// it is.
type adBlock struct {
started bool
// lists are those compiled, or compiling, last; gen counts the
// compiles, so that only the latest one's rules apply.
lists []string
gen int
rules []*mygo.ContentRules // main thread
}
// adRules returns the rules browser panes' pages apply: none while the
// setting is off or the rules are not compiled yet.
func (a *App) adRules() []*mygo.ContentRules {
if !a.set.BlockAds {
return nil
}
a.compileAds()
return a.ads.rules
}
// compileAds compiles the lists the settings name, unless those compiled
// or compiling last are those.
func (a *App) compileAds() {
if a.ads.started && slices.Equal(a.ads.lists, a.set.AdLists) {
return
}
a.ads.started, a.ads.lists = true, slices.Clone(a.set.AdLists)
a.ads.gen++
gen, lists := a.ads.gen, a.ads.lists
go func() {
rules, err := compileAdBlock(lists)
if err != nil {
log.Printf("panenest: ad blocking: %v", err)
}
a.update(func() {
if a.ads.gen != gen {
return // other lists compile now
}
a.ads.rules = rules
a.applyAdBlock()
})
}()
}
// applyAdBlock gives the open browser panes' pages the rules, or takes
// them away, as the settings are; they apply to the loads from then on.
func (a *App) applyAdBlock() {
rules := a.adRules()
for _, e := range a.webs {
if e.view != nil && !e.view.IsDestroyed() {
e.view.Page().SetContentRules(rules...)
}
}
}
// compiling has one compile run at a time: each compiles the lists
// panenest-ads-0, panenest-ads-1 and so on, and replaces what the one before it
// compiled under those ids.
var compiling sync.Mutex
// compileAdBlock compiles the filter lists of ids, as many content rule
// lists as they take, keeping those it compiled if one fails.
func compileAdBlock(ids []string) ([]*mygo.ContentRules, error) {
compiling.Lock()
defer compiling.Unlock()
var sources []adblock.Source
for _, id := range ids {
if s, ok := adblock.Find(id); ok {
sources = append(sources, s)
}
}
if len(sources) == 0 {
return nil, nil
}
filters, err := adblock.Load(context.Background(), filepath.Join(proto.Dir(), "adblock"), sources)
if err != nil {
return nil, err
}
var r adblock.Rules
for _, f := range filters {
r.Convert(f)
}
var rules []*mygo.ContentRules
for i, list := range r.Lists() {
cr, cerr := mygo.CompileContentRules(fmt.Sprintf("panenest-ads-%d", i), list)
if cerr != nil {
err = cerr
continue
}
rules = append(rules, cr)
}
return rules, err
}