/
set-delegates.js
248 lines (216 loc) · 7.43 KB
/
set-delegates.js
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
var ObjCClass = require('cocoascript-class').default
var parseWebArguments = require('./parseWebArguments')
var CONSTANTS = require('./constants')
// We create one ObjC class for ourselves here
var WindowDelegateClass
var NavigationDelegateClass
var WebScriptHandlerClass
// TODO: events
// - 'page-favicon-updated'
// - 'new-window'
// - 'did-navigate-in-page'
// - 'will-prevent-unload'
// - 'crashed'
// - 'unresponsive'
// - 'responsive'
// - 'destroyed'
// - 'before-input-event'
// - 'certificate-error'
// - 'found-in-page'
// - 'media-started-playing'
// - 'media-paused'
// - 'did-change-theme-color'
// - 'update-target-url'
// - 'cursor-changed'
// - 'context-menu'
// - 'select-bluetooth-device'
// - 'paint'
// - 'console-message'
module.exports = function(browserWindow, panel, webview) {
if (!WindowDelegateClass) {
WindowDelegateClass = ObjCClass({
classname: 'WindowDelegateClass',
utils: null,
panel: null,
// Tells the delegate that the window has been resized.
'windowDidResize:': function() {
this.utils.emit('resize')
},
// Tells the delegate that the window has been resized.
'windowDidMiniaturize:': function() {
this.utils.emit('minimize')
},
// Tells the delegate that the window has been resized.
'windowDidDeminiaturize:': function() {
this.utils.emit('restore')
},
// Tells the delegate that the window has been resized.
'windowDidEnterFullScreen:': function() {
this.utils.emit('enter-full-screen')
},
// Tells the delegate that the window has been resized.
'windowDidExitFullScreen:': function() {
this.utils.emit('leave-full-screen')
},
// Tells the delegate that the window has been resized.
'windowDidMove:': function() {
this.utils.emit('move')
this.utils.emit('moved')
},
// Tells the delegate that the window has been resized.
'windowShouldClose:': function() {
var shouldClose = true
this.utils.emit('close', {
get defaultPrevented() {
return !shouldClose
},
preventDefault: function() {
shouldClose = false
},
})
return shouldClose
},
'windowWillClose:': function() {
this.utils.emit('closed')
},
'windowDidBecomeKey:': function() {
this.utils.emit('focus', this.panel.currentEvent())
},
'windowDidResignKey:': function() {
this.utils.emit('blur')
},
})
}
if (!NavigationDelegateClass) {
NavigationDelegateClass = ObjCClass({
classname: 'NavigationDelegateClass',
state: NSMutableDictionary.dictionaryWithDictionary({
wasReady: 0,
}),
utils: null,
// // Called when the web view begins to receive web content.
'webView:didCommitNavigation:': function(webView) {
this.utils.emit('will-navigate', {}, String(String(webView.url())))
},
// // Called when web content begins to load in a web view.
'webView:didStartProvisionalNavigation:': function() {
this.utils.emit('did-start-navigation')
this.utils.emit('did-start-loading')
},
// Called when a web view receives a server redirect.
'webView:didReceiveServerRedirectForProvisionalNavigation:': function() {
this.utils.emit('did-get-redirect-request')
},
// // Called when the web view needs to respond to an authentication challenge.
'webView:didReceiveAuthenticationChallenge:completionHandler:': function(
webView,
challenge,
completionHandler
) {
function callback(username, password) {
completionHandler(
0,
NSURLCredential.credentialWithUser_password_persistence(
username,
password,
1
)
)
}
var protectionSpace = challenge.protectionSpace()
this.utils.emit(
'login',
{},
{
method: String(protectionSpace.authenticationMethod()),
url: 'not implemented', // TODO:
referrer: 'not implemented', // TODO:
},
{
isProxy: !!protectionSpace.isProxy(),
scheme: String(protectionSpace.protocol()),
host: String(protectionSpace.host()),
port: Number(protectionSpace.port()),
realm: String(protectionSpace.realm()),
},
callback
)
},
// Called when an error occurs during navigation.
// 'webView:didFailNavigation:withError:': function(
// webView,
// navigation,
// error
// ) {},
// Called when an error occurs while the web view is loading content.
'webView:didFailProvisionalNavigation:withError:': function(
webView,
navigation,
error
) {
this.utils.emit('did-fail-load', error)
},
// Called when the navigation is complete.
'webView:didFinishNavigation:': function() {
if (this.state.wasReady == 0) {
// eslint-disable-line
this.utils.emitBrowserEvent('ready-to-show')
this.state.setObject_forKey(1, 'wasReady')
}
this.utils.emit('did-navigate')
this.utils.emit('did-frame-navigate')
this.utils.emit('did-stop-loading')
this.utils.emit('did-finish-load')
this.utils.emit('did-frame-finish-load')
},
// Called when the web view’s web content process is terminated.
'webViewWebContentProcessDidTerminate:': function() {
this.utils.emit('dom-ready')
},
// Decides whether to allow or cancel a navigation.
// webView:decidePolicyForNavigationAction:decisionHandler:
// Decides whether to allow or cancel a navigation after its response is known.
// webView:decidePolicyForNavigationResponse:decisionHandler:
})
}
if (!WebScriptHandlerClass) {
WebScriptHandlerClass = ObjCClass({
classname: 'WebScriptHandlerClass',
utils: null,
'userContentController:didReceiveScriptMessage:': function(_, message) {
var webArguments = JSON.parse(String(message.body()))
var args = this.utils.parseWebArguments([JSON.stringify(webArguments)])
if (!args) {
return
}
this.utils.emit.apply(this, args)
},
})
}
var navigationDelegate = NavigationDelegateClass.new()
navigationDelegate.utils = NSDictionary.dictionaryWithDictionary({
setTitle: browserWindow.setTitle.bind(browserWindow),
emitBrowserEvent: browserWindow.emit.bind(browserWindow),
emit: browserWindow.webContents.emit.bind(browserWindow.webContents),
})
// reset state as well
navigationDelegate.state = NSMutableDictionary.dictionaryWithDictionary({
wasReady: 0,
})
webview.setNavigationDelegate(navigationDelegate)
var webScriptHandler = WebScriptHandlerClass.new()
webScriptHandler.utils = NSDictionary.dictionaryWithDictionary({
emit: browserWindow.webContents.emit.bind(browserWindow.webContents),
parseWebArguments: parseWebArguments,
})
webview
.configuration()
.userContentController()
.addScriptMessageHandler_name(webScriptHandler, CONSTANTS.JS_BRIDGE)
var windowDelegate = WindowDelegateClass.new()
windowDelegate.utils = NSDictionary.dictionaryWithDictionary({
emit: browserWindow.emit.bind(browserWindow),
})
windowDelegate.panel = panel
panel.setDelegate(windowDelegate)
}