-
Notifications
You must be signed in to change notification settings - Fork 28
/
application.go
362 lines (312 loc) · 11.4 KB
/
application.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
// Package cef All CEF implementations of Energy in Go
package cef
import (
"github.com/energye/energy/v2/cef/internal/cef"
"github.com/energye/energy/v2/cef/internal/def"
"github.com/energye/energy/v2/cef/process"
"github.com/energye/energy/v2/common/imports"
. "github.com/energye/energy/v2/consts"
"github.com/energye/energy/v2/logger"
"github.com/energye/golcl/lcl/api"
"unsafe"
)
var application *TCEFApplication
// TCEFApplication CEF应用对象
type TCEFApplication struct {
instance unsafe.Pointer
specificVersion SpecificVersion // 特定版本 default -1
ui UITool
onContextCreated GlobalCEFAppEventOnContextCreated
onProcessMessageReceived RenderProcessMessageReceived
onWebKitInitialized GlobalCEFAppEventOnWebKitInitialized
onRegCustomSchemes GlobalCEFAppEventOnRegCustomSchemes
onRenderLoadStart GlobalCEFAppEventOnRenderLoadStart
externalMessagePump bool
multiThreadedMessageLoop bool
}
// NewApplication 创建CEF应用
//
// 参数: disableRegisDefaultEvent = true 时不会注册默认事件
func NewApplication(disableRegisDefaultEvent ...bool) *TCEFApplication {
if application == nil {
application = CreateApplication()
if len(disableRegisDefaultEvent) == 0 || !disableRegisDefaultEvent[0] {
application.registerDefaultEvent()
}
application.initDefaultSettings()
// 将应用设置到内部实现
cef.SetApplication(application)
}
return application
}
// SetApplication
//
// 设置全局 Application
func SetApplication(app *TCEFApplication) {
if application == nil {
application = app
}
}
// CreateApplication
//
// 创建CEF Application
// 初始化CEF时必须创建,多进程模式每个application配置都应该相同
func CreateApplication() *TCEFApplication {
var result uintptr
imports.Proc(def.CEFApplication_Create).Call(uintptr(unsafe.Pointer(&result)))
return &TCEFApplication{instance: unsafe.Pointer(result), specificVersion: SV_INVALID}
}
// AddCrDelegate MacOSX Delegate
func (m *TCEFApplication) AddCrDelegate() {
imports.Proc(def.CEF_AddCrDelegate).Call()
}
// registerDefaultEvent 注册默认事件
func (m *TCEFApplication) registerDefaultEvent() {
m.defaultSetOnContextCreated()
m.defaultSetOnProcessMessageReceived()
m.defaultSetOnWebKitInitialized()
m.defaultSetOnRegCustomSchemes()
//m.defaultSetOnRenderLoadStart()
}
// Instance 实例
func (m *TCEFApplication) Instance() uintptr {
return uintptr(m.instance)
}
// StartMainProcess 启动主进程
func (m *TCEFApplication) StartMainProcess() bool {
if m.instance != nil {
v8init()
logger.Debug("application single exe,", process.Args.ProcessType(), "process start")
r1, _, _ := imports.Proc(def.CEFStartMainProcess).Call()
return api.GoBool(r1)
}
return false
}
// StartSubProcess 启动子进程, 如果指定了子进程执行程序, 将执行指定的子进程程序
func (m *TCEFApplication) StartSubProcess() (result bool) {
if m.instance != nil {
v8init()
logger.Debug("application multiple exe,", process.Args.ProcessType(), "process start")
r1, _, _ := imports.Proc(def.CEFStartSubProcess).Call()
result = api.GoBool(r1)
}
return false
}
// DoMessageLoopWork
func (m *TCEFApplication) DoMessageLoopWork() {
imports.Proc(def.CEFApplication_DoMessageLoopWork).Call()
}
// RunMessageLoop
// Chrome runtime
func (m *TCEFApplication) RunMessageLoop() {
defer func() {
logger.Debug("application run value loop end")
api.EnergyLibRelease()
}()
logger.Debug("application run value loop start")
imports.Proc(def.CEFApplication_RunMessageLoop).Call()
}
// QuitMessageLoop 退出消息轮询
func (m *TCEFApplication) QuitMessageLoop() {
logger.Debug("application quit value loop")
imports.Proc(def.CEFApplication_QuitMessageLoop).Call()
}
func (m *TCEFApplication) StopScheduler() {
GlobalWorkScheduler.StopScheduler()
}
func (m *TCEFApplication) Destroy() {
imports.Proc(def.CEFApplication_Destroy).Call()
}
func (m *TCEFApplication) Free() {
if m.instance != nil {
imports.Proc(def.CEFApplication_Free).Call()
m.instance = nil
}
}
// AddCustomCommandLine
//
// 添加自定义进程启动时添加的命令行参数
func (m *TCEFApplication) AddCustomCommandLine(commandLine, value string) {
imports.Proc(def.AddCustomCommandLine).Call(api.PascalStr(commandLine), api.PascalStr(value))
}
// SetOnRegCustomSchemes
//
// 自定义协议注册回调函数
func (m *TCEFApplication) SetOnRegCustomSchemes(fn GlobalCEFAppEventOnRegCustomSchemes) {
m.onRegCustomSchemes = fn
}
func (m *TCEFApplication) setOnRegCustomSchemes(fn GlobalCEFAppEventOnRegCustomSchemes) {
imports.Proc(def.CEFGlobalApp_SetOnRegCustomSchemes).Call(api.MakeEventDataPtr(fn))
}
func (m *TCEFApplication) defaultSetOnRegCustomSchemes() {
m.setOnRegCustomSchemes(func(registrar *TCefSchemeRegistrarRef) {
regCustomSchemes(registrar)
if m.onRegCustomSchemes != nil {
m.onRegCustomSchemes(registrar)
}
})
}
// SetOnRegisterCustomPreferences
//
// TODO 该函数还未完全实现
func (m *TCEFApplication) SetOnRegisterCustomPreferences(fn GlobalCEFAppEventOnRegisterCustomPreferences) {
imports.Proc(def.CEFGlobalApp_SetOnRegisterCustomPreferences).Call(api.MakeEventDataPtr(fn))
}
// SetOnContextInitialized
//
// 上下文初始化
func (m *TCEFApplication) SetOnContextInitialized(fn GlobalCEFAppEventOnContextInitialized) {
imports.Proc(def.CEFGlobalApp_SetOnContextInitialized).Call(api.MakeEventDataPtr(fn))
}
// SetOnBeforeChildProcessLaunch
//
// 启动子进程之前自定义命令行参数设置
func (m *TCEFApplication) SetOnBeforeChildProcessLaunch(fn GlobalCEFAppEventOnBeforeChildProcessLaunch) {
imports.Proc(def.CEFGlobalApp_SetOnBeforeChildProcessLaunch).Call(api.MakeEventDataPtr(fn))
}
// SetOnGetDefaultClient
//
// 获取并返回CefClient, 我们自己创建并返回到 *ICefClient = myCefClient
func (m *TCEFApplication) SetOnGetDefaultClient(fn GlobalCEFAppEventOnGetDefaultClient) {
imports.Proc(def.CEFGlobalApp_SetOnGetDefaultClient).Call(api.MakeEventDataPtr(fn))
}
// SetOnGetLocalizedString
//
// 获取并返回本地化
func (m *TCEFApplication) SetOnGetLocalizedString(fn GlobalCEFAppEventOnGetLocalizedString) {
imports.Proc(def.CEFGlobalApp_SetOnGetLocalizedString).Call(api.MakeEventDataPtr(fn))
}
// SetOnGetDataResource
//
// 获取并返回本地资源
func (m *TCEFApplication) SetOnGetDataResource(fn GlobalCEFAppEventOnGetDataResource) {
imports.Proc(def.CEFGlobalApp_SetOnGetDataResource).Call(api.MakeEventDataPtr(fn))
}
// SetOnGetDataResourceForScale
//
// 获取并返回本地资源大小
func (m *TCEFApplication) SetOnGetDataResourceForScale(fn GlobalCEFAppEventOnGetDataResourceForScale) {
imports.Proc(def.CEFGlobalApp_SetOnGetDataResourceForScale).Call(api.MakeEventDataPtr(fn))
}
func (m *TCEFApplication) SetOnWebKitInitialized(fn GlobalCEFAppEventOnWebKitInitialized) {
m.onWebKitInitialized = fn
}
func (m *TCEFApplication) setOnWebKitInitialized(fn GlobalCEFAppEventOnWebKitInitialized) {
imports.Proc(def.CEFGlobalApp_SetOnWebKitInitialized).Call(api.MakeEventDataPtr(fn))
}
func (m *TCEFApplication) defaultSetOnWebKitInitialized() {
m.setOnWebKitInitialized(func() {
appWebKitInitialized()
if m.onWebKitInitialized != nil {
m.onWebKitInitialized()
}
})
}
func (m *TCEFApplication) SetOnBrowserCreated(fn GlobalCEFAppEventOnBrowserCreated) {
imports.Proc(def.CEFGlobalApp_SetOnBrowserCreated).Call(api.MakeEventDataPtr(fn))
}
func (m *TCEFApplication) SetOnBrowserDestroyed(fn GlobalCEFAppEventOnBrowserDestroyed) {
imports.Proc(def.CEFGlobalApp_SetOnBrowserDestroyed).Call(api.MakeEventDataPtr(fn))
}
func (m *TCEFApplication) SetOnContextCreated(fn GlobalCEFAppEventOnContextCreated) {
m.onContextCreated = fn
}
func (m *TCEFApplication) setOnContextCreated(fn GlobalCEFAppEventOnContextCreated) {
imports.Proc(def.CEFGlobalApp_SetOnContextCreated).Call(api.MakeEventDataPtr(fn))
}
func (m *TCEFApplication) defaultSetOnContextCreated() {
m.setOnContextCreated(func(browse *ICefBrowser, frame *ICefFrame, context *ICefV8Context) bool {
var flag bool
if m.onContextCreated != nil {
flag = m.onContextCreated(browse, frame, context)
}
if !flag {
appOnContextCreated(browse, frame, context)
}
return false
})
}
func (m *TCEFApplication) SetOnContextReleased(fn GlobalCEFAppEventOnContextReleased) {
imports.Proc(def.CEFGlobalApp_SetOnContextReleased).Call(api.MakeEventDataPtr(fn))
}
func (m *TCEFApplication) SetOnUncaughtException(fn GlobalCEFAppEventOnUncaughtException) {
imports.Proc(def.CEFGlobalApp_SetOnUncaughtException).Call(api.MakeEventDataPtr(fn))
}
func (m *TCEFApplication) SetOnFocusedNodeChanged(fn GlobalCEFAppEventOnFocusedNodeChanged) {
imports.Proc(def.CEFGlobalApp_SetOnFocusedNodeChanged).Call(api.MakeEventDataPtr(fn))
}
// SetOnProcessMessageReceived
//
// 进程间通信处理消息接收回调函数
func (m *TCEFApplication) SetOnProcessMessageReceived(fn RenderProcessMessageReceived) {
m.onProcessMessageReceived = fn
}
func (m *TCEFApplication) setOnProcessMessageReceived(fn RenderProcessMessageReceived) {
imports.Proc(def.CEFGlobalApp_SetOnProcessMessageReceived).Call(api.MakeEventDataPtr(fn))
}
func (m *TCEFApplication) defaultSetOnProcessMessageReceived() {
m.setOnProcessMessageReceived(func(browse *ICefBrowser, frame *ICefFrame, sourceProcess CefProcessId, processMessage *ICefProcessMessage) bool {
var result = renderProcessMessageReceived(browse, frame, sourceProcess, processMessage)
if m.onProcessMessageReceived != nil && !result {
result = m.onProcessMessageReceived(browse, frame, sourceProcess, processMessage)
}
return result
})
}
func (m *TCEFApplication) SetOnRenderLoadingStateChange(fn GlobalCEFAppEventOnRenderLoadingStateChange) {
imports.Proc(def.CEFGlobalApp_SetOnRenderLoadingStateChange).Call(api.MakeEventDataPtr(fn))
}
func (m *TCEFApplication) SetOnRenderLoadStart(fn GlobalCEFAppEventOnRenderLoadStart) {
if m.IsSpecVer49() {
return
}
//m.onRenderLoadStart = fn
imports.Proc(def.CEFGlobalApp_SetOnRenderLoadStart).Call(api.MakeEventDataPtr(fn))
}
//func (m *TCEFApplication) setOnRenderLoadStart(fn GlobalCEFAppEventOnRenderLoadStart) {
// if m.IsSpecVer49() {
// return
// }
// imports.Proc(def.CEFGlobalApp_SetOnRenderLoadStart).Call(api.MakeEventDataPtr(fn))
//}
//func (m *TCEFApplication) defaultSetOnRenderLoadStart() {
// if m.IsSpecVer49() {
// return
// }
// m.setOnRenderLoadStart(func(browser *ICefBrowser, frame *ICefFrame, transitionType TCefTransitionType) {
// if m.onRenderLoadStart != nil {
// m.onRenderLoadStart(browser, frame, transitionType)
// }
// })
//}
func (m *TCEFApplication) SetOnRenderLoadEnd(fn GlobalCEFAppEventOnRenderLoadEnd) {
if m.IsSpecVer49() {
return
}
imports.Proc(def.CEFGlobalApp_SetOnRenderLoadEnd).Call(api.MakeEventDataPtr(fn))
}
func (m *TCEFApplication) SetOnRenderLoadError(fn GlobalCEFAppEventOnRenderLoadError) {
if m.IsSpecVer49() {
return
}
imports.Proc(def.CEFGlobalApp_SetOnRenderLoadError).Call(api.MakeEventDataPtr(fn))
}
func (m *TCEFApplication) SetOnScheduleMessagePumpWork(fn GlobalCEFAppEventOnScheduleMessagePumpWork) {
if m.IsSpecVer49() {
return
}
var callback uintptr
if fn != nil {
callback = api.MakeEventDataPtr(fn)
}
imports.Proc(def.CEFGlobalApp_SetOnScheduleMessagePumpWork).Call(callback)
}