Back to index

lightning-sunbird  0.9+nobinonly
TestStreamLoader.cpp
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include "TestCommon.h"
00003 #include "nsNetUtil.h"
00004 #include "nsEventQueueUtils.h"
00005 #include "prlog.h"
00006 
00007 #if defined(PR_LOGGING)
00008 //
00009 // set NSPR_LOG_MODULES=Test:5
00010 //
00011 static PRLogModuleInfo *gTestLog = nsnull;
00012 #endif
00013 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
00014 
00015 static PRBool gKeepRunning = PR_TRUE;
00016 static nsIEventQueue* gEventQ = nsnull;
00017 
00018 class MyStreamLoaderObserver : public nsIStreamLoaderObserver
00019 {
00020 public:
00021   NS_DECL_ISUPPORTS
00022   NS_DECL_NSISTREAMLOADEROBSERVER
00023 };
00024 
00025 NS_IMPL_ISUPPORTS1(MyStreamLoaderObserver, nsIStreamLoaderObserver)
00026 
00027 NS_IMETHODIMP
00028 MyStreamLoaderObserver::OnStreamComplete(nsIStreamLoader *loader,
00029                                          nsISupports     *ctxt,
00030                                          nsresult         status,
00031                                          PRUint32         resultLen,
00032                                          const PRUint8   *result)
00033 {
00034   LOG(("OnStreamComplete [status=%x resultLen=%u]\n", status, resultLen));
00035 
00036   nsCOMPtr<nsIRequest> request;
00037   loader->GetRequest(getter_AddRefs(request));
00038   LOG(("  request=%p\n", request.get()));
00039 
00040   gKeepRunning = PR_FALSE;
00041   return NS_OK;
00042 }
00043 
00044 int main(int argc, char **argv)
00045 {
00046   if (test_common_init(&argc, &argv) != 0)
00047     return -1;
00048 
00049   if (argc < 2) {
00050     printf("usage: %s <url>\n", argv[0]);
00051     return -1;
00052   }
00053 
00054 #if defined(PR_LOGGING)
00055   gTestLog = PR_NewLogModule("Test");
00056 #endif
00057 
00058   nsresult rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
00059   if (NS_FAILED(rv))
00060     return -1;
00061 
00062   {
00063     // Create the Event Queue for this thread...
00064     rv = NS_GetMainEventQ(&gEventQ);
00065     if (NS_FAILED(rv))
00066       return -1;
00067 
00068     nsCOMPtr<nsIURI> uri;
00069     rv = NS_NewURI(getter_AddRefs(uri), nsDependentCString(argv[1]));
00070     if (NS_FAILED(rv))
00071       return -1;
00072 
00073     nsCOMPtr<nsIChannel> chan;
00074     rv = NS_NewChannel(getter_AddRefs(chan), uri);
00075     if (NS_FAILED(rv))
00076       return -1;
00077 
00078     nsCOMPtr<nsIStreamLoaderObserver> observer = new MyStreamLoaderObserver();
00079     if (!observer)
00080       return -1;
00081 
00082     nsCOMPtr<nsIStreamLoader> loader;
00083     rv = NS_NewStreamLoader(getter_AddRefs(loader), chan, observer, nsnull);
00084     if (NS_FAILED(rv))
00085       return -1;
00086 
00087     // Enter the message pump to allow the URL load to proceed.
00088     while (gKeepRunning) {
00089       PLEvent *e;
00090       gEventQ->WaitForEvent(&e);
00091       gEventQ->HandleEvent(e);
00092     }
00093   } // this scopes the nsCOMPtrs
00094   // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
00095   NS_ShutdownXPCOM(nsnull);
00096   return rv;
00097 }