summaryrefslogtreecommitdiffstats
path: root/components/statusbar/Progress.jsm
blob: 69d55db49b12a675a2e5be5995e454a119679593 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const EXPORTED_SYMBOLS = ["S4EProgressService"];

const CI = Components.interfaces;
const CU = Components.utils;

CU.import("resource://gre/modules/XPCOMUtils.jsm");

function S4EProgressService(gBrowser, service, getters, statusService) {
  this._gBrowser = gBrowser;
  this._service = service;
  this._getters = getters;
  this._statusService = statusService;

  this._gBrowser.addProgressListener(this);
}

S4EProgressService.prototype =
{
  _gBrowser:      null,
  _service:       null,
  _getters:       null,
  _statusService: null,

  _busyUI:        false,

  set value(val)
  {
    let toolbar_progress = this._getters.toolbarProgress;
    if(toolbar_progress)
    {
      toolbar_progress.value = val;
    }

    let throbber_progress = this._getters.throbberProgress;
    if(throbber_progress)
    {
      if(val)
      {
        throbber_progress.setAttribute("progress", val);
      }
      else
      {
        throbber_progress.removeAttribute("progress");
      }
    }
  },

  set collapsed(val)
  {
    let toolbar_progress = this._getters.toolbarProgress;
    if(toolbar_progress)
    {
      toolbar_progress.collapsed = val;
    }

    let throbber_progress = this._getters.throbberProgress;
    if(throbber_progress)
    {
      if(val)
      {
        throbber_progress.removeAttribute("busy");
      }
      else
      {
        throbber_progress.setAttribute("busy", true);
      }
    }
  },

  destroy: function()
  {
    this._gBrowser.removeProgressListener(this);

    ["_gBrowser", "_service", "_getters", "_statusService"].forEach(function(prop)
    {
      delete this[prop];
    }, this);
  },

  onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage)
  {
    this._statusService.setNetworkStatus(aMessage, this._busyUI);
  },

  onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus)
  {
    let nsIWPL = CI.nsIWebProgressListener;

    if(!this._busyUI
    && aStateFlags & nsIWPL.STATE_START
    && aStateFlags & nsIWPL.STATE_IS_NETWORK
    && !(aStateFlags & nsIWPL.STATE_RESTORING))
    {
      this._busyUI = true;
      this.value = 0;
      this.collapsed = false;
    }
    else if(aStateFlags & nsIWPL.STATE_STOP)
    {
      if(aRequest)
      {
        let msg = "";
        let location;
        if(aRequest instanceof CI.nsIChannel || "URI" in aRequest)
        {
          location = aRequest.URI;
          if(location.spec != "about:blank")
          {
            switch (aStatus)
            {
              case Components.results.NS_BINDING_ABORTED:
                msg = this._getters.strings.getString("nv_stopped");
                break;
              case Components.results.NS_ERROR_NET_TIMEOUT:
                msg = this._getters.strings.getString("nv_timeout");
                break;
            }
          }
        }

        if(!msg && (!location || location.spec != "about:blank"))
        {
          msg = this._getters.strings.getString("nv_done");
        }

        this._statusService.setDefaultStatus(msg);
        this._statusService.setNetworkStatus("", this._busyUI);
      }

      if(this._busyUI)
      {
        this._busyUI = false;
        this.collapsed = true;
        this.value = 0;
      }
    }
  },

  onUpdateCurrentBrowser: function(aStateFlags, aStatus, aMessage, aTotalProgress)
  {
    let nsIWPL = CI.nsIWebProgressListener;
    let loadingDone = aStateFlags & nsIWPL.STATE_STOP;

    this.onStateChange(
      this._gBrowser.webProgress,
      { URI: this._gBrowser.currentURI },
      ((loadingDone ? nsIWPL.STATE_STOP : nsIWPL.STATE_START) | (aStateFlags & nsIWPL.STATE_IS_NETWORK)),
      aStatus
    );

    if(!loadingDone)
    {
      this.onProgressChange(this._gBrowser.webProgress, null, 0, 0, aTotalProgress, 1);
      this.onStatusChange(this._gBrowser.webProgress, null, 0, aMessage);
    }
  },

  onProgressChange: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress)
  {
    if (aMaxTotalProgress > 0 && this._busyUI)
    {
      // This is highly optimized.  Don't touch this code unless
      // you are intimately familiar with the cost of setting
      // attrs on XUL elements. -- hyatt
      let percentage = (aCurTotalProgress * 100) / aMaxTotalProgress;
      this.value = percentage;
    }
  },

  onProgressChange64: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress)
  {
    return this.onProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
  },

  QueryInterface: XPCOMUtils.generateQI([ CI.nsIWebProgressListener, CI.nsIWebProgressListener2 ])
};