This repository has been archived by the owner on Oct 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webView.go
294 lines (273 loc) · 7.45 KB
/
webView.go
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package golem
// #cgo pkg-config: webkit2gtk-4.0
// #include <webkit2/webkit2.h>
import "C"
import (
"fmt"
"html"
"net/url"
"unsafe"
"github.com/conformal/gotk3/gdk"
"github.com/conformal/gotk3/glib"
"github.com/conformal/gotk3/gtk"
"github.com/tkerber/golem/cmd"
"github.com/tkerber/golem/golem/ui"
ggtk "github.com/tkerber/golem/gtk"
"github.com/tkerber/golem/webkit"
)
// webView wraps a webkit WebView to do additional bookkeeping.
type webView struct {
*webkit.WebView
*webExtension
*tabCfg
id uint64
top int64
height int64
parent *Golem
settings *webkit.Settings
window *Window
tabUI *ui.TabBarTab
fullscreen bool
searchForward bool
handles []glib.SignalHandle
}
// newWebView creates a new webView.
func (w *Window) newWebView() (*webView, error) {
rets := ggtk.GlibMainContextInvoke(
webkit.NewWebViewWithUserContentManager,
w.parent.userContentManager)
if rets[1] != nil {
return nil, rets[1].(error)
}
wv := rets[0].(*webkit.WebView)
// Each WebView gets it's own settings, to allow toggling settings on a
// per tab and/or per window basis.
newSettings := w.defaultSettings.Clone()
wv.SetSettings(newSettings)
webExten := new(webExtension)
ret := &webView{
wv,
webExten,
w.windowCfg.tabCfg.clone(),
wv.GetPageID(),
0,
0,
w.parent,
newSettings,
w,
nil,
false,
true,
make([]glib.SignalHandle, 0, 4),
}
// Attach to the create signal, which creates new tabs on demand.
handle, err := ret.WebView.Connect("create",
func(_ interface{}, ptr uintptr) {
// TODO clean this up. It should probably be somewhere in the
// webkit package.
boxed := (*C.WebKitNavigationAction)(unsafe.Pointer(ptr))
req := C.webkit_navigation_action_get_request(boxed)
cStr := (*C.char)(C.webkit_uri_request_get_uri(req))
if ret.window == nil {
ret.window.logError("A tab currently not associated to a " +
"window attempted to open a new tab. The request was " +
"dropped.")
} else {
ggtk.GlibMainContextInvoke(func() {
wvs, err := ret.window.NewTabs(C.GoString(cStr))
if err != nil {
ret.window.logError("Failed creation of new tab...")
} else {
// Focus our new tab.
ret.window.TabGo(ret.window.tabIndex(wvs[0]))
}
})
}
})
if err != nil {
return nil, err
}
ret.handles = append(ret.handles, handle)
// Attach to decision policies.
handle, err = ret.WebView.Connect("decide-policy",
func(
_ interface{},
decision webkit.PolicyDecision,
t C.WebKitPolicyDecisionType) bool {
switch t {
case C.WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
fallthrough
case C.WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
decision := decision.(*webkit.NavigationPolicyDecision)
action := decision.GetNavigationAction()
button := action.GetMouseButton()
modifiers := action.GetModifiers()
if button == 2 || (modifiers&cmd.ControlMask) != 0 {
// We don't actually want to open this window directly.
// we want it in a new tab.
decision.Ignore()
if ret.window == nil {
ret.window.logError("A tab currently not associated " +
"to a window attempted to open a new tab. The " +
"request was dropped.")
return true
}
req := action.GetRequest()
ggtk.GlibMainContextInvoke(func() {
_, err := ret.window.newTabWithRequest(req)
if err != nil {
ret.window.logError("Failed creation of new tab...")
}
})
return true
}
case C.WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
decision := decision.(*webkit.ResponsePolicyDecision)
resp := decision.GetResponse()
mimetype := resp.GetMimeType()
switch mimetype {
case "application/pdf", "application/x-pdf":
if resp.GetURI() == ret.WebView.GetURI() {
site, err := Asset("srv/pdf.js/frame.html.fmt")
if err == nil && w.parent.pdfjsEnabled {
decision.Ignore()
ret.WebView.LoadAlternateHTML(
[]byte(fmt.Sprintf(
string(site),
html.EscapeString(url.QueryEscape(resp.GetURI())))),
resp.GetURI(),
fmt.Sprintf(
"golem-unsafe://pdf.js/frame.html?%s",
resp.GetURI()))
return true
}
}
return false
default:
return false
}
}
return false
})
if err != nil {
return nil, err
}
ret.handles = append(ret.handles, handle)
handle, err = ret.WebView.Connect(
"button-press-event",
func(_ interface{}, e *gdk.Event) bool {
if ret.window == nil {
(*Window)(nil).logError("Button press registered on non-" +
"visible webview. Dropping.")
return false
}
return ret.window.handleBackForwardButtons(nil, e)
})
if err != nil {
return nil, err
}
ret.handles = append(ret.handles, handle)
// history handle
handle, err = ret.WebView.Connect("load-changed",
func(_ interface{}, e C.WebKitLoadEvent) {
switch e {
case C.WEBKIT_LOAD_FINISHED:
go ret.parent.updateHistory(wv.GetURI(), wv.GetTitle())
}
})
if err == nil {
ret.handles = append(ret.handles, handle)
}
// tab ui handles.
handle, err = ret.WebView.Connect("notify::title",
func() {
if ret.tabUI != nil {
ret.tabUI.SetTitle(wv.GetTitle())
}
})
if err == nil {
ret.handles = append(ret.handles, handle)
}
handle, err = ret.WebView.Connect("notify::estimated-load-progress",
func() {
if ret.tabUI != nil {
ret.tabUI.SetLoadProgress(wv.GetEstimatedLoadProgress())
}
})
if err == nil {
ret.handles = append(ret.handles, handle)
}
handle, err = ret.WebView.Connect("notify::favicon", ret.faviconChanged)
if err == nil {
ret.handles = append(ret.handles, handle)
}
// fullscreen handles
handle, err = wv.Connect("enter-fullscreen", func() bool {
ret.fullscreen = true
return false
})
if err == nil {
ret.handles = append(ret.handles, handle)
}
handle, err = wv.Connect("leave-fullscreen", func() bool {
ret.fullscreen = false
return false
})
if err == nil {
ret.handles = append(ret.handles, handle)
}
// Add webview to golem and return.
w.parent.wMutex.Lock()
defer w.parent.wMutex.Unlock()
w.parent.webViews[ret.id] = ret
return ret, nil
}
// faviconChanged resets the favicon in the tab bar display.
func (wv *webView) faviconChanged() {
if wv.tabUI != nil {
favicon, _ := wv.GetFavicon()
wv.tabUI.SetIcon(favicon)
}
}
// GetTop retrieves the scroll distance from the top of the web view.
func (wv *webView) GetTop() int64 {
return wv.top
}
// GetHeight retrieves the height of the web view.
func (wv *webView) GetHeight() int64 {
return wv.height
}
// GetWebView retrieves the webkit webview.
func (wv *webView) GetWebView() *webkit.WebView {
return wv.WebView
}
// IsQuickmarked checks if the current uri is quickmarked.
func (wv *webView) IsQuickmarked() bool {
_, ok := wv.parent.hasQuickmark[wv.GetURI()]
return ok
}
// IsBookmarked checks if the current uri is bookmarked.
func (wv *webView) IsBookmarked() bool {
_, ok := wv.parent.isBookmark[wv.GetURI()]
return ok
}
// detach detaches the webview from the ui.
func (wv *webView) detach() {
wv.window = nil
wv.tabUI = nil
if p, _ := wv.WebView.GetParent(); p != nil {
cont := >k.Container{*p}
cont.Remove(wv.WebView)
}
}
// close updates bookkeeping after the web view is closed.
func (wv *webView) close() {
for _, handle := range wv.handles {
ggtk.GlibMainContextInvoke(wv.WebView.HandlerDisconnect, handle)
}
wv.parent.wMutex.Lock()
delete(wv.parent.webViews, wv.id)
wv.parent.wMutex.Unlock()
ggtk.GlibMainContextInvoke(wv.detach)
schedGc()
}