-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebview.go
253 lines (201 loc) · 6.35 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
// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package walk
import (
"fmt"
"syscall"
"unsafe"
)
import . "github.com/lxn/go-winapi"
const webViewWindowClass = `\o/ Walk_WebView_Class \o/`
func init() {
MustRegisterWindowClass(webViewWindowClass)
}
type WebView struct {
WidgetBase
clientSite webViewIOleClientSite // IMPORTANT: Must remain first member after WidgetBase
browserObject *IOleObject
urlProperty *Property
}
func NewWebView(parent Container) (*WebView, error) {
if hr := OleInitialize(); hr != S_OK && hr != S_FALSE {
return nil, newError(fmt.Sprint("OleInitialize Error: ", hr))
}
wv := &WebView{
clientSite: webViewIOleClientSite{
IOleClientSite: IOleClientSite{
LpVtbl: webViewIOleClientSiteVtbl,
},
inPlaceSite: webViewIOleInPlaceSite{
IOleInPlaceSite: IOleInPlaceSite{
LpVtbl: webViewIOleInPlaceSiteVtbl,
},
inPlaceFrame: webViewIOleInPlaceFrame{
IOleInPlaceFrame: IOleInPlaceFrame{
LpVtbl: webViewIOleInPlaceFrameVtbl,
},
},
},
docHostUIHandler: webViewIDocHostUIHandler{
IDocHostUIHandler: IDocHostUIHandler{
LpVtbl: webViewIDocHostUIHandlerVtbl,
},
},
webBrowserEvents2: webViewDWebBrowserEvents2{
DWebBrowserEvents2: DWebBrowserEvents2{
LpVtbl: webViewDWebBrowserEvents2Vtbl,
},
},
},
}
if err := InitChildWidget(
wv,
parent,
webViewWindowClass,
WS_CLIPCHILDREN|WS_VISIBLE,
0); err != nil {
return nil, err
}
wv.clientSite.inPlaceSite.inPlaceFrame.webView = wv
succeeded := false
defer func() {
if !succeeded {
wv.Dispose()
}
}()
var classFactoryPtr unsafe.Pointer
if hr := CoGetClassObject(&CLSID_WebBrowser, CLSCTX_INPROC_HANDLER|CLSCTX_INPROC_SERVER, nil, &IID_IClassFactory, &classFactoryPtr); FAILED(hr) {
return nil, errorFromHRESULT("CoGetClassObject", hr)
}
classFactory := (*IClassFactory)(classFactoryPtr)
defer classFactory.Release()
var browserObjectPtr unsafe.Pointer
if hr := classFactory.CreateInstance(nil, &IID_IOleObject, &browserObjectPtr); FAILED(hr) {
return nil, errorFromHRESULT("IClassFactory.CreateInstance", hr)
}
browserObject := (*IOleObject)(browserObjectPtr)
wv.browserObject = browserObject
if hr := browserObject.SetClientSite((*IOleClientSite)(unsafe.Pointer(&wv.clientSite))); FAILED(hr) {
return nil, errorFromHRESULT("IOleObject.SetClientSite", hr)
}
if hr := browserObject.SetHostNames(syscall.StringToUTF16Ptr("Walk.WebView"), nil); FAILED(hr) {
return nil, errorFromHRESULT("IOleObject.SetHostNames", hr)
}
if hr := OleSetContainedObject((*IUnknown)(unsafe.Pointer(browserObject)), true); FAILED(hr) {
return nil, errorFromHRESULT("OleSetContainedObject", hr)
}
var rect RECT
GetClientRect(wv.hWnd, &rect)
if hr := browserObject.DoVerb(OLEIVERB_SHOW, nil, (*IOleClientSite)(unsafe.Pointer(&wv.clientSite)), -1, wv.hWnd, &rect); FAILED(hr) {
return nil, errorFromHRESULT("IOleObject.DoVerb", hr)
}
var cpcPtr unsafe.Pointer
if hr := browserObject.QueryInterface(&IID_IConnectionPointContainer, &cpcPtr); FAILED(hr) {
return nil, errorFromHRESULT("IOleObject.QueryInterface(IID_IConnectionPointContainer)", hr)
}
cpc := (*IConnectionPointContainer)(cpcPtr)
defer cpc.Release()
var cp *IConnectionPoint
if hr := cpc.FindConnectionPoint(&DIID_DWebBrowserEvents2, &cp); FAILED(hr) {
return nil, errorFromHRESULT("IConnectionPointContainer.FindConnectionPoint(DIID_DWebBrowserEvents2)", hr)
}
defer cp.Release()
var cookie uint32
if hr := cp.Advise(unsafe.Pointer(&wv.clientSite.webBrowserEvents2), &cookie); FAILED(hr) {
return nil, errorFromHRESULT("IConnectionPoint.Advise", hr)
}
wv.onResize()
wv.urlProperty = NewProperty(
"URL",
func() interface{} {
url, _ := wv.URL()
return url
},
func(v interface{}) error {
return wv.SetURL(v.(string))
},
nil)
wv.MustRegisterProperties(wv.urlProperty)
succeeded = true
return wv, nil
}
func (wv *WebView) Dispose() {
if wv.browserObject != nil {
wv.browserObject.Close(OLECLOSE_NOSAVE)
wv.browserObject.Release()
wv.browserObject = nil
OleUninitialize()
}
wv.WidgetBase.Dispose()
}
func (*WebView) LayoutFlags() LayoutFlags {
return ShrinkableHorz | ShrinkableVert | GrowableHorz | GrowableVert | GreedyHorz | GreedyVert
}
func (*WebView) SizeHint() Size {
return Size{100, 100}
}
func (wv *WebView) URL() (url string, err error) {
err = wv.withWebBrowser2(func(webBrowser2 *IWebBrowser2) error {
var urlBstr *uint16 /*BSTR*/
if hr := webBrowser2.Get_LocationURL(&urlBstr); FAILED(hr) {
return errorFromHRESULT("IWebBrowser2.Get_LocationURL", hr)
}
defer SysFreeString(urlBstr)
url = BSTRToString(urlBstr)
return nil
})
return
}
func (wv *WebView) SetURL(url string) error {
return wv.withWebBrowser2(func(webBrowser2 *IWebBrowser2) error {
urlBstr := StringToVariantBSTR(url)
flags := IntToVariantI4(0)
targetFrameName := StringToVariantBSTR("_self")
if hr := webBrowser2.Navigate2(urlBstr, flags, targetFrameName, nil, nil); FAILED(hr) {
return errorFromHRESULT("IWebBrowser2.Navigate2", hr)
}
return nil
})
}
func (wv *WebView) URLChanged() *Event {
return wv.urlProperty.Changed()
}
func (wv *WebView) Refresh() error {
return wv.withWebBrowser2(func(webBrowser2 *IWebBrowser2) error {
if hr := webBrowser2.Refresh(); FAILED(hr) {
return errorFromHRESULT("IWebBrowser2.Refresh", hr)
}
return nil
})
}
func (wv *WebView) withWebBrowser2(f func(webBrowser2 *IWebBrowser2) error) error {
var webBrowser2Ptr unsafe.Pointer
if hr := wv.browserObject.QueryInterface(&IID_IWebBrowser2, &webBrowser2Ptr); FAILED(hr) {
return errorFromHRESULT("IOleObject.QueryInterface", hr)
}
webBrowser2 := (*IWebBrowser2)(webBrowser2Ptr)
defer webBrowser2.Release()
return f(webBrowser2)
}
func (wv *WebView) onResize() {
// FIXME: handle error?
wv.withWebBrowser2(func(webBrowser2 *IWebBrowser2) error {
bounds := wv.ClientBounds()
webBrowser2.Put_Left(0)
webBrowser2.Put_Top(0)
webBrowser2.Put_Width(int32(bounds.Width))
webBrowser2.Put_Height(int32(bounds.Height))
return nil
})
}
func (wv *WebView) WndProc(hwnd HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case WM_SIZE, WM_SIZING:
if wv.clientSite.inPlaceSite.inPlaceFrame.webView == nil {
break
}
wv.onResize()
}
return wv.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
}