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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/bindings/CSSWorkspaceBinding.js

Issue 2300523002: DevTools: cleanup WI.CSSWorkspaceBindings (Closed)
Patch Set: address comments Created 4 years, 3 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 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** 5 /**
6 * @constructor 6 * @constructor
7 * @implements {WebInspector.TargetManager.Observer} 7 * @implements {WebInspector.TargetManager.Observer}
8 * @param {!WebInspector.TargetManager} targetManager 8 * @param {!WebInspector.TargetManager} targetManager
9 * @param {!WebInspector.Workspace} workspace 9 * @param {!WebInspector.Workspace} workspace
10 * @param {!WebInspector.NetworkMapping} networkMapping 10 * @param {!WebInspector.NetworkMapping} networkMapping
11 */ 11 */
12 WebInspector.CSSWorkspaceBinding = function(targetManager, workspace, networkMap ping) 12 WebInspector.CSSWorkspaceBinding = function(targetManager, workspace, networkMap ping)
13 { 13 {
14 this._workspace = workspace; 14 this._workspace = workspace;
15 this._networkMapping = networkMapping; 15 this._networkMapping = networkMapping;
16 16
17 /** @type {!Map.<!WebInspector.CSSModel, !WebInspector.CSSWorkspaceBinding.T argetInfo>} */ 17 /** @type {!Map.<!WebInspector.CSSModel, !WebInspector.CSSWorkspaceBinding.T argetInfo>} */
18 this._modelToTargetInfo = new Map(); 18 this._modelToTargetInfo = new Map();
19 targetManager.observeTargets(this); 19 targetManager.observeTargets(this);
20
21 targetManager.addModelListener(WebInspector.ResourceTreeModel, WebInspector. ResourceTreeModel.Events.MainFrameNavigated, this._mainFrameCreatedOrNavigated, this);
22 } 20 }
23 21
24 WebInspector.CSSWorkspaceBinding.prototype = { 22 WebInspector.CSSWorkspaceBinding.prototype = {
25 /** 23 /**
26 * @override 24 * @override
27 * @param {!WebInspector.Target} target 25 * @param {!WebInspector.Target} target
28 */ 26 */
29 targetAdded: function(target) 27 targetAdded: function(target)
30 { 28 {
31 var cssModel = WebInspector.CSSModel.fromTarget(target); 29 var cssModel = WebInspector.CSSModel.fromTarget(target);
32 if (cssModel) 30 if (cssModel)
33 this._modelToTargetInfo.set(cssModel, new WebInspector.CSSWorkspaceB inding.TargetInfo(cssModel, this._workspace, this._networkMapping)); 31 this._modelToTargetInfo.set(cssModel, new WebInspector.CSSWorkspaceB inding.TargetInfo(cssModel, this._workspace, this._networkMapping));
34 }, 32 },
35 33
36 /** 34 /**
37 * @override 35 * @override
38 * @param {!WebInspector.Target} target 36 * @param {!WebInspector.Target} target
39 */ 37 */
40 targetRemoved: function(target) 38 targetRemoved: function(target)
41 { 39 {
42 var cssModel = WebInspector.CSSModel.fromTarget(target); 40 var cssModel = WebInspector.CSSModel.fromTarget(target);
43 if (cssModel) 41 if (cssModel)
44 this._modelToTargetInfo.remove(cssModel)._dispose(); 42 this._modelToTargetInfo.remove(cssModel)._dispose();
45 }, 43 },
46 44
47 /** 45 /**
48 * @param {!WebInspector.CSSStyleSheetHeader} header 46 * @param {!WebInspector.CSSStyleSheetHeader} header
49 * @param {!WebInspector.CSSSourceMapping} mapping 47 * @return {?WebInspector.CSSWorkspaceBinding.TargetInfo}
50 */ 48 */
51 pushSourceMapping: function(header, mapping) 49 _targetInfo: function(header)
52 { 50 {
53 this._ensureInfoForHeader(header)._pushSourceMapping(mapping); 51 return this._modelToTargetInfo.get(header.cssModel()) || null;
54 }, 52 },
55 53
56 /** 54 /**
57 * @param {!WebInspector.CSSStyleSheetHeader} header 55 * @param {!WebInspector.CSSStyleSheetHeader} header
58 * @return {?WebInspector.CSSWorkspaceBinding.HeaderInfo} 56 * @return {!WebInspector.CSSWorkspaceBinding.TargetInfo}
59 */ 57 */
60 _headerInfo: function(header) 58 _ensureTargetInfo: function(header)
61 {
62 var map = this._modelToTargetInfo.get(header.cssModel());
63 return map._headerInfo(header.id) || null;
64 },
65
66 /**
67 * @param {!WebInspector.CSSStyleSheetHeader} header
68 * @return {!WebInspector.CSSWorkspaceBinding.HeaderInfo}
69 */
70 _ensureInfoForHeader: function(header)
71 { 59 {
72 var targetInfo = this._modelToTargetInfo.get(header.cssModel()); 60 var targetInfo = this._modelToTargetInfo.get(header.cssModel());
73 if (!targetInfo) { 61 if (!targetInfo) {
74 targetInfo = new WebInspector.CSSWorkspaceBinding.TargetInfo(header. cssModel(), this._workspace, this._networkMapping); 62 targetInfo = new WebInspector.CSSWorkspaceBinding.TargetInfo(header. cssModel(), this._workspace, this._networkMapping);
75 this._modelToTargetInfo.set(header.cssModel(), targetInfo); 63 this._modelToTargetInfo.set(header.cssModel(), targetInfo);
76 } 64 }
77 return targetInfo._ensureInfoForHeader(header); 65 return targetInfo;
78 }, 66 },
79 67
80 /** 68 /**
81 * @param {!WebInspector.Event} event
82 */
83 _mainFrameCreatedOrNavigated: function(event)
84 {
85 var target = /** @type {!WebInspector.ResourceTreeModel} */ (event.targe t).target();
86 var cssModel = WebInspector.CSSModel.fromTarget(target);
87 if (cssModel)
88 this._modelToTargetInfo.get(cssModel)._reset();
89 },
90
91 /**
92 * @param {!WebInspector.CSSStyleSheetHeader} header 69 * @param {!WebInspector.CSSStyleSheetHeader} header
93 */ 70 */
94 updateLocations: function(header) 71 updateLocations: function(header)
95 { 72 {
96 var info = this._headerInfo(header); 73 var targetInfo = this._targetInfo(header);
97 if (info) 74 if (targetInfo)
98 info._updateLocations(); 75 targetInfo._updateLocations(header);
99 }, 76 },
100 77
101 /** 78 /**
102 * @param {!WebInspector.CSSLocation} rawLocation 79 * @param {!WebInspector.CSSLocation} rawLocation
103 * @param {function(!WebInspector.LiveLocation)} updateDelegate 80 * @param {function(!WebInspector.LiveLocation)} updateDelegate
104 * @param {!WebInspector.LiveLocationPool} locationPool 81 * @param {!WebInspector.LiveLocationPool} locationPool
105 * @return {!WebInspector.CSSWorkspaceBinding.LiveLocation} 82 * @return {!WebInspector.CSSWorkspaceBinding.LiveLocation}
106 */ 83 */
107 createLiveLocation: function(rawLocation, updateDelegate, locationPool) 84 createLiveLocation: function(rawLocation, updateDelegate, locationPool)
108 { 85 {
109 var header = rawLocation.styleSheetId ? rawLocation.cssModel().styleShee tHeaderForId(rawLocation.styleSheetId) : null; 86 var header = rawLocation.styleSheetId ? rawLocation.cssModel().styleShee tHeaderForId(rawLocation.styleSheetId) : null;
110 return new WebInspector.CSSWorkspaceBinding.LiveLocation(rawLocation.css Model(), header, rawLocation, this, updateDelegate, locationPool); 87 return new WebInspector.CSSWorkspaceBinding.LiveLocation(rawLocation.css Model(), header, rawLocation, this, updateDelegate, locationPool);
111 }, 88 },
112 89
113 /** 90 /**
114 * @param {!WebInspector.CSSWorkspaceBinding.LiveLocation} location 91 * @param {!WebInspector.CSSWorkspaceBinding.LiveLocation} location
115 */ 92 */
116 _addLiveLocation: function(location) 93 _addLiveLocation: function(location)
117 { 94 {
118 this._ensureInfoForHeader(location._header)._addLocation(location); 95 this._ensureTargetInfo(location._header)._addLocation(location);
119 }, 96 },
120 97
121 /** 98 /**
122 * @param {!WebInspector.CSSWorkspaceBinding.LiveLocation} location 99 * @param {!WebInspector.CSSWorkspaceBinding.LiveLocation} location
123 */ 100 */
124 _removeLiveLocation: function(location) 101 _removeLiveLocation: function(location)
125 { 102 {
126 var info = this._headerInfo(location._header); 103 var targetInfo = this._targetInfo(location._header);
127 if (info) 104 if (targetInfo)
128 info._removeLocation(location); 105 targetInfo._removeLocation(location);
129 }, 106 },
130 107
131 /** 108 /**
132 * @param {!WebInspector.CSSProperty} cssProperty 109 * @param {!WebInspector.CSSProperty} cssProperty
133 * @param {boolean} forName 110 * @param {boolean} forName
134 * @return {?WebInspector.UILocation} 111 * @return {?WebInspector.UILocation}
135 */ 112 */
136 propertyUILocation: function(cssProperty, forName) 113 propertyUILocation: function(cssProperty, forName)
137 { 114 {
138 var style = cssProperty.ownerStyle; 115 var style = cssProperty.ownerStyle;
(...skipping 17 matching lines...) Expand all
156 * @param {?WebInspector.CSSLocation} rawLocation 133 * @param {?WebInspector.CSSLocation} rawLocation
157 * @return {?WebInspector.UILocation} 134 * @return {?WebInspector.UILocation}
158 */ 135 */
159 rawLocationToUILocation: function(rawLocation) 136 rawLocationToUILocation: function(rawLocation)
160 { 137 {
161 if (!rawLocation) 138 if (!rawLocation)
162 return null; 139 return null;
163 var header = rawLocation.cssModel().styleSheetHeaderForId(rawLocation.st yleSheetId); 140 var header = rawLocation.cssModel().styleSheetHeaderForId(rawLocation.st yleSheetId);
164 if (!header) 141 if (!header)
165 return null; 142 return null;
166 var info = this._headerInfo(header); 143 var targetInfo = this._targetInfo(header);
167 return info ? info._rawLocationToUILocation(rawLocation.lineNumber, rawL ocation.columnNumber) : null; 144 return targetInfo ? targetInfo._rawLocationToUILocation(header, rawLocat ion.lineNumber, rawLocation.columnNumber) : null;
168 } 145 }
169 } 146 }
170 147
171 /** 148 /**
172 * @constructor 149 * @constructor
173 * @param {!WebInspector.CSSModel} cssModel 150 * @param {!WebInspector.CSSModel} cssModel
174 * @param {!WebInspector.Workspace} workspace 151 * @param {!WebInspector.Workspace} workspace
175 * @param {!WebInspector.NetworkMapping} networkMapping 152 * @param {!WebInspector.NetworkMapping} networkMapping
176 */ 153 */
177 WebInspector.CSSWorkspaceBinding.TargetInfo = function(cssModel, workspace, netw orkMapping) 154 WebInspector.CSSWorkspaceBinding.TargetInfo = function(cssModel, workspace, netw orkMapping)
178 { 155 {
179 this._cssModel = cssModel; 156 this._cssModel = cssModel;
180 this._stylesSourceMapping = new WebInspector.StylesSourceMapping(cssModel, w orkspace, networkMapping); 157 this._stylesSourceMapping = new WebInspector.StylesSourceMapping(cssModel, w orkspace, networkMapping);
181 this._sassSourceMapping = new WebInspector.SASSSourceMapping(cssModel, netwo rkMapping, WebInspector.NetworkProject.forTarget(cssModel.target())); 158 this._sassSourceMapping = new WebInspector.SASSSourceMapping(cssModel, netwo rkMapping, WebInspector.NetworkProject.forTarget(cssModel.target()));
182 159
183 /** @type {!Map.<string, !WebInspector.CSSWorkspaceBinding.HeaderInfo>} */ 160 /** @type {!Multimap<!WebInspector.CSSStyleSheetHeader, !WebInspector.LiveLo cation>} */
184 this._headerInfoById = new Map(); 161 this._locations = new Multimap();
185 162
186 cssModel.addEventListener(WebInspector.CSSModel.Events.StyleSheetAdded, this ._styleSheetAdded, this); 163 cssModel.addEventListener(WebInspector.CSSModel.Events.StyleSheetAdded, this ._styleSheetAdded, this);
187 cssModel.addEventListener(WebInspector.CSSModel.Events.StyleSheetRemoved, th is._styleSheetRemoved, this); 164 cssModel.addEventListener(WebInspector.CSSModel.Events.StyleSheetRemoved, th is._styleSheetRemoved, this);
188 } 165 }
189 166
190 WebInspector.CSSWorkspaceBinding.TargetInfo.prototype = { 167 WebInspector.CSSWorkspaceBinding.TargetInfo.prototype = {
191 /** 168 /**
192 * @param {!WebInspector.Event} event 169 * @param {!WebInspector.Event} event
193 */ 170 */
194 _styleSheetAdded: function(event) 171 _styleSheetAdded: function(event)
195 { 172 {
196 var header = /** @type {!WebInspector.CSSStyleSheetHeader} */ (event.dat a); 173 var header = /** @type {!WebInspector.CSSStyleSheetHeader} */ (event.dat a);
197 this._stylesSourceMapping.addHeader(header); 174 this._stylesSourceMapping.addHeader(header);
198 this._sassSourceMapping.addHeader(header); 175 this._sassSourceMapping.addHeader(header);
199 }, 176 },
200 177
201 /** 178 /**
202 * @param {!WebInspector.Event} event 179 * @param {!WebInspector.Event} event
203 */ 180 */
204 _styleSheetRemoved: function(event) 181 _styleSheetRemoved: function(event)
205 { 182 {
206 var header = /** @type {!WebInspector.CSSStyleSheetHeader} */ (event.dat a); 183 var header = /** @type {!WebInspector.CSSStyleSheetHeader} */ (event.dat a);
207 this._stylesSourceMapping.removeHeader(header); 184 this._stylesSourceMapping.removeHeader(header);
208 this._sassSourceMapping.removeHeader(header); 185 this._sassSourceMapping.removeHeader(header);
209 this._headerInfoById.remove(header.id);
210 }, 186 },
211 187
212 /** 188 /**
213 * @param {!CSSAgent.StyleSheetId} id 189 * @param {!WebInspector.CSSWorkspaceBinding.LiveLocation} location
214 */ 190 */
215 _headerInfo: function(id) 191 _addLocation: function(location)
216 { 192 {
217 return this._headerInfoById.get(id); 193 var header = location._header;
194 this._locations.set(header, location);
195 location.update();
196 },
197
198 /**
199 * @param {!WebInspector.CSSWorkspaceBinding.LiveLocation} location
200 */
201 _removeLocation: function(location)
202 {
203 this._locations.remove(location._header, location);
218 }, 204 },
219 205
220 /** 206 /**
221 * @param {!WebInspector.CSSStyleSheetHeader} header 207 * @param {!WebInspector.CSSStyleSheetHeader} header
222 * @return {!WebInspector.CSSWorkspaceBinding.HeaderInfo}
223 */ 208 */
224 _ensureInfoForHeader: function(header) 209 _updateLocations: function(header)
225 { 210 {
226 var info = this._headerInfoById.get(header.id); 211 for (var location of this._locations.get(header))
227 if (!info) { 212 location.update();
228 info = new WebInspector.CSSWorkspaceBinding.HeaderInfo(header); 213 },
229 this._headerInfoById.set(header.id, info); 214
230 } 215 /**
231 return info; 216 * @param {!WebInspector.CSSStyleSheetHeader} header
217 * @param {number} lineNumber
218 * @param {number=} columnNumber
219 * @return {?WebInspector.UILocation}
220 */
221 _rawLocationToUILocation: function(header, lineNumber, columnNumber)
222 {
223 var rawLocation = new WebInspector.CSSLocation(header, lineNumber, colum nNumber);
224 var uiLocation = null;
225 uiLocation = uiLocation || this._sassSourceMapping.rawLocationToUILocati on(rawLocation);
226 uiLocation = uiLocation || this._stylesSourceMapping.rawLocationToUILoca tion(rawLocation);
227 return uiLocation;
232 }, 228 },
233 229
234 _dispose: function() 230 _dispose: function()
235 { 231 {
236 this._reset();
237 this._cssModel.removeEventListener(WebInspector.CSSModel.Events.StyleShe etAdded, this._styleSheetAdded, this); 232 this._cssModel.removeEventListener(WebInspector.CSSModel.Events.StyleShe etAdded, this._styleSheetAdded, this);
238 this._cssModel.removeEventListener(WebInspector.CSSModel.Events.StyleShe etRemoved, this._styleSheetRemoved, this); 233 this._cssModel.removeEventListener(WebInspector.CSSModel.Events.StyleShe etRemoved, this._styleSheetRemoved, this);
239 },
240
241 _reset: function()
242 {
243 this._headerInfoById.clear();
244 } 234 }
245 } 235 }
246 236
247 /**
248 * @constructor
249 * @param {!WebInspector.CSSStyleSheetHeader} header
250 */
251 WebInspector.CSSWorkspaceBinding.HeaderInfo = function(header)
252 {
253 this._header = header;
254
255 /** @type {!Array.<!WebInspector.CSSSourceMapping>} */
256 this._sourceMappings = [];
257
258 /** @type {!Set.<!WebInspector.LiveLocation>} */
259 this._locations = new Set();
260 }
261
262 WebInspector.CSSWorkspaceBinding.HeaderInfo.prototype = {
263 /**
264 * @param {!WebInspector.LiveLocation} location
265 */
266 _addLocation: function(location)
267 {
268 this._locations.add(location);
269 location.update();
270 },
271
272 /**
273 * @param {!WebInspector.LiveLocation} location
274 */
275 _removeLocation: function(location)
276 {
277 this._locations.delete(location);
278 },
279
280 _updateLocations: function()
281 {
282 var items = this._locations.valuesArray();
283 for (var i = 0; i < items.length; ++i)
284 items[i].update();
285 },
286
287 /**
288 * @param {number} lineNumber
289 * @param {number=} columnNumber
290 * @return {?WebInspector.UILocation}
291 */
292 _rawLocationToUILocation: function(lineNumber, columnNumber)
293 {
294 var uiLocation = null;
295 var rawLocation = new WebInspector.CSSLocation(this._header, lineNumber, columnNumber);
296 for (var i = this._sourceMappings.length - 1; !uiLocation && i >= 0; --i )
297 uiLocation = this._sourceMappings[i].rawLocationToUILocation(rawLoca tion);
298 return uiLocation;
299 },
300
301 /**
302 * @param {!WebInspector.CSSSourceMapping} sourceMapping
303 */
304 _pushSourceMapping: function(sourceMapping)
305 {
306 if (this._sourceMappings.indexOf(sourceMapping) !== -1)
307 return;
308 this._sourceMappings.push(sourceMapping);
309 this._updateLocations();
310 }
311 }
312
313 /** 237 /**
314 * @constructor 238 * @constructor
315 * @extends {WebInspector.LiveLocationWithPool} 239 * @extends {WebInspector.LiveLocationWithPool}
316 * @param {!WebInspector.CSSModel} cssModel 240 * @param {!WebInspector.CSSModel} cssModel
317 * @param {?WebInspector.CSSStyleSheetHeader} header 241 * @param {?WebInspector.CSSStyleSheetHeader} header
318 * @param {!WebInspector.CSSLocation} rawLocation 242 * @param {!WebInspector.CSSLocation} rawLocation
319 * @param {!WebInspector.CSSWorkspaceBinding} binding 243 * @param {!WebInspector.CSSWorkspaceBinding} binding
320 * @param {function(!WebInspector.LiveLocation)} updateDelegate 244 * @param {function(!WebInspector.LiveLocation)} updateDelegate
321 * @param {!WebInspector.LiveLocationPool} locationPool 245 * @param {!WebInspector.LiveLocationPool} locationPool
322 */ 246 */
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 }, 300 },
377 301
378 /** 302 /**
379 * @override 303 * @override
380 * @return {?WebInspector.UILocation} 304 * @return {?WebInspector.UILocation}
381 */ 305 */
382 uiLocation: function() 306 uiLocation: function()
383 { 307 {
384 var cssLocation = this._rawLocation; 308 var cssLocation = this._rawLocation;
385 if (this._header) { 309 if (this._header) {
386 var headerInfo = this._binding._headerInfo(this._header); 310 var targetInfo = this._binding._targetInfo(this._header);
387 return headerInfo._rawLocationToUILocation(cssLocation.lineNumber, c ssLocation.columnNumber); 311 return targetInfo._rawLocationToUILocation(this._header, cssLocation .lineNumber, cssLocation.columnNumber);
388 } 312 }
389 var uiSourceCode = this._binding._networkMapping.uiSourceCodeForStyleURL (cssLocation.url, cssLocation.header()); 313 var uiSourceCode = this._binding._networkMapping.uiSourceCodeForStyleURL (cssLocation.url, cssLocation.header());
390 if (!uiSourceCode) 314 if (!uiSourceCode)
391 return null; 315 return null;
392 return uiSourceCode.uiLocation(cssLocation.lineNumber, cssLocation.colum nNumber); 316 return uiSourceCode.uiLocation(cssLocation.lineNumber, cssLocation.colum nNumber);
393 }, 317 },
394 318
395 /** 319 /**
396 * @override 320 * @override
397 */ 321 */
(...skipping 12 matching lines...) Expand all
410 */ 334 */
411 isBlackboxed: function() 335 isBlackboxed: function()
412 { 336 {
413 return false; 337 return false;
414 }, 338 },
415 339
416 __proto__: WebInspector.LiveLocationWithPool.prototype 340 __proto__: WebInspector.LiveLocationWithPool.prototype
417 } 341 }
418 342
419 /** 343 /**
420 * @interface
421 */
422 WebInspector.CSSSourceMapping = function()
423 {
424 }
425
426 WebInspector.CSSSourceMapping.prototype = {
427 /**
428 * @param {!WebInspector.CSSLocation} rawLocation
429 * @return {?WebInspector.UILocation}
430 */
431 rawLocationToUILocation: function(rawLocation) { },
432
433 /**
434 * @param {!WebInspector.UISourceCode} uiSourceCode
435 * @param {number} lineNumber
436 * @param {number} columnNumber
437 * @return {?WebInspector.CSSLocation}
438 */
439 uiLocationToRawLocation: function(uiSourceCode, lineNumber, columnNumber) { },
440
441 /**
442 * @return {boolean}
443 */
444 isIdentity: function() { },
445
446 /**
447 * @param {!WebInspector.UISourceCode} uiSourceCode
448 * @param {number} lineNumber
449 * @return {boolean}
450 */
451 uiLineHasMapping: function(uiSourceCode, lineNumber) { }
452 }
453
454 /**
455 * @type {!WebInspector.CSSWorkspaceBinding} 344 * @type {!WebInspector.CSSWorkspaceBinding}
456 */ 345 */
457 WebInspector.cssWorkspaceBinding; 346 WebInspector.cssWorkspaceBinding;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698