Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(258)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/sdk/NetworkLog.js

Issue 2628693002: [Devtools] Move initiator data into NetworkLog from NetworkRequest (Closed)
Patch Set: changes Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 42
43 /** @type {!Array<!SDK.NetworkRequest>} */ 43 /** @type {!Array<!SDK.NetworkRequest>} */
44 this._requests = []; 44 this._requests = [];
45 /** @type {!Object<string, !SDK.NetworkRequest>} */ 45 /** @type {!Object<string, !SDK.NetworkRequest>} */
46 this._requestForId = {}; 46 this._requestForId = {};
47 networkManager.addEventListener(SDK.NetworkManager.Events.RequestStarted, th is._onRequestStarted, this); 47 networkManager.addEventListener(SDK.NetworkManager.Events.RequestStarted, th is._onRequestStarted, this);
48 resourceTreeModel.addEventListener( 48 resourceTreeModel.addEventListener(
49 SDK.ResourceTreeModel.Events.MainFrameNavigated, this._onMainFrameNaviga ted, this); 49 SDK.ResourceTreeModel.Events.MainFrameNavigated, this._onMainFrameNaviga ted, this);
50 resourceTreeModel.addEventListener(SDK.ResourceTreeModel.Events.Load, this._ onLoad, this); 50 resourceTreeModel.addEventListener(SDK.ResourceTreeModel.Events.Load, this._ onLoad, this);
51 resourceTreeModel.addEventListener(SDK.ResourceTreeModel.Events.DOMContentLo aded, this._onDOMContentLoaded, this); 51 resourceTreeModel.addEventListener(SDK.ResourceTreeModel.Events.DOMContentLo aded, this._onDOMContentLoaded, this);
52 networkManager.on(SDK.NetworkManager.RequestRedirectEvent, this._onRequestRe direct, this);
52 } 53 }
53 54
54 /** 55 /**
55 * @param {!SDK.Target} target 56 * @param {!SDK.Target} target
56 * @return {?SDK.NetworkLog} 57 * @return {?SDK.NetworkLog}
57 */ 58 */
58 static fromTarget(target) { 59 static fromTarget(target) {
59 return target.model(SDK.NetworkLog); 60 return target.model(SDK.NetworkLog);
60 } 61 }
61 62
(...skipping 18 matching lines...) Expand all
80 var result = []; 81 var result = [];
81 for (var target of SDK.targetManager.targets()) { 82 for (var target of SDK.targetManager.targets()) {
82 var networkLog = SDK.NetworkLog.fromTarget(target); 83 var networkLog = SDK.NetworkLog.fromTarget(target);
83 if (networkLog) 84 if (networkLog)
84 result = result.concat(networkLog.requests()); 85 result = result.concat(networkLog.requests());
85 } 86 }
86 return result; 87 return result;
87 } 88 }
88 89
89 /** 90 /**
91 * @param {!SDK.NetworkRequest} request
92 * @return {?SDK.NetworkLog}
93 */
94 static fromRequest(request) {
95 return SDK.NetworkLog.fromTarget(request.target());
96 }
97
98 /**
99 * @param {!SDK.NetworkRequest} request
100 */
101 static _initializeInitiatorSymbolIfNeeded(request) {
102 if (!request[SDK.NetworkLog._initiatorDataSymbol]) {
103 /** @type {!{info: ?SDK.NetworkLog._InitiatorInfo, chain: !Set<!SDK.Networ kRequest>, request: (?SDK.NetworkRequest|undefined)}} */
104 request[SDK.NetworkLog._initiatorDataSymbol] = {
105 info: null,
106 chain: null,
107 request: undefined,
108 };
109 }
110 }
111
112 /**
113 * @param {!SDK.NetworkRequest} request
114 * @return {!SDK.NetworkLog._InitiatorInfo}
115 */
116 static initiatorInfoForRequest(request) {
117 SDK.NetworkLog._initializeInitiatorSymbolIfNeeded(request);
118 if (request[SDK.NetworkLog._initiatorDataSymbol].info)
119 return request[SDK.NetworkLog._initiatorDataSymbol].info;
120
121 var type = SDK.NetworkRequest.InitiatorType.Other;
122 var url = '';
123 var lineNumber = -Infinity;
124 var columnNumber = -Infinity;
125 var scriptId = null;
126 var initiator = request.initiator();
127
128 if (request.redirectSource) {
129 type = SDK.NetworkRequest.InitiatorType.Redirect;
130 url = request.redirectSource.url;
131 } else if (initiator) {
132 if (initiator.type === Protocol.Network.InitiatorType.Parser) {
133 type = SDK.NetworkRequest.InitiatorType.Parser;
134 url = initiator.url ? initiator.url : url;
135 lineNumber = initiator.lineNumber ? initiator.lineNumber : lineNumber;
136 } else if (initiator.type === Protocol.Network.InitiatorType.Script) {
137 for (var stack = initiator.stack; stack; stack = stack.parent) {
138 var topFrame = stack.callFrames.length ? stack.callFrames[0] : null;
139 if (!topFrame)
140 continue;
141 type = SDK.NetworkRequest.InitiatorType.Script;
142 url = topFrame.url || Common.UIString('<anonymous>');
143 lineNumber = topFrame.lineNumber;
144 columnNumber = topFrame.columnNumber;
145 scriptId = topFrame.scriptId;
146 break;
147 }
148 }
149 }
150
151 request[SDK.NetworkLog._initiatorDataSymbol].info =
152 {type: type, url: url, lineNumber: lineNumber, columnNumber: columnNumbe r, scriptId: scriptId};
153 return request[SDK.NetworkLog._initiatorDataSymbol].info;
154 }
155
156 /**
157 * @param {!SDK.NetworkRequest} request
158 * @return {!SDK.NetworkLog.InitiatorGraph}
159 */
160 static initiatorGraphForRequest(request) {
161 /** @type {!Set<!SDK.NetworkRequest>} */
162 var initiated = new Set();
163 var networkLog = SDK.NetworkLog.fromRequest(request);
164 if (!networkLog)
165 return {initiators: new Set(), initiated: new Set()};
166
167 var requests = networkLog.requests();
168 for (var logRequest of requests) {
169 var localInitiators = initiatorChain(logRequest);
170 if (localInitiators.has(request))
171 initiated.add(logRequest);
172 }
173 return {initiators: initiatorChain(request), initiated: initiated};
174
175 /**
176 * @param {!SDK.NetworkRequest} request
177 * @return {!Set<!SDK.NetworkRequest>}
178 */
179 function initiatorChain(request) {
180 SDK.NetworkLog._initializeInitiatorSymbolIfNeeded(request);
181 var initiatorChainCache =
182 /** @type {?Set<!SDK.NetworkRequest>} */ (request[SDK.NetworkLog._init iatorDataSymbol].chain);
183 if (initiatorChainCache)
184 return initiatorChainCache;
185
186 initiatorChainCache = new Set();
187
188 var checkRequest = request;
189 while (checkRequest) {
190 initiatorChainCache.add(checkRequest);
191 checkRequest = initiatorRequest(checkRequest);
192 }
193 request[SDK.NetworkLog._initiatorDataSymbol].chain = initiatorChainCache;
194 return initiatorChainCache;
195 }
196
197 /**
198 * @param {!SDK.NetworkRequest} request
199 * @return {?SDK.NetworkRequest}
200 */
201 function initiatorRequest(request) {
202 SDK.NetworkLog._initializeInitiatorSymbolIfNeeded(request);
203 if (request[SDK.NetworkLog._initiatorDataSymbol].request !== undefined)
204 return request[SDK.NetworkLog._initiatorDataSymbol].request;
205 var networkLog = SDK.NetworkLog.fromRequest(request);
206 var url = SDK.NetworkLog.initiatorInfoForRequest(request).url;
207 request[SDK.NetworkLog._initiatorDataSymbol].request = networkLog.requestF orURL(url);
208 return request[SDK.NetworkLog._initiatorDataSymbol].request;
209 }
210 }
211
212 /**
213 * @param {!SDK.NetworkRequest} request
214 * @return {?SDK.PageLoad}
215 */
216 static pageLoadForRequest(request) {
217 return request[SDK.NetworkLog._pageLoadForRequestSymbol];
218 }
219
220 /**
90 * @return {!Array.<!SDK.NetworkRequest>} 221 * @return {!Array.<!SDK.NetworkRequest>}
91 */ 222 */
92 requests() { 223 requests() {
93 return this._requests; 224 return this._requests;
94 } 225 }
95 226
96 /** 227 /**
97 * @param {string} url 228 * @param {string} url
98 * @return {?SDK.NetworkRequest} 229 * @return {?SDK.NetworkRequest}
99 */ 230 */
100 requestForURL(url) { 231 requestForURL(url) {
101 for (var i = 0; i < this._requests.length; ++i) { 232 for (var i = 0; i < this._requests.length; ++i) {
102 if (this._requests[i].url() === url) 233 if (this._requests[i].url() === url)
103 return this._requests[i]; 234 return this._requests[i];
104 } 235 }
105 return null; 236 return null;
106 } 237 }
107 238
108 /** 239 /**
109 * @param {!SDK.NetworkRequest} request
110 * @return {!SDK.PageLoad}
111 */
112 pageLoadForRequest(request) {
113 return request.__page;
114 }
115
116 /**
117 * @param {!Common.Event} event 240 * @param {!Common.Event} event
118 */ 241 */
119 _onMainFrameNavigated(event) { 242 _onMainFrameNavigated(event) {
120 var mainFrame = /** type {SDK.ResourceTreeFrame} */ event.data; 243 var mainFrame = /** type {SDK.ResourceTreeFrame} */ event.data;
121 // Preserve requests from the new session. 244 // Preserve requests from the new session.
122 this._currentPageLoad = null; 245 this._currentPageLoad = null;
123 var oldRequests = this._requests.splice(0, this._requests.length); 246 var oldRequests = this._requests.splice(0, this._requests.length);
124 this._requestForId = {}; 247 this._requestForId = {};
125 for (var i = 0; i < oldRequests.length; ++i) { 248 for (var i = 0; i < oldRequests.length; ++i) {
126 var request = oldRequests[i]; 249 var request = oldRequests[i];
127 if (request.loaderId === mainFrame.loaderId) { 250 if (request.loaderId === mainFrame.loaderId) {
128 if (!this._currentPageLoad) 251 if (!this._currentPageLoad)
129 this._currentPageLoad = new SDK.PageLoad(request); 252 this._currentPageLoad = new SDK.PageLoad(request);
130 this._requests.push(request); 253 this._requests.push(request);
131 this._requestForId[request.requestId()] = request; 254 this._requestForId[request.requestId()] = request;
132 request.__page = this._currentPageLoad; 255 request[SDK.NetworkLog._pageLoadForRequestSymbol] = this._currentPageLoa d;
133 } 256 }
134 } 257 }
135 } 258 }
136 259
137 /** 260 /**
138 * @param {!Common.Event} event 261 * @param {!Common.Event} event
139 */ 262 */
140 _onRequestStarted(event) { 263 _onRequestStarted(event) {
141 var request = /** @type {!SDK.NetworkRequest} */ (event.data); 264 var request = /** @type {!SDK.NetworkRequest} */ (event.data);
142 this._requests.push(request); 265 this._requests.push(request);
143 this._requestForId[request.requestId()] = request; 266 this._requestForId[request.requestId()] = request;
144 request.__page = this._currentPageLoad; 267 request[SDK.NetworkLog._pageLoadForRequestSymbol] = this._currentPageLoad;
145 } 268 }
146 269
147 /** 270 /**
271 * @param {!SDK.NetworkManager.RequestRedirectEvent} event
272 */
273 _onRequestRedirect(event) {
274 var request = event.request;
275 delete request[SDK.NetworkLog._initiatorDataSymbol];
276 }
277
278 /**
148 * @param {!Common.Event} event 279 * @param {!Common.Event} event
149 */ 280 */
150 _onDOMContentLoaded(event) { 281 _onDOMContentLoaded(event) {
151 if (this._currentPageLoad) 282 if (this._currentPageLoad)
152 this._currentPageLoad.contentLoadTime = event.data; 283 this._currentPageLoad.contentLoadTime = event.data;
153 } 284 }
154 285
155 /** 286 /**
156 * @param {!Common.Event} event 287 * @param {!Common.Event} event
157 */ 288 */
158 _onLoad(event) { 289 _onLoad(event) {
159 if (this._currentPageLoad) 290 if (this._currentPageLoad)
160 this._currentPageLoad.loadTime = event.data; 291 this._currentPageLoad.loadTime = event.data;
161 } 292 }
162 293
163 /** 294 /**
164 * @param {!Protocol.Network.RequestId} requestId 295 * @param {!Protocol.Network.RequestId} requestId
165 * @return {?SDK.NetworkRequest} 296 * @return {?SDK.NetworkRequest}
166 */ 297 */
167 requestForId(requestId) { 298 requestForId(requestId) {
168 return this._requestForId[requestId]; 299 return this._requestForId[requestId];
169 } 300 }
170 }; 301 };
171 302
172
173 /** 303 /**
174 * @unrestricted 304 * @unrestricted
175 */ 305 */
176 SDK.PageLoad = class { 306 SDK.PageLoad = class {
177 /** 307 /**
178 * @param {!SDK.NetworkRequest} mainRequest 308 * @param {!SDK.NetworkRequest} mainRequest
179 */ 309 */
180 constructor(mainRequest) { 310 constructor(mainRequest) {
181 this.id = ++SDK.PageLoad._lastIdentifier; 311 this.id = ++SDK.PageLoad._lastIdentifier;
182 this.url = mainRequest.url(); 312 this.url = mainRequest.url();
183 this.startTime = mainRequest.startTime; 313 this.startTime = mainRequest.startTime;
184 } 314 }
185 }; 315 };
186 316
187 SDK.PageLoad._lastIdentifier = 0; 317 SDK.PageLoad._lastIdentifier = 0;
318
319 /** @typedef {!{initiators: !Set<!SDK.NetworkRequest>, initiated: !Set<!SDK.Netw orkRequest>}} */
320 SDK.NetworkLog.InitiatorGraph;
321
322 /** @typedef {!{type: !SDK.NetworkRequest.InitiatorType, url: string, lineNumber : number, columnNumber: number, scriptId: ?string}} */
323 SDK.NetworkLog._InitiatorInfo;
324
325 SDK.NetworkLog._initiatorDataSymbol = Symbol('InitiatorData');
326 SDK.NetworkLog._pageLoadForRequestSymbol = Symbol('PageLoadForRequest');
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698