Home | History | Annotate | Download | only in front_end
      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 /**
     32  * @interface
     33  * @extends {WebInspector.EventTarget}
     34  */
     35 WebInspector.Progress = function()
     36 {
     37 }
     38 
     39 WebInspector.Progress.Events = {
     40     Canceled: "Canceled"
     41 }
     42 
     43 WebInspector.Progress.prototype = {
     44     /**
     45      * @param {number} totalWork
     46      */
     47     setTotalWork: function(totalWork) { },
     48 
     49     /**
     50      * @param {string} title
     51      */
     52     setTitle: function(title) { },
     53 
     54     /**
     55      * @param {number} worked
     56      * @param {string=} title
     57      */
     58     setWorked: function(worked, title) { },
     59 
     60     /**
     61      * @param {number=} worked
     62      */
     63     worked: function(worked) { },
     64 
     65     done: function() { },
     66 
     67     /**
     68      * @return {boolean}
     69      */
     70     isCanceled: function() { return false; },
     71 
     72     /**
     73      * @param {string} eventType
     74      * @param {function(WebInspector.Event)} listener
     75      * @param {Object=} thisObject
     76      */
     77     addEventListener: function(eventType, listener, thisObject) { }
     78 }
     79 
     80 /**
     81  * @constructor
     82  * @param {WebInspector.Progress} parent
     83  * @extends {WebInspector.Object}
     84  */
     85 WebInspector.CompositeProgress = function(parent)
     86 {
     87     this._parent = parent;
     88     this._children = [];
     89     this._childrenDone = 0;
     90     this._parent.setTotalWork(1);
     91     this._parent.setWorked(0);
     92     parent.addEventListener(WebInspector.Progress.Events.Canceled, this._parentCanceled.bind(this));
     93 }
     94 
     95 WebInspector.CompositeProgress.prototype = {
     96     _childDone: function()
     97     {
     98         if (++this._childrenDone === this._children.length)
     99             this._parent.done();
    100     },
    101 
    102     _parentCanceled: function()
    103     {
    104         this.dispatchEventToListeners(WebInspector.Progress.Events.Canceled);
    105         for (var i = 0; i < this._children.length; ++i) {
    106             this._children[i].dispatchEventToListeners(WebInspector.Progress.Events.Canceled);
    107         }
    108     },
    109 
    110     /**
    111      * @param {number=} weight
    112      */
    113     createSubProgress: function(weight)
    114     {
    115         var child = new WebInspector.SubProgress(this, weight);
    116         this._children.push(child);
    117         return child;
    118     },
    119 
    120     _update: function()
    121     {
    122         var totalWeights = 0;
    123         var done = 0;
    124 
    125         for (var i = 0; i < this._children.length; ++i) {
    126             var child = this._children[i];
    127             if (child._totalWork)
    128                 done += child._weight * child._worked / child._totalWork;
    129             totalWeights += child._weight;
    130         }
    131         this._parent.setWorked(done / totalWeights);
    132     },
    133 
    134     __proto__: WebInspector.Object.prototype
    135 }
    136 
    137 /**
    138  * @constructor
    139  * @implements {WebInspector.Progress}
    140  * @extends {WebInspector.Object}
    141  * @param {WebInspector.CompositeProgress} composite
    142  * @param {number=} weight
    143  */
    144 WebInspector.SubProgress = function(composite, weight)
    145 {
    146     this._composite = composite;
    147     this._weight = weight || 1;
    148     this._worked = 0;
    149 }
    150 
    151 WebInspector.SubProgress.prototype = {
    152     /**
    153      * @return {boolean}
    154      */
    155     isCanceled: function()
    156     {
    157         return this._composite._parent.isCanceled();
    158     },
    159 
    160     /**
    161      * @param {string} title
    162      */
    163     setTitle: function(title)
    164     {
    165         this._composite._parent.setTitle(title);
    166     },
    167 
    168     done: function()
    169     {
    170         this.setWorked(this._totalWork);
    171         this._composite._childDone();
    172     },
    173 
    174     /**
    175      * @param {number} totalWork
    176      */
    177     setTotalWork: function(totalWork)
    178     {
    179         this._totalWork = totalWork;
    180         this._composite._update();
    181     },
    182 
    183     /**
    184      * @param {number} worked
    185      * @param {string=} title
    186      */
    187     setWorked: function(worked, title)
    188     {
    189         this._worked = worked;
    190         if (typeof title !== "undefined")
    191             this.setTitle(title);
    192         this._composite._update();
    193     },
    194 
    195     /**
    196      * @param {number=} worked
    197      */
    198     worked: function(worked)
    199     {
    200         this.setWorked(this._worked + (worked || 1));
    201     },
    202 
    203     __proto__: WebInspector.Object.prototype
    204 }
    205