ExtensionAuditCategory.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (C) 2012 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @constructor
  32. * @extends {WebInspector.AuditCategory}
  33. * @param {string} extensionOrigin
  34. * @param {string} id
  35. * @param {string} displayName
  36. * @param {number=} ruleCount
  37. */
  38. WebInspector.ExtensionAuditCategory = function(extensionOrigin, id, displayName, ruleCount)
  39. {
  40. this._extensionOrigin = extensionOrigin;
  41. this._id = id;
  42. this._displayName = displayName;
  43. this._ruleCount = ruleCount;
  44. }
  45. WebInspector.ExtensionAuditCategory.prototype = {
  46. // AuditCategory interface
  47. get id()
  48. {
  49. return this._id;
  50. },
  51. get displayName()
  52. {
  53. return this._displayName;
  54. },
  55. /**
  56. * @param {Array.<WebInspector.NetworkRequest>} requests
  57. * @param {function(WebInspector.AuditRuleResult)} ruleResultCallback
  58. * @param {function()} categoryDoneCallback
  59. * @param {WebInspector.Progress} progress
  60. */
  61. run: function(requests, ruleResultCallback, categoryDoneCallback, progress)
  62. {
  63. var results = new WebInspector.ExtensionAuditCategoryResults(this, ruleResultCallback, categoryDoneCallback, progress);
  64. WebInspector.extensionServer.startAuditRun(this, results);
  65. }
  66. }
  67. /**
  68. * @constructor
  69. * @param {WebInspector.ExtensionAuditCategory} category
  70. * @param {function(WebInspector.AuditRuleResult)} ruleResultCallback
  71. * @param {function()} categoryDoneCallback
  72. * @param {WebInspector.Progress} progress
  73. */
  74. WebInspector.ExtensionAuditCategoryResults = function(category, ruleResultCallback, categoryDoneCallback, progress)
  75. {
  76. this._category = category;
  77. this._ruleResultCallback = ruleResultCallback;
  78. this._categoryDoneCallback = categoryDoneCallback;
  79. this._progress = progress;
  80. this._progress.setTotalWork(1);
  81. this._expectedResults = category._ruleCount;
  82. this._actualResults = 0;
  83. this.id = category.id + "-" + ++WebInspector.ExtensionAuditCategoryResults._lastId;
  84. }
  85. WebInspector.ExtensionAuditCategoryResults.prototype = {
  86. done: function()
  87. {
  88. WebInspector.extensionServer.stopAuditRun(this);
  89. this._progress.done();
  90. this._categoryDoneCallback();
  91. },
  92. addResult: function(displayName, description, severity, details)
  93. {
  94. var result = new WebInspector.AuditRuleResult(displayName);
  95. result.addChild(description);
  96. result.severity = severity;
  97. if (details)
  98. this._addNode(result, details);
  99. this._addResult(result);
  100. },
  101. _addNode: function(parent, node)
  102. {
  103. var contents = WebInspector.auditFormatters.partiallyApply(WebInspector.ExtensionAuditFormatters, this, node.contents);
  104. var addedNode = parent.addChild(contents, node.expanded);
  105. if (node.children) {
  106. for (var i = 0; i < node.children.length; ++i)
  107. this._addNode(addedNode, node.children[i]);
  108. }
  109. },
  110. _addResult: function(result)
  111. {
  112. this._ruleResultCallback(result);
  113. ++this._actualResults;
  114. if (typeof this._expectedResults === "number") {
  115. this._progress.setWorked(this._actualResults / this._expectedResults);
  116. if (this._actualResults === this._expectedResults)
  117. this.done();
  118. }
  119. },
  120. /**
  121. * @param {number} progress
  122. */
  123. updateProgress: function(progress)
  124. {
  125. this._progress.setWorked(progress);
  126. },
  127. /**
  128. * @param {string} expression
  129. * @param {function(WebInspector.RemoteObject)} callback
  130. */
  131. evaluate: function(expression, evaluateOptions, callback)
  132. {
  133. /**
  134. * @param {?string} error
  135. * @param {?RuntimeAgent.RemoteObject} result
  136. * @param {boolean=} wasThrown
  137. */
  138. function onEvaluate(error, result, wasThrown)
  139. {
  140. if (wasThrown)
  141. return;
  142. var object = WebInspector.RemoteObject.fromPayload(result);
  143. callback(object);
  144. }
  145. WebInspector.extensionServer.evaluate(expression, false, false, evaluateOptions, this._category._extensionOrigin, onEvaluate);
  146. }
  147. }
  148. WebInspector.ExtensionAuditFormatters = {
  149. /**
  150. * @this {WebInspector.ExtensionAuditCategoryResults}
  151. * @param {string} expression
  152. * @param {string} title
  153. * @param {Object} evaluateOptions
  154. */
  155. object: function(expression, title, evaluateOptions)
  156. {
  157. var parentElement = document.createElement("div");
  158. function onEvaluate(remoteObject)
  159. {
  160. var section = new WebInspector.ObjectPropertiesSection(remoteObject, title);
  161. section.expanded = true;
  162. section.editable = false;
  163. parentElement.appendChild(section.element);
  164. }
  165. this.evaluate(expression, evaluateOptions, onEvaluate);
  166. return parentElement;
  167. },
  168. /**
  169. * @this {WebInspector.ExtensionAuditCategoryResults}
  170. * @param {string} expression
  171. * @param {Object} evaluateOptions
  172. */
  173. node: function(expression, evaluateOptions)
  174. {
  175. var parentElement = document.createElement("div");
  176. /**
  177. * @param {?number} nodeId
  178. */
  179. function onNodeAvailable(nodeId)
  180. {
  181. if (!nodeId)
  182. return;
  183. var treeOutline = new WebInspector.ElementsTreeOutline(false, false);
  184. treeOutline.rootDOMNode = WebInspector.domAgent.nodeForId(nodeId);
  185. treeOutline.element.addStyleClass("outline-disclosure");
  186. treeOutline.setVisible(true);
  187. parentElement.appendChild(treeOutline.element);
  188. }
  189. /**
  190. * @param {WebInspector.RemoteObject} remoteObject
  191. */
  192. function onEvaluate(remoteObject)
  193. {
  194. remoteObject.pushNodeToFrontend(onNodeAvailable);
  195. }
  196. this.evaluate(expression, evaluateOptions, onEvaluate);
  197. return parentElement;
  198. }
  199. }
  200. WebInspector.ExtensionAuditCategoryResults._lastId = 0;