1/*
2 * Copyright (C) 2013 University of Washington. All rights reserved.
3 * Copyright (C) 2014 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27
28WebInspector.ReplaySession = function(identifier)
29{
30    WebInspector.Object.call(this);
31
32    this.identifier = identifier;
33    this._segments = [];
34    this._timestamp = null;
35};
36
37WebInspector.ReplaySession.fromPayload = function(identifier, payload)
38{
39    var session = new WebInspector.ReplaySession(identifier);
40    session._updateFromPayload(payload);
41    return session;
42}
43
44WebInspector.ReplaySession.Event = {
45    SegmentsChanged: "replay-session-segments-changed",
46};
47
48WebInspector.ReplaySession.prototype = {
49    constructor: WebInspector.ReplaySession,
50    __proto__: WebInspector.Object.prototype,
51
52    get segments()
53    {
54        return this._segments.slice();
55    },
56
57    segmentsChanged: function()
58    {
59        // The replay manager won't update the session's list of segments nor create a new session.
60        ReplayAgent.getSerializedSession.promise(this.identifier)
61            .then(this._updateFromPayload.bind(this));
62    },
63
64    _updateFromPayload: function(payload)
65    {
66        console.assert(payload.id === this.identifier);
67
68        var segmentIds = payload.segments;
69        var oldSegments = this._segments;
70        var pendingSegments = [];
71        for (var segmentId of segmentIds)
72            pendingSegments.push(WebInspector.replayManager.getSegment(segmentId));
73
74        var session = this;
75        Promise.all(pendingSegments).then(
76            function(segmentsArray) {
77                session._segments = segmentsArray;
78                session.dispatchEventToListeners(WebInspector.ReplaySession.Event.SegmentsChanged, {oldSegments: oldSegments});
79            },
80            function(error) {
81                console.error("Problem resolving segments: ", error);
82            }
83        );
84    }
85};
86