1/*
2 * Copyright (C) 2011 Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/**
32 * @constructor
33 */
34WebInspector.NetworkLog = function()
35{
36    this._requests = [];
37    this._requestForId = {};
38    WebInspector.networkManager.addEventListener(WebInspector.NetworkManager.EventTypes.RequestStarted, this._onRequestStarted, this);
39    WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.MainFrameNavigated, this._onMainFrameNavigated, this);
40    WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.Load, this._onLoad, this);
41    WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.DOMContentLoaded, this._onDOMContentLoaded, this);
42}
43
44WebInspector.NetworkLog.prototype = {
45    /**
46     * @return {!Array.<!WebInspector.NetworkRequest>}
47     */
48    get requests()
49    {
50        return this._requests;
51    },
52
53    /**
54     * @param {string} url
55     * @return {?WebInspector.NetworkRequest}
56     */
57    requestForURL: function(url)
58    {
59        for (var i = 0; i < this._requests.length; ++i) {
60            if (this._requests[i].url === url)
61                return this._requests[i];
62        }
63        return null;
64    },
65
66    /**
67     * @param {!WebInspector.NetworkRequest} request
68     * @return {!WebInspector.PageLoad}
69     */
70    pageLoadForRequest: function(request)
71    {
72        return request.__page;
73    },
74
75    /**
76     * @param {!WebInspector.Event} event
77     */
78    _onMainFrameNavigated: function(event)
79    {
80        var mainFrame = /** type {WebInspector.ResourceTreeFrame} */ event.data;
81        // Preserve requests from the new session.
82        this._currentPageLoad = null;
83        var oldRequests = this._requests.splice(0, this._requests.length);
84        this._requestForId = {};
85        for (var i = 0; i < oldRequests.length; ++i) {
86            var request = oldRequests[i];
87            if (request.loaderId === mainFrame.loaderId) {
88                if (!this._currentPageLoad)
89                    this._currentPageLoad = new WebInspector.PageLoad(request);
90                this._requests.push(request);
91                this._requestForId[request.requestId] = request;
92                request.__page = this._currentPageLoad;
93            }
94        }
95    },
96
97    /**
98     * @param {!WebInspector.Event} event
99     */
100    _onRequestStarted: function(event)
101    {
102        var request = /** @type {!WebInspector.NetworkRequest} */ (event.data);
103        this._requests.push(request);
104        this._requestForId[request.requestId] = request;
105        request.__page = this._currentPageLoad;
106    },
107
108    /**
109     * @param {!WebInspector.Event} event
110     */
111    _onDOMContentLoaded: function(event)
112    {
113        if (this._currentPageLoad)
114            this._currentPageLoad.contentLoadTime = event.data;
115    },
116
117    /**
118     * @param {!WebInspector.Event} event
119     */
120    _onLoad: function(event)
121    {
122        if (this._currentPageLoad)
123            this._currentPageLoad.loadTime = event.data;
124    },
125
126    /**
127     * @param {!NetworkAgent.RequestId} requestId
128     * @return {?WebInspector.NetworkRequest}
129     */
130    requestForId: function(requestId)
131    {
132        return this._requestForId[requestId];
133    }
134}
135
136/**
137 * @type {!WebInspector.NetworkLog}
138 */
139WebInspector.networkLog;
140
141/**
142 * @constructor
143 * @param {!WebInspector.NetworkRequest} mainRequest
144 */
145WebInspector.PageLoad = function(mainRequest)
146{
147    this.id = ++WebInspector.PageLoad._lastIdentifier;
148    this.url = mainRequest.url;
149    this.startTime = mainRequest.startTime;
150}
151
152WebInspector.PageLoad._lastIdentifier = 0;
153