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
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.
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"
43 #include "nsIComponentManager.h"
44 #include "nsIServiceManager.h"
47 #include "nsJARChannel.h"
48 #include "nsXPIDLString.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()
71 nsJARProtocolHandler::Init()
75 mJARCache
= do_CreateInstance(kZipReaderCacheCID
, &rv
);
76 if (NS_FAILED(rv
)) return rv
;
78 rv
= mJARCache
->Init(NS_JAR_CACHE_SIZE
);
83 nsJARProtocolHandler::MimeService()
86 mMimeService
= do_GetService("@mozilla.org/mime;1");
88 return mMimeService
.get();
91 NS_IMPL_THREADSAFE_ISUPPORTS3(nsJARProtocolHandler
,
92 nsIJARProtocolHandler
,
94 nsISupportsWeakReference
)
97 nsJARProtocolHandler::GetSingleton()
100 gJarHandler
= new nsJARProtocolHandler();
104 NS_ADDREF(gJarHandler
);
105 nsresult rv
= gJarHandler
->Init();
107 NS_RELEASE(gJarHandler
);
111 NS_ADDREF(gJarHandler
);
116 nsJARProtocolHandler::GetJARCache(nsIZipReaderCache
* *result
)
123 ////////////////////////////////////////////////////////////////////////////////
124 // nsIProtocolHandler methods:
127 nsJARProtocolHandler::GetScheme(nsACString
&result
)
129 result
.AssignLiteral("jar");
134 nsJARProtocolHandler::GetDefaultPort(PRInt32
*result
)
136 *result
= -1; // no port for JAR: URLs
141 nsJARProtocolHandler::GetProtocolFlags(PRUint32
*result
)
143 // URI_LOADABLE_BY_ANYONE, since it's our inner URI that will matter
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! */
153 nsJARProtocolHandler::NewURI(const nsACString
&aSpec
,
154 const char *aCharset
,
160 nsRefPtr
<nsJARURI
> jarURI
= new nsJARURI();
162 return NS_ERROR_OUT_OF_MEMORY
;
164 rv
= jarURI
->Init(aCharset
);
165 NS_ENSURE_SUCCESS(rv
, rv
);
167 rv
= jarURI
->SetSpecWithBase(aSpec
, aBaseURI
);
171 NS_ADDREF(*result
= jarURI
);
176 nsJARProtocolHandler::NewChannel(nsIURI
*uri
, nsIChannel
**result
)
178 nsJARChannel
*chan
= new nsJARChannel();
180 return NS_ERROR_OUT_OF_MEMORY
;
183 nsresult rv
= chan
->Init(uri
);
195 nsJARProtocolHandler::AllowPort(PRInt32 port
, const char *scheme
, PRBool
*_retval
)
197 // don't override anything.
202 ////////////////////////////////////////////////////////////////////////////////