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

Side by Side Diff: Source/devtools/front_end/sdk/CSSParser.js

Issue 685203003: DevTools: Get rid of synchronous XHRs in the frontend code (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Address comments Created 6 years, 1 month 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 2014 The Chromium Authors. All rights reserved. 2 * Copyright 2014 The Chromium Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be 3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file. 4 * found in the LICENSE file.
5 */ 5 */
6 6
7 /** 7 /**
8 * @constructor 8 * @constructor
9 * @extends {WebInspector.Object} 9 * @extends {WebInspector.Object}
10 */ 10 */
11 WebInspector.CSSParser = function() 11 WebInspector.CSSParser = function()
12 { 12 {
13 this._worker = Runtime.startWorker("script_formatter_worker");
14 this._worker.onmessage = this._onRuleChunk.bind(this);
15 this._rules = []; 13 this._rules = [];
16 } 14 this._pendingWorkerPromise = WorkerRuntime.startWorker("script_formatter_wor ker").then(this._init.bind(this));
17
18 WebInspector.CSSParser.Events = {
19 RulesParsed: "RulesParsed"
20 } 15 }
21 16
22 WebInspector.CSSParser.prototype = { 17 WebInspector.CSSParser.prototype = {
23 /** 18 /**
19 * @param {!Worker} worker
20 */
21 _init: function(worker)
22 {
23 this._worker = worker;
24 if (this._disposed) {
25 this.dispose();
26 return;
27 }
28 this._worker.onmessage = this._onRuleChunk.bind(this);
29 return this._worker;
30 },
31
32 /**
33 * @param {!Promise.<string>} contentPromise
34 */
35 _innerParseContentPromise: function(contentPromise)
36 {
37 return Promise.all([this._pendingWorkerPromise, contentPromise]).then(pr ocess.bind(this));
38
39 /**
40 * @param {!Array.<?>} results
41 * @return {!Promise.<!Array.<!WebInspector.CSSParser.Rule>>}
42 * @this {WebInspector.CSSParser}
43 */
44 function process(results)
45 {
46 return this._innerParse(/** @type {string} */ (results[1]));
47 }
48 },
49
50 /**
24 * @param {!WebInspector.CSSStyleSheetHeader} styleSheetHeader 51 * @param {!WebInspector.CSSStyleSheetHeader} styleSheetHeader
25 * @param {function(!Array.<!WebInspector.CSSParser.Rule>)=} callback 52 * @return {!Promise.<!Array.<!WebInspector.CSSParser.Rule>>}
26 */ 53 */
27 fetchAndParse: function(styleSheetHeader, callback) 54 fetchAndParse: function(styleSheetHeader)
28 { 55 {
29 this._lock(); 56 this._lock();
30 this._finishedCallback = callback; 57 return this._innerParseContentPromise(styleSheetHeader.requestContentPro mise());
pfeldman 2014/11/10 10:23:25 return styleSheetHeader.requestContent().then(this
31 styleSheetHeader.requestContent(this._innerParse.bind(this));
32 }, 58 },
33 59
34 /** 60 /**
35 * @param {string} text 61 * @param {string} text
36 * @param {function(!Array.<!WebInspector.CSSParser.Rule>)=} callback 62 * @return {!Promise.<!Array.<!WebInspector.CSSParser.Rule>>}
37 */ 63 */
38 parse: function(text, callback) 64 parse: function(text)
39 { 65 {
40 this._lock(); 66 this._lock();
41 this._finishedCallback = callback; 67 return this._innerParseContentPromise(Promise.resolve(text));
pfeldman 2014/11/10 10:23:25 return this._innerParse(text);
42 this._innerParse(text);
43 }, 68 },
44 69
45 dispose: function() 70 dispose: function()
46 { 71 {
47 if (this._worker) { 72 if (this._worker) {
48 this._worker.terminate(); 73 this._worker.terminate();
49 delete this._worker; 74 delete this._worker;
75 } else {
76 this._disposed = true;
50 } 77 }
51 }, 78 },
52 79
53 /** 80 /**
54 * @return {!Array.<!WebInspector.CSSParser.Rule>} 81 * @return {!Array.<!WebInspector.CSSParser.Rule>}
55 */ 82 */
56 rules: function() 83 rules: function()
57 { 84 {
58 return this._rules; 85 return this._rules;
59 }, 86 },
60 87
61 _lock: function() 88 _lock: function()
62 { 89 {
63 console.assert(!this._parsingStyleSheet, "Received request to parse styl esheet before previous was completed."); 90 console.assert(!this._parsingStyleSheet, "Received request to parse styl esheet before previous was completed.");
64 this._parsingStyleSheet = true; 91 this._parsingStyleSheet = true;
65 }, 92 },
66 93
67 _unlock: function() 94 _unlock: function()
68 { 95 {
69 delete this._parsingStyleSheet; 96 delete this._parsingStyleSheet;
70 }, 97 },
71 98
72 /** 99 /**
73 * @param {?string} text 100 * @param {?string} text
101 * @return {!Promise.<!Array.<!WebInspector.CSSParser.Rule>>}
74 */ 102 */
75 _innerParse: function(text) 103 _innerParse: function(text)
76 { 104 {
105 if (!this._worker) {
106 this._unlock();
pfeldman 2014/11/10 10:23:25 return new Promise(promiseBody); function promise
107 return Promise.resolve([]);
108 }
77 this._rules = []; 109 this._rules = [];
78 this._worker.postMessage({ method: "parseCSS", params: { content: text } }); 110 this._worker.postMessage({ method: "parseCSS", params: { content: text } });
111 return new Promise(rememberFulfillCallback.bind(this));
112
113 /**
114 * @this {!WebInspector.CSSParser}
115 */
116 function rememberFulfillCallback(fulfill)
117 {
118 console.assert(!this._fulfillCallback, "_innerParse() invoked during parsing");
119 this._fulfillCallback = fulfill;
pfeldman 2014/11/10 10:23:25 I am lost here.
120 }
79 }, 121 },
80 122
81 /** 123 /**
82 * @param {!MessageEvent} event 124 * @param {!MessageEvent} event
83 */ 125 */
84 _onRuleChunk: function(event) 126 _onRuleChunk: function(event)
85 { 127 {
86 var data = /** @type {!WebInspector.CSSParser.DataChunk} */ (event.data) ; 128 var data = /** @type {!WebInspector.CSSParser.DataChunk} */ (event.data) ;
87 var chunk = data.chunk; 129 var chunk = data.chunk;
88 for (var i = 0; i < chunk.length; ++i) 130 for (var i = 0; i < chunk.length; ++i)
89 this._rules.push(chunk[i]); 131 this._rules.push(chunk[i]);
90 132
91 if (data.isLastChunk) 133 if (data.isLastChunk)
92 this._onFinishedParsing(); 134 this._onFinishedParsing();
93 this.dispatchEventToListeners(WebInspector.CSSParser.Events.RulesParsed) ;
94 }, 135 },
95 136
96 _onFinishedParsing: function() 137 _onFinishedParsing: function()
97 { 138 {
98 this._unlock(); 139 this._unlock();
99 if (this._finishedCallback) 140 if (this._fulfillCallback)
100 this._finishedCallback(this._rules); 141 this._fulfillCallback(this._rules);
142 delete this._fulfillCallback;
101 }, 143 },
102 144
103 __proto__: WebInspector.Object.prototype, 145 __proto__: WebInspector.Object.prototype,
104 } 146 }
105 147
106 /** 148 /**
107 * @typedef {{isLastChunk: boolean, chunk: !Array.<!WebInspector.CSSParser.Rule> }} 149 * @typedef {{isLastChunk: boolean, chunk: !Array.<!WebInspector.CSSParser.Rule> }}
108 */ 150 */
109 WebInspector.CSSParser.DataChunk; 151 WebInspector.CSSParser.DataChunk;
110 152
111 /** 153 /**
112 * @typedef {{selectorText: string, lineNumber: number, columnNumber: number, pr operties: !Array.<!WebInspector.CSSParser.Property>}} 154 * @typedef {{selectorText: string, lineNumber: number, columnNumber: number, pr operties: !Array.<!WebInspector.CSSParser.Property>}}
113 */ 155 */
114 WebInspector.CSSParser.StyleRule; 156 WebInspector.CSSParser.StyleRule;
115 157
116 /** 158 /**
117 * @typedef {{atRule: string, lineNumber: number, columnNumber: number}} 159 * @typedef {{atRule: string, lineNumber: number, columnNumber: number}}
118 */ 160 */
119 WebInspector.CSSParser.AtRule; 161 WebInspector.CSSParser.AtRule;
120 162
121 /** 163 /**
122 * @typedef {(WebInspector.CSSParser.StyleRule|WebInspector.CSSParser.AtRule)} 164 * @typedef {(WebInspector.CSSParser.StyleRule|WebInspector.CSSParser.AtRule)}
123 */ 165 */
124 WebInspector.CSSParser.Rule; 166 WebInspector.CSSParser.Rule;
125 167
126 /** 168 /**
127 * @typedef {{name: string, value: string}} 169 * @typedef {{name: string, value: string}}
128 */ 170 */
129 WebInspector.CSSParser.Property; 171 WebInspector.CSSParser.Property;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698