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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* 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/. */
#include "nsBrowserStatusFilter.h"
#include "nsIChannel.h"
#include "nsITimer.h"
#include "nsIServiceManager.h"
#include "nsString.h"
// XXX
// XXX DO NOT TOUCH THIS CODE UNLESS YOU KNOW WHAT YOU'RE DOING !!!
// XXX
//-----------------------------------------------------------------------------
// nsBrowserStatusFilter <public>
//-----------------------------------------------------------------------------
nsBrowserStatusFilter::nsBrowserStatusFilter()
: mCurProgress(0)
, mMaxProgress(0)
, mStatusIsDirty(true)
, mCurrentPercentage(0)
, mTotalRequests(0)
, mFinishedRequests(0)
, mUseRealProgressFlag(false)
, mDelayedStatus(false)
, mDelayedProgress(false)
{
}
nsBrowserStatusFilter::~nsBrowserStatusFilter()
{
if (mTimer) {
mTimer->Cancel();
}
}
//-----------------------------------------------------------------------------
// nsBrowserStatusFilter::nsISupports
//-----------------------------------------------------------------------------
NS_IMPL_ISUPPORTS(nsBrowserStatusFilter,
nsIWebProgress,
nsIWebProgressListener,
nsIWebProgressListener2,
nsISupportsWeakReference)
//-----------------------------------------------------------------------------
// nsBrowserStatusFilter::nsIWebProgress
//-----------------------------------------------------------------------------
NS_IMETHODIMP
nsBrowserStatusFilter::AddProgressListener(nsIWebProgressListener *aListener,
uint32_t aNotifyMask)
{
mListener = aListener;
return NS_OK;
}
NS_IMETHODIMP
nsBrowserStatusFilter::RemoveProgressListener(nsIWebProgressListener *aListener)
{
if (aListener == mListener)
mListener = nullptr;
return NS_OK;
}
NS_IMETHODIMP
nsBrowserStatusFilter::GetDOMWindow(mozIDOMWindowProxy **aResult)
{
NS_NOTREACHED("nsBrowserStatusFilter::GetDOMWindow");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsBrowserStatusFilter::GetDOMWindowID(uint64_t *aResult)
{
*aResult = 0;
NS_NOTREACHED("nsBrowserStatusFilter::GetDOMWindowID");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsBrowserStatusFilter::GetIsTopLevel(bool *aIsTopLevel)
{
*aIsTopLevel = false;
NS_NOTREACHED("nsBrowserStatusFilter::GetIsTopLevel");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsBrowserStatusFilter::GetIsLoadingDocument(bool *aIsLoadingDocument)
{
NS_NOTREACHED("nsBrowserStatusFilter::GetIsLoadingDocument");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsBrowserStatusFilter::GetLoadType(uint32_t *aLoadType)
{
*aLoadType = 0;
NS_NOTREACHED("nsBrowserStatusFilter::GetLoadType");
return NS_ERROR_NOT_IMPLEMENTED;
}
//-----------------------------------------------------------------------------
// nsBrowserStatusFilter::nsIWebProgressListener
//-----------------------------------------------------------------------------
NS_IMETHODIMP
nsBrowserStatusFilter::OnStateChange(nsIWebProgress *aWebProgress,
nsIRequest *aRequest,
uint32_t aStateFlags,
nsresult aStatus)
{
if (!mListener)
return NS_OK;
if (aStateFlags & STATE_START) {
if (aStateFlags & STATE_IS_NETWORK) {
ResetMembers();
}
if (aStateFlags & STATE_IS_REQUEST) {
++mTotalRequests;
// if the total requests exceeds 1, then we'll base our progress
// notifications on the percentage of completed requests.
// otherwise, progress for the single request will be reported.
mUseRealProgressFlag = (mTotalRequests == 1);
}
}
else if (aStateFlags & STATE_STOP) {
if (aStateFlags & STATE_IS_REQUEST) {
++mFinishedRequests;
// Note: Do not return from here. This is necessary so that the
// STATE_STOP can still be relayed to the listener if needed
// (bug 209330)
if (!mUseRealProgressFlag && mTotalRequests)
OnProgressChange(nullptr, nullptr, 0, 0,
mFinishedRequests, mTotalRequests);
}
}
else if (aStateFlags & STATE_TRANSFERRING) {
if (aStateFlags & STATE_IS_REQUEST) {
if (!mUseRealProgressFlag && mTotalRequests)
return OnProgressChange(nullptr, nullptr, 0, 0,
mFinishedRequests, mTotalRequests);
}
// no need to forward this state change
return NS_OK;
} else {
// no need to forward this state change
return NS_OK;
}
// If we're here, we have either STATE_START or STATE_STOP. The
// listener only cares about these in certain conditions.
bool isLoadingDocument = false;
if ((aStateFlags & nsIWebProgressListener::STATE_IS_NETWORK ||
(aStateFlags & nsIWebProgressListener::STATE_IS_REQUEST &&
mFinishedRequests == mTotalRequests &&
(aWebProgress->GetIsLoadingDocument(&isLoadingDocument),
!isLoadingDocument)))) {
if (mTimer && (aStateFlags & nsIWebProgressListener::STATE_STOP)) {
mTimer->Cancel();
ProcessTimeout();
}
return mListener->OnStateChange(aWebProgress, aRequest, aStateFlags,
aStatus);
}
return NS_OK;
}
NS_IMETHODIMP
nsBrowserStatusFilter::OnProgressChange(nsIWebProgress *aWebProgress,
nsIRequest *aRequest,
int32_t aCurSelfProgress,
int32_t aMaxSelfProgress,
int32_t aCurTotalProgress,
int32_t aMaxTotalProgress)
{
if (!mListener)
return NS_OK;
if (!mUseRealProgressFlag && aRequest)
return NS_OK;
//
// limit frequency of calls to OnProgressChange
//
mCurProgress = (int64_t)aCurTotalProgress;
mMaxProgress = (int64_t)aMaxTotalProgress;
if (mDelayedProgress)
return NS_OK;
if (!mDelayedStatus) {
MaybeSendProgress();
StartDelayTimer();
}
mDelayedProgress = true;
return NS_OK;
}
NS_IMETHODIMP
nsBrowserStatusFilter::OnLocationChange(nsIWebProgress *aWebProgress,
nsIRequest *aRequest,
nsIURI *aLocation,
uint32_t aFlags)
{
if (!mListener)
return NS_OK;
return mListener->OnLocationChange(aWebProgress, aRequest, aLocation,
aFlags);
}
NS_IMETHODIMP
nsBrowserStatusFilter::OnStatusChange(nsIWebProgress *aWebProgress,
nsIRequest *aRequest,
nsresult aStatus,
const char16_t *aMessage)
{
if (!mListener)
return NS_OK;
//
// limit frequency of calls to OnStatusChange
//
if (mStatusIsDirty || !mCurrentStatusMsg.Equals(aMessage)) {
mStatusIsDirty = true;
mStatusMsg = aMessage;
}
if (mDelayedStatus)
return NS_OK;
if (!mDelayedProgress) {
MaybeSendStatus();
StartDelayTimer();
}
mDelayedStatus = true;
return NS_OK;
}
NS_IMETHODIMP
nsBrowserStatusFilter::OnSecurityChange(nsIWebProgress *aWebProgress,
nsIRequest *aRequest,
uint32_t aState)
{
if (!mListener)
return NS_OK;
return mListener->OnSecurityChange(aWebProgress, aRequest, aState);
}
//-----------------------------------------------------------------------------
// nsBrowserStatusFilter::nsIWebProgressListener2
//-----------------------------------------------------------------------------
NS_IMETHODIMP
nsBrowserStatusFilter::OnProgressChange64(nsIWebProgress *aWebProgress,
nsIRequest *aRequest,
int64_t aCurSelfProgress,
int64_t aMaxSelfProgress,
int64_t aCurTotalProgress,
int64_t aMaxTotalProgress)
{
// XXX truncates 64-bit to 32-bit
return OnProgressChange(aWebProgress, aRequest,
(int32_t)aCurSelfProgress,
(int32_t)aMaxSelfProgress,
(int32_t)aCurTotalProgress,
(int32_t)aMaxTotalProgress);
}
NS_IMETHODIMP
nsBrowserStatusFilter::OnRefreshAttempted(nsIWebProgress *aWebProgress,
nsIURI *aUri,
int32_t aDelay,
bool aSameUri,
bool *allowRefresh)
{
nsCOMPtr<nsIWebProgressListener2> listener =
do_QueryInterface(mListener);
if (!listener) {
*allowRefresh = true;
return NS_OK;
}
return listener->OnRefreshAttempted(aWebProgress, aUri, aDelay, aSameUri,
allowRefresh);
}
//-----------------------------------------------------------------------------
// nsBrowserStatusFilter <private>
//-----------------------------------------------------------------------------
void
nsBrowserStatusFilter::ResetMembers()
{
mTotalRequests = 0;
mFinishedRequests = 0;
mUseRealProgressFlag = false;
mMaxProgress = 0;
mCurProgress = 0;
mCurrentPercentage = 0;
mStatusIsDirty = true;
}
void
nsBrowserStatusFilter::MaybeSendProgress()
{
if (mCurProgress > mMaxProgress || mCurProgress <= 0)
return;
// check our percentage
int32_t percentage = (int32_t) double(mCurProgress) * 100 / mMaxProgress;
// The progress meter only updates for increases greater than 3 percent
if (percentage > (mCurrentPercentage + 3)) {
mCurrentPercentage = percentage;
// XXX truncates 64-bit to 32-bit
mListener->OnProgressChange(nullptr, nullptr, 0, 0,
(int32_t)mCurProgress,
(int32_t)mMaxProgress);
}
}
void
nsBrowserStatusFilter::MaybeSendStatus()
{
if (mStatusIsDirty) {
mListener->OnStatusChange(nullptr, nullptr, NS_OK, mStatusMsg.get());
mCurrentStatusMsg = mStatusMsg;
mStatusIsDirty = false;
}
}
nsresult
nsBrowserStatusFilter::StartDelayTimer()
{
NS_ASSERTION(!DelayInEffect(), "delay should not be in effect");
mTimer = do_CreateInstance("@mozilla.org/timer;1");
if (!mTimer)
return NS_ERROR_FAILURE;
return mTimer->InitWithNamedFuncCallback(
TimeoutHandler, this, 160, nsITimer::TYPE_ONE_SHOT,
"nsBrowserStatusFilter::TimeoutHandler");
}
void
nsBrowserStatusFilter::ProcessTimeout()
{
mTimer = nullptr;
if (!mListener)
return;
if (mDelayedStatus) {
mDelayedStatus = false;
MaybeSendStatus();
}
if (mDelayedProgress) {
mDelayedProgress = false;
MaybeSendProgress();
}
}
void
nsBrowserStatusFilter::TimeoutHandler(nsITimer *aTimer, void *aClosure)
{
nsBrowserStatusFilter *self = reinterpret_cast<nsBrowserStatusFilter *>(aClosure);
if (!self) {
NS_ERROR("no self");
return;
}
self->ProcessTimeout();
}
|