-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathcall_function.js
399 lines (369 loc) · 12.6 KB
/
call_function.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* Enum for WebDriver status codes.
* @enum {number}
*/
const StatusCode = {
STALE_ELEMENT_REFERENCE: 10,
JAVA_SCRIPT_ERROR: 17,
NO_SUCH_SHADOW_ROOT: 65,
DETACHED_SHADOW_ROOT: 66,
};
/**
* Enum for node types.
* @enum {number}
*/
const NodeType = {
ELEMENT: 1,
DOCUMENT: 9,
};
/**
* Dictionary key to use for holding an element ID.
* @const
* @type {string}
*/
var ELEMENT_KEY = 'ELEMENT';
/**
* Dictionary key to use for holding a shadow element ID.
* @const
* @type {string}
*/
const SHADOW_ROOT_KEY = 'shadow-6066-11e4-a52e-4f735466cecf';
const W3C_ELEMENT_KEY = 'element-6066-11e4-a52e-4f735466cecf';
const FRAME_KEY = 'frame-075b-4da1-b6ba-e579c2d3230a';
const WINDOW_KEY = 'window-fcc6-11e5-b4f8-330a88ab9d7f';
const REF_KEYS = [
W3C_ELEMENT_KEY,
SHADOW_ROOT_KEY,
FRAME_KEY,
WINDOW_KEY,
ELEMENT_KEY,
];
/**
* True if using W3C Element references.
* @const
* @type {boolean}
*/
var w3cEnabled = false;
/**
* True if shadow dom is enabled.
* @const
* @type {boolean}
*/
const SHADOW_DOM_ENABLED = typeof ShadowRoot === 'function';
/**
* Constructs new error to be thrown with given code and message.
* @param {string} message Message reported to user.
* @param {StatusCode} code StatusCode for error.
* @return {!Error} Error object that can be thrown.
*/
function newError(message, code) {
const error = new Error(message);
error.code = code;
return error;
}
function isNodeReachable(node) {
const Window = window.cdc_adoQpoasnfa76pfcZLmcfl_Window || window.Window;
const nodeRoot = getNodeRootThroughAnyShadows(node);
return (nodeRoot == document.documentElement.parentNode)
|| (nodeRoot instanceof Window);
}
/**
* Returns the root element of the node. Found by traversing parentNodes until
* a node with no parent is found. This node is considered the root.
* @param {?Node} node The node to find the root element for.
* @return {?Node} The root node.
*/
function getNodeRoot(node) {
while (node && node.parentNode) {
node = node.parentNode;
}
return node;
}
/**
* Returns the root element of the node, jumping up through shadow roots if
* any are found.
*/
function getNodeRootThroughAnyShadows(node) {
let root = getNodeRoot(node);
while (SHADOW_DOM_ENABLED && root instanceof ShadowRoot) {
root = getNodeRoot(root.host);
}
return root;
}
/**
* Returns whether given value is an element.
* @param {*} value The value to identify as object.
* @return {boolean} True if value is a cacheable element.
*/
function isElement(value) {
// As of crrev.com/1316933002, typeof() for some elements will return
// 'function', not 'object'. So we need to check for both non-null objects, as
// well Elements that also happen to be callable functions (e.g. <embed> and
// <object> elements). Note that we can not use |value instanceof Object| here
// since this does not work with frames/iframes, for example
// frames[0].document.body instanceof Object == false even though
// typeof(frames[0].document.body) == 'object'.
try {
return ((typeof(value) == 'object' && value != null) ||
(typeof(value) == 'function' && value.nodeName &&
value.nodeType == NodeType.ELEMENT)) &&
(value.nodeType == NodeType.ELEMENT ||
value.nodeType == NodeType.DOCUMENT ||
(SHADOW_DOM_ENABLED && value instanceof ShadowRoot));
} catch {
// OOPIF content window
return false;
}
}
/**
* Returns whether given value is a collection (iterable with
* 'length' property).
* @param {*} value The value to identify as a collection.
* @return {boolean} True if value is an iterable collection.
*/
function isCollection(value) {
const Symbol = window.cdc_adoQpoasnfa76pfcZLmcfl_Symbol || window.Symbol;
return (typeof value[Symbol.iterator] === 'function') &&
('length' in value) &&
(typeof value.length === 'number');
}
/**
* Deep-clones item, given object references in seen, using cloning algorithm
* algo. Implements "clone an object" from W3C-spec (#dfn-clone-an-object).
* @param {*} item Object or collection to deep clone.
* @param {!Array<*>} seen Object references that have already been seen.
* @param {function(*, Array<*>) : *} algo Cloning algorithm to use to
* deep clone properties of item.
* @param {!Array<*>} nodes List of serialized nodes
* @return {*} Clone of item with status of cloning.
*/
function cloneWithAlgorithm(item, seen, algo, nodes) {
let tmp = null;
function maybeCopyProperty(prop) {
let sourceValue = null;
try {
sourceValue = item[prop];
} catch(e) {
throw newError('error reading property', StatusCode.JAVA_SCRIPT_ERROR);
}
return algo(sourceValue, seen, nodes);
}
if (isCollection(item)) {
const Array = window.cdc_adoQpoasnfa76pfcZLmcfl_Array || window.Array;
tmp = new Array(item.length);
for (let i = 0; i < item.length; ++i)
tmp[i] = maybeCopyProperty(i);
} else {
tmp = {};
for (let prop in item)
tmp[prop] = maybeCopyProperty(prop);
}
return tmp;
}
/**
* Wrapper to cloneWithAlgorithm, with circular reference detection logic.
* @param {*} item Object or collection to deep clone.
* @param {!Array<*>} seen Object references that have already been seen.
* @param {function(*, Array<*>) : *} algo Cloning algorithm to use to
* deep clone properties of item.
* @param {!Array<*>} nodes List of serialized nodes
* @return {*} Clone of item with status of cloning.
*/
function cloneWithCircularCheck(item, seen, algo, nodes) {
if (seen.includes(item))
throw newError('circular reference', StatusCode.JAVA_SCRIPT_ERROR);
seen.push(item);
const result = cloneWithAlgorithm(item, seen, algo, nodes);
seen.pop();
return result;
}
/*
* Prohibits call of object.prototype.toJSoN()
*/
function serializationGuard(object) {
const handler = {
get(target, name) {
const value = target[name]
if (typeof value !== 'function')
return value;
// Objects that have own toJSON are never guarded with a proxy.
// All other functions are replaced with {} in preprocessResult.
// The only remaining case when a client tries to access a method is a
// call to non-own toJSON by JSON.stringify.
// In this case this method needs to be concealed.
return undefined;
}
}
const Proxy = window.cdc_adoQpoasnfa76pfcZLmcfl_Proxy || window.Proxy;
return new Proxy(object, handler);
}
/**
* Returns deep clone of given value, replacing element references with a
* serialized string representing that element.
* @param {*} item Object or collection to deep clone.
* @param {!Array<*>} seen Object references that have already been seen.
* @param {!Array<*>} nodes List of serialized nodes
* @return {*} Clone of item with status of cloning.
*/
function preprocessResult(item, seen, nodes) {
if (item === undefined || item === null)
return null;
if (typeof item === 'boolean' ||
typeof item === 'number' ||
typeof item === 'string')
return item;
// We never descend to own property toJSON.
// Any other function must be serialized as an object.
if (typeof item === 'function')
return {};
if (isElement(item)) {
if (!isNodeReachable(item)) {
if (item instanceof ShadowRoot)
throw newError('shadow root is detached from the current frame',
StatusCode.DETACHED_SHADOW_ROOT);
throw newError('stale element not found in the current frame',
StatusCode.STALE_ELEMENT_REFERENCE);
}
const ret = {};
let key = ELEMENT_KEY;
if (item instanceof ShadowRoot) {
if (!item.nodeType ||
item.nodeType !== item.DOCUMENT_FRAGMENT_NODE ||
!item.host) {
throw newError('no such shadow root', StatusCode.NO_SUCH_SHADOW_ROOT);
}
key = SHADOW_ROOT_KEY;
}
ret[key] = nodes.length;
nodes.push(item);
return serializationGuard(ret);
}
let WindowProxy = window.cdc_adoQpoasnfa76pfcZLmcfl_Window || window.Window;
let is_oopif = false;
try {
WindowProxy = item.cdc_adoQpoasnfa76pfcZLmcfl_Window || item.Window
|| WindowProxy;
} catch {
is_oopif = true;
}
if (is_oopif || item instanceof WindowProxy) {
const ret = {};
ret[WINDOW_KEY] = nodes.length;
nodes.push(item);
return serializationGuard(ret);
}
if (Object.hasOwn(item, 'toJSON') && typeof item.toJSON === 'function') {
// Not guarded because we want item.toJSON to be invoked by
// JSON.stringify.
return item;
}
// Deep cloning of Array and Objects.
return serializationGuard(
cloneWithCircularCheck(item, seen, preprocessResult, nodes));
}
/**
* Returns deserialized deep clone of given value, replacing serialized string
* references to elements with a element reference, if found.
* @param {*} item Object or collection to deep clone.
* @param {!Array<*>} seen Object references that have already been seen.
* @param {!Array<*>} nodes List of referred nodes
* @return {*} Clone of item with status of cloning.
*/
function resolveReferencesRecursive(item, seen, nodes) {
if (item === undefined ||
item === null ||
typeof item === 'boolean' ||
typeof item === 'number' ||
typeof item === 'string' ||
typeof item === 'function')
return item;
for (const key of REF_KEYS) {
if (!item.hasOwnProperty(key))
continue;
let idx = item[key];
if (idx < 0 || idx >= nodes.length) {
throw newError('unable to resove node reference. '
+ 'Node index is out of range.', StatusCode.JAVA_SCRIPT_ERROR);
}
if (key == FRAME_KEY)
return nodes[idx].contentWindow;
return nodes[idx];
}
if (isCollection(item) || typeof item === 'object')
return cloneWithAlgorithm(item, seen, resolveReferencesRecursive, nodes);
throw newError('unhandled object', StatusCode.JAVA_SCRIPT_ERROR);
}
/**
* Returns deserialized deep clone of given value, replacing serialized string
* references to elements with a element reference, if found.
* @param {*} item Object or collection to deep clone.
* @param {!Array<*>} nodes List of referred nodes
* @return {*} Clone of item with status of cloning.
*/
function resolveReferences(args, nodes) {
for (let idx = 0; idx < nodes.length; ++idx) {
if (!isNodeReachable(nodes[idx])) {
if (nodes[idx] instanceof ShadowRoot)
throw newError('shadow root is detached from the current frame',
StatusCode.DETACHED_SHADOW_ROOT);
throw newError('stale element not found in the current frame',
StatusCode.STALE_ELEMENT_REFERENCE);
}
}
return resolveReferencesRecursive(args, [], nodes);
}
/**
* Calls a given function and returns its value.
*
* The inputs to and outputs of the function will be unwrapped and wrapped
* respectively, unless otherwise specified. This wrapping involves converting
* between cached object reference IDs and actual JS objects.
*
* @param {function(...[*]) : *} func The function to invoke.
* @param {!Array<*>} args The array of arguments to supply to the function,
* which will be unwrapped before invoking the function.
* @param {boolean} w3c Whether to return a W3C compliant element reference.
* @param {!Array<*>} Nodes referred in the arguments.
* @return {*} An object containing a status and value property, where status
* is a WebDriver status code and value is the wrapped value. If an
* unwrapped return was specified, this will be the function's pure return
* value.
*/
function callFunction(func, args, w3c, nodes) {
if (w3c) {
w3cEnabled = true;
ELEMENT_KEY = W3C_ELEMENT_KEY;
}
function buildError(error) {
const errorResponse = serializationGuard({
status: error.code || StatusCode.JAVA_SCRIPT_ERROR,
value: error.message || error
});
const JSON = window.cdc_adoQpoasnfa76pfcZLmcfl_JSON || window.JSON;
return [JSON.stringify(errorResponse)];
}
const Promise = window.cdc_adoQpoasnfa76pfcZLmcfl_Promise || window.Promise;
let unwrappedArgs = null;
try {
unwrappedArgs = resolveReferences(args, nodes);
} catch (error) {
return Promise.resolve(buildError(error));
}
try {
const tmp = func.apply(null, unwrappedArgs);
return Promise.resolve(tmp).then((result) => {
ret_nodes = [];
const response = {
status: 0,
value: preprocessResult(result, [], ret_nodes)
};
const JSON = window.cdc_adoQpoasnfa76pfcZLmcfl_JSON || window.JSON;
return [JSON.stringify(response), ...ret_nodes];
}).catch(buildError);
} catch (error) {
return Promise.resolve(buildError(error));
}
}