b=450088 backing out (new reftest failed)
[wine-gecko.git] / modules / libjar / nsJARProtocolHandler.cpp
blob3cc534be3d6ccd8a8e1e680105c027cfb7e5f806
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Benjamin Smedberg <benjamin@smedbergs.us>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsAutoPtr.h"
40 #include "nsJARProtocolHandler.h"
41 #include "nsIIOService.h"
42 #include "nsCRT.h"
43 #include "nsIComponentManager.h"
44 #include "nsIServiceManager.h"
45 #include "nsJARURI.h"
46 #include "nsIURL.h"
47 #include "nsJARChannel.h"
48 #include "nsXPIDLString.h"
49 #include "nsString.h"
50 #include "nsNetCID.h"
51 #include "nsIMIMEService.h"
52 #include "nsMimeTypes.h"
54 static NS_DEFINE_CID(kZipReaderCacheCID, NS_ZIPREADERCACHE_CID);
56 #define NS_JAR_CACHE_SIZE 32
58 //-----------------------------------------------------------------------------
60 nsJARProtocolHandler *gJarHandler = nsnull;
62 nsJARProtocolHandler::nsJARProtocolHandler()
66 nsJARProtocolHandler::~nsJARProtocolHandler()
70 nsresult
71 nsJARProtocolHandler::Init()
73 nsresult rv;
75 mJARCache = do_CreateInstance(kZipReaderCacheCID, &rv);
76 if (NS_FAILED(rv)) return rv;
78 rv = mJARCache->Init(NS_JAR_CACHE_SIZE);
79 return rv;
82 nsIMIMEService *
83 nsJARProtocolHandler::MimeService()
85 if (!mMimeService)
86 mMimeService = do_GetService("@mozilla.org/mime;1");
88 return mMimeService.get();
91 NS_IMPL_THREADSAFE_ISUPPORTS3(nsJARProtocolHandler,
92 nsIJARProtocolHandler,
93 nsIProtocolHandler,
94 nsISupportsWeakReference)
96 nsJARProtocolHandler*
97 nsJARProtocolHandler::GetSingleton()
99 if (!gJarHandler) {
100 gJarHandler = new nsJARProtocolHandler();
101 if (!gJarHandler)
102 return nsnull;
104 NS_ADDREF(gJarHandler);
105 nsresult rv = gJarHandler->Init();
106 if (NS_FAILED(rv)) {
107 NS_RELEASE(gJarHandler);
108 return nsnull;
111 NS_ADDREF(gJarHandler);
112 return gJarHandler;
115 NS_IMETHODIMP
116 nsJARProtocolHandler::GetJARCache(nsIZipReaderCache* *result)
118 *result = mJARCache;
119 NS_ADDREF(*result);
120 return NS_OK;
123 ////////////////////////////////////////////////////////////////////////////////
124 // nsIProtocolHandler methods:
126 NS_IMETHODIMP
127 nsJARProtocolHandler::GetScheme(nsACString &result)
129 result.AssignLiteral("jar");
130 return NS_OK;
133 NS_IMETHODIMP
134 nsJARProtocolHandler::GetDefaultPort(PRInt32 *result)
136 *result = -1; // no port for JAR: URLs
137 return NS_OK;
140 NS_IMETHODIMP
141 nsJARProtocolHandler::GetProtocolFlags(PRUint32 *result)
143 // URI_LOADABLE_BY_ANYONE, since it's our inner URI that will matter
144 // anyway.
145 *result = URI_NORELATIVE | URI_NOAUTH | URI_LOADABLE_BY_ANYONE;
146 /* Although jar uris have their own concept of relative urls
147 it is very different from the standard behaviour, so we
148 have to say norelative here! */
149 return NS_OK;
152 NS_IMETHODIMP
153 nsJARProtocolHandler::NewURI(const nsACString &aSpec,
154 const char *aCharset,
155 nsIURI *aBaseURI,
156 nsIURI **result)
158 nsresult rv = NS_OK;
160 nsRefPtr<nsJARURI> jarURI = new nsJARURI();
161 if (!jarURI)
162 return NS_ERROR_OUT_OF_MEMORY;
164 rv = jarURI->Init(aCharset);
165 NS_ENSURE_SUCCESS(rv, rv);
167 rv = jarURI->SetSpecWithBase(aSpec, aBaseURI);
168 if (NS_FAILED(rv))
169 return rv;
171 NS_ADDREF(*result = jarURI);
172 return rv;
175 NS_IMETHODIMP
176 nsJARProtocolHandler::NewChannel(nsIURI *uri, nsIChannel **result)
178 nsJARChannel *chan = new nsJARChannel();
179 if (!chan)
180 return NS_ERROR_OUT_OF_MEMORY;
181 NS_ADDREF(chan);
183 nsresult rv = chan->Init(uri);
184 if (NS_FAILED(rv)) {
185 NS_RELEASE(chan);
186 return rv;
189 *result = chan;
190 return NS_OK;
194 NS_IMETHODIMP
195 nsJARProtocolHandler::AllowPort(PRInt32 port, const char *scheme, PRBool *_retval)
197 // don't override anything.
198 *_retval = PR_FALSE;
199 return NS_OK;
202 ////////////////////////////////////////////////////////////////////////////////