AuditController.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2012 Google Inc. All rights reserved.
  3. * Copyright (C) 2013 Samsung Electronics. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Google Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /**
  32. * @constructor
  33. * @param {WebInspector.AuditsPanel} auditsPanel
  34. */
  35. WebInspector.AuditController = function(auditsPanel)
  36. {
  37. this._auditsPanel = auditsPanel;
  38. WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.Load, this._didMainResourceLoad, this);
  39. }
  40. WebInspector.AuditController.prototype = {
  41. /**
  42. * @param {!Array.<!WebInspector.AuditCategory>} categories
  43. * @param {function(string, !Array.<!WebInspector.AuditCategoryResult>)} resultCallback
  44. */
  45. _executeAudit: function(categories, resultCallback)
  46. {
  47. this._progress.setTitle(WebInspector.UIString("Running audit"));
  48. function ruleResultReadyCallback(categoryResult, ruleResult)
  49. {
  50. if (ruleResult && ruleResult.children)
  51. categoryResult.addRuleResult(ruleResult);
  52. if (this._progress.isCanceled())
  53. this._progress.done();
  54. }
  55. var results = [];
  56. var mainResourceURL = WebInspector.inspectedPageURL;
  57. var categoriesDone = 0;
  58. function categoryDoneCallback()
  59. {
  60. if (++categoriesDone !== categories.length)
  61. return;
  62. this._progress.done();
  63. resultCallback(mainResourceURL, results)
  64. }
  65. var requests = WebInspector.networkLog.requests.slice();
  66. var compositeProgress = new WebInspector.CompositeProgress(this._progress);
  67. var subprogresses = [];
  68. for (var i = 0; i < categories.length; ++i)
  69. subprogresses.push(compositeProgress.createSubProgress());
  70. for (var i = 0; i < categories.length; ++i) {
  71. var category = categories[i];
  72. var result = new WebInspector.AuditCategoryResult(category);
  73. results.push(result);
  74. category.run(requests, ruleResultReadyCallback.bind(this, result), categoryDoneCallback.bind(this), subprogresses[i]);
  75. }
  76. },
  77. /**
  78. * @param {function()} launcherCallback
  79. * @param {string} mainResourceURL
  80. * @param {!Array.<!WebInspector.AuditCategoryResult>} results
  81. */
  82. _auditFinishedCallback: function(launcherCallback, mainResourceURL, results)
  83. {
  84. this._auditsPanel.auditFinishedCallback(mainResourceURL, results);
  85. if (!this._progress.isCanceled())
  86. launcherCallback();
  87. },
  88. /**
  89. * @param {Array.<string>} categoryIds
  90. * @param {WebInspector.Progress} progress
  91. * @param {boolean} runImmediately
  92. * @param {function()} startedCallback
  93. * @param {function()} finishedCallback
  94. */
  95. initiateAudit: function(categoryIds, progress, runImmediately, startedCallback, finishedCallback)
  96. {
  97. if (!categoryIds || !categoryIds.length)
  98. return;
  99. this._progress = progress;
  100. var categories = [];
  101. for (var i = 0; i < categoryIds.length; ++i)
  102. categories.push(this._auditsPanel.categoriesById[categoryIds[i]]);
  103. function startAuditWhenResourcesReady()
  104. {
  105. startedCallback();
  106. this._executeAudit(categories, this._auditFinishedCallback.bind(this, finishedCallback));
  107. }
  108. if (runImmediately)
  109. startAuditWhenResourcesReady.call(this);
  110. else
  111. this._reloadResources(startAuditWhenResourcesReady.bind(this));
  112. WebInspector.userMetrics.AuditsStarted.record();
  113. },
  114. /**
  115. * @param {function()=} callback
  116. */
  117. _reloadResources: function(callback)
  118. {
  119. this._pageReloadCallback = callback;
  120. PageAgent.reload(false);
  121. },
  122. _didMainResourceLoad: function()
  123. {
  124. if (this._pageReloadCallback) {
  125. var callback = this._pageReloadCallback;
  126. delete this._pageReloadCallback;
  127. callback();
  128. }
  129. }
  130. }