Back to index

lightning-sunbird  0.9+nobinonly
TestIncrementalDownload.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* vim:set ts=2 sw=2 sts=2 cin et: */
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is Google Inc.
00019  * Portions created by the Initial Developer are Copyright (C) 2005
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Darin Fisher <darin@meer.net>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include <stdlib.h>
00040 #include "nsNetUtil.h"
00041 #include "nsIIncrementalDownload.h"
00042 #include "nsIRequestObserver.h"
00043 #include "nsIProgressEventSink.h"
00044 #include "nsIEventQueueService.h"
00045 #include "nsIEventQueue.h"
00046 #include "nsAutoPtr.h"
00047 #include "prprf.h"
00048 #include "prenv.h"
00049 
00050 //-----------------------------------------------------------------------------
00051 
00052 static nsresult SetupEventQ()
00053 {
00054   nsresult rv;
00055 
00056   nsCOMPtr<nsIEventQueueService> eqs =
00057       do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv);
00058   if (NS_FAILED(rv))
00059     return rv;
00060 
00061   return eqs->CreateMonitoredThreadEventQueue();
00062 }
00063 
00064 static nsresult PumpEvents()
00065 {
00066   nsresult rv;
00067 
00068   nsCOMPtr<nsIEventQueueService> eqs =
00069       do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv);
00070   if (NS_FAILED(rv))
00071     return rv;
00072 
00073   nsCOMPtr<nsIEventQueue> eq;
00074   rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(eq));
00075   if (NS_FAILED(rv))
00076     return rv;
00077 
00078   return eq->EventLoop();
00079 }
00080 
00081 static void QuitPumpingEvents()
00082 {
00083   PR_Interrupt(PR_GetCurrentThread());
00084 }
00085 
00086 //-----------------------------------------------------------------------------
00087 
00088 class FetchObserver : public nsIRequestObserver
00089                     , public nsIProgressEventSink
00090 {
00091 public:
00092   NS_DECL_ISUPPORTS
00093   NS_DECL_NSIREQUESTOBSERVER
00094   NS_DECL_NSIPROGRESSEVENTSINK
00095 };
00096 
00097 NS_IMPL_ISUPPORTS2(FetchObserver, nsIRequestObserver,
00098                    nsIProgressEventSink)
00099 
00100 NS_IMETHODIMP
00101 FetchObserver::OnStartRequest(nsIRequest *request, nsISupports *context)
00102 {
00103   printf("FetchObserver::OnStartRequest\n");
00104   return NS_OK;
00105 }
00106 
00107 NS_IMETHODIMP
00108 FetchObserver::OnProgress(nsIRequest *request, nsISupports *context,
00109                           PRUint64 progress, PRUint64 progressMax)
00110 {
00111   printf("FetchObserver::OnProgress [%lu/%lu]\n",
00112       PRUint32(progress), PRUint32(progressMax));
00113   return NS_OK;
00114 }
00115 
00116 NS_IMETHODIMP
00117 FetchObserver::OnStatus(nsIRequest *request, nsISupports *context,
00118                         nsresult status, const PRUnichar *statusText)
00119 {
00120   return NS_OK;
00121 }
00122 
00123 NS_IMETHODIMP
00124 FetchObserver::OnStopRequest(nsIRequest *request, nsISupports *context,
00125                              nsresult status)
00126 {
00127   printf("FetchObserver::OnStopRequest [status=%x]\n", status);
00128 
00129   QuitPumpingEvents();
00130   return NS_OK;
00131 }
00132 
00133 //-----------------------------------------------------------------------------
00134 
00135 static nsresult
00136 DoIncrementalFetch(const char *uriSpec, const char *resultPath, PRInt32 chunkSize,
00137                    PRInt32 interval)
00138 {
00139   nsCOMPtr<nsILocalFile> resultFile;
00140   nsresult rv = NS_NewNativeLocalFile(nsDependentCString(resultPath),
00141                                       PR_FALSE, getter_AddRefs(resultFile));
00142   if (NS_FAILED(rv))
00143     return rv;
00144 
00145   nsCOMPtr<nsIURI> uri;
00146   rv = NS_NewURI(getter_AddRefs(uri), uriSpec);
00147   if (NS_FAILED(rv))
00148     return rv;
00149 
00150   nsCOMPtr<nsIRequestObserver> observer = new FetchObserver();
00151   if (!observer)
00152     return NS_ERROR_OUT_OF_MEMORY;
00153   
00154   nsCOMPtr<nsIIncrementalDownload> download =
00155       do_CreateInstance(NS_INCREMENTALDOWNLOAD_CONTRACTID, &rv);
00156   if (NS_FAILED(rv))
00157     return rv;
00158 
00159   rv = download->Init(uri, resultFile, chunkSize, interval);
00160   if (NS_FAILED(rv))
00161     return rv;
00162 
00163   rv = SetupEventQ();
00164   if (NS_FAILED(rv))
00165     return rv;
00166 
00167   rv = download->Start(observer, nsnull);
00168   if (NS_FAILED(rv))
00169     return rv;
00170 
00171   return PumpEvents();
00172 }
00173 
00174 int
00175 main(int argc, char **argv)
00176 {
00177   if (PR_GetEnv("MOZ_BREAK_ON_MAIN"))
00178     NS_BREAK();
00179 
00180   if (argc < 5) {
00181     fprintf(stderr, "USAGE: TestIncrementalDownload <url> <file> <chunksize> <interval-in-seconds>\n");
00182     return -1;
00183   }
00184 
00185   nsresult rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
00186   if (NS_FAILED(rv))
00187     return -1;
00188 
00189   PRInt32 chunkSize = atoi(argv[3]);
00190   PRInt32 interval = atoi(argv[4]);
00191 
00192   rv = DoIncrementalFetch(argv[1], argv[2], chunkSize, interval);
00193   if (NS_FAILED(rv))
00194     fprintf(stderr, "ERROR: DoIncrementalFetch failed [%x]\n", rv);
00195 
00196   NS_ShutdownXPCOM(nsnull);
00197   return 0;
00198 }