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

Unified Diff: Source/devtools/front_end/sdk/CSSStyleModel.js

Issue 1196193016: DevTools: [CSS] promisify CSS domain (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: compile types 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 side-by-side diff with in-line comments
Download patch
Index: Source/devtools/front_end/sdk/CSSStyleModel.js
diff --git a/Source/devtools/front_end/sdk/CSSStyleModel.js b/Source/devtools/front_end/sdk/CSSStyleModel.js
index 0e8126684d394325f8a867722e3f5ae2782d83b2..fd483b641f7980a0e3da66ea207e03fdc9116f91 100644
--- a/Source/devtools/front_end/sdk/CSSStyleModel.js
+++ b/Source/devtools/front_end/sdk/CSSStyleModel.js
@@ -45,7 +45,7 @@ WebInspector.CSSStyleModel = function(target)
this._domModel.addEventListener(WebInspector.DOMModel.Events.UndoRedoCompleted, this._undoRedoCompleted, this);
target.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.MainFrameNavigated, this._mainFrameNavigated, this);
target.registerCSSDispatcher(new WebInspector.CSSDispatcher(this));
- this._agent.enable(this._wasEnabled.bind(this));
+ this._agent.enable().spread(this._wasEnabled.bind(this));
pfeldman 2015/06/24 16:38:12 Why spread?
lushnikov 2015/06/24 16:45:25 Oops. Accidental.
/** @type {!Map.<string, !WebInspector.CSSStyleSheetHeader>} */
this._styleSheetIdToHeader = new Map();
/** @type {!Map.<string, !Object.<!PageAgent.FrameId, !Array.<!CSSAgent.StyleSheetId>>>} */
@@ -90,16 +90,17 @@ WebInspector.CSSStyleModel.prototype = {
/**
* @param {?Protocol.Error} error
* @param {?Array.<!CSSAgent.CSSMedia>} payload
+ * @return {!Array.<!WebInspector.CSSMedia>}
* @this {!WebInspector.CSSStyleModel}
*/
- function callback(error, payload)
+ function parsePayload(error, payload)
{
- var models = [];
- if (!error && payload)
- models = WebInspector.CSSMedia.parseMediaArrayPayload(this, payload);
- userCallback(models);
+ return !error && payload ? WebInspector.CSSMedia.parseMediaArrayPayload(this, payload) : [];
}
- this._agent.getMediaQueries(callback.bind(this));
+
+ this._agent.getMediaQueries(parsePayload.bind(this))
+ .catchException([])
pfeldman 2015/06/24 16:38:12 1. It is not checking whether [] matcher the signa
lushnikov 2015/06/24 16:45:25 Thats minor, but ill fix it
+ .then(userCallback);
pfeldman 2015/06/24 16:38:12 So any error in the user callback is ignored? What
lushnikov 2015/06/24 16:45:25 It gets reported in the devtools console.
},
/**
@@ -110,8 +111,15 @@ WebInspector.CSSStyleModel.prototype = {
return this._isEnabled;
},
- _wasEnabled: function()
+ /**
+ * @param {?Protocol.Error} error
+ */
+ _wasEnabled: function(error)
{
+ if (error) {
+ console.error("Failed to enabled CSS agent: " + error);
+ return;
+ }
this._isEnabled = true;
this.dispatchEventToListeners(WebInspector.CSSStyleModel.Events.ModelWasEnabled);
},
@@ -125,56 +133,46 @@ WebInspector.CSSStyleModel.prototype = {
getMatchedStylesAsync: function(nodeId, excludePseudo, excludeInherited, userCallback)
{
/**
- * @param {function(?*)} userCallback
* @param {?Protocol.Error} error
* @param {!Array.<!CSSAgent.RuleMatch>=} matchedPayload
* @param {!Array.<!CSSAgent.PseudoIdMatches>=} pseudoPayload
* @param {!Array.<!CSSAgent.InheritedStyleEntry>=} inheritedPayload
+ * @return {?*}
* @this {WebInspector.CSSStyleModel}
*/
- function callback(userCallback, error, matchedPayload, pseudoPayload, inheritedPayload)
+ function parsePayload(error, matchedPayload, pseudoPayload, inheritedPayload)
{
- if (error) {
- if (userCallback)
- userCallback(null);
- return;
- }
-
+ if (error)
+ return null;
var result = {};
- // Due to CSSOM inconsistencies, backend might send us illegal data. @see crbug.com/178410
- try {
- result.matchedCSSRules = WebInspector.CSSStyleModel.parseRuleMatchArrayPayload(this, matchedPayload);
-
- result.pseudoElements = [];
- if (pseudoPayload) {
- for (var i = 0; i < pseudoPayload.length; ++i) {
- var entryPayload = pseudoPayload[i];
- result.pseudoElements.push({ pseudoId: entryPayload.pseudoId, rules: WebInspector.CSSStyleModel.parseRuleMatchArrayPayload(this, entryPayload.matches) });
- }
- }
+ result.matchedCSSRules = WebInspector.CSSStyleModel.parseRuleMatchArrayPayload(this, matchedPayload);
- result.inherited = [];
- if (inheritedPayload) {
- for (var i = 0; i < inheritedPayload.length; ++i) {
- var entryPayload = inheritedPayload[i];
- var entry = {};
- if (entryPayload.inlineStyle)
- entry.inlineStyle = WebInspector.CSSStyleDeclaration.parsePayload(this, entryPayload.inlineStyle);
- if (entryPayload.matchedCSSRules)
- entry.matchedCSSRules = WebInspector.CSSStyleModel.parseRuleMatchArrayPayload(this, entryPayload.matchedCSSRules);
- result.inherited.push(entry);
- }
+ result.pseudoElements = [];
+ if (pseudoPayload) {
+ for (var i = 0; i < pseudoPayload.length; ++i) {
+ var entryPayload = pseudoPayload[i];
+ result.pseudoElements.push({ pseudoId: entryPayload.pseudoId, rules: WebInspector.CSSStyleModel.parseRuleMatchArrayPayload(this, entryPayload.matches) });
}
- } catch (e) {
- console.error(e);
- result = null;
}
- if (userCallback)
- userCallback(result);
+ result.inherited = [];
+ if (inheritedPayload) {
+ for (var i = 0; i < inheritedPayload.length; ++i) {
+ var entryPayload = inheritedPayload[i];
+ var entry = {};
+ if (entryPayload.inlineStyle)
+ entry.inlineStyle = WebInspector.CSSStyleDeclaration.parsePayload(this, entryPayload.inlineStyle);
+ if (entryPayload.matchedCSSRules)
+ entry.matchedCSSRules = WebInspector.CSSStyleModel.parseRuleMatchArrayPayload(this, entryPayload.matchedCSSRules);
+ result.inherited.push(entry);
+ }
+ }
+ return result;
}
- this._agent.getMatchedStylesForNode(nodeId, excludePseudo, excludeInherited, callback.bind(this, userCallback));
+ this._agent.getMatchedStylesForNode(nodeId, excludePseudo, excludeInherited, parsePayload.bind(this))
+ .catchException(null)
+ .then(userCallback);
},
/**
@@ -192,14 +190,19 @@ WebInspector.CSSStyleModel.prototype = {
*/
getPlatformFontsForNode: function(nodeId, callback)
{
+ /**
+ * @param {?Protocol.Error} error
+ * @param {?string} cssFamilyName
+ * @param {?Array.<!CSSAgent.PlatformFontUsage>} fonts
+ * @return {!Array.<?>}
+ */
function platformFontsCallback(error, cssFamilyName, fonts)
{
- if (error)
- callback(null, null);
- else
- callback(cssFamilyName, fonts);
+ return error ? [null, null] : [cssFamilyName, fonts];
}
- this._agent.getPlatformFontsForNode(nodeId, platformFontsCallback);
+ this._agent.getPlatformFontsForNode(nodeId, platformFontsCallback)
+ .catchException([null, null])
+ .spread(callback)
},
/**
@@ -233,21 +236,27 @@ WebInspector.CSSStyleModel.prototype = {
getInlineStylesAsync: function(nodeId, userCallback)
{
/**
- * @param {function(?WebInspector.CSSStyleDeclaration, ?WebInspector.CSSStyleDeclaration)} userCallback
* @param {?Protocol.Error} error
* @param {?CSSAgent.CSSStyle=} inlinePayload
* @param {?CSSAgent.CSSStyle=} attributesStylePayload
+ * @return {!Array.<?CSSStyleDeclaration>}
* @this {WebInspector.CSSStyleModel}
*/
- function callback(userCallback, error, inlinePayload, attributesStylePayload)
+ function parsePayload(error, inlinePayload, attributesStylePayload)
{
- if (error || !inlinePayload)
- userCallback(null, null);
- else
- userCallback(WebInspector.CSSStyleDeclaration.parsePayload(this, inlinePayload), attributesStylePayload ? WebInspector.CSSStyleDeclaration.parsePayload(this, attributesStylePayload) : null);
+ var result = [null, null];
+ if (error)
+ return result;
+ if (inlinePayload)
+ result[0] = WebInspector.CSSStyleDeclaration.parsePayload(this, inlinePayload);
+ if (attributesStylePayload)
+ result[1] = WebInspector.CSSStyleDeclaration.parsePayload(this, attributesStylePayload);
+ return result;
}
- this._agent.getInlineStylesForNode(nodeId, callback.bind(this, userCallback));
+ this._agent.getInlineStylesForNode(nodeId, parsePayload.bind(this))
+ .catchException([null, null])
+ .spread(userCallback);
},
/**
@@ -281,83 +290,75 @@ WebInspector.CSSStyleModel.prototype = {
* @param {!CSSAgent.CSSRule} rule
* @param {!DOMAgent.NodeId} nodeId
* @param {string} newSelector
- * @param {function(!WebInspector.CSSRule)} successCallback
- * @param {function()} failureCallback
+ * @param {function(?WebInspector.CSSRule)} userCallback
*/
- setRuleSelector: function(rule, nodeId, newSelector, successCallback, failureCallback)
+ setRuleSelector: function(rule, nodeId, newSelector, userCallback)
{
/**
- * @param {!DOMAgent.NodeId} nodeId
- * @param {function(!WebInspector.CSSRule)} successCallback
- * @param {function()} failureCallback
* @param {?Protocol.Error} error
- * @param {string} newSelector
- * @param {!CSSAgent.CSSRule} rulePayload
+ * @param {?CSSAgent.CSSRule} rulePayload
+ * @return {?CSSAgent.CSSRule}
* @this {WebInspector.CSSStyleModel}
*/
- function callback(nodeId, successCallback, failureCallback, newSelector, error, rulePayload)
+ function callback(error, rulePayload)
{
this._pendingCommandsMajorState.pop();
- if (error) {
- failureCallback();
- return;
- }
+ if (error || !rulePayload)
+ return null;
this._domModel.markUndoableState();
- this._computeMatchingSelectors(rulePayload, nodeId, successCallback, failureCallback);
+ return rulePayload;
}
if (!rule.styleSheetId)
throw "No rule stylesheet id";
WebInspector.userMetrics.StyleRuleEdited.record();
this._pendingCommandsMajorState.push(true);
- this._agent.setRuleSelector(rule.styleSheetId, rule.selectorRange, newSelector, callback.bind(this, nodeId, successCallback, failureCallback, newSelector));
+ this._agent.setRuleSelector(rule.styleSheetId, rule.selectorRange, newSelector, callback.bind(this))
+ .then(this._computeMatchingSelectors.bind(this, nodeId))
+ .catchException(null)
+ .then(userCallback);
},
/**
* @param {!WebInspector.CSSMedia} media
* @param {string} newMediaText
- * @param {function(!WebInspector.CSSMedia)} successCallback
- * @param {function()} failureCallback
+ * @param {function(?WebInspector.CSSMedia)} userCallback
*/
- setMediaText: function(media, newMediaText, successCallback, failureCallback)
+ setMediaText: function(media, newMediaText, userCallback)
{
/**
- * @param {function(!WebInspector.CSSMedia)} successCallback
- * @param {function()} failureCallback
* @param {?Protocol.Error} error
* @param {!CSSAgent.CSSMedia} mediaPayload
+ * @return {?WebInspector.CSSMedia}
* @this {WebInspector.CSSStyleModel}
*/
- function callback(successCallback, failureCallback, error, mediaPayload)
+ function parsePayload(error, mediaPayload)
{
this._pendingCommandsMajorState.pop();
- if (error) {
- failureCallback();
- return;
- }
+ if (!mediaPayload)
+ return null;
this._domModel.markUndoableState();
- successCallback(WebInspector.CSSMedia.parsePayload(this, mediaPayload));
+ return WebInspector.CSSMedia.parsePayload(this, mediaPayload);
}
console.assert(!!media.parentStyleSheetId);
WebInspector.userMetrics.StyleRuleEdited.record();
this._pendingCommandsMajorState.push(true);
- this._agent.setMediaText(media.parentStyleSheetId, media.range, newMediaText, callback.bind(this, successCallback, failureCallback));
+ this._agent.setMediaText(media.parentStyleSheetId, media.range, newMediaText, parsePayload.bind(this))
+ .catchException(null)
+ .then(userCallback);
},
/**
- * @param {!CSSAgent.CSSRule} rulePayload
* @param {!DOMAgent.NodeId} nodeId
- * @param {function(!WebInspector.CSSRule)} successCallback
- * @param {function()} failureCallback
+ * @param {?CSSAgent.CSSRule} rulePayload
+ * @return {!Promise.<?WebInspector.CSSRule>}
*/
- _computeMatchingSelectors: function(rulePayload, nodeId, successCallback, failureCallback)
+ _computeMatchingSelectors: function(nodeId, rulePayload)
{
var ownerDocumentId = this._ownerDocumentId(nodeId);
- if (!ownerDocumentId) {
- failureCallback();
- return;
- }
+ if (!ownerDocumentId || !rulePayload)
+ return Promise.resolve(/** @type {?WebInspector.CSSRule} */(null));
var rule = WebInspector.CSSRule.parsePayload(this, rulePayload);
var matchingSelectors = [];
var allSelectorsBarrier = new CallbackBarrier();
@@ -366,9 +367,11 @@ WebInspector.CSSStyleModel.prototype = {
var boundCallback = allSelectorsBarrier.createCallback(selectorQueried.bind(null, i, nodeId, matchingSelectors));
this._domModel.querySelectorAll(ownerDocumentId, selector.value, boundCallback);
}
- allSelectorsBarrier.callWhenDone(function() {
- rule.matchingSelectors = matchingSelectors;
- successCallback(rule);
+ return new Promise(function(resolve) {
+ allSelectorsBarrier.callWhenDone(function() {
+ rule.matchingSelectors = matchingSelectors;
+ resolve(rule);
+ });
});
/**
@@ -391,64 +394,62 @@ WebInspector.CSSStyleModel.prototype = {
* @param {!WebInspector.DOMNode} node
* @param {string} ruleText
* @param {!WebInspector.TextRange} ruleLocation
- * @param {function(!WebInspector.CSSRule)} successCallback
- * @param {function()} failureCallback
+ * @param {function(?WebInspector.CSSRule)} userCallback
*/
- addRule: function(styleSheetId, node, ruleText, ruleLocation, successCallback, failureCallback)
+ addRule: function(styleSheetId, node, ruleText, ruleLocation, userCallback)
{
this._pendingCommandsMajorState.push(true);
- this._agent.addRule(styleSheetId, ruleText, ruleLocation, callback.bind(this));
+ this._agent.addRule(styleSheetId, ruleText, ruleLocation, parsePayload.bind(this))
+ .then(this._computeMatchingSelectors.bind(this, node.id))
+ .catchException(null)
+ .then(userCallback);
/**
* @param {?Protocol.Error} error
- * @param {!CSSAgent.CSSRule} rulePayload
+ * @param {?CSSAgent.CSSRule} rulePayload
+ * @return {?CSSAgent.CSSRule}
* @this {WebInspector.CSSStyleModel}
*/
- function callback(error, rulePayload)
+ function parsePayload(error, rulePayload)
{
this._pendingCommandsMajorState.pop();
- if (error) {
- // Invalid syntax for a selector
- failureCallback();
- } else {
- this._domModel.markUndoableState();
- this._computeMatchingSelectors(rulePayload, node.id, successCallback, failureCallback);
- }
+ if (error || !rulePayload)
+ return null;
+ this._domModel.markUndoableState();
+ return rulePayload;
}
},
/**
* @param {!WebInspector.DOMNode} node
- * @param {function(?WebInspector.CSSStyleSheetHeader)} callback
+ * @param {function(?WebInspector.CSSStyleSheetHeader)} userCallback
*/
- requestViaInspectorStylesheet: function(node, callback)
+ requestViaInspectorStylesheet: function(node, userCallback)
{
var frameId = node.frameId() || this.target().resourceTreeModel.mainFrame.id;
var headers = this._styleSheetIdToHeader.valuesArray();
for (var i = 0; i < headers.length; ++i) {
var styleSheetHeader = headers[i];
if (styleSheetHeader.frameId === frameId && styleSheetHeader.isViaInspector()) {
- callback(styleSheetHeader);
+ userCallback(styleSheetHeader);
return;
}
}
/**
- * @this {WebInspector.CSSStyleModel}
* @param {?Protocol.Error} error
- * @param {!CSSAgent.StyleSheetId} styleSheetId
+ * @param {?CSSAgent.StyleSheetId} styleSheetId
+ * @return {?WebInspector.CSSStyleSheetHeader}
+ * @this {WebInspector.CSSStyleModel}
*/
function innerCallback(error, styleSheetId)
{
- if (error) {
- console.error(error);
- callback(null);
- }
-
- callback(this._styleSheetIdToHeader.get(styleSheetId) || null);
+ return !error && styleSheetId ? this._styleSheetIdToHeader.get(styleSheetId) || null : null;
}
- this._agent.createStyleSheet(frameId, innerCallback.bind(this));
+ this._agent.createStyleSheet(frameId, innerCallback.bind(this))
+ .catchException(null)
+ .then(userCallback)
},
mediaQueryResultChanged: function()
@@ -619,7 +620,7 @@ WebInspector.CSSStyleModel.prototype = {
this._isEnabled = false;
} else {
this._resetStyleSheets();
- this._agent.enable(this._wasEnabled.bind(this));
+ this._agent.enable().spread(this._wasEnabled.bind(this));
}
},
@@ -904,34 +905,36 @@ WebInspector.CSSStyleDeclaration.prototype = {
/**
* @param {string} text
* @param {boolean} majorChange
- * @param {function(?WebInspector.CSSStyleDeclaration)} callback
+ * @param {function(?WebInspector.CSSStyleDeclaration)} userCallback
*/
- setText: function(text, majorChange, callback)
+ setText: function(text, majorChange, userCallback)
{
if (!this.styleSheetId) {
- callback(null);
+ userCallback(null);
return;
}
/**
* @param {?Protocol.Error} error
- * @param {!CSSAgent.CSSStyle} stylePayload
+ * @param {?CSSAgent.CSSStyle} stylePayload
+ * @return {?WebInspector.CSSStyleDeclaration}
* @this {WebInspector.CSSStyleDeclaration}
*/
- function mycallback(error, stylePayload)
+ function parsePayload(error, stylePayload)
{
this._cssModel._pendingCommandsMajorState.pop();
- if (!error) {
- if (majorChange)
- this._cssModel._domModel.markUndoableState();
- callback(WebInspector.CSSStyleDeclaration.parsePayload(this._cssModel, stylePayload));
- return;
- }
- callback(null);
+ if (error || !stylePayload)
+ return null;
+
+ if (majorChange)
+ this._cssModel._domModel.markUndoableState();
+ return WebInspector.CSSStyleDeclaration.parsePayload(this._cssModel, stylePayload);
}
this._cssModel._pendingCommandsMajorState.push(majorChange);
- this._cssModel._agent.setStyleText(this.styleSheetId, this.range.serializeToObject(), text, mycallback.bind(this));
+ this._cssModel._agent.setStyleText(this.styleSheetId, this.range.serializeToObject(), text, parsePayload.bind(this))
+ .catchException(null)
+ .then(userCallback)
},
/**
@@ -1771,24 +1774,28 @@ WebInspector.CSSStyleSheetHeader.prototype = {
/**
* @override
- * @param {function(string)} callback
+ * @param {function(string)} userCallback
*/
- requestContent: function(callback)
+ requestContent: function(userCallback)
{
- this._cssModel._agent.getStyleSheetText(this.id, textCallback.bind(this));
+ this._cssModel._agent.getStyleSheetText(this.id, textCallback.bind(this))
+ .catchException("")
+ .then(userCallback)
/**
+ * @param {?Protocol.Error} error
+ * @param {?string} text
+ * @return {string}
* @this {WebInspector.CSSStyleSheetHeader}
*/
function textCallback(error, text)
{
- if (error) {
- WebInspector.console.error("Failed to get text for stylesheet " + this.id + ": " + error);
+ if (error || !text) {
+ WebInspector.console.error("Failed to get text for stylesheet " + this.id + ": " + error)
text = "";
// Fall through.
}
- text = this._trimSourceURL(text);
- callback(text);
+ return this._trimSourceURL(text);
}
},
@@ -1815,7 +1822,8 @@ WebInspector.CSSStyleSheetHeader.prototype = {
newText = this._trimSourceURL(newText);
if (this.hasSourceURL)
newText += "\n/*# sourceURL=" + this.sourceURL + " */";
- this._cssModel._agent.setStyleSheetText(this.id, newText, callback);
+ this._cssModel._agent.setStyleSheetText(this.id, newText)
+ .spread(callback);
},
/**
@@ -1899,17 +1907,28 @@ WebInspector.CSSStyleModel.ComputedStyleLoader.prototype = {
this._nodeIdToCallbackData[nodeId] = [userCallback];
- this._cssModel._agent.getComputedStyleForNode(nodeId, resultCallback.bind(this, nodeId));
+ this._cssModel._agent.getComputedStyleForNode(nodeId, parsePayload.bind(this))
+ .catchException(null)
+ .then(broadcast.bind(this, nodeId))
/**
- * @param {!DOMAgent.NodeId} nodeId
* @param {?Protocol.Error} error
* @param {!Array.<!CSSAgent.CSSComputedStyleProperty>} computedPayload
+ * @return {?WebInspector.CSSStyleDeclaration}
+ * @this {WebInspector.CSSStyleModel.ComputedStyleLoader}
+ */
+ function parsePayload(error, computedPayload)
+ {
+ return !error && computedPayload ? WebInspector.CSSStyleDeclaration.parseComputedStylePayload(this._cssModel, computedPayload) : null;
+ }
+
+ /**
+ * @param {!DOMAgent.NodeId} nodeId
+ * @param {?WebInspector.CSSStyleDeclaration} computedStyle
* @this {WebInspector.CSSStyleModel.ComputedStyleLoader}
*/
- function resultCallback(nodeId, error, computedPayload)
+ function broadcast(nodeId, computedStyle)
{
- var computedStyle = (error || !computedPayload) ? null : WebInspector.CSSStyleDeclaration.parseComputedStylePayload(this._cssModel, computedPayload);
var callbacks = this._nodeIdToCallbackData[nodeId];
// The loader has been reset.

Powered by Google App Engine
This is Rietveld 408576698