nsJARProtocolHandler.cpp (2745B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #include "mozilla/ClearOnShutdown.h" 7 #include "nsJARProtocolHandler.h" 8 #include "nsComponentManagerUtils.h" 9 #include "nsCRT.h" 10 #include "nsJARURI.h" 11 #include "nsJARChannel.h" 12 #include "nsString.h" 13 #include "nsNetCID.h" 14 #include "nsIMIMEService.h" 15 #include "nsMimeTypes.h" 16 #include "nsThreadUtils.h" 17 18 static NS_DEFINE_CID(kZipReaderCacheCID, NS_ZIPREADERCACHE_CID); 19 20 #define NS_JAR_CACHE_SIZE 32 21 22 //----------------------------------------------------------------------------- 23 24 mozilla::StaticRefPtr<nsJARProtocolHandler> gJarHandler; 25 26 nsJARProtocolHandler::nsJARProtocolHandler() { MOZ_ASSERT(NS_IsMainThread()); } 27 28 nsJARProtocolHandler::~nsJARProtocolHandler() {} 29 30 nsresult nsJARProtocolHandler::Init() { 31 nsresult rv; 32 33 mJARCache = do_CreateInstance(kZipReaderCacheCID, &rv); 34 if (NS_FAILED(rv)) return rv; 35 36 rv = mJARCache->Init(NS_JAR_CACHE_SIZE); 37 return rv; 38 } 39 40 nsIMIMEService* nsJARProtocolHandler::MimeService() { 41 if (!mMimeService) mMimeService = do_GetService("@mozilla.org/mime;1"); 42 43 return mMimeService.get(); 44 } 45 46 NS_IMPL_ISUPPORTS(nsJARProtocolHandler, nsIProtocolHandler, 47 nsISupportsWeakReference) 48 49 already_AddRefed<nsJARProtocolHandler> nsJARProtocolHandler::GetSingleton() { 50 if (!gJarHandler) { 51 gJarHandler = new nsJARProtocolHandler(); 52 if (NS_SUCCEEDED(gJarHandler->Init())) { 53 ClearOnShutdown(&gJarHandler); 54 } else { 55 gJarHandler = nullptr; 56 } 57 } 58 return do_AddRef(gJarHandler); 59 } 60 61 //////////////////////////////////////////////////////////////////////////////// 62 // nsIProtocolHandler methods: 63 64 NS_IMETHODIMP 65 nsJARProtocolHandler::GetScheme(nsACString& result) { 66 result.AssignLiteral("jar"); 67 return NS_OK; 68 } 69 70 NS_IMETHODIMP 71 nsJARProtocolHandler::NewChannel(nsIURI* uri, nsILoadInfo* aLoadInfo, 72 nsIChannel** result) { 73 RefPtr<nsJARChannel> chan = new nsJARChannel(); 74 if (!chan) { 75 return NS_ERROR_OUT_OF_MEMORY; 76 } 77 78 nsresult rv = chan->Init(uri); 79 if (NS_FAILED(rv)) { 80 return rv; 81 } 82 83 // set the loadInfo on the new channel 84 rv = chan->SetLoadInfo(aLoadInfo); 85 if (NS_FAILED(rv)) { 86 return rv; 87 } 88 89 *result = chan.forget().take(); 90 return NS_OK; 91 } 92 93 NS_IMETHODIMP 94 nsJARProtocolHandler::AllowPort(int32_t port, const char* scheme, 95 bool* _retval) { 96 // don't override anything. 97 *_retval = false; 98 return NS_OK; 99 } 100 101 ////////////////////////////////////////////////////////////////////////////////