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

Side by Side Diff: Source/devtools/front_end/StylesSourceMapping.js

Issue 25879002: DevTools: Get rid of styleFile on UISourceCode (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Removed test Created 7 years, 2 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 | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2012 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 22 matching lines...) Expand all
33 * @implements {WebInspector.SourceMapping} 33 * @implements {WebInspector.SourceMapping}
34 * @param {WebInspector.CSSStyleModel} cssModel 34 * @param {WebInspector.CSSStyleModel} cssModel
35 * @param {WebInspector.Workspace} workspace 35 * @param {WebInspector.Workspace} workspace
36 */ 36 */
37 WebInspector.StylesSourceMapping = function(cssModel, workspace) 37 WebInspector.StylesSourceMapping = function(cssModel, workspace)
38 { 38 {
39 this._cssModel = cssModel; 39 this._cssModel = cssModel;
40 this._workspace = workspace; 40 this._workspace = workspace;
41 this._workspace.addEventListener(WebInspector.Workspace.Events.ProjectWillRe set, this._projectWillReset, this); 41 this._workspace.addEventListener(WebInspector.Workspace.Events.ProjectWillRe set, this._projectWillReset, this);
42 this._workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeA dded, this._uiSourceCodeAddedToWorkspace, this); 42 this._workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeA dded, this._uiSourceCodeAddedToWorkspace, this);
43 this._workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeR emoved, this._uiSourceCodeRemoved, this);
43 44
44 WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeMod el.EventTypes.MainFrameCreatedOrNavigated, this._mainFrameCreatedOrNavigated, th is); 45 WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeMod el.EventTypes.MainFrameCreatedOrNavigated, this._mainFrameCreatedOrNavigated, th is);
46
47 this._cssModel.addEventListener(WebInspector.CSSStyleModel.Events.StyleSheet Changed, this._styleSheetChanged, this);
45 this._initialize(); 48 this._initialize();
46 } 49 }
47 50
48 WebInspector.StylesSourceMapping.prototype = { 51 WebInspector.StylesSourceMapping.prototype = {
49 /** 52 /**
50 * @param {WebInspector.RawLocation} rawLocation 53 * @param {WebInspector.RawLocation} rawLocation
51 * @return {WebInspector.UILocation} 54 * @return {WebInspector.UILocation}
52 */ 55 */
53 rawLocationToUILocation: function(rawLocation) 56 rawLocationToUILocation: function(rawLocation)
54 { 57 {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 this._unbindUISourceCode(uiSourceCode); 123 this._unbindUISourceCode(uiSourceCode);
121 } 124 }
122 } 125 }
123 }, 126 },
124 127
125 /** 128 /**
126 * @param {WebInspector.UISourceCode} uiSourceCode 129 * @param {WebInspector.UISourceCode} uiSourceCode
127 */ 130 */
128 _unbindUISourceCode: function(uiSourceCode) 131 _unbindUISourceCode: function(uiSourceCode)
129 { 132 {
130 if (!uiSourceCode.styleFile()) 133 var styleFile = this._styleFiles.get(uiSourceCode);
134 if (!styleFile)
131 return; 135 return;
132 uiSourceCode.styleFile().dispose(); 136 styleFile.dispose();
133 uiSourceCode.setStyleFile(null); 137 this._styleFiles.remove(uiSourceCode);
134 }, 138 },
135 139
136 /** 140 /**
137 * @param {WebInspector.Event} event 141 * @param {WebInspector.Event} event
138 */ 142 */
139 _uiSourceCodeAddedToWorkspace: function(event) 143 _uiSourceCodeAddedToWorkspace: function(event)
140 { 144 {
141 var uiSourceCode = /** @type {WebInspector.UISourceCode} */ (event.data) ; 145 var uiSourceCode = /** @type {WebInspector.UISourceCode} */ (event.data) ;
142 var url = uiSourceCode.url; 146 var url = uiSourceCode.url;
143 if (!url || !this._urlToHeadersByFrameId[url]) 147 if (!url || !this._urlToHeadersByFrameId[url])
144 return; 148 return;
145 this._bindUISourceCode(uiSourceCode, this._urlToHeadersByFrameId[url].va lues()[0].values()[0]); 149 this._bindUISourceCode(uiSourceCode, this._urlToHeadersByFrameId[url].va lues()[0].values()[0]);
146 }, 150 },
147 151
148 /** 152 /**
149 * @param {WebInspector.UISourceCode} uiSourceCode 153 * @param {WebInspector.UISourceCode} uiSourceCode
150 * @param {WebInspector.CSSStyleSheetHeader} header 154 * @param {WebInspector.CSSStyleSheetHeader} header
151 */ 155 */
152 _bindUISourceCode: function(uiSourceCode, header) 156 _bindUISourceCode: function(uiSourceCode, header)
153 { 157 {
154 if (uiSourceCode.styleFile() || header.isInline) 158 if (this._styleFiles.get(uiSourceCode) || header.isInline)
155 return; 159 return;
156 var url = uiSourceCode.url; 160 var url = uiSourceCode.url;
157 uiSourceCode.setStyleFile(new WebInspector.StyleFile(uiSourceCode)); 161 this._styleFiles.put(uiSourceCode, new WebInspector.StyleFile(uiSourceCo de, this));
158 header.updateLocations(); 162 header.updateLocations();
159 }, 163 },
160 164
161 /** 165 /**
162 * @param {WebInspector.Event} event 166 * @param {WebInspector.Event} event
163 */ 167 */
164 _projectWillReset: function(event) 168 _projectWillReset: function(event)
165 { 169 {
166 var project = /** @type {WebInspector.Project} */ (event.data); 170 var project = /** @type {WebInspector.Project} */ (event.data);
167 var uiSourceCodes = project.uiSourceCodes(); 171 var uiSourceCodes = project.uiSourceCodes();
168 for (var i = 0; i < uiSourceCodes; ++i) 172 for (var i = 0; i < uiSourceCodes; ++i)
169 delete this._urlToHeadersByFrameId[uiSourceCodes[i].url]; 173 this._unbindUISourceCode(uiSourceCodes[i]);
174 },
175
176 /**
177 * @param {WebInspector.Event} event
178 */
179 _uiSourceCodeRemoved: function(event)
180 {
181 var uiSourceCode = /** @type {WebInspector.UISourceCode} */ (event.data) ;
182 this._unbindUISourceCode(uiSourceCode);
170 }, 183 },
171 184
172 _initialize: function() 185 _initialize: function()
173 { 186 {
174 /** @type {!Object.<string, !StringMap.<!StringMap.<!WebInspector.CSSSty leSheetHeader>>>} */ 187 /** @type {!Object.<string, !StringMap.<!StringMap.<!WebInspector.CSSSty leSheetHeader>>>} */
175 this._urlToHeadersByFrameId = {}; 188 this._urlToHeadersByFrameId = {};
189 /** @type {!Map.<WebInspector.UISourceCode, WebInspector.StyleFile>} */
190 this._styleFiles = new Map();
176 }, 191 },
177 192
178 /** 193 /**
179 * @param {WebInspector.Event} event 194 * @param {WebInspector.Event} event
180 */ 195 */
181 _mainFrameCreatedOrNavigated: function(event) 196 _mainFrameCreatedOrNavigated: function(event)
182 { 197 {
183 for (var url in this._urlToHeadersByFrameId) { 198 for (var url in this._urlToHeadersByFrameId) {
184 var uiSourceCode = this._workspace.uiSourceCodeForURL(url); 199 var uiSourceCode = this._workspace.uiSourceCodeForURL(url);
185 if (!uiSourceCode) 200 if (!uiSourceCode)
186 continue; 201 continue;
187 this._unbindUISourceCode(uiSourceCode); 202 this._unbindUISourceCode(uiSourceCode);
188 } 203 }
189 this._initialize(); 204 this._initialize();
205 },
206
207 /**
208 * @param {WebInspector.UISourceCode} uiSourceCode
209 * @param {string} content
210 * @param {boolean} majorChange
211 * @param {function(?string)} userCallback
212 */
213 _setStyleContent: function(uiSourceCode, content, majorChange, userCallback)
214 {
215 var styleSheetIds = this._cssModel.styleSheetIdsForURL(uiSourceCode.url) ;
216 if (!styleSheetIds.length) {
217 userCallback("No stylesheet found: " + uiSourceCode.url);
218 return;
219 }
220
221 this._isSettingContent = true;
222 function callback(error)
223 {
224 userCallback(error);
225 delete this._isSettingContent;
226 }
227 this._cssModel.setStyleSheetText(styleSheetIds[0], content, majorChange, callback.bind(this));
228 },
229
230 /**
231 * @param {WebInspector.Event} event
232 */
233 _styleSheetChanged: function(event)
234 {
235 if (this._isSettingContent)
236 return;
237
238 if (!event.data.majorChange)
239 return;
240
241 /**
242 * @param {?string} error
243 * @param {string} content
244 */
245 function callback(error, content)
246 {
247 if (!error)
248 this._innerStyleSheetChanged(event.data.styleSheetId, content);
249 }
250 CSSAgent.getStyleSheetText(event.data.styleSheetId, callback.bind(this)) ;
251 },
252
253 /**
254 * @param {CSSAgent.StyleSheetId} styleSheetId
255 * @param {string} content
256 */
257 _innerStyleSheetChanged: function(styleSheetId, content)
258 {
259 var header = this._cssModel.styleSheetHeaderForId(styleSheetId);
260 if (!header)
261 return;
262 var styleSheetURL = header.resourceURL();
263 if (!styleSheetURL)
264 return;
265
266 var uiSourceCode = this._workspace.uiSourceCodeForURL(styleSheetURL)
267 if (!uiSourceCode)
268 return;
269
270 var styleFile = this._styleFiles.get(uiSourceCode);
271 if (styleFile)
272 styleFile.addRevision(content);
190 } 273 }
191 } 274 }
192 275
193 /** 276 /**
194 * @constructor 277 * @constructor
195 * @param {WebInspector.UISourceCode} uiSourceCode 278 * @param {WebInspector.UISourceCode} uiSourceCode
279 * @param {WebInspector.StylesSourceMapping} mapping
196 */ 280 */
197 WebInspector.StyleFile = function(uiSourceCode) 281 WebInspector.StyleFile = function(uiSourceCode, mapping)
198 { 282 {
199 this._uiSourceCode = uiSourceCode; 283 this._uiSourceCode = uiSourceCode;
284 this._mapping = mapping;
200 this._uiSourceCode.addEventListener(WebInspector.UISourceCode.Events.Working CopyChanged, this._workingCopyChanged, this); 285 this._uiSourceCode.addEventListener(WebInspector.UISourceCode.Events.Working CopyChanged, this._workingCopyChanged, this);
201 this._uiSourceCode.addEventListener(WebInspector.UISourceCode.Events.Working CopyCommitted, this._workingCopyCommitted, this); 286 this._uiSourceCode.addEventListener(WebInspector.UISourceCode.Events.Working CopyCommitted, this._workingCopyCommitted, this);
202 } 287 }
203 288
204 WebInspector.StyleFile.updateTimeout = 200; 289 WebInspector.StyleFile.updateTimeout = 200;
205 290
206 WebInspector.StyleFile.sourceURLRegex = /\n[\040\t]*\/\*#[\040\t]sourceURL=[\040 \t]*([^\s]*)[\040\t]*\*\/[\040\t]*$/m; 291 WebInspector.StyleFile.sourceURLRegex = /\n[\040\t]*\/\*#[\040\t]sourceURL=[\040 \t]*([^\s]*)[\040\t]*\*\/[\040\t]*$/m;
207 292
208 WebInspector.StyleFile.prototype = { 293 WebInspector.StyleFile.prototype = {
209 _workingCopyCommitted: function(event) 294 _workingCopyCommitted: function(event)
(...skipping 15 matching lines...) Expand all
225 } else 310 } else
226 this._commitIncrementalEdit(false); 311 this._commitIncrementalEdit(false);
227 }, 312 },
228 313
229 /** 314 /**
230 * @param {boolean} majorChange 315 * @param {boolean} majorChange
231 */ 316 */
232 _commitIncrementalEdit: function(majorChange) 317 _commitIncrementalEdit: function(majorChange)
233 { 318 {
234 this._clearIncrementalUpdateTimer(); 319 this._clearIncrementalUpdateTimer();
235 WebInspector.styleContentBinding.setStyleContent(this._uiSourceCode, thi s._uiSourceCode.workingCopy(), majorChange, this._styleContentSet.bind(this)); 320 this._mapping._setStyleContent(this._uiSourceCode, this._uiSourceCode.wo rkingCopy(), majorChange, this._styleContentSet.bind(this));
236 }, 321 },
237 322
238 /** 323 /**
239 * @param {?string} error 324 * @param {?string} error
240 */ 325 */
241 _styleContentSet: function(error) 326 _styleContentSet: function(error)
242 { 327 {
243 if (error) 328 if (error)
244 WebInspector.showErrorMessage(error); 329 WebInspector.showErrorMessage(error);
245 }, 330 },
(...skipping 17 matching lines...) Expand all
263 this._uiSourceCode.addRevision(content); 348 this._uiSourceCode.addRevision(content);
264 delete this._isAddingRevision; 349 delete this._isAddingRevision;
265 }, 350 },
266 351
267 dispose: function() 352 dispose: function()
268 { 353 {
269 this._uiSourceCode.removeEventListener(WebInspector.UISourceCode.Events. WorkingCopyCommitted, this._workingCopyCommitted, this); 354 this._uiSourceCode.removeEventListener(WebInspector.UISourceCode.Events. WorkingCopyCommitted, this._workingCopyCommitted, this);
270 this._uiSourceCode.removeEventListener(WebInspector.UISourceCode.Events. WorkingCopyChanged, this._workingCopyChanged, this); 355 this._uiSourceCode.removeEventListener(WebInspector.UISourceCode.Events. WorkingCopyChanged, this._workingCopyChanged, this);
271 } 356 }
272 } 357 }
273
274 /**
275 * @constructor
276 * @param {WebInspector.CSSStyleModel} cssModel
277 */
278 WebInspector.StyleContentBinding = function(cssModel, workspace)
279 {
280 this._cssModel = cssModel;
281 this._workspace = workspace;
282 this._cssModel.addEventListener(WebInspector.CSSStyleModel.Events.StyleSheet Changed, this._styleSheetChanged, this);
283 }
284
285 WebInspector.StyleContentBinding.prototype = {
286 /**
287 * @param {WebInspector.UISourceCode} uiSourceCode
288 * @param {string} content
289 * @param {boolean} majorChange
290 * @param {function(?string)} userCallback
291 */
292 setStyleContent: function(uiSourceCode, content, majorChange, userCallback)
293 {
294 var styleSheetIds = this._cssModel.styleSheetIdsForURL(uiSourceCode.url) ;
295 if (!styleSheetIds.length) {
296 userCallback("No stylesheet found: " + uiSourceCode.url);
297 return;
298 }
299
300 this._isSettingContent = true;
301 function callback(error)
302 {
303 userCallback(error);
304 delete this._isSettingContent;
305 }
306 this._cssModel.setStyleSheetText(styleSheetIds[0], content, majorChange, callback.bind(this));
307 },
308
309 /**
310 * @param {WebInspector.Event} event
311 */
312 _styleSheetChanged: function(event)
313 {
314 if (this._isSettingContent)
315 return;
316
317 if (!event.data.majorChange)
318 return;
319
320 /**
321 * @param {?string} error
322 * @param {string} content
323 */
324 function callback(error, content)
325 {
326 if (!error)
327 this._innerStyleSheetChanged(event.data.styleSheetId, content);
328 }
329 CSSAgent.getStyleSheetText(event.data.styleSheetId, callback.bind(this)) ;
330 },
331
332 /**
333 * @param {CSSAgent.StyleSheetId} styleSheetId
334 * @param {string} content
335 */
336 _innerStyleSheetChanged: function(styleSheetId, content)
337 {
338 var header = this._cssModel.styleSheetHeaderForId(styleSheetId);
339 if (!header)
340 return;
341 var styleSheetURL = header.resourceURL();
342 if (!styleSheetURL)
343 return;
344
345 var uiSourceCode = this._workspace.uiSourceCodeForURL(styleSheetURL)
346 if (!uiSourceCode)
347 return;
348
349 if (uiSourceCode.styleFile())
350 uiSourceCode.styleFile().addRevision(content);
351 }
352 }
353
354 /**
355 * @type {?WebInspector.StyleContentBinding}
356 */
357 WebInspector.styleContentBinding = null;
OLDNEW
« no previous file with comments | « LayoutTests/inspector/styles/stylesheet-removal-expected.txt ('k') | Source/devtools/front_end/UISourceCode.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698