File: RecordingAction.js

package info (click to toggle)
webkit2gtk 2.18.6-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 159,080 kB
  • sloc: cpp: 1,636,147; ansic: 45,350; python: 14,988; perl: 13,794; ruby: 9,803; xml: 9,342; asm: 5,312; yacc: 2,167; lex: 1,007; sh: 773; makefile: 61
file content (396 lines) | stat: -rw-r--r-- 12,050 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2017 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

WI.RecordingAction = class RecordingAction
{
    constructor(name, parameters, trace)
    {
        this._payloadName = name;
        this._payloadParameters = parameters;
        this._payloadTrace = trace;

        this._name = "";
        this._parameters = [];
        this._trace = [];

        this._valid = true;

        this._isFunction = false;
        this._isGetter = false;
        this._isVisual = false;
        this._stateModifiers = new Set;
    }

    // Static

    // Payload format: [name, parameters, trace]
    static fromPayload(payload)
    {
        if (!Array.isArray(payload))
            payload = [];

        if (isNaN(payload[0]))
            payload[0] = -1;

        if (!Array.isArray(payload[1]))
            payload[1] = [];

        if (!Array.isArray(payload[2]))
            payload[2] = [];

        return new WI.RecordingAction(...payload);
    }

    static isFunctionForType(type, name)
    {
        let functionNames = WI.RecordingAction._functionNames[type];
        if (!functionNames)
            return false;

        return functionNames.has(name);
    }

    // Public

    get name() { return this._name; }
    get parameters() { return this._parameters; }
    get trace() { return this._trace; }

    get valid() { return this._valid; }
    set valid(valid) { this._valid = !!valid; }

    get isFunction() { return this._isFunction; }
    get isGetter() { return this._isGetter; }
    get isVisual() { return this._isVisual; }
    get stateModifiers() { return this._stateModifiers; }

    swizzle(recording)
    {
        this._name = recording.swizzle(this._payloadName, WI.Recording.Swizzle.String);

        this._parameters = this._payloadParameters.map((item, i) => {
            let type = this.parameterSwizzleTypeForTypeAtIndex(recording.type, i);
            if (!type)
                return item;

            let swizzled = recording.swizzle(item, type);
            if (swizzled === WI.Recording.Swizzle.Invalid)
                this._valid = false;

            return swizzled;
        });

        for (let item of this._payloadTrace) {
            try {
                let array = recording.swizzle(item, WI.Recording.Swizzle.Array);
                let callFrame = WI.CallFrame.fromPayload(WI.mainTarget, {
                    functionName: recording.swizzle(array[0], WI.Recording.Swizzle.String),
                    url: recording.swizzle(array[1], WI.Recording.Swizzle.String),
                    lineNumber: array[2],
                    columnNumber: array[3],
                });
                this._trace.push(callFrame);
            } catch { }
        }

        this._isFunction = WI.RecordingAction.isFunctionForType(recording.type, this._name);
        this._isGetter = !this._isFunction && !this._parameters.length;

        let visualNames = WI.RecordingAction._visualNames[recording.type];
        this._isVisual = visualNames ? visualNames.has(this._name) : false;

        this._stateModifiers = new Set([this._name]);
        let stateModifiers = WI.RecordingAction._stateModifiers[recording.type];
        if (stateModifiers) {
            let modifiedByAction = stateModifiers[this._name] || [];
            for (let item of modifiedByAction)
                this._stateModifiers.add(item);
        }
    }

    parameterSwizzleTypeForTypeAtIndex(type, index)
    {
        let functionNames = WI.RecordingAction._parameterSwizzleTypeForTypeAtIndex[type];
        if (!functionNames)
            return null;

        let parameterLengths = functionNames[this._name];
        if (!parameterLengths)
            return null;

        let argumentSwizzleTypes = parameterLengths[this._payloadParameters.length];
        if (!argumentSwizzleTypes)
            return null;

        return argumentSwizzleTypes[index] || null;
    }

    toJSON()
    {
        return [this._payloadName, this._payloadParameters, this._payloadTrace];
    }
};

// This object instructs the frontend as to how to reconstruct deduplicated objects found in the
// "data" section of a recording payload. It will only swizzle values if they are of the expected
// type in the right index of the version of the action (determined by the number of parameters) for
// the recording type. Since a recording can be created by importing a JSON file, this is used to
// make sure that inputs are only considered valid if they conform to the structure defined below.
// 
// For Example:
//
// IDL:
//
//     void foo(optional DOMString s = "bar");
//     void foo(DOMPath path, optional DOMString s = "bar");
//     void foo(float a, float b, float c, float d);
//
// Swizzle Entries:
//
//     - For the 1 parameter version, the parameter at index 0 needs to be swizzled as a string
//     - For the 2 parameter version, the parameters need to be swizzled as a Path and String
//     - For the 4 parameter version, numbers don't need to be swizzled, so it is not included
//
//     "foo": {
//         1: {0: String}
//         2: {0: Path2D, 1: String}
//     }

{
    let {CanvasStyle, Element, Image, ImageData, Path2D, String} = WI.Recording.Swizzle;

    WI.RecordingAction._parameterSwizzleTypeForTypeAtIndex = {
        [WI.Recording.Type.Canvas2D]: {
            "clip": {
                1: {0: String},
                2: {0: Path2D, 1: String},
            },
            "createImageData": {
                1: {0: ImageData},
            },
            "createPattern": {
                2: {0: Image, 1: String},
            },
            "direction": {
                1: {0: String},
            },
            "drawImage": {
                3: {0: Image},
                5: {0: Image},
                9: {0: Image},
            },
            "drawImageFromRect": {
                10: {0: Image, 9: String},
            },
            "drawFocusIfNeeded": {
                1: {0: Element},
                2: {0: Path2D, 1: Element},
            },
            "fill": {
                1: {0: String},
                2: {0: Path2D, 1: String},
            },
            "fillStyle": {
                1: {0: CanvasStyle},
            },
            "fillText": {
                3: {0: String},
                4: {0: String},
            },
            "font": {
                1: {0: String},
            },
            "globalCompositeOperation": {
                1: {0: String},
            },
            "imageSmoothingQuality": {
                1: {0: String},
            },
            "isPointInPath": {
                4: {0: Path2D, 3: String},
            },
            "isPointInStroke": {
                3: {0: Path2D, 3: String},
            },
            "lineCap": {
                1: {0: String},
            },
            "lineJoin": {
                1: {0: String},
            },
            "measureText": {
                1: {0: String},
            },
            "putImageData": {
                3: {0: ImageData},
                7: {0: ImageData},
            },
            "setCompositeOperation": {
                1: {0: String},
            },
            "setFillColor": {
                1: {0: String},
                2: {0: String},
            },
            "setLineCap": {
                1: {0: String},
            },
            "setLineJoin": {
                1: {0: String},
            },
            "setShadow": {
                4: {3: String},
                5: {3: String},
            },
            "setStrokeColor": {
                1: {0: String},
                2: {0: String},
            },
            "shadowColor": {
                1: {0: String},
            },
            "stroke": {
                1: {0: Path2D},
            },
            "strokeStyle": {
                1: {0: CanvasStyle},
            },
            "strokeText": {
                3: {0: String},
                4: {0: String},
            },
            "textAlign": {
                1: {0: String},
            },
            "textBaseline": {
                1: {0: String},
            },
            "webkitPutImageData": {
                3: {0: ImageData},
                7: {0: ImageData},
            },
        },
    };
}

WI.RecordingAction._functionNames = {
    [WI.Recording.Type.Canvas2D]: new Set([
        "arc",
        "arcTo",
        "beginPath",
        "bezierCurveTo",
        "clearRect",
        "clearShadow",
        "clip",
        "clip",
        "closePath",
        "commit",
        "createImageData",
        "createLinearGradient",
        "createPattern",
        "createRadialGradient",
        "drawFocusIfNeeded",
        "drawImage",
        "drawImageFromRect",
        "ellipse",
        "fill",
        "fill",
        "fillRect",
        "fillText",
        "getImageData",
        "getLineDash",
        "isPointInPath",
        "isPointInPath",
        "isPointInStroke",
        "isPointInStroke",
        "lineTo",
        "measureText",
        "moveTo",
        "putImageData",
        "quadraticCurveTo",
        "rect",
        "resetTransform",
        "restore",
        "rotate",
        "save",
        "scale",
        "setAlpha",
        "setCompositeOperation",
        "setFillColor",
        "setLineCap",
        "setLineDash",
        "setLineJoin",
        "setLineWidth",
        "setMiterLimit",
        "setShadow",
        "setStrokeColor",
        "setTransform",
        "stroke",
        "stroke",
        "strokeRect",
        "strokeText",
        "transform",
        "translate",
        "webkitGetImageDataHD",
        "webkitPutImageDataHD",
    ]),
};

WI.RecordingAction._visualNames = {
    [WI.Recording.Type.Canvas2D]: new Set([
        "clearRect",
        "drawFocusIfNeeded",
        "drawImage",
        "drawImageFromRect",
        "fill",
        "fillRect",
        "fillText",
        "putImageData",
        "stroke",
        "strokeRect",
        "strokeText",
        "webkitPutImageDataHD",
    ]),
};

WI.RecordingAction._stateModifiers = {
    [WI.Recording.Type.Canvas2D]: {
        clearShadow: ["shadowOffsetX", "shadowOffsetY", "shadowBlur", "shadowColor"],
        resetTransform: ["transform"],
        rotate: ["transform"],
        scale: ["transform"],
        setAlpha: ["globalAlpha"],
        setCompositeOperation: ["globalCompositeOperation"],
        setFillColor: ["fillStyle"],
        setLineCap: ["lineCap"],
        setLineJoin: ["lineJoin"],
        setLineWidth: ["lineWidth"],
        setMiterLimit: ["miterLimit"],
        setShadow: ["shadowOffsetX", "shadowOffsetY", "shadowBlur", "shadowColor"],
        setStrokeColor: ["strokeStyle"],
        setTransform: ["transform"],
        translate: ["transform"],
    },
};