modules/libjar/nsJARProtocolHandler.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsAutoPtr.h"
michael@0 7 #include "nsJARProtocolHandler.h"
michael@0 8 #include "nsIIOService.h"
michael@0 9 #include "nsCRT.h"
michael@0 10 #include "nsIComponentManager.h"
michael@0 11 #include "nsIServiceManager.h"
michael@0 12 #include "nsJARURI.h"
michael@0 13 #include "nsIURL.h"
michael@0 14 #include "nsJARChannel.h"
michael@0 15 #include "nsXPIDLString.h"
michael@0 16 #include "nsString.h"
michael@0 17 #include "nsNetCID.h"
michael@0 18 #include "nsIMIMEService.h"
michael@0 19 #include "nsMimeTypes.h"
michael@0 20 #include "nsIRemoteOpenFileListener.h"
michael@0 21 #include "nsIHashable.h"
michael@0 22 #include "nsThreadUtils.h"
michael@0 23 #include "nsXULAppAPI.h"
michael@0 24 #include "nsTArray.h"
michael@0 25
michael@0 26 static NS_DEFINE_CID(kZipReaderCacheCID, NS_ZIPREADERCACHE_CID);
michael@0 27
michael@0 28 #define NS_JAR_CACHE_SIZE 32
michael@0 29
michael@0 30 //-----------------------------------------------------------------------------
michael@0 31
michael@0 32 nsJARProtocolHandler *gJarHandler = nullptr;
michael@0 33
michael@0 34 nsJARProtocolHandler::nsJARProtocolHandler()
michael@0 35 : mIsMainProcess(XRE_GetProcessType() == GeckoProcessType_Default)
michael@0 36 {
michael@0 37 MOZ_ASSERT(NS_IsMainThread());
michael@0 38 }
michael@0 39
michael@0 40 nsJARProtocolHandler::~nsJARProtocolHandler()
michael@0 41 {
michael@0 42 MOZ_ASSERT(gJarHandler == this);
michael@0 43 gJarHandler = nullptr;
michael@0 44 }
michael@0 45
michael@0 46 nsresult
michael@0 47 nsJARProtocolHandler::Init()
michael@0 48 {
michael@0 49 nsresult rv;
michael@0 50
michael@0 51 mJARCache = do_CreateInstance(kZipReaderCacheCID, &rv);
michael@0 52 if (NS_FAILED(rv)) return rv;
michael@0 53
michael@0 54 rv = mJARCache->Init(NS_JAR_CACHE_SIZE);
michael@0 55 return rv;
michael@0 56 }
michael@0 57
michael@0 58 nsIMIMEService *
michael@0 59 nsJARProtocolHandler::MimeService()
michael@0 60 {
michael@0 61 if (!mMimeService)
michael@0 62 mMimeService = do_GetService("@mozilla.org/mime;1");
michael@0 63
michael@0 64 return mMimeService.get();
michael@0 65 }
michael@0 66
michael@0 67 bool
michael@0 68 nsJARProtocolHandler::RemoteOpenFileInProgress(
michael@0 69 nsIHashable *aRemoteFile,
michael@0 70 nsIRemoteOpenFileListener *aListener)
michael@0 71 {
michael@0 72 MOZ_ASSERT(NS_IsMainThread());
michael@0 73 MOZ_ASSERT(aRemoteFile);
michael@0 74 MOZ_ASSERT(aListener);
michael@0 75
michael@0 76 if (IsMainProcess()) {
michael@0 77 MOZ_CRASH("Shouldn't be called in the main process!");
michael@0 78 }
michael@0 79
michael@0 80 RemoteFileListenerArray *listeners;
michael@0 81 if (mRemoteFileListeners.Get(aRemoteFile, &listeners)) {
michael@0 82 listeners->AppendElement(aListener);
michael@0 83 return true;
michael@0 84 }
michael@0 85
michael@0 86 // We deliberately don't put the listener in the new array since the first
michael@0 87 // load is handled differently.
michael@0 88 mRemoteFileListeners.Put(aRemoteFile, new RemoteFileListenerArray());
michael@0 89 return false;
michael@0 90 }
michael@0 91
michael@0 92 void
michael@0 93 nsJARProtocolHandler::RemoteOpenFileComplete(nsIHashable *aRemoteFile,
michael@0 94 nsresult aStatus)
michael@0 95 {
michael@0 96 MOZ_ASSERT(NS_IsMainThread());
michael@0 97 MOZ_ASSERT(aRemoteFile);
michael@0 98
michael@0 99 if (IsMainProcess()) {
michael@0 100 MOZ_CRASH("Shouldn't be called in the main process!");
michael@0 101 }
michael@0 102
michael@0 103 RemoteFileListenerArray *tempListeners;
michael@0 104 if (!mRemoteFileListeners.Get(aRemoteFile, &tempListeners)) {
michael@0 105 return;
michael@0 106 }
michael@0 107
michael@0 108 // Save the listeners in a stack array. The call to Remove() below will
michael@0 109 // delete the tempListeners array.
michael@0 110 RemoteFileListenerArray listeners;
michael@0 111 tempListeners->SwapElements(listeners);
michael@0 112
michael@0 113 mRemoteFileListeners.Remove(aRemoteFile);
michael@0 114
michael@0 115 // Technically we must fail OnRemoteFileComplete() since OpenNSPRFileDesc()
michael@0 116 // won't succeed here. We've trained nsJARChannel to recognize
michael@0 117 // NS_ERROR_ALREADY_OPENED in this case as "proceed to JAR cache hit."
michael@0 118 nsresult status = NS_SUCCEEDED(aStatus) ? NS_ERROR_ALREADY_OPENED : aStatus;
michael@0 119
michael@0 120 uint32_t count = listeners.Length();
michael@0 121 for (uint32_t index = 0; index < count; index++) {
michael@0 122 listeners[index]->OnRemoteFileOpenComplete(status);
michael@0 123 }
michael@0 124 }
michael@0 125
michael@0 126 NS_IMPL_ISUPPORTS(nsJARProtocolHandler,
michael@0 127 nsIJARProtocolHandler,
michael@0 128 nsIProtocolHandler,
michael@0 129 nsISupportsWeakReference)
michael@0 130
michael@0 131 nsJARProtocolHandler*
michael@0 132 nsJARProtocolHandler::GetSingleton()
michael@0 133 {
michael@0 134 if (!gJarHandler) {
michael@0 135 gJarHandler = new nsJARProtocolHandler();
michael@0 136 if (!gJarHandler)
michael@0 137 return nullptr;
michael@0 138
michael@0 139 NS_ADDREF(gJarHandler);
michael@0 140 nsresult rv = gJarHandler->Init();
michael@0 141 if (NS_FAILED(rv)) {
michael@0 142 NS_RELEASE(gJarHandler);
michael@0 143 return nullptr;
michael@0 144 }
michael@0 145 }
michael@0 146 NS_ADDREF(gJarHandler);
michael@0 147 return gJarHandler;
michael@0 148 }
michael@0 149
michael@0 150 NS_IMETHODIMP
michael@0 151 nsJARProtocolHandler::GetJARCache(nsIZipReaderCache* *result)
michael@0 152 {
michael@0 153 *result = mJARCache;
michael@0 154 NS_ADDREF(*result);
michael@0 155 return NS_OK;
michael@0 156 }
michael@0 157
michael@0 158 ////////////////////////////////////////////////////////////////////////////////
michael@0 159 // nsIProtocolHandler methods:
michael@0 160
michael@0 161 NS_IMETHODIMP
michael@0 162 nsJARProtocolHandler::GetScheme(nsACString &result)
michael@0 163 {
michael@0 164 result.AssignLiteral("jar");
michael@0 165 return NS_OK;
michael@0 166 }
michael@0 167
michael@0 168 NS_IMETHODIMP
michael@0 169 nsJARProtocolHandler::GetDefaultPort(int32_t *result)
michael@0 170 {
michael@0 171 *result = -1; // no port for JAR: URLs
michael@0 172 return NS_OK;
michael@0 173 }
michael@0 174
michael@0 175 NS_IMETHODIMP
michael@0 176 nsJARProtocolHandler::GetProtocolFlags(uint32_t *result)
michael@0 177 {
michael@0 178 // URI_LOADABLE_BY_ANYONE, since it's our inner URI that will matter
michael@0 179 // anyway.
michael@0 180 *result = URI_NORELATIVE | URI_NOAUTH | URI_LOADABLE_BY_ANYONE;
michael@0 181 /* Although jar uris have their own concept of relative urls
michael@0 182 it is very different from the standard behaviour, so we
michael@0 183 have to say norelative here! */
michael@0 184 return NS_OK;
michael@0 185 }
michael@0 186
michael@0 187 NS_IMETHODIMP
michael@0 188 nsJARProtocolHandler::NewURI(const nsACString &aSpec,
michael@0 189 const char *aCharset,
michael@0 190 nsIURI *aBaseURI,
michael@0 191 nsIURI **result)
michael@0 192 {
michael@0 193 nsresult rv = NS_OK;
michael@0 194
michael@0 195 nsRefPtr<nsJARURI> jarURI = new nsJARURI();
michael@0 196 if (!jarURI)
michael@0 197 return NS_ERROR_OUT_OF_MEMORY;
michael@0 198
michael@0 199 rv = jarURI->Init(aCharset);
michael@0 200 NS_ENSURE_SUCCESS(rv, rv);
michael@0 201
michael@0 202 rv = jarURI->SetSpecWithBase(aSpec, aBaseURI);
michael@0 203 if (NS_FAILED(rv))
michael@0 204 return rv;
michael@0 205
michael@0 206 NS_ADDREF(*result = jarURI);
michael@0 207 return rv;
michael@0 208 }
michael@0 209
michael@0 210 NS_IMETHODIMP
michael@0 211 nsJARProtocolHandler::NewChannel(nsIURI *uri, nsIChannel **result)
michael@0 212 {
michael@0 213 nsJARChannel *chan = new nsJARChannel();
michael@0 214 if (!chan)
michael@0 215 return NS_ERROR_OUT_OF_MEMORY;
michael@0 216 NS_ADDREF(chan);
michael@0 217
michael@0 218 nsresult rv = chan->Init(uri);
michael@0 219 if (NS_FAILED(rv)) {
michael@0 220 NS_RELEASE(chan);
michael@0 221 return rv;
michael@0 222 }
michael@0 223
michael@0 224 *result = chan;
michael@0 225 return NS_OK;
michael@0 226 }
michael@0 227
michael@0 228
michael@0 229 NS_IMETHODIMP
michael@0 230 nsJARProtocolHandler::AllowPort(int32_t port, const char *scheme, bool *_retval)
michael@0 231 {
michael@0 232 // don't override anything.
michael@0 233 *_retval = false;
michael@0 234 return NS_OK;
michael@0 235 }
michael@0 236
michael@0 237 ////////////////////////////////////////////////////////////////////////////////

mercurial