Back to index

lightning-sunbird  0.9+nobinonly
TestRes.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsIResProtocolHandler.h"
00039 #include "nsIServiceManager.h"
00040 #include "nsIIOService.h"
00041 #include "nsIInputStream.h"
00042 #include "nsIComponentManager.h"
00043 #include "nsIComponentRegistrar.h"
00044 #include "nsIStreamListener.h"
00045 #include "nsIEventQueueService.h"
00046 #include "nsIURI.h"
00047 #include "nsCRT.h"
00048 #include "nsNetCID.h"
00049 
00050 static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
00051 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
00052 
00054 
00055 nsresult
00056 SetupMapping()
00057 {
00058     nsresult rv;
00059 
00060     nsCOMPtr<nsIIOService> serv(do_GetService(kIOServiceCID, &rv));
00061     if (NS_FAILED(rv)) return rv;
00062 
00063     nsCOMPtr<nsIProtocolHandler> ph;
00064     rv = serv->GetProtocolHandler("res", getter_AddRefs(ph));
00065     if (NS_FAILED(rv)) return rv;
00066 
00067     nsCOMPtr<nsIResProtocolHandler> resPH = do_QueryInterface(ph, &rv);
00068     if (NS_FAILED(rv)) return rv;
00069 
00070     rv = resPH->AppendSubstitution("foo", "file://y|/");
00071     if (NS_FAILED(rv)) return rv;
00072 
00073     rv = resPH->AppendSubstitution("foo", "file://y|/mozilla/dist/win32_D.OBJ/bin/");
00074     if (NS_FAILED(rv)) return rv;
00075 
00076     return rv;
00077 }
00078 
00080 
00081 nsresult
00082 TestOpenInputStream(const char* url)
00083 {
00084     nsresult rv;
00085 
00086     nsCOMPtr<nsIIOService> serv(do_GetService(kIOServiceCID, &rv));
00087     if (NS_FAILED(rv)) return rv;
00088 
00089     nsCOMPtr<nsIChannel> channel;
00090     rv = serv->NewChannel(url,
00091                           nsnull, // base uri
00092                           getter_AddRefs(channel));
00093     if (NS_FAILED(rv)) return rv;
00094 
00095     nsCOMPtr<nsIInputStream> in;
00096     rv = channel->Open(getter_AddRefs(in));
00097     if (NS_FAILED(rv)) {
00098         fprintf(stdout, "failed to OpenInputStream for %s\n", url);
00099         return NS_OK;
00100     }
00101 
00102     char buf[1024];
00103     while (1) {
00104         PRUint32 amt;
00105         rv = in->Read(buf, sizeof(buf), &amt);
00106         if (NS_FAILED(rv)) return rv;
00107         if (amt == 0) break;    // eof
00108 
00109         char* str = buf;
00110         while (amt-- > 0) {
00111             fputc(*str++, stdout);
00112         }
00113     }
00114     nsCOMPtr<nsIURI> uri;
00115     char* str;
00116 
00117     rv = channel->GetOriginalURI(getter_AddRefs(uri));
00118     if (NS_FAILED(rv)) return rv;
00119     rv = uri->GetSpec(&str);
00120     if (NS_FAILED(rv)) return rv;
00121     fprintf(stdout, "%s resolved to ", str);
00122     nsCRT::free(str);
00123 
00124     rv = channel->GetURI(getter_AddRefs(uri));
00125     if (NS_FAILED(rv)) return rv;
00126     rv = uri->GetSpec(&str);
00127     if (NS_FAILED(rv)) return rv;
00128     fprintf(stdout, "%s\n", str);
00129     nsCRT::free(str);
00130 
00131     return NS_OK;
00132 }
00133 
00135 
00136 PRBool gDone = PR_FALSE;
00137 nsIEventQueue* gEventQ = nsnull;
00138 
00139 class Listener : public nsIStreamListener 
00140 {
00141 public:
00142     NS_DECL_ISUPPORTS
00143 
00144     Listener() {}
00145     virtual ~Listener() {}
00146 
00147     NS_IMETHOD OnStartRequest(nsIRequest *request, nsISupports *ctxt) {
00148         nsresult rv;
00149         nsCOMPtr<nsIURI> uri;
00150         nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
00151 
00152         rv = channel->GetURI(getter_AddRefs(uri));
00153         if (NS_SUCCEEDED(rv)) {
00154             char* str;
00155             rv = uri->GetSpec(&str);
00156             if (NS_SUCCEEDED(rv)) {
00157                 fprintf(stdout, "Starting to load %s\n", str);
00158                 nsCRT::free(str);
00159             }
00160         }
00161         return NS_OK;
00162     }
00163 
00164     NS_IMETHOD OnStopRequest(nsIRequest *request, nsISupports *ctxt, 
00165                              nsresult aStatus) {
00166         nsresult rv;
00167         nsCOMPtr<nsIURI> uri;
00168         nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
00169 
00170         rv = channel->GetURI(getter_AddRefs(uri));
00171         if (NS_SUCCEEDED(rv)) {
00172             char* str;
00173             rv = uri->GetSpec(&str);
00174             if (NS_SUCCEEDED(rv)) {
00175                 fprintf(stdout, "Ending load %s, status=%x\n", str, aStatus);
00176                 nsCRT::free(str);
00177             }
00178         }
00179         gDone = PR_TRUE;
00180         return NS_OK;
00181     }
00182 
00183     NS_IMETHOD OnDataAvailable(nsIRequest *request, nsISupports *ctxt, 
00184                                nsIInputStream *inStr,
00185                                PRUint32 sourceOffset, PRUint32 count) {
00186         nsresult rv;
00187         char buf[1024];
00188         while (count > 0) {
00189             PRUint32 amt;
00190             rv = inStr->Read(buf, sizeof(buf), &amt);
00191             count -= amt;
00192             char* c = buf;
00193             while (amt-- > 0) {
00194                 fputc(*c++, stdout);
00195             }
00196         }
00197         return NS_OK;
00198     }
00199 };
00200 
00201 NS_IMPL_ISUPPORTS2(Listener, nsIStreamListener, nsIRequestObserver)
00202 
00203 nsresult
00204 TestAsyncRead(const char* url)
00205 {
00206     nsresult rv;
00207 
00208     nsCOMPtr<nsIEventQueueService> eventQService = 
00209              do_GetService(kEventQueueServiceCID, &rv);
00210     if (NS_FAILED(rv)) return rv;
00211 
00212     rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
00213     if (NS_FAILED(rv)) return rv;
00214 
00215     nsCOMPtr<nsIIOService> serv(do_GetService(kIOServiceCID, &rv));
00216     if (NS_FAILED(rv)) return rv;
00217 
00218     nsCOMPtr<nsIChannel> channel;
00219     rv = serv->NewChannel(url,
00220                           nsnull, // base uri
00221                           getter_AddRefs(channel));
00222     if (NS_FAILED(rv)) return rv;
00223 
00224     nsCOMPtr<nsIStreamListener> listener = new Listener();
00225     if (listener == nsnull)
00226         return NS_ERROR_OUT_OF_MEMORY;
00227     rv = channel->AsyncOpen(nsnull, listener);
00228     if (NS_FAILED(rv)) return rv;
00229 
00230     while (!gDone) {
00231         PLEvent* event;
00232         rv = gEventQ->GetEvent(&event);
00233         if (NS_FAILED(rv)) return rv;
00234         rv = gEventQ->HandleEvent(event);
00235         if (NS_FAILED(rv)) return rv;
00236     }
00237 
00238     return rv;
00239 }
00240 
00241 int
00242 main(int argc, char* argv[])
00243 {
00244     nsresult rv;
00245     {
00246         nsCOMPtr<nsIServiceManager> servMan;
00247         NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00248         nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00249         NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00250         if (registrar)
00251             registrar->AutoRegister(nsnull);
00252 
00253         NS_ASSERTION(NS_SUCCEEDED(rv), "AutoregisterComponents failed");
00254 
00255         if (argc < 2) {
00256             printf("usage: %s resource://foo/<path-to-resolve>\n", argv[0]);
00257             return -1;
00258         }
00259 
00260         rv = SetupMapping();
00261         NS_ASSERTION(NS_SUCCEEDED(rv), "SetupMapping failed");
00262         if (NS_FAILED(rv)) return rv;
00263 
00264         rv = TestOpenInputStream(argv[1]);
00265         NS_ASSERTION(NS_SUCCEEDED(rv), "TestOpenInputStream failed");
00266 
00267         rv = TestAsyncRead(argv[1]);
00268         NS_ASSERTION(NS_SUCCEEDED(rv), "TestAsyncRead failed");
00269     } // this scopes the nsCOMPtrs
00270     // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
00271     rv = NS_ShutdownXPCOM(nsnull);
00272     NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
00273     return rv;
00274 }
00275