Web Inspector: Timelines - Export fails for MediaTimelineRecords with originator...
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Models / MediaTimelineRecord.js
1 /*
2  * Copyright (C) 2018 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WI.MediaTimelineRecord = class MediaTimelineRecord extends WI.TimelineRecord
27 {
28     constructor(eventType, timestamp, {domNode, domEvent, isLowPower} = {})
29     {
30         console.assert(Object.values(WI.MediaTimelineRecord.EventType).includes(eventType));
31
32         super(WI.TimelineRecord.Type.Media, timestamp, timestamp);
33
34         this._eventType = eventType;
35         this._domNode = domNode || null;
36         this._domEvent = domEvent || null;
37         this._isLowPower = isLowPower || false;
38     }
39
40     // Import / Export
41
42     static fromJSON(json)
43     {
44         let {eventType, timestamp} = json;
45         return new WI.MediaTimelineRecord(eventType, timestamp, json);
46     }
47
48     toJSON()
49     {
50         // FIXME: DOMNode
51
52         // Don't include the DOMEvent's originator.
53         let domEvent = this._domEvent;
54         if (domEvent && domEvent.originator) {
55             domEvent = Object.shallowCopy(domEvent);
56             domEvent.originator = undefined;
57         }
58
59         return {
60             type: this.type,
61             eventType: this._eventType,
62             timestamp: this.startTime,
63             domEvent,
64             isLowPower: this._isLowPower,
65         };
66     }
67
68     // Public
69
70     get eventType() { return this._eventType; }
71     get domNode() { return this._domNode; }
72     get domEvent() { return this._domEvent; }
73     get isLowPower() { return this._isLowPower; }
74
75     get displayName()
76     {
77         if (this._eventType === WI.MediaTimelineRecord.EventType.DOMEvent && this._domEvent) {
78             let eventName = this._domEvent.eventName;
79             if (eventName === "webkitfullscreenchange" && this._domEvent.data)
80                 return this._domEvent.data.enabled ? WI.UIString("Entered Full-Screen Mode") : WI.UIString("Exited Full-Screen Mode");
81             return eventName;
82         }
83
84         if (this._eventType === WI.MediaTimelineRecord.EventType.LowPower)
85             return this._isLowPower ? WI.UIString("Entered Low-Power Mode") : WI.UIString("Exited Low-Power Mode");
86
87         if (this._domNode)
88             return this._domNode.displayName;
89
90         console.error("Unknown media record event type: ", this._eventType, this);
91         return WI.UIString("Media Event");
92     }
93
94     saveIdentityToCookie(cookie)
95     {
96         super.saveIdentityToCookie(cookie);
97
98         cookie["media-timeline-record-event-type"] = this._eventType;
99         if (this._domNode)
100             cookie["media-timeline-record-dom-node"] = this._domNode.path();
101         if (this._domEvent)
102             cookie["media-timeline-record-dom-event"] = this._domEvent.eventName;
103         if (this._isLowPower || (this._domEvent && this._domEvent.data && this._domEvent.data.enabled))
104             cookie["media-timeline-record-active"] = true;
105     }
106 };
107
108 WI.MediaTimelineRecord.EventType = {
109     DOMEvent: "dom-event",
110     LowPower: "low-power",
111 };