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

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

Issue 1204393002: DevTools: [CSS] promisify CSSStyleModel fetching methods (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 6 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) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 PseudoStateForced: "PseudoStateForced", 76 PseudoStateForced: "PseudoStateForced",
77 StyleSheetAdded: "StyleSheetAdded", 77 StyleSheetAdded: "StyleSheetAdded",
78 StyleSheetChanged: "StyleSheetChanged", 78 StyleSheetChanged: "StyleSheetChanged",
79 StyleSheetRemoved: "StyleSheetRemoved" 79 StyleSheetRemoved: "StyleSheetRemoved"
80 } 80 }
81 81
82 WebInspector.CSSStyleModel.MediaTypes = ["all", "braille", "embossed", "handheld ", "print", "projection", "screen", "speech", "tty", "tv"]; 82 WebInspector.CSSStyleModel.MediaTypes = ["all", "braille", "embossed", "handheld ", "print", "projection", "screen", "speech", "tty", "tv"];
83 83
84 WebInspector.CSSStyleModel.prototype = { 84 WebInspector.CSSStyleModel.prototype = {
85 /** 85 /**
86 * @param {function(!Array.<!WebInspector.CSSMedia>)} userCallback 86 * @return {!Promise.<!Array.<!WebInspector.CSSMedia>>}
87 */ 87 */
88 getMediaQueries: function(userCallback) 88 mediaQueriesPromise: function()
89 { 89 {
90 /** 90 /**
91 * @param {?Protocol.Error} error 91 * @param {?Protocol.Error} error
92 * @param {?Array.<!CSSAgent.CSSMedia>} payload 92 * @param {?Array.<!CSSAgent.CSSMedia>} payload
93 * @return {!Array.<!WebInspector.CSSMedia>} 93 * @return {!Array.<!WebInspector.CSSMedia>}
94 * @this {!WebInspector.CSSStyleModel} 94 * @this {!WebInspector.CSSStyleModel}
95 */ 95 */
96 function parsePayload(error, payload) 96 function parsePayload(error, payload)
97 { 97 {
98 return !error && payload ? WebInspector.CSSMedia.parseMediaArrayPayl oad(this, payload) : []; 98 return !error && payload ? WebInspector.CSSMedia.parseMediaArrayPayl oad(this, payload) : [];
99 } 99 }
100 100
101 this._agent.getMediaQueries(parsePayload.bind(this)) 101 return this._agent.getMediaQueries(parsePayload.bind(this));
102 .catchException([])
103 .then(userCallback);
104 }, 102 },
105 103
106 /** 104 /**
107 * @return {boolean} 105 * @return {boolean}
108 */ 106 */
109 isEnabled: function() 107 isEnabled: function()
110 { 108 {
111 return this._isEnabled; 109 return this._isEnabled;
112 }, 110 },
113 111
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 } 163 }
166 } 164 }
167 return new WebInspector.CSSStyleModel.MatchedStyleResult(matchedRule s, inherited, pseudoElements); 165 return new WebInspector.CSSStyleModel.MatchedStyleResult(matchedRule s, inherited, pseudoElements);
168 } 166 }
169 167
170 return this._agent.getMatchedStylesForNode(nodeId, excludePseudo, exclud eInherited, callback.bind(this)); 168 return this._agent.getMatchedStylesForNode(nodeId, excludePseudo, exclud eInherited, callback.bind(this));
171 }, 169 },
172 170
173 /** 171 /**
174 * @param {!DOMAgent.NodeId} nodeId 172 * @param {!DOMAgent.NodeId} nodeId
175 * @param {boolean} excludePseudo 173 * @return {!Promise.<?WebInspector.CSSStyleDeclaration>}
176 * @param {boolean} excludeInherited
177 * @param {function(?WebInspector.CSSStyleModel.MatchedStyleResult)} userCal lback
178 */ 174 */
179 getMatchedStylesAsync: function(nodeId, excludePseudo, excludeInherited, use rCallback) 175 computedStylePromise: function(nodeId)
180 { 176 {
181 this.matchedStylesPromise(nodeId, excludePseudo, excludeInherited) 177 return this._styleLoader.computedStylePromise(nodeId);
182 .catchException(null)
183 .then(userCallback);
184 },
185
186 /**
187 * @param {!DOMAgent.NodeId} nodeId
188 * @param {function(?WebInspector.CSSStyleDeclaration)} userCallback
189 */
190 getComputedStyleAsync: function(nodeId, userCallback)
191 {
192 this._styleLoader.getComputedStyle(nodeId, userCallback);
193 }, 178 },
194 179
195 /** 180 /**
196 * @param {number} nodeId 181 * @param {number} nodeId
197 * @param {function(?Array.<!CSSAgent.PlatformFontUsage>)} callback 182 * @return {!Promise.<?Array.<!CSSAgent.PlatformFontUsage>>}
198 */ 183 */
199 getPlatformFontsForNode: function(nodeId, callback) 184 platformFontsPromise: function(nodeId)
200 { 185 {
201 /** 186 /**
202 * @param {?Protocol.Error} error 187 * @param {?Protocol.Error} error
203 * @param {?Array.<!CSSAgent.PlatformFontUsage>} fonts 188 * @param {?Array.<!CSSAgent.PlatformFontUsage>} fonts
204 * @return {?Array.<!CSSAgent.PlatformFontUsage>} 189 * @return {?Array.<!CSSAgent.PlatformFontUsage>}
205 */ 190 */
206 function platformFontsCallback(error, fonts) 191 function platformFontsCallback(error, fonts)
207 { 192 {
208 return !error && fonts ? fonts : null; 193 return !error && fonts ? fonts : null;
209 } 194 }
210 195
211 this._agent.getPlatformFontsForNode(nodeId, platformFontsCallback) 196 return this._agent.getPlatformFontsForNode(nodeId, platformFontsCallback );
212 .catchException(null)
213 .then(callback)
214 }, 197 },
215 198
216 /** 199 /**
217 * @return {!Array.<!WebInspector.CSSStyleSheetHeader>} 200 * @return {!Array.<!WebInspector.CSSStyleSheetHeader>}
218 */ 201 */
219 allStyleSheets: function() 202 allStyleSheets: function()
220 { 203 {
221 var values = this._styleSheetIdToHeader.valuesArray(); 204 var values = this._styleSheetIdToHeader.valuesArray();
222 /** 205 /**
223 * @param {!WebInspector.CSSStyleSheetHeader} a 206 * @param {!WebInspector.CSSStyleSheetHeader} a
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 return null; 239 return null;
257 var inlineStyle = inlinePayload ? WebInspector.CSSStyleDeclaration.p arsePayload(this, inlinePayload) : null; 240 var inlineStyle = inlinePayload ? WebInspector.CSSStyleDeclaration.p arsePayload(this, inlinePayload) : null;
258 var attributesStyle = attributesStylePayload ? WebInspector.CSSStyle Declaration.parsePayload(this, attributesStylePayload) : null; 241 var attributesStyle = attributesStylePayload ? WebInspector.CSSStyle Declaration.parsePayload(this, attributesStylePayload) : null;
259 return new WebInspector.CSSStyleModel.InlineStyleResult(inlineStyle, attributesStyle); 242 return new WebInspector.CSSStyleModel.InlineStyleResult(inlineStyle, attributesStyle);
260 } 243 }
261 244
262 return this._agent.getInlineStylesForNode(nodeId, callback.bind(this)); 245 return this._agent.getInlineStylesForNode(nodeId, callback.bind(this));
263 }, 246 },
264 247
265 /** 248 /**
266 * @param {!DOMAgent.NodeId} nodeId
267 * @param {function(?WebInspector.CSSStyleModel.InlineStyleResult)} userCall back
268 */
269 getInlineStylesAsync: function(nodeId, userCallback)
270 {
271 this.inlineStylesPromise(nodeId)
272 .catchException(null)
273 .then(userCallback);
274 },
275
276 /**
277 * @param {!WebInspector.DOMNode} node 249 * @param {!WebInspector.DOMNode} node
278 * @param {string} pseudoClass 250 * @param {string} pseudoClass
279 * @param {boolean} enable 251 * @param {boolean} enable
280 * @return {boolean} 252 * @return {boolean}
281 */ 253 */
282 forcePseudoState: function(node, pseudoClass, enable) 254 forcePseudoState: function(node, pseudoClass, enable)
283 { 255 {
284 var pseudoClasses = node.getUserProperty(WebInspector.CSSStyleModel.Pseu doStatePropertyName) || []; 256 var pseudoClasses = node.getUserProperty(WebInspector.CSSStyleModel.Pseu doStatePropertyName) || [];
285 if (enable) { 257 if (enable) {
286 if (pseudoClasses.indexOf(pseudoClass) >= 0) 258 if (pseudoClasses.indexOf(pseudoClass) >= 0)
(...skipping 1623 matching lines...) Expand 10 before | Expand all | Expand 10 after
1910 }, 1882 },
1911 } 1883 }
1912 1884
1913 /** 1885 /**
1914 * @constructor 1886 * @constructor
1915 * @param {!WebInspector.CSSStyleModel} cssModel 1887 * @param {!WebInspector.CSSStyleModel} cssModel
1916 */ 1888 */
1917 WebInspector.CSSStyleModel.ComputedStyleLoader = function(cssModel) 1889 WebInspector.CSSStyleModel.ComputedStyleLoader = function(cssModel)
1918 { 1890 {
1919 this._cssModel = cssModel; 1891 this._cssModel = cssModel;
1920 /** @type {!Object.<*, !Array.<function(?WebInspector.CSSStyleDeclaration)>> } */ 1892 /** @type {!Map.<!DOMAgent.NodeId, !Promise.<?WebInspector.CSSStyleDeclarati on>>} */
1921 this._nodeIdToCallbackData = {}; 1893 this._nodeIdToPromise = new Map();
1922 } 1894 }
1923 1895
1924 WebInspector.CSSStyleModel.ComputedStyleLoader.prototype = { 1896 WebInspector.CSSStyleModel.ComputedStyleLoader.prototype = {
1925 /** 1897 /**
1926 * @param {!DOMAgent.NodeId} nodeId 1898 * @param {!DOMAgent.NodeId} nodeId
1927 * @param {function(?WebInspector.CSSStyleDeclaration)} userCallback 1899 * @return {!Promise.<?WebInspector.CSSStyleDeclaration>}
1928 */ 1900 */
1929 getComputedStyle: function(nodeId, userCallback) 1901 computedStylePromise: function(nodeId)
1930 { 1902 {
1931 if (this._nodeIdToCallbackData[nodeId]) { 1903 if (!this._nodeIdToPromise[nodeId])
1932 this._nodeIdToCallbackData[nodeId].push(userCallback); 1904 this._nodeIdToPromise[nodeId] = this._cssModel._agent.getComputedSty leForNode(nodeId, parsePayload.bind(this)).then(cleanUp.bind(this));
1933 return;
1934 }
1935 1905
1936 this._nodeIdToCallbackData[nodeId] = [userCallback]; 1906 return this._nodeIdToPromise[nodeId];
1937
1938 this._cssModel._agent.getComputedStyleForNode(nodeId, parsePayload.bind( this))
1939 .catchException(null)
1940 .then(broadcast.bind(this, nodeId))
1941 1907
1942 /** 1908 /**
1943 * @param {?Protocol.Error} error 1909 * @param {?Protocol.Error} error
1944 * @param {!Array.<!CSSAgent.CSSComputedStyleProperty>} computedPayload 1910 * @param {!Array.<!CSSAgent.CSSComputedStyleProperty>} computedPayload
1945 * @return {?WebInspector.CSSStyleDeclaration} 1911 * @return {?WebInspector.CSSStyleDeclaration}
1946 * @this {WebInspector.CSSStyleModel.ComputedStyleLoader} 1912 * @this {WebInspector.CSSStyleModel.ComputedStyleLoader}
1947 */ 1913 */
1948 function parsePayload(error, computedPayload) 1914 function parsePayload(error, computedPayload)
1949 { 1915 {
1950 return !error && computedPayload ? WebInspector.CSSStyleDeclaration. parseComputedStylePayload(this._cssModel, computedPayload) : null; 1916 return !error && computedPayload ? WebInspector.CSSStyleDeclaration. parseComputedStylePayload(this._cssModel, computedPayload) : null;
1951 } 1917 }
1952 1918
1953 /** 1919 /**
1954 * @param {!DOMAgent.NodeId} nodeId
1955 * @param {?WebInspector.CSSStyleDeclaration} computedStyle 1920 * @param {?WebInspector.CSSStyleDeclaration} computedStyle
1921 * @return {?WebInspector.CSSStyleDeclaration}
1956 * @this {WebInspector.CSSStyleModel.ComputedStyleLoader} 1922 * @this {WebInspector.CSSStyleModel.ComputedStyleLoader}
1957 */ 1923 */
1958 function broadcast(nodeId, computedStyle) 1924 function cleanUp(computedStyle)
1959 { 1925 {
1960 var callbacks = this._nodeIdToCallbackData[nodeId]; 1926 delete this._nodeIdToPromise[nodeId];
1961 1927 return computedStyle;
1962 // The loader has been reset.
1963 if (!callbacks)
1964 return;
1965
1966 delete this._nodeIdToCallbackData[nodeId];
1967 for (var i = 0; i < callbacks.length; ++i)
1968 callbacks[i](computedStyle);
1969 } 1928 }
1970 } 1929 }
1971 } 1930 }
1972 1931
1973 /** 1932 /**
1974 * @param {!WebInspector.Target} target 1933 * @param {!WebInspector.Target} target
1975 * @return {?WebInspector.CSSStyleModel} 1934 * @return {?WebInspector.CSSStyleModel}
1976 */ 1935 */
1977 WebInspector.CSSStyleModel.fromTarget = function(target) 1936 WebInspector.CSSStyleModel.fromTarget = function(target)
1978 { 1937 {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
2029 * @constructor 1988 * @constructor
2030 * @param {?WebInspector.CSSStyleDeclaration} inlineStyle 1989 * @param {?WebInspector.CSSStyleDeclaration} inlineStyle
2031 * @param {?WebInspector.CSSStyleDeclaration} attributesStyle 1990 * @param {?WebInspector.CSSStyleDeclaration} attributesStyle
2032 */ 1991 */
2033 WebInspector.CSSStyleModel.InlineStyleResult = function(inlineStyle, attributesS tyle) 1992 WebInspector.CSSStyleModel.InlineStyleResult = function(inlineStyle, attributesS tyle)
2034 { 1993 {
2035 this.inlineStyle = inlineStyle; 1994 this.inlineStyle = inlineStyle;
2036 this.attributesStyle = attributesStyle; 1995 this.attributesStyle = attributesStyle;
2037 } 1996 }
2038 1997
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698