Back to index

lightning-sunbird  0.9+nobinonly
nsJARProtocolHandler.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; 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  *   Benjamin Smedberg <benjamin@smedbergs.us>
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 "nsAutoPtr.h"
00040 #include "nsJARProtocolHandler.h"
00041 #include "nsIIOService.h"
00042 #include "nsCRT.h"
00043 #include "nsIComponentManager.h"
00044 #include "nsIServiceManager.h"
00045 #include "nsJARURI.h"
00046 #include "nsIURL.h"
00047 #include "nsJARChannel.h"
00048 #include "nsXPIDLString.h"
00049 #include "nsString.h"
00050 #include "nsNetCID.h"
00051 #include "nsIMIMEService.h"
00052 #include "nsMimeTypes.h"
00053 
00054 static NS_DEFINE_CID(kZipReaderCacheCID, NS_ZIPREADERCACHE_CID);
00055 
00056 #define NS_JAR_CACHE_SIZE 32
00057 
00058 //-----------------------------------------------------------------------------
00059 
00060 nsJARProtocolHandler *gJarHandler = nsnull;
00061 
00062 nsJARProtocolHandler::nsJARProtocolHandler()
00063 {
00064     gJarHandler = this;
00065 }
00066 
00067 nsJARProtocolHandler::~nsJARProtocolHandler()
00068 {
00069     gJarHandler = nsnull;
00070 }
00071 
00072 nsresult
00073 nsJARProtocolHandler::Init()
00074 {
00075     nsresult rv;
00076 
00077     mJARCache = do_CreateInstance(kZipReaderCacheCID, &rv);
00078     if (NS_FAILED(rv)) return rv;
00079 
00080     rv = mJARCache->Init(NS_JAR_CACHE_SIZE);
00081     return rv;
00082 }
00083 
00084 nsIMIMEService * 
00085 nsJARProtocolHandler::MimeService()
00086 {
00087     if (!mMimeService)
00088         mMimeService = do_GetService("@mozilla.org/mime;1");
00089 
00090     return mMimeService.get();
00091 }
00092 
00093 NS_IMPL_THREADSAFE_ISUPPORTS3(nsJARProtocolHandler,
00094                               nsIJARProtocolHandler,
00095                               nsIProtocolHandler,
00096                               nsISupportsWeakReference)
00097 
00098 NS_METHOD
00099 nsJARProtocolHandler::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
00100 {
00101     if (aOuter)
00102         return NS_ERROR_NO_AGGREGATION;
00103 
00104     nsJARProtocolHandler* ph = new nsJARProtocolHandler();
00105     if (ph == nsnull)
00106         return NS_ERROR_OUT_OF_MEMORY;
00107     NS_ADDREF(ph);
00108     nsresult rv = ph->Init();
00109     if (NS_SUCCEEDED(rv)) {
00110         rv = ph->QueryInterface(aIID, aResult);
00111     }
00112     NS_RELEASE(ph);
00113     return rv;
00114 }
00115 
00116 NS_IMETHODIMP
00117 nsJARProtocolHandler::GetJARCache(nsIZipReaderCache* *result)
00118 {
00119     *result = mJARCache;
00120     NS_ADDREF(*result);
00121     return NS_OK;
00122 }
00123 
00125 // nsIProtocolHandler methods:
00126 
00127 NS_IMETHODIMP
00128 nsJARProtocolHandler::GetScheme(nsACString &result)
00129 {
00130     result.AssignLiteral("jar");
00131     return NS_OK;
00132 }
00133 
00134 NS_IMETHODIMP
00135 nsJARProtocolHandler::GetDefaultPort(PRInt32 *result)
00136 {
00137     *result = -1;        // no port for JAR: URLs
00138     return NS_OK;
00139 }
00140 
00141 NS_IMETHODIMP
00142 nsJARProtocolHandler::GetProtocolFlags(PRUint32 *result)
00143 {
00144     *result = URI_NORELATIVE | URI_NOAUTH;
00145     /* Although jar uris have their own concept of relative urls
00146        it is very different from the standard behaviour, so we
00147        have to say norelative here! */
00148     return NS_OK;
00149 }
00150 
00151 NS_IMETHODIMP
00152 nsJARProtocolHandler::NewURI(const nsACString &aSpec,
00153                              const char *aCharset,
00154                              nsIURI *aBaseURI,
00155                              nsIURI **result)
00156 {
00157     nsresult rv = NS_OK;
00158 
00159     nsRefPtr<nsJARURI> jarURI = new nsJARURI();
00160     if (!jarURI)
00161         return NS_ERROR_OUT_OF_MEMORY;
00162 
00163     rv = jarURI->Init(aCharset);
00164     NS_ENSURE_SUCCESS(rv, rv);
00165 
00166     rv = jarURI->SetSpecWithBase(aSpec, aBaseURI);
00167     if (NS_FAILED(rv))
00168         return rv;
00169 
00170     NS_ADDREF(*result = jarURI);
00171     return rv;
00172 }
00173 
00174 NS_IMETHODIMP
00175 nsJARProtocolHandler::NewChannel(nsIURI *uri, nsIChannel **result)
00176 {
00177     nsJARChannel *chan = new nsJARChannel();
00178     if (!chan)
00179         return NS_ERROR_OUT_OF_MEMORY;
00180     NS_ADDREF(chan);
00181 
00182     nsresult rv = chan->Init(uri);
00183     if (NS_FAILED(rv)) {
00184         NS_RELEASE(chan);
00185         return rv;
00186     }
00187 
00188     *result = chan;
00189     return NS_OK;
00190 }
00191 
00192 
00193 NS_IMETHODIMP
00194 nsJARProtocolHandler::AllowPort(PRInt32 port, const char *scheme, PRBool *_retval)
00195 {
00196     // don't override anything.
00197     *_retval = PR_FALSE;
00198     return NS_OK;
00199 }
00200