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

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: 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 var result = []; 80 var result = [];
81 for (var target of SDK.targetManager.targets()) { 81 for (var target of SDK.targetManager.targets()) {
82 var networkLog = SDK.NetworkLog.fromTarget(target); 82 var networkLog = SDK.NetworkLog.fromTarget(target);
83 if (networkLog) 83 if (networkLog)
84 result = result.concat(networkLog.requests()); 84 result = result.concat(networkLog.requests());
85 } 85 }
86 return result; 86 return result;
87 } 87 }
88 88
89 /** 89 /**
90 * @param {!SDK.NetworkRequest} request
91 * @return {?SDK.NetworkLog}
92 */
93 static networkLogForRequest(request) {
94 return request[SDK.NetworkLog._networkLogForRequestSymbol] || null;
dgozman 2017/01/13 00:51:54 You can still do NetworkLog.FromTarget(request.tar
allada 2017/01/17 22:10:30 Done.
95 }
96
97 /**
98 * @param {!SDK.NetworkRequest} request
99 * @return {!{type: !SDK.NetworkRequest.InitiatorType, url: string, lineNumber : number, columnNumber: number, scriptId: ?string}}
100 */
101 static initiatorInfoForRequest(request) {
102 if (request[SDK.NetworkLog._initiatorInfoSymbol])
103 return request[SDK.NetworkLog._initiatorInfoSymbol];
104
105 var type = SDK.NetworkRequest.InitiatorType.Other;
106 var url = '';
107 var lineNumber = -Infinity;
108 var columnNumber = -Infinity;
109 var scriptId = null;
110 var initiator = request.initiator();
111
112 if (request.redirectSource) {
113 type = SDK.NetworkRequest.InitiatorType.Redirect;
114 url = request.redirectSource.url;
115 } else if (initiator) {
116 if (initiator.type === Protocol.Network.InitiatorType.Parser) {
117 type = SDK.NetworkRequest.InitiatorType.Parser;
118 url = initiator.url ? initiator.url : url;
119 lineNumber = initiator.lineNumber ? initiator.lineNumber : lineNumber;
120 } else if (initiator.type === Protocol.Network.InitiatorType.Script) {
121 for (var stack = initiator.stack; stack; stack = stack.parent) {
122 var topFrame = stack.callFrames.length ? stack.callFrames[0] : null;
123 if (!topFrame)
124 continue;
125 type = SDK.NetworkRequest.InitiatorType.Script;
126 url = topFrame.url || Common.UIString('<anonymous>');
127 lineNumber = topFrame.lineNumber;
128 columnNumber = topFrame.columnNumber;
129 scriptId = topFrame.scriptId;
130 break;
131 }
132 }
133 }
134
135 request[SDK.NetworkLog._initiatorInfoSymbol] =
136 {type: type, url: url, lineNumber: lineNumber, columnNumber: columnNumbe r, scriptId: scriptId};
137 return request[SDK.NetworkLog._initiatorInfoSymbol];
138 }
139
140 /**
141 * @param {!SDK.NetworkRequest} request
142 * @return {!SDK.NetworkLog.InitiatorGraph}
143 */
144 static initiatorGraphForRequest(request) {
145 /** @type {!Set<!SDK.NetworkRequest>} */
146 var initiated = new Set();
147 var networkLog = SDK.NetworkLog.networkLogForRequest(request);
148 if (!networkLog)
149 return {initiators: new Set(), initiated: new Set()};
150
151 var requests = networkLog.requests();
152 for (var logRequest of requests) {
153 var localInitiators = initiatorChain(logRequest);
154 if (localInitiators.has(request))
155 initiated.add(logRequest);
156 }
157 return {initiators: initiatorChain(request), initiated: initiated};
158
159 /**
160 * @param {!SDK.NetworkRequest} request
161 * @return {!Set<!SDK.NetworkRequest>}
162 */
163 function initiatorChain(request) {
164 var initiatorChainCache =
165 /** @type {!Set<!SDK.NetworkRequest>} */ (request[SDK.NetworkLog._init iatorChainSymbol]);
166 if (initiatorChainCache)
167 return initiatorChainCache;
168
169 initiatorChainCache = new Set();
170
171 var checkRequest = request;
172 while (checkRequest) {
173 initiatorChainCache.add(checkRequest);
174 checkRequest = initiatorRequest(checkRequest);
175 }
176 request[SDK.NetworkLog._initiatorChainSymbol] = initiatorChainCache;
177 return initiatorChainCache;
178 }
179
180 /**
181 * @param {!SDK.NetworkRequest} request
182 * @return {?SDK.NetworkRequest}
183 */
184 function initiatorRequest(request) {
185 if (request[SDK.NetworkLog._initiatorRequestSymbol] !== undefined)
186 return request[SDK.NetworkLog._initiatorRequestSymbol];
187 var networkLog = SDK.NetworkLog.networkLogForRequest(request);
188 var url = SDK.NetworkLog.initiatorInfoForRequest(request).url;
189 request[SDK.NetworkLog._initiatorRequestSymbol] = networkLog.requestForURL (url);
190 return request[SDK.NetworkLog._initiatorRequestSymbol];
191 }
192 }
193
194 /**
195 * @param {!SDK.NetworkRequest} request
196 * @return {?SDK.PageLoad}
197 */
198 static pageLoadForRequest(request) {
199 return request[SDK.NetworkLog._pageLoadForRequestSymbol];
200 }
201
202 /**
90 * @return {!Array.<!SDK.NetworkRequest>} 203 * @return {!Array.<!SDK.NetworkRequest>}
91 */ 204 */
92 requests() { 205 requests() {
93 return this._requests; 206 return this._requests;
94 } 207 }
95 208
96 /** 209 /**
97 * @param {string} url 210 * @param {string} url
98 * @return {?SDK.NetworkRequest} 211 * @return {?SDK.NetworkRequest}
99 */ 212 */
100 requestForURL(url) { 213 requestForURL(url) {
101 for (var i = 0; i < this._requests.length; ++i) { 214 for (var i = 0; i < this._requests.length; ++i) {
102 if (this._requests[i].url() === url) 215 if (this._requests[i].url() === url)
103 return this._requests[i]; 216 return this._requests[i];
104 } 217 }
105 return null; 218 return null;
106 } 219 }
107 220
108 /** 221 /**
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 222 * @param {!Common.Event} event
118 */ 223 */
119 _onMainFrameNavigated(event) { 224 _onMainFrameNavigated(event) {
120 var mainFrame = /** type {SDK.ResourceTreeFrame} */ event.data; 225 var mainFrame = /** type {SDK.ResourceTreeFrame} */ event.data;
121 // Preserve requests from the new session. 226 // Preserve requests from the new session.
122 this._currentPageLoad = null; 227 this._currentPageLoad = null;
123 var oldRequests = this._requests.splice(0, this._requests.length); 228 var oldRequests = this._requests.splice(0, this._requests.length);
124 this._requestForId = {}; 229 this._requestForId = {};
125 for (var i = 0; i < oldRequests.length; ++i) { 230 for (var i = 0; i < oldRequests.length; ++i) {
126 var request = oldRequests[i]; 231 var request = oldRequests[i];
127 if (request.loaderId === mainFrame.loaderId) { 232 if (request.loaderId === mainFrame.loaderId) {
128 if (!this._currentPageLoad) 233 if (!this._currentPageLoad)
129 this._currentPageLoad = new SDK.PageLoad(request); 234 this._currentPageLoad = new SDK.PageLoad(request);
130 this._requests.push(request); 235 this._requests.push(request);
131 this._requestForId[request.requestId()] = request; 236 this._requestForId[request.requestId()] = request;
132 request.__page = this._currentPageLoad; 237 request[SDK.NetworkLog._pageLoadForRequestSymbol] = this._currentPageLoa d;
133 } 238 }
134 } 239 }
135 } 240 }
136 241
137 /** 242 /**
138 * @param {!Common.Event} event 243 * @param {!Common.Event} event
139 */ 244 */
140 _onRequestStarted(event) { 245 _onRequestStarted(event) {
141 var request = /** @type {!SDK.NetworkRequest} */ (event.data); 246 var request = /** @type {!SDK.NetworkRequest} */ (event.data);
142 this._requests.push(request); 247 this._requests.push(request);
143 this._requestForId[request.requestId()] = request; 248 this._requestForId[request.requestId()] = request;
144 request.__page = this._currentPageLoad; 249 request[SDK.NetworkLog._pageLoadForRequestSymbol] = this._currentPageLoad;
250 request.addEventListener(
dgozman 2017/01/13 00:51:54 Remove this listener somewhere?
allada 2017/01/17 22:10:30 Done.
251 SDK.NetworkRequest.Events.RedirectSourceChanged, () => delete request[SD K.NetworkLog._initiatorInfoSymbol]);
dgozman 2017/01/13 00:51:54 nit: let's wrap the body in {}
allada 2017/01/17 22:10:30 Done.
145 } 252 }
146 253
147 /** 254 /**
148 * @param {!Common.Event} event 255 * @param {!Common.Event} event
149 */ 256 */
150 _onDOMContentLoaded(event) { 257 _onDOMContentLoaded(event) {
151 if (this._currentPageLoad) 258 if (this._currentPageLoad)
152 this._currentPageLoad.contentLoadTime = event.data; 259 this._currentPageLoad.contentLoadTime = event.data;
153 } 260 }
154 261
(...skipping 23 matching lines...) Expand all
178 * @param {!SDK.NetworkRequest} mainRequest 285 * @param {!SDK.NetworkRequest} mainRequest
179 */ 286 */
180 constructor(mainRequest) { 287 constructor(mainRequest) {
181 this.id = ++SDK.PageLoad._lastIdentifier; 288 this.id = ++SDK.PageLoad._lastIdentifier;
182 this.url = mainRequest.url(); 289 this.url = mainRequest.url();
183 this.startTime = mainRequest.startTime; 290 this.startTime = mainRequest.startTime;
184 } 291 }
185 }; 292 };
186 293
187 SDK.PageLoad._lastIdentifier = 0; 294 SDK.PageLoad._lastIdentifier = 0;
295
296 /** @typedef {!{initiators: !Set<!SDK.NetworkRequest>, initiated: !Set<!SDK.Netw orkRequest>}} */
297 SDK.NetworkLog.InitiatorGraph;
298
299 SDK.NetworkLog._networkLogForRequestSymbol = Symbol('NetworkLogForRequest');
300 SDK.NetworkLog._initiatorInfoSymbol = Symbol('InitiatorInfo');
301 SDK.NetworkLog._initiatorRequestSymbol = Symbol('InitiatorRequest');
302 SDK.NetworkLog._initiatorChainSymbol = Symbol('InitiatorChain');
caseq 2017/01/13 01:16:59 Why not merge all initiator-related info for a req
allada 2017/01/17 22:10:30 Done.
303 SDK.NetworkLog._pageLoadForRequestSymbol = Symbol('PageLoadForRequest');
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698