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