1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2001
20 * the Initial Developer. All Rights Reserved.
23 * Darin Fisher <darin@netscape.com> (original author)
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.
37 * ***** END LICENSE BLOCK ***** */
40 #include "nsRequestObserverProxy.h"
41 #include "nsIRequest.h"
42 #include "nsIServiceManager.h"
43 #include "nsProxyRelease.h"
44 #include "nsAutoPtr.h"
48 #if defined(PR_LOGGING)
49 static PRLogModuleInfo
*gRequestObserverProxyLog
;
52 #define LOG(args) PR_LOG(gRequestObserverProxyLog, PR_LOG_DEBUG, args)
54 //-----------------------------------------------------------------------------
55 // nsARequestObserverEvent internal class...
56 //-----------------------------------------------------------------------------
58 nsARequestObserverEvent::nsARequestObserverEvent(nsIRequest
*request
,
63 NS_PRECONDITION(mRequest
, "null pointer");
66 //-----------------------------------------------------------------------------
67 // nsOnStartRequestEvent internal class...
68 //-----------------------------------------------------------------------------
70 class nsOnStartRequestEvent
: public nsARequestObserverEvent
72 nsRefPtr
<nsRequestObserverProxy
> mProxy
;
74 nsOnStartRequestEvent(nsRequestObserverProxy
*proxy
,
77 : nsARequestObserverEvent(request
, context
)
80 NS_PRECONDITION(mProxy
, "null pointer");
83 virtual ~nsOnStartRequestEvent() {}
87 LOG(("nsOnStartRequestEvent::HandleEvent [req=%x]\n", mRequest
.get()));
89 if (!mProxy
->mObserver
) {
90 NS_NOTREACHED("already handled onStopRequest event (observer is null)");
94 LOG(("handle startevent=%8lX\n",(long)this));
95 nsresult rv
= mProxy
->mObserver
->OnStartRequest(mRequest
, mContext
);
97 LOG(("OnStartRequest failed [rv=%x] canceling request!\n", rv
));
98 rv
= mRequest
->Cancel(rv
);
99 NS_ASSERTION(NS_SUCCEEDED(rv
), "Cancel failed for request!");
106 //-----------------------------------------------------------------------------
107 // nsOnStopRequestEvent internal class...
108 //-----------------------------------------------------------------------------
110 class nsOnStopRequestEvent
: public nsARequestObserverEvent
112 nsRefPtr
<nsRequestObserverProxy
> mProxy
;
114 nsOnStopRequestEvent(nsRequestObserverProxy
*proxy
,
115 nsIRequest
*request
, nsISupports
*context
)
116 : nsARequestObserverEvent(request
, context
)
119 NS_PRECONDITION(mProxy
, "null pointer");
122 virtual ~nsOnStopRequestEvent() {}
126 nsresult rv
, status
= NS_OK
;
128 LOG(("nsOnStopRequestEvent::HandleEvent [req=%x]\n", mRequest
.get()));
130 nsCOMPtr
<nsIRequestObserver
> observer
= mProxy
->mObserver
;
132 NS_NOTREACHED("already handled onStopRequest event (observer is null)");
135 // Do not allow any more events to be handled after OnStopRequest
136 mProxy
->mObserver
= 0;
138 rv
= mRequest
->GetStatus(&status
);
139 NS_ASSERTION(NS_SUCCEEDED(rv
), "GetStatus failed for request!");
141 LOG(("handle stopevent=%8lX\n",(long)this));
142 (void) observer
->OnStopRequest(mRequest
, mContext
, status
);
148 //-----------------------------------------------------------------------------
149 // nsRequestObserverProxy <public>
150 //-----------------------------------------------------------------------------
152 nsRequestObserverProxy::~nsRequestObserverProxy()
155 // order is crucial here... we must be careful to clear mObserver
156 // before posting the proxy release event. otherwise, we'd risk
157 // releasing the object on this thread.
158 nsIRequestObserver
*obs
= nsnull
;
160 NS_ProxyRelease(mTarget
, obs
);
164 //-----------------------------------------------------------------------------
165 // nsRequestObserverProxy::nsISupports implementation...
166 //-----------------------------------------------------------------------------
168 NS_IMPL_THREADSAFE_ISUPPORTS2(nsRequestObserverProxy
,
170 nsIRequestObserverProxy
)
172 //-----------------------------------------------------------------------------
173 // nsRequestObserverProxy::nsIRequestObserver implementation...
174 //-----------------------------------------------------------------------------
177 nsRequestObserverProxy::OnStartRequest(nsIRequest
*request
,
178 nsISupports
*context
)
180 LOG(("nsRequestObserverProxy::OnStartRequest [this=%x req=%x]\n", this, request
));
182 nsOnStartRequestEvent
*ev
=
183 new nsOnStartRequestEvent(this, request
, context
);
185 return NS_ERROR_OUT_OF_MEMORY
;
187 LOG(("post startevent=%p queue=%p\n", ev
, mTarget
.get()));
188 nsresult rv
= FireEvent(ev
);
195 nsRequestObserverProxy::OnStopRequest(nsIRequest
*request
,
196 nsISupports
*context
,
199 LOG(("nsRequestObserverProxy: OnStopRequest [this=%x req=%x status=%x]\n",
200 this, request
, status
));
202 // The status argument is ignored because, by the time the OnStopRequestEvent
203 // is actually processed, the status of the request may have changed :-(
204 // To make sure that an accurate status code is always used, GetStatus() is
205 // called when the OnStopRequestEvent is actually processed (see above).
207 nsOnStopRequestEvent
*ev
=
208 new nsOnStopRequestEvent(this, request
, context
);
210 return NS_ERROR_OUT_OF_MEMORY
;
212 LOG(("post stopevent=%p queue=%p\n", ev
, mTarget
.get()));
213 nsresult rv
= FireEvent(ev
);
219 //-----------------------------------------------------------------------------
220 // nsRequestObserverProxy::nsIRequestObserverProxy implementation...
221 //-----------------------------------------------------------------------------
224 nsRequestObserverProxy::Init(nsIRequestObserver
*observer
,
225 nsIEventTarget
*target
)
227 NS_ENSURE_ARG_POINTER(observer
);
229 #if defined(PR_LOGGING)
230 if (!gRequestObserverProxyLog
)
231 gRequestObserverProxyLog
= PR_NewLogModule("nsRequestObserverProxy");
234 mObserver
= observer
;
236 SetTarget(target
? target
: NS_GetCurrentThread());
237 NS_ENSURE_STATE(mTarget
);
242 //-----------------------------------------------------------------------------
243 // nsRequestObserverProxy implementation...
244 //-----------------------------------------------------------------------------
247 nsRequestObserverProxy::FireEvent(nsARequestObserverEvent
*event
)
249 NS_ENSURE_TRUE(mTarget
, NS_ERROR_NOT_INITIALIZED
);
250 return mTarget
->Dispatch(event
, NS_DISPATCH_NORMAL
);