michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsAutoPtr.h" michael@0: #include "nsJARProtocolHandler.h" michael@0: #include "nsIIOService.h" michael@0: #include "nsCRT.h" michael@0: #include "nsIComponentManager.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsJARURI.h" michael@0: #include "nsIURL.h" michael@0: #include "nsJARChannel.h" michael@0: #include "nsXPIDLString.h" michael@0: #include "nsString.h" michael@0: #include "nsNetCID.h" michael@0: #include "nsIMIMEService.h" michael@0: #include "nsMimeTypes.h" michael@0: #include "nsIRemoteOpenFileListener.h" michael@0: #include "nsIHashable.h" michael@0: #include "nsThreadUtils.h" michael@0: #include "nsXULAppAPI.h" michael@0: #include "nsTArray.h" michael@0: michael@0: static NS_DEFINE_CID(kZipReaderCacheCID, NS_ZIPREADERCACHE_CID); michael@0: michael@0: #define NS_JAR_CACHE_SIZE 32 michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: nsJARProtocolHandler *gJarHandler = nullptr; michael@0: michael@0: nsJARProtocolHandler::nsJARProtocolHandler() michael@0: : mIsMainProcess(XRE_GetProcessType() == GeckoProcessType_Default) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: } michael@0: michael@0: nsJARProtocolHandler::~nsJARProtocolHandler() michael@0: { michael@0: MOZ_ASSERT(gJarHandler == this); michael@0: gJarHandler = nullptr; michael@0: } michael@0: michael@0: nsresult michael@0: nsJARProtocolHandler::Init() michael@0: { michael@0: nsresult rv; michael@0: michael@0: mJARCache = do_CreateInstance(kZipReaderCacheCID, &rv); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: rv = mJARCache->Init(NS_JAR_CACHE_SIZE); michael@0: return rv; michael@0: } michael@0: michael@0: nsIMIMEService * michael@0: nsJARProtocolHandler::MimeService() michael@0: { michael@0: if (!mMimeService) michael@0: mMimeService = do_GetService("@mozilla.org/mime;1"); michael@0: michael@0: return mMimeService.get(); michael@0: } michael@0: michael@0: bool michael@0: nsJARProtocolHandler::RemoteOpenFileInProgress( michael@0: nsIHashable *aRemoteFile, michael@0: nsIRemoteOpenFileListener *aListener) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: MOZ_ASSERT(aRemoteFile); michael@0: MOZ_ASSERT(aListener); michael@0: michael@0: if (IsMainProcess()) { michael@0: MOZ_CRASH("Shouldn't be called in the main process!"); michael@0: } michael@0: michael@0: RemoteFileListenerArray *listeners; michael@0: if (mRemoteFileListeners.Get(aRemoteFile, &listeners)) { michael@0: listeners->AppendElement(aListener); michael@0: return true; michael@0: } michael@0: michael@0: // We deliberately don't put the listener in the new array since the first michael@0: // load is handled differently. michael@0: mRemoteFileListeners.Put(aRemoteFile, new RemoteFileListenerArray()); michael@0: return false; michael@0: } michael@0: michael@0: void michael@0: nsJARProtocolHandler::RemoteOpenFileComplete(nsIHashable *aRemoteFile, michael@0: nsresult aStatus) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: MOZ_ASSERT(aRemoteFile); michael@0: michael@0: if (IsMainProcess()) { michael@0: MOZ_CRASH("Shouldn't be called in the main process!"); michael@0: } michael@0: michael@0: RemoteFileListenerArray *tempListeners; michael@0: if (!mRemoteFileListeners.Get(aRemoteFile, &tempListeners)) { michael@0: return; michael@0: } michael@0: michael@0: // Save the listeners in a stack array. The call to Remove() below will michael@0: // delete the tempListeners array. michael@0: RemoteFileListenerArray listeners; michael@0: tempListeners->SwapElements(listeners); michael@0: michael@0: mRemoteFileListeners.Remove(aRemoteFile); michael@0: michael@0: // Technically we must fail OnRemoteFileComplete() since OpenNSPRFileDesc() michael@0: // won't succeed here. We've trained nsJARChannel to recognize michael@0: // NS_ERROR_ALREADY_OPENED in this case as "proceed to JAR cache hit." michael@0: nsresult status = NS_SUCCEEDED(aStatus) ? NS_ERROR_ALREADY_OPENED : aStatus; michael@0: michael@0: uint32_t count = listeners.Length(); michael@0: for (uint32_t index = 0; index < count; index++) { michael@0: listeners[index]->OnRemoteFileOpenComplete(status); michael@0: } michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsJARProtocolHandler, michael@0: nsIJARProtocolHandler, michael@0: nsIProtocolHandler, michael@0: nsISupportsWeakReference) michael@0: michael@0: nsJARProtocolHandler* michael@0: nsJARProtocolHandler::GetSingleton() michael@0: { michael@0: if (!gJarHandler) { michael@0: gJarHandler = new nsJARProtocolHandler(); michael@0: if (!gJarHandler) michael@0: return nullptr; michael@0: michael@0: NS_ADDREF(gJarHandler); michael@0: nsresult rv = gJarHandler->Init(); michael@0: if (NS_FAILED(rv)) { michael@0: NS_RELEASE(gJarHandler); michael@0: return nullptr; michael@0: } michael@0: } michael@0: NS_ADDREF(gJarHandler); michael@0: return gJarHandler; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsJARProtocolHandler::GetJARCache(nsIZipReaderCache* *result) michael@0: { michael@0: *result = mJARCache; michael@0: NS_ADDREF(*result); michael@0: return NS_OK; michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // nsIProtocolHandler methods: michael@0: michael@0: NS_IMETHODIMP michael@0: nsJARProtocolHandler::GetScheme(nsACString &result) michael@0: { michael@0: result.AssignLiteral("jar"); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsJARProtocolHandler::GetDefaultPort(int32_t *result) michael@0: { michael@0: *result = -1; // no port for JAR: URLs michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsJARProtocolHandler::GetProtocolFlags(uint32_t *result) michael@0: { michael@0: // URI_LOADABLE_BY_ANYONE, since it's our inner URI that will matter michael@0: // anyway. michael@0: *result = URI_NORELATIVE | URI_NOAUTH | URI_LOADABLE_BY_ANYONE; michael@0: /* Although jar uris have their own concept of relative urls michael@0: it is very different from the standard behaviour, so we michael@0: have to say norelative here! */ michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsJARProtocolHandler::NewURI(const nsACString &aSpec, michael@0: const char *aCharset, michael@0: nsIURI *aBaseURI, michael@0: nsIURI **result) michael@0: { michael@0: nsresult rv = NS_OK; michael@0: michael@0: nsRefPtr jarURI = new nsJARURI(); michael@0: if (!jarURI) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: rv = jarURI->Init(aCharset); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = jarURI->SetSpecWithBase(aSpec, aBaseURI); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: NS_ADDREF(*result = jarURI); michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsJARProtocolHandler::NewChannel(nsIURI *uri, nsIChannel **result) michael@0: { michael@0: nsJARChannel *chan = new nsJARChannel(); michael@0: if (!chan) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: NS_ADDREF(chan); michael@0: michael@0: nsresult rv = chan->Init(uri); michael@0: if (NS_FAILED(rv)) { michael@0: NS_RELEASE(chan); michael@0: return rv; michael@0: } michael@0: michael@0: *result = chan; michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsJARProtocolHandler::AllowPort(int32_t port, const char *scheme, bool *_retval) michael@0: { michael@0: // don't override anything. michael@0: *_retval = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: ////////////////////////////////////////////////////////////////////////////////