-
Notifications
You must be signed in to change notification settings - Fork 12
/
gowebview.go
223 lines (169 loc) · 6.5 KB
/
gowebview.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
package gowebview
import (
"crypto/x509"
"os"
"path/filepath"
"strings"
)
//go:generate go run ./generator/generate.go
// WebView is the interface implemented by each webview.
type WebView interface {
// Run runs the main loop until it's terminated. After this function exits -
// you must destroy the webview.
Run()
// Terminate stops the main loop. It is safe to call this function from
// a background thread.
Terminate()
// Destroy destroys a webview and closes the native window.
Destroy()
// Window returns a native window handle pointer. When using GTK backend the
// pointer is GtkWindow pointer, when using Cocoa backend the pointer is
// NSWindow pointer, when using Win32 backend the pointer is HWND pointer.
Window() uintptr
// SetTitle updates the title of the native window. Must be called from the UI
// thread.
SetTitle(title string)
// SetSize updates native window size. See Hint constants.
SetSize(point *Point, hint Hint)
// Navigate navigates webview to the given URL. URL may be a data URI, i.e.
// "data:text/text,<html>...</html>".
SetURL(url string)
// SetVisibility updates the WindowMode, such as minimized or maximized
SetVisibility(v Visibility)
// Init injects JavaScript code at the initialization of the new page. Every
// time the webview will open a the new page - this initialization code will
// be executed. It is guaranteed that code is executed before window.onload.
//Init(js string)
// Eval evaluates arbitrary JavaScript code. Evaluation happens asynchronously,
// also the result of the expression is ignored. Use RPC bindings if you want
// to receive notifications about the results of the evaluation.
//Eval(js string)
}
// New calls NewWindow to create a new window and a new webview instance. If debug
// is non-zero - developer tools will be enabled (if the platform supports them).
func New(config *Config) (WebView, error) {
if config == nil {
config = new(Config)
}
if config.WindowConfig == nil {
config.WindowConfig = &WindowConfig{}
}
if config.TransportConfig == nil {
config.TransportConfig = &TransportConfig{}
}
if config.WindowConfig.Title == "" {
dir, err := os.Executable()
if err != nil {
dir = "gowebview"
}
filename := filepath.Base(filepath.Clean(dir))
config.WindowConfig.Title = strings.Title(strings.TrimSuffix(filename, filepath.Ext(filename)))
}
if config.WindowConfig.Size == nil {
config.WindowConfig.Size = &Point{X: 600, Y: 600}
}
if config.WindowConfig.Path == "" {
config.WindowConfig.Path = filepath.Join(os.TempDir(), config.WindowConfig.Title)
}
return newWindow(config)
}
// Config are used to set the initial and default values to the WebView.
type Config struct {
// WindowConfig keeps configurations about the window
WindowConfig *WindowConfig
// TransportConfig keeps configurations about the network traffic
TransportConfig *TransportConfig
// URL defines the default page.
URL string
// Debug if is non-zero the Developer Tools will be enabled (if supported).
Debug bool
}
// WindowConfig describes topics related to the Window/View.
type WindowConfig struct {
// Title defines the title of the window.
Title string
// Size defines the Width x Height of the window.
Size *Point
// Path defines the path where the DLL will be exported.
Path string
// Visibility defines how the page must open.
Visibility Visibility
// Window defines the window handle (GtkWindow, NSWindow, HWND pointer or View pointer for Android).
// For Gio (Android): it MUST point to `e.View` from `app.ViewEvent`
Window uintptr
// VM defines the JNI VM for Android
// For Gio (Android): it MUST point to `app.JavaVM()`
VM uintptr
}
// WindowConfig describes topics related to the network traffic.
type TransportConfig struct {
// Proxy defines the proxy which the connection will be pass through.
Proxy *HTTPProxy
// InsecureBypassCustomProxy if true it might ignore the Proxy settings provided, if fails it to be used. Otherwise
// it could return ErrFeatureNotSupported or ErrImpossibleProxy when create the WebView.
//
// It doesn't have any effect if Proxy is undefined (nil).
// WARNING: It's might be danger if you define an custom Proxy, since it expose the connection without any proxy.
InsecureIgnoreCustomProxy bool
// Authorities defines custom authorities that your app trusts. It could be combined with standard authorities from
// the machine, it might adds the certificate persistent on the user machine and might requires user interaction
// to approve such action.
CertificateAuthorities []x509.Certificate
// @TODO Support CertificatePinning
// CertificatePinning if set will drop the connection if connect to one website that doesn't have the exactly
// same certificate hash.
// CertificateKeyPinning []byte
// @TODO Support InsecureIgnoreCAVerification
// InsecureIgnoreCAVerification if true will load pages can be loaded without verifies the certificate.
// WARNING: It's might be danger and expose you to MITM.
//InsecureIgnoreCertificateVerification bool
// IgnoreNetworkIsolation if true will load pages from the local-network (such as 127.0.0.1).
IgnoreNetworkIsolation bool
}
// Hint are used to configure window sizing and resizing
type Hint int
const (
// HintNone set the current and default width and height
HintNone Hint = iota
// HintFixed prevents the window size to be changed by a user
HintFixed
// HintMin set the minimum bounds
HintMin
// HintMax set the maximum bounds
HintMax
)
// Visibility are used to configure if the window mode (maximized or minimized)
type Visibility int
const (
// VisibilityDefault will open the window at their default WindowMode
VisibilityDefault Visibility = iota
// VisibilityMaximized will open the window as maximized
// Windowless systems (like Android) will open as fullscreen
VisibilityMaximized
// VisibilityMinimized will open the window as minimized
// Windowless systems (like Android) will hides the webview (return the previous view)
VisibilityMinimized
)
// Point are used to configure the size or coordinates.
type Point struct {
X, Y int64
}
// HTTPProxy are used to configure the Proxy
type HTTPProxy struct {
IP string
Port string
}
// Network implements net.Addr
func (p *HTTPProxy) Network() string {
return "tcp"
}
// String implements net.Addr
func (p *HTTPProxy) String() string {
if p == nil || (p.Port == "" && p.IP == "") {
return ""
}
if strings.Index(p.IP, `:`) >= 0 && !strings.HasPrefix(p.IP, `[`) && !strings.HasPrefix(p.IP, `]`) {
return "[" + p.IP + "]:" + p.Port
}
return p.IP + ":" + p.Port
}