2c32bb23b008728fe466957d2366d8d86db0007b
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Models / Timeline.js
1 /*
2  * Copyright (C) 2013 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 WebInspector.Timeline = class Timeline extends WebInspector.Object
27 {
28     constructor(type)
29     {
30         super();
31
32         this._type = type;
33
34         this.reset(true);
35     }
36
37     // Static
38
39     static create(type)
40     {
41         if (type === WebInspector.TimelineRecord.Type.Network)
42             return new WebInspector.NetworkTimeline(type);
43
44         if (type === WebInspector.TimelineRecord.Type.Memory)
45             return new WebInspector.MemoryTimeline(type);
46
47         return new WebInspector.Timeline(type);
48     }
49
50     // Public
51
52     get type()
53     {
54         return this._type;
55     }
56
57     get startTime()
58     {
59         return this._startTime;
60     }
61
62     get endTime()
63     {
64         return this._endTime;
65     }
66
67     get records()
68     {
69         return this._records;
70     }
71
72     reset(suppressEvents)
73     {
74         this._records = [];
75         this._startTime = NaN;
76         this._endTime = NaN;
77
78         if (!suppressEvents) {
79             this.dispatchEventToListeners(WebInspector.Timeline.Event.TimesUpdated);
80             this.dispatchEventToListeners(WebInspector.Timeline.Event.Reset);
81         }
82     }
83
84     addRecord(record)
85     {
86         if (record.updatesDynamically)
87             record.addEventListener(WebInspector.TimelineRecord.Event.Updated, this._recordUpdated, this);
88
89         this._records.push(record);
90
91         this._updateTimesIfNeeded(record);
92
93         this.dispatchEventToListeners(WebInspector.Timeline.Event.RecordAdded, {record});
94     }
95
96     saveIdentityToCookie(cookie)
97     {
98         cookie[WebInspector.Timeline.TimelineTypeCookieKey] = this._type;
99     }
100
101     refresh()
102     {
103         this.dispatchEventToListeners(WebInspector.Timeline.Event.Refreshed);
104     }
105
106     recordsInTimeRange(startTime, endTime, includeRecordBeforeStart)
107     {
108         let lowerIndex = this._records.lowerBound(startTime, (time, record) => time - record.timestamp);
109         let upperIndex = this._records.upperBound(endTime, (time, record) => time - record.timestamp);
110
111         // Include the record right before the start time.
112         if (includeRecordBeforeStart && lowerIndex > 0)
113             lowerIndex--;
114
115         return this._records.slice(lowerIndex, upperIndex);
116     }
117
118     // Private
119
120     _updateTimesIfNeeded(record)
121     {
122         var changed = false;
123
124         if (isNaN(this._startTime) || record.startTime < this._startTime) {
125             this._startTime = record.startTime;
126             changed = true;
127         }
128
129         if (isNaN(this._endTime) || this._endTime < record.endTime) {
130             this._endTime = record.endTime;
131             changed = true;
132         }
133
134         if (changed)
135             this.dispatchEventToListeners(WebInspector.Timeline.Event.TimesUpdated);
136     }
137
138     _recordUpdated(event)
139     {
140         this._updateTimesIfNeeded(event.target);
141     }
142 };
143
144 WebInspector.Timeline.Event = {
145     Reset: "timeline-reset",
146     RecordAdded: "timeline-record-added",
147     TimesUpdated: "timeline-times-updated",
148     Refreshed: "timeline-refreshed",
149 };
150
151 WebInspector.Timeline.TimelineTypeCookieKey = "timeline-type";