2
#include "nsIEventQueueService.h"
3
#include "nsIServiceManager.h"
4
#include "nsIComponentRegistrar.h"
5
#include "nsIInterfaceRequestor.h"
6
#include "nsIInterfaceRequestorUtils.h"
7
#include "nsIProgressEventSink.h"
9
#define RETURN_IF_FAILED(rv, step) \
11
if (NS_FAILED(rv)) { \
12
printf(">>> %s failed: rv=%x\n", step, rv); \
17
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
18
static nsIEventQueue* gEventQ = nsnull;
19
static PRBool gKeepRunning = PR_TRUE;
21
//-----------------------------------------------------------------------------
22
// nsIStreamListener implementation
23
//-----------------------------------------------------------------------------
25
class MyListener : public nsIStreamListener
29
NS_DECL_NSIREQUESTOBSERVER
30
NS_DECL_NSISTREAMLISTENER
33
virtual ~MyListener() {}
36
NS_IMPL_ISUPPORTS2(MyListener,
41
MyListener::OnStartRequest(nsIRequest *req, nsISupports *ctxt)
43
printf(">>> OnStartRequest\n");
48
MyListener::OnStopRequest(nsIRequest *req, nsISupports *ctxt, nsresult status)
50
printf(">>> OnStopRequest status=%x\n", status);
51
gKeepRunning = PR_FALSE;
56
MyListener::OnDataAvailable(nsIRequest *req, nsISupports *ctxt,
57
nsIInputStream *stream,
58
PRUint32 offset, PRUint32 count)
60
printf(">>> OnDataAvailable [count=%u]\n", count);
67
PRUint32 amount = PR_MIN(count, sizeof(buf));
69
rv = stream->Read(buf, amount, &bytesRead);
71
printf(">>> stream->Read failed with rv=%x\n", rv);
75
fwrite(buf, 1, bytesRead, stdout);
82
//-----------------------------------------------------------------------------
83
// NotificationCallbacks implementation
84
//-----------------------------------------------------------------------------
86
class MyNotifications : public nsIInterfaceRequestor
87
, public nsIProgressEventSink
91
NS_DECL_NSIINTERFACEREQUESTOR
92
NS_DECL_NSIPROGRESSEVENTSINK
95
virtual ~MyNotifications() {}
98
NS_IMPL_THREADSAFE_ISUPPORTS2(MyNotifications,
99
nsIInterfaceRequestor,
100
nsIProgressEventSink)
103
MyNotifications::GetInterface(const nsIID &iid, void **result)
105
return QueryInterface(iid, result);
109
MyNotifications::OnStatus(nsIRequest *req, nsISupports *ctx,
110
nsresult status, const PRUnichar *statusText)
112
printf("status: %x\n", status);
117
MyNotifications::OnProgress(nsIRequest *req, nsISupports *ctx,
118
PRUint32 progress, PRUint32 progressMax)
120
printf("progress: %u/%u\n", progress, progressMax);
124
//-----------------------------------------------------------------------------
126
//-----------------------------------------------------------------------------
129
int main(int argc, char **argv)
134
printf("usage: TestHttp <url>\n");
138
nsCOMPtr<nsIServiceManager> servMan;
139
NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
140
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
141
NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
143
registrar->AutoRegister(nsnull);
145
// Create the Event Queue for this thread...
146
nsCOMPtr<nsIEventQueueService> eqs =
147
do_GetService(kEventQueueServiceCID, &rv);
148
RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");
150
rv = eqs->CreateMonitoredThreadEventQueue();
151
RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
153
rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
154
RETURN_IF_FAILED(rv, "GetThreadEventQueue");
156
nsCOMPtr<nsIURI> uri;
157
nsCOMPtr<nsIChannel> chan;
158
nsCOMPtr<nsIStreamListener> listener = new MyListener();
159
nsCOMPtr<nsIInterfaceRequestor> callbacks = new MyNotifications();
161
rv = NS_NewURI(getter_AddRefs(uri), argv[1]);
162
RETURN_IF_FAILED(rv, "NS_NewURI");
164
rv = NS_NewChannel(getter_AddRefs(chan), uri, nsnull, nsnull, callbacks);
165
RETURN_IF_FAILED(rv, "NS_OpenURI");
167
rv = chan->AsyncOpen(listener, nsnull);
168
RETURN_IF_FAILED(rv, "AsyncOpen");
171
gEventQ->ProcessPendingEvents();
173
printf(">>> done\n");
174
} // this scopes the nsCOMPtrs
175
// no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
176
rv = NS_ShutdownXPCOM(nsnull);
177
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");