• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5'use strict';
6
7/**
8 * Event of the ProgressCenter class.
9 * @enum {string}
10 * @const
11 */
12var ProgressCenterEvent = Object.freeze({
13  /**
14   * Background page notifies item update to application windows.
15   */
16  ITEM_UPDATED: 'itemUpdated',
17
18  /**
19   * Background page notifies all the items are cleared.
20   */
21  RESET: 'reset'
22});
23
24/**
25 * State of progress items.
26 * @enum {string}
27 * @const
28 */
29var ProgressItemState = Object.freeze({
30  PROGRESSING: 'progressing',
31  COMPLETED: 'completed',
32  ERROR: 'error',
33  CANCELED: 'canceled'
34});
35
36/**
37 * Type of progress items.
38 * @enum {string}
39 * @const
40 */
41var ProgressItemType = Object.freeze({
42  // The item is file copy operation.
43  COPY: 'copy',
44  // The item is file move operation.
45  MOVE: 'move',
46  // The item is file delete operation.
47  DELETE: 'delete',
48  // The item is file zip operation.
49  ZIP: 'zip',
50  // The item is drive sync operation.
51  SYNC: 'sync',
52  // The item is general file transfer operation.
53  // This is used for the mixed operation of summarized item.
54  TRANSFER: 'transfer'
55});
56
57/**
58 * Item of the progress center.
59 * @constructor
60 */
61var ProgressCenterItem = function() {
62  /**
63   * Item ID.
64   * @type {string}
65   * @private
66   */
67  this.id_ = null;
68
69  /**
70   * State of the progress item.
71   * @type {ProgressItemState}
72   */
73  this.state = ProgressItemState.PROGRESSING;
74
75  /**
76   * Message of the progress item.
77   * @type {string}
78   */
79  this.message = '';
80
81  /**
82   * Max value of the progress.
83   * @type {number}
84   */
85  this.progressMax = 0;
86
87  /**
88   * Current value of the progress.
89   * @type {number}
90   */
91  this.progressValue = 0;
92
93  /**
94   * Type of progress item.
95   * @type {ProgressItemType}
96   */
97  this.type = null;
98
99  /**
100   * Whether the item represents a single item or not.
101   * @type {boolean}
102   */
103  this.single = true;
104
105  /**
106   * If the property is true, only the message of item shown in the progress
107   * center and the notification of the item is created as priority = -1.
108   * @type {boolean}
109   */
110  this.quiet = false;
111
112  /**
113   * Callback function to cancel the item.
114   * @type {function()}
115   */
116  this.cancelCallback = null;
117
118  // This object is instantiated many time. Object.seal use more memory
119  // than Object.preventExtensions (crbug.com/412307)
120  Object.preventExtensions(this);
121};
122
123ProgressCenterItem.prototype = {
124  /**
125   * Setter of Item ID.
126   * @param {string} value New value of ID.
127   */
128  set id(value) {
129    if (!this.id_)
130      this.id_ = value;
131    else
132      console.error('The ID is already set. (current ID: ' + this.id_ + ')');
133  },
134
135  /**
136   * Getter of Item ID.
137   * @return {string} Item ID.
138   */
139  get id() {
140    return this.id_;
141  },
142
143  /**
144   * Gets progress rate in percent.
145   *
146   * If the current state is canceled or completed, it always returns 0 or 100
147   * respectively.
148   *
149   * @return {number} Progress rate in percent.
150   */
151  get progressRateInPercent() {
152    switch (this.state) {
153      case ProgressItemState.CANCELED: return 0;
154      case ProgressItemState.COMPLETED: return 100;
155      default: return ~~(100 * this.progressValue / this.progressMax);
156    }
157  },
158
159  /**
160   * Whether the item can be canceled or not.
161   * @return {boolean} True if the item can be canceled.
162   */
163  get cancelable() {
164    return !!(this.state == ProgressItemState.PROGRESSING &&
165              this.cancelCallback &&
166              this.single);
167  }
168};
169
170/**
171 * Clones the item.
172 * @return {ProgressCenterItem} New item having the same properties with this.
173 */
174ProgressCenterItem.prototype.clone = function() {
175  var newItem = new ProgressCenterItem();
176  newItem.id = this.id;
177  newItem.state = this.state;
178  newItem.message = this.message;
179  newItem.progressMax = this.progressMax;
180  newItem.progressValue = this.progressValue;
181  newItem.type = this.type;
182  newItem.single = this.single;
183  newItem.quiet = this.quiet;
184  newItem.cancelCallback = this.cancelCallback;
185  return newItem;
186};
187