2 #include "nsIEventQueueService.h"
3 #include "nsIServiceManager.h"
4 #include "nsIComponentRegistrar.h"
5 #include "nsIInterfaceRequestor.h"
6 #include "nsIInterfaceRequestorUtils.h"
7 #include "nsIProgressEventSink.h"
10 #define RETURN_IF_FAILED(rv, step) \
12 if (NS_FAILED(rv)) { \
13 printf(">>> %s failed: rv=%x\n", step, rv); \
18 static NS_DEFINE_CID(kEventQueueServiceCID
, NS_EVENTQUEUESERVICE_CID
);
19 static nsIEventQueue
* gEventQ
= nullptr;
20 static bool gKeepRunning
= true;
22 //-----------------------------------------------------------------------------
23 // nsIStreamListener implementation
24 //-----------------------------------------------------------------------------
26 class MyListener
: public nsIStreamListener
30 NS_DECL_NSIREQUESTOBSERVER
31 NS_DECL_NSISTREAMLISTENER
34 virtual ~MyListener() {}
37 NS_IMPL_ISUPPORTS(MyListener
,
42 MyListener::OnStartRequest(nsIRequest
*req
, nsISupports
*ctxt
)
44 printf(">>> OnStartRequest\n");
49 MyListener::OnStopRequest(nsIRequest
*req
, nsISupports
*ctxt
, nsresult status
)
51 printf(">>> OnStopRequest status=%x\n", status
);
57 MyListener::OnDataAvailable(nsIRequest
*req
, nsISupports
*ctxt
,
58 nsIInputStream
*stream
,
59 uint64_t offset
, uint32_t count
)
61 printf(">>> OnDataAvailable [count=%u]\n", count
);
68 uint32_t amount
= std::min
<uint32_t>(count
, sizeof(buf
));
70 rv
= stream
->Read(buf
, amount
, &bytesRead
);
72 printf(">>> stream->Read failed with rv=%x\n", rv
);
76 fwrite(buf
, 1, bytesRead
, stdout
);
83 //-----------------------------------------------------------------------------
84 // NotificationCallbacks implementation
85 //-----------------------------------------------------------------------------
87 class MyNotifications
: public nsIInterfaceRequestor
88 , public nsIProgressEventSink
91 NS_DECL_THREADSAFE_ISUPPORTS
92 NS_DECL_NSIINTERFACEREQUESTOR
93 NS_DECL_NSIPROGRESSEVENTSINK
96 virtual ~MyNotifications() {}
99 NS_IMPL_ISUPPORTS(MyNotifications
,
100 nsIInterfaceRequestor
,
101 nsIProgressEventSink
)
104 MyNotifications::GetInterface(const nsIID
&iid
, void **result
)
106 return QueryInterface(iid
, result
);
110 MyNotifications::OnStatus(nsIRequest
*req
, nsISupports
*ctx
,
111 nsresult status
, const char16_t
*statusText
)
113 printf("status: %x\n", status
);
118 MyNotifications::OnProgress(nsIRequest
*req
, nsISupports
*ctx
,
119 uint64_t progress
, uint64_t progressMax
)
121 printf("progress: %llu/%llu\n", progress
, progressMax
);
125 //-----------------------------------------------------------------------------
127 //-----------------------------------------------------------------------------
130 int main(int argc
, char **argv
)
135 printf("usage: TestHttp <url>\n");
139 nsCOMPtr
<nsIServiceManager
> servMan
;
140 NS_InitXPCOM2(getter_AddRefs(servMan
), nullptr, nullptr);
141 nsCOMPtr
<nsIComponentRegistrar
> registrar
= do_QueryInterface(servMan
);
142 NS_ASSERTION(registrar
, "Null nsIComponentRegistrar");
144 registrar
->AutoRegister(nullptr);
146 // Create the Event Queue for this thread...
147 nsCOMPtr
<nsIEventQueueService
> eqs
=
148 do_GetService(kEventQueueServiceCID
, &rv
);
149 RETURN_IF_FAILED(rv
, "do_GetService(EventQueueService)");
151 rv
= eqs
->CreateMonitoredThreadEventQueue();
152 RETURN_IF_FAILED(rv
, "CreateMonitoredThreadEventQueue");
154 rv
= eqs
->GetThreadEventQueue(NS_CURRENT_THREAD
, &gEventQ
);
155 RETURN_IF_FAILED(rv
, "GetThreadEventQueue");
157 nsCOMPtr
<nsIURI
> uri
;
158 nsCOMPtr
<nsIChannel
> chan
;
159 nsCOMPtr
<nsIStreamListener
> listener
= new MyListener();
160 nsCOMPtr
<nsIInterfaceRequestor
> callbacks
= new MyNotifications();
162 rv
= NS_NewURI(getter_AddRefs(uri
), argv
[1]);
163 RETURN_IF_FAILED(rv
, "NS_NewURI");
165 rv
= NS_NewChannel(getter_AddRefs(chan
), uri
, nullptr, nullptr, callbacks
);
166 RETURN_IF_FAILED(rv
, "NS_OpenURI");
168 rv
= chan
->AsyncOpen(listener
, nullptr);
169 RETURN_IF_FAILED(rv
, "AsyncOpen");
172 gEventQ
->ProcessPendingEvents();
174 printf(">>> done\n");
175 } // this scopes the nsCOMPtrs
176 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
177 rv
= NS_ShutdownXPCOM(nullptr);
178 NS_ASSERTION(NS_SUCCEEDED(rv
), "NS_ShutdownXPCOM failed");