1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 2002
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Darin Fisher <darin@netscape.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsBrowserStatusFilter.h"
40 : #include "nsIChannel.h"
41 : #include "nsITimer.h"
42 : #include "nsIServiceManager.h"
43 : #include "nsString.h"
44 :
45 : // XXX
46 : // XXX DO NOT TOUCH THIS CODE UNLESS YOU KNOW WHAT YOU'RE DOING !!!
47 : // XXX
48 :
49 : //-----------------------------------------------------------------------------
50 : // nsBrowserStatusFilter <public>
51 : //-----------------------------------------------------------------------------
52 :
53 0 : nsBrowserStatusFilter::nsBrowserStatusFilter()
54 : : mCurProgress(0)
55 : , mMaxProgress(0)
56 : , mStatusIsDirty(true)
57 : , mCurrentPercentage(0)
58 : , mTotalRequests(0)
59 : , mFinishedRequests(0)
60 : , mUseRealProgressFlag(false)
61 : , mDelayedStatus(false)
62 0 : , mDelayedProgress(false)
63 : {
64 0 : }
65 :
66 0 : nsBrowserStatusFilter::~nsBrowserStatusFilter()
67 : {
68 0 : if (mTimer) {
69 0 : mTimer->Cancel();
70 : }
71 0 : }
72 :
73 : //-----------------------------------------------------------------------------
74 : // nsBrowserStatusFilter::nsISupports
75 : //-----------------------------------------------------------------------------
76 :
77 0 : NS_IMPL_ISUPPORTS4(nsBrowserStatusFilter,
78 : nsIWebProgress,
79 : nsIWebProgressListener,
80 : nsIWebProgressListener2,
81 : nsISupportsWeakReference)
82 :
83 : //-----------------------------------------------------------------------------
84 : // nsBrowserStatusFilter::nsIWebProgress
85 : //-----------------------------------------------------------------------------
86 :
87 : NS_IMETHODIMP
88 0 : nsBrowserStatusFilter::AddProgressListener(nsIWebProgressListener *aListener,
89 : PRUint32 aNotifyMask)
90 : {
91 0 : mListener = aListener;
92 0 : return NS_OK;
93 : }
94 :
95 : NS_IMETHODIMP
96 0 : nsBrowserStatusFilter::RemoveProgressListener(nsIWebProgressListener *aListener)
97 : {
98 0 : if (aListener == mListener)
99 0 : mListener = nsnull;
100 0 : return NS_OK;
101 : }
102 :
103 : NS_IMETHODIMP
104 0 : nsBrowserStatusFilter::GetDOMWindow(nsIDOMWindow **aResult)
105 : {
106 0 : NS_NOTREACHED("nsBrowserStatusFilter::GetDOMWindow");
107 0 : return NS_ERROR_NOT_IMPLEMENTED;
108 : }
109 :
110 : NS_IMETHODIMP
111 0 : nsBrowserStatusFilter::GetIsLoadingDocument(bool *aIsLoadingDocument)
112 : {
113 0 : NS_NOTREACHED("nsBrowserStatusFilter::GetIsLoadingDocument");
114 0 : return NS_ERROR_NOT_IMPLEMENTED;
115 : }
116 :
117 :
118 : //-----------------------------------------------------------------------------
119 : // nsBrowserStatusFilter::nsIWebProgressListener
120 : //-----------------------------------------------------------------------------
121 :
122 : NS_IMETHODIMP
123 0 : nsBrowserStatusFilter::OnStateChange(nsIWebProgress *aWebProgress,
124 : nsIRequest *aRequest,
125 : PRUint32 aStateFlags,
126 : nsresult aStatus)
127 : {
128 0 : if (!mListener)
129 0 : return NS_OK;
130 :
131 0 : if (aStateFlags & STATE_START) {
132 0 : if (aStateFlags & STATE_IS_NETWORK) {
133 0 : ResetMembers();
134 : }
135 0 : if (aStateFlags & STATE_IS_REQUEST) {
136 0 : ++mTotalRequests;
137 :
138 : // if the total requests exceeds 1, then we'll base our progress
139 : // notifications on the percentage of completed requests.
140 : // otherwise, progress for the single request will be reported.
141 0 : mUseRealProgressFlag = (mTotalRequests == 1);
142 : }
143 : }
144 0 : else if (aStateFlags & STATE_STOP) {
145 0 : if (aStateFlags & STATE_IS_REQUEST) {
146 0 : ++mFinishedRequests;
147 : // Note: Do not return from here. This is necessary so that the
148 : // STATE_STOP can still be relayed to the listener if needed
149 : // (bug 209330)
150 0 : if (!mUseRealProgressFlag && mTotalRequests)
151 : OnProgressChange(nsnull, nsnull, 0, 0,
152 0 : mFinishedRequests, mTotalRequests);
153 : }
154 : }
155 0 : else if (aStateFlags & STATE_TRANSFERRING) {
156 0 : if (aStateFlags & STATE_IS_REQUEST) {
157 0 : if (!mUseRealProgressFlag && mTotalRequests)
158 : return OnProgressChange(nsnull, nsnull, 0, 0,
159 0 : mFinishedRequests, mTotalRequests);
160 : }
161 :
162 : // no need to forward this state change
163 0 : return NS_OK;
164 : } else {
165 : // no need to forward this state change
166 0 : return NS_OK;
167 : }
168 :
169 : // If we're here, we have either STATE_START or STATE_STOP. The
170 : // listener only cares about these in certain conditions.
171 0 : bool isLoadingDocument = false;
172 0 : if ((aStateFlags & nsIWebProgressListener::STATE_IS_NETWORK ||
173 : (aStateFlags & nsIWebProgressListener::STATE_IS_REQUEST &&
174 : mFinishedRequests == mTotalRequests &&
175 0 : (aWebProgress->GetIsLoadingDocument(&isLoadingDocument),
176 0 : !isLoadingDocument)))) {
177 0 : if (mTimer && (aStateFlags & nsIWebProgressListener::STATE_STOP)) {
178 0 : mTimer->Cancel();
179 0 : ProcessTimeout();
180 : }
181 :
182 0 : return mListener->OnStateChange(aWebProgress, aRequest, aStateFlags,
183 0 : aStatus);
184 : }
185 :
186 0 : return NS_OK;
187 : }
188 :
189 : NS_IMETHODIMP
190 0 : nsBrowserStatusFilter::OnProgressChange(nsIWebProgress *aWebProgress,
191 : nsIRequest *aRequest,
192 : PRInt32 aCurSelfProgress,
193 : PRInt32 aMaxSelfProgress,
194 : PRInt32 aCurTotalProgress,
195 : PRInt32 aMaxTotalProgress)
196 : {
197 0 : if (!mListener)
198 0 : return NS_OK;
199 :
200 0 : if (!mUseRealProgressFlag && aRequest)
201 0 : return NS_OK;
202 :
203 : //
204 : // limit frequency of calls to OnProgressChange
205 : //
206 :
207 0 : mCurProgress = (PRInt64)aCurTotalProgress;
208 0 : mMaxProgress = (PRInt64)aMaxTotalProgress;
209 :
210 0 : if (mDelayedProgress)
211 0 : return NS_OK;
212 :
213 0 : if (!mDelayedStatus) {
214 0 : MaybeSendProgress();
215 0 : StartDelayTimer();
216 : }
217 :
218 0 : mDelayedProgress = true;
219 :
220 0 : return NS_OK;
221 : }
222 :
223 : NS_IMETHODIMP
224 0 : nsBrowserStatusFilter::OnLocationChange(nsIWebProgress *aWebProgress,
225 : nsIRequest *aRequest,
226 : nsIURI *aLocation,
227 : PRUint32 aFlags)
228 : {
229 0 : if (!mListener)
230 0 : return NS_OK;
231 :
232 0 : return mListener->OnLocationChange(aWebProgress, aRequest, aLocation,
233 0 : aFlags);
234 : }
235 :
236 : NS_IMETHODIMP
237 0 : nsBrowserStatusFilter::OnStatusChange(nsIWebProgress *aWebProgress,
238 : nsIRequest *aRequest,
239 : nsresult aStatus,
240 : const PRUnichar *aMessage)
241 : {
242 0 : if (!mListener)
243 0 : return NS_OK;
244 :
245 : //
246 : // limit frequency of calls to OnStatusChange
247 : //
248 0 : if (mStatusIsDirty || !mCurrentStatusMsg.Equals(aMessage)) {
249 0 : mStatusIsDirty = true;
250 0 : mStatusMsg = aMessage;
251 : }
252 :
253 0 : if (mDelayedStatus)
254 0 : return NS_OK;
255 :
256 0 : if (!mDelayedProgress) {
257 0 : MaybeSendStatus();
258 0 : StartDelayTimer();
259 : }
260 :
261 0 : mDelayedStatus = true;
262 :
263 0 : return NS_OK;
264 : }
265 :
266 : NS_IMETHODIMP
267 0 : nsBrowserStatusFilter::OnSecurityChange(nsIWebProgress *aWebProgress,
268 : nsIRequest *aRequest,
269 : PRUint32 aState)
270 : {
271 0 : if (!mListener)
272 0 : return NS_OK;
273 :
274 0 : return mListener->OnSecurityChange(aWebProgress, aRequest, aState);
275 : }
276 :
277 : //-----------------------------------------------------------------------------
278 : // nsBrowserStatusFilter::nsIWebProgressListener2
279 : //-----------------------------------------------------------------------------
280 : NS_IMETHODIMP
281 0 : nsBrowserStatusFilter::OnProgressChange64(nsIWebProgress *aWebProgress,
282 : nsIRequest *aRequest,
283 : PRInt64 aCurSelfProgress,
284 : PRInt64 aMaxSelfProgress,
285 : PRInt64 aCurTotalProgress,
286 : PRInt64 aMaxTotalProgress)
287 : {
288 : // XXX truncates 64-bit to 32-bit
289 : return OnProgressChange(aWebProgress, aRequest,
290 : (PRInt32)aCurSelfProgress,
291 : (PRInt32)aMaxSelfProgress,
292 : (PRInt32)aCurTotalProgress,
293 0 : (PRInt32)aMaxTotalProgress);
294 : }
295 :
296 : NS_IMETHODIMP
297 0 : nsBrowserStatusFilter::OnRefreshAttempted(nsIWebProgress *aWebProgress,
298 : nsIURI *aUri,
299 : PRInt32 aDelay,
300 : bool aSameUri,
301 : bool *allowRefresh)
302 : {
303 : nsCOMPtr<nsIWebProgressListener2> listener =
304 0 : do_QueryInterface(mListener);
305 0 : if (!listener) {
306 0 : *allowRefresh = true;
307 0 : return NS_OK;
308 : }
309 :
310 0 : return listener->OnRefreshAttempted(aWebProgress, aUri, aDelay, aSameUri,
311 0 : allowRefresh);
312 : }
313 :
314 : //-----------------------------------------------------------------------------
315 : // nsBrowserStatusFilter <private>
316 : //-----------------------------------------------------------------------------
317 :
318 : void
319 0 : nsBrowserStatusFilter::ResetMembers()
320 : {
321 0 : mTotalRequests = 0;
322 0 : mFinishedRequests = 0;
323 0 : mUseRealProgressFlag = false;
324 0 : mMaxProgress = 0;
325 0 : mCurProgress = 0;
326 0 : mCurrentPercentage = 0;
327 0 : mStatusIsDirty = true;
328 0 : }
329 :
330 : void
331 0 : nsBrowserStatusFilter::MaybeSendProgress()
332 : {
333 0 : if (mCurProgress > mMaxProgress || mCurProgress <= 0)
334 0 : return;
335 :
336 : // check our percentage
337 0 : PRInt32 percentage = (PRInt32) double(mCurProgress) * 100 / mMaxProgress;
338 :
339 : // The progress meter only updates for increases greater than 3 percent
340 0 : if (percentage > (mCurrentPercentage + 3)) {
341 0 : mCurrentPercentage = percentage;
342 : // XXX truncates 64-bit to 32-bit
343 0 : mListener->OnProgressChange(nsnull, nsnull, 0, 0,
344 : (PRInt32)mCurProgress,
345 0 : (PRInt32)mMaxProgress);
346 : }
347 : }
348 :
349 : void
350 0 : nsBrowserStatusFilter::MaybeSendStatus()
351 : {
352 0 : if (mStatusIsDirty) {
353 0 : mListener->OnStatusChange(nsnull, nsnull, 0, mStatusMsg.get());
354 0 : mCurrentStatusMsg = mStatusMsg;
355 0 : mStatusIsDirty = false;
356 : }
357 0 : }
358 :
359 : nsresult
360 0 : nsBrowserStatusFilter::StartDelayTimer()
361 : {
362 0 : NS_ASSERTION(!DelayInEffect(), "delay should not be in effect");
363 :
364 0 : mTimer = do_CreateInstance("@mozilla.org/timer;1");
365 0 : if (!mTimer)
366 0 : return NS_ERROR_FAILURE;
367 :
368 0 : return mTimer->InitWithFuncCallback(TimeoutHandler, this, 160,
369 0 : nsITimer::TYPE_ONE_SHOT);
370 : }
371 :
372 : void
373 0 : nsBrowserStatusFilter::ProcessTimeout()
374 : {
375 0 : mTimer = nsnull;
376 :
377 0 : if (!mListener)
378 0 : return;
379 :
380 0 : if (mDelayedStatus) {
381 0 : mDelayedStatus = false;
382 0 : MaybeSendStatus();
383 : }
384 :
385 0 : if (mDelayedProgress) {
386 0 : mDelayedProgress = false;
387 0 : MaybeSendProgress();
388 : }
389 : }
390 :
391 : void
392 0 : nsBrowserStatusFilter::TimeoutHandler(nsITimer *aTimer, void *aClosure)
393 : {
394 0 : nsBrowserStatusFilter *self = reinterpret_cast<nsBrowserStatusFilter *>(aClosure);
395 0 : if (!self) {
396 0 : NS_ERROR("no self");
397 0 : return;
398 : }
399 :
400 0 : self->ProcessTimeout();
401 : }
|