Home | History | Annotate | Download | only in front_end
      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 /**
     33  * @constructor
     34  * @param {WebInspector.AuditsPanel} auditsPanel
     35  */
     36 WebInspector.AuditController = function(auditsPanel)
     37 {
     38     this._auditsPanel = auditsPanel;
     39     WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.Load, this._didMainResourceLoad, this);
     40 }
     41 
     42 WebInspector.AuditController.prototype = {
     43     /**
     44      * @param {!Array.<!WebInspector.AuditCategory>} categories
     45      * @param {function(string, !Array.<!WebInspector.AuditCategoryResult>)} resultCallback
     46      */
     47     _executeAudit: function(categories, resultCallback)
     48     {
     49         this._progress.setTitle(WebInspector.UIString("Running audit"));
     50 
     51         function ruleResultReadyCallback(categoryResult, ruleResult)
     52         {
     53             if (ruleResult && ruleResult.children)
     54                 categoryResult.addRuleResult(ruleResult);
     55 
     56             if (this._progress.isCanceled())
     57                 this._progress.done();
     58         }
     59 
     60         var results = [];
     61         var mainResourceURL = WebInspector.inspectedPageURL;
     62         var categoriesDone = 0;
     63         function categoryDoneCallback()
     64         {
     65             if (++categoriesDone !== categories.length)
     66                 return;
     67             this._progress.done();
     68             resultCallback(mainResourceURL, results)
     69         }
     70 
     71         var requests = WebInspector.networkLog.requests.slice();
     72         var compositeProgress = new WebInspector.CompositeProgress(this._progress);
     73         var subprogresses = [];
     74         for (var i = 0; i < categories.length; ++i)
     75             subprogresses.push(compositeProgress.createSubProgress());
     76         for (var i = 0; i < categories.length; ++i) {
     77             var category = categories[i];
     78             var result = new WebInspector.AuditCategoryResult(category);
     79             results.push(result);
     80             category.run(requests, ruleResultReadyCallback.bind(this, result), categoryDoneCallback.bind(this), subprogresses[i]);
     81         }
     82     },
     83 
     84     /**
     85      * @param {function()} launcherCallback
     86      * @param {string} mainResourceURL
     87      * @param {!Array.<!WebInspector.AuditCategoryResult>} results
     88      */
     89     _auditFinishedCallback: function(launcherCallback, mainResourceURL, results)
     90     {
     91         this._auditsPanel.auditFinishedCallback(mainResourceURL, results);
     92         if (!this._progress.isCanceled())
     93             launcherCallback();
     94     },
     95 
     96     /**
     97      * @param {Array.<string>} categoryIds
     98      * @param {WebInspector.Progress} progress
     99      * @param {boolean} runImmediately
    100      * @param {function()} startedCallback
    101      * @param {function()} finishedCallback
    102      */
    103     initiateAudit: function(categoryIds, progress, runImmediately, startedCallback, finishedCallback)
    104     {
    105         if (!categoryIds || !categoryIds.length)
    106             return;
    107 
    108         this._progress = progress;
    109 
    110         var categories = [];
    111         for (var i = 0; i < categoryIds.length; ++i)
    112             categories.push(this._auditsPanel.categoriesById[categoryIds[i]]);
    113 
    114         function startAuditWhenResourcesReady()
    115         {
    116             startedCallback();
    117             this._executeAudit(categories, this._auditFinishedCallback.bind(this, finishedCallback));
    118         }
    119 
    120         if (runImmediately)
    121             startAuditWhenResourcesReady.call(this);
    122         else
    123             this._reloadResources(startAuditWhenResourcesReady.bind(this));
    124 
    125         WebInspector.userMetrics.AuditsStarted.record();
    126     },
    127 
    128     /**
    129      * @param {function()=} callback
    130      */
    131     _reloadResources: function(callback)
    132     {
    133         this._pageReloadCallback = callback;
    134         PageAgent.reload(false);
    135     },
    136 
    137     _didMainResourceLoad: function()
    138     {
    139         if (this._pageReloadCallback) {
    140             var callback = this._pageReloadCallback;
    141             delete this._pageReloadCallback;
    142             callback();
    143         }
    144     }
    145 }
    146