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 * @extends {WebInspector.TargetAware} 34 * @param {!WebInspector.Target} target 35 */ 36WebInspector.NetworkLog = function(target) 37{ 38 WebInspector.TargetAware.call(this, target); 39 40 this._requests = []; 41 this._requestForId = {}; 42 target.networkManager.addEventListener(WebInspector.NetworkManager.EventTypes.RequestStarted, this._onRequestStarted, this); 43 target.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.MainFrameNavigated, this._onMainFrameNavigated, this); 44 target.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.Load, this._onLoad, this); 45 target.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.DOMContentLoaded, this._onDOMContentLoaded, this); 46} 47 48WebInspector.NetworkLog.prototype = { 49 /** 50 * @return {!Array.<!WebInspector.NetworkRequest>} 51 */ 52 get requests() 53 { 54 return this._requests; 55 }, 56 57 /** 58 * @param {string} url 59 * @return {?WebInspector.NetworkRequest} 60 */ 61 requestForURL: function(url) 62 { 63 for (var i = 0; i < this._requests.length; ++i) { 64 if (this._requests[i].url === url) 65 return this._requests[i]; 66 } 67 return null; 68 }, 69 70 /** 71 * @param {!WebInspector.NetworkRequest} request 72 * @return {!WebInspector.PageLoad} 73 */ 74 pageLoadForRequest: function(request) 75 { 76 return request.__page; 77 }, 78 79 /** 80 * @param {!WebInspector.Event} event 81 */ 82 _onMainFrameNavigated: function(event) 83 { 84 var mainFrame = /** type {WebInspector.ResourceTreeFrame} */ event.data; 85 // Preserve requests from the new session. 86 this._currentPageLoad = null; 87 var oldRequests = this._requests.splice(0, this._requests.length); 88 this._requestForId = {}; 89 for (var i = 0; i < oldRequests.length; ++i) { 90 var request = oldRequests[i]; 91 if (request.loaderId === mainFrame.loaderId) { 92 if (!this._currentPageLoad) 93 this._currentPageLoad = new WebInspector.PageLoad(request); 94 this._requests.push(request); 95 this._requestForId[request.requestId] = request; 96 request.__page = this._currentPageLoad; 97 } 98 } 99 }, 100 101 /** 102 * @param {!WebInspector.Event} event 103 */ 104 _onRequestStarted: function(event) 105 { 106 var request = /** @type {!WebInspector.NetworkRequest} */ (event.data); 107 this._requests.push(request); 108 this._requestForId[request.requestId] = request; 109 request.__page = this._currentPageLoad; 110 }, 111 112 /** 113 * @param {!WebInspector.Event} event 114 */ 115 _onDOMContentLoaded: function(event) 116 { 117 if (this._currentPageLoad) 118 this._currentPageLoad.contentLoadTime = event.data; 119 }, 120 121 /** 122 * @param {!WebInspector.Event} event 123 */ 124 _onLoad: function(event) 125 { 126 if (this._currentPageLoad) 127 this._currentPageLoad.loadTime = event.data; 128 }, 129 130 /** 131 * @param {!NetworkAgent.RequestId} requestId 132 * @return {?WebInspector.NetworkRequest} 133 */ 134 requestForId: function(requestId) 135 { 136 return this._requestForId[requestId]; 137 }, 138 139 __proto__: WebInspector.TargetAware.prototype 140} 141 142/** 143 * @type {!WebInspector.NetworkLog} 144 */ 145WebInspector.networkLog; 146 147/** 148 * @constructor 149 * @param {!WebInspector.NetworkRequest} mainRequest 150 */ 151WebInspector.PageLoad = function(mainRequest) 152{ 153 this.id = ++WebInspector.PageLoad._lastIdentifier; 154 this.url = mainRequest.url; 155 this.startTime = mainRequest.startTime; 156} 157 158WebInspector.PageLoad._lastIdentifier = 0; 159