uriloader/exthandler/nsMIMEInfoImpl.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "nsMIMEInfoImpl.h"
michael@0 8 #include "nsXPIDLString.h"
michael@0 9 #include "nsReadableUtils.h"
michael@0 10 #include "nsStringEnumerator.h"
michael@0 11 #include "nsIFile.h"
michael@0 12 #include "nsIFileURL.h"
michael@0 13 #include "nsEscape.h"
michael@0 14 #include "nsNetUtil.h"
michael@0 15 #include "nsIURILoader.h"
michael@0 16 #include "nsCURILoader.h"
michael@0 17
michael@0 18 // nsISupports methods
michael@0 19 NS_IMPL_ADDREF(nsMIMEInfoBase)
michael@0 20 NS_IMPL_RELEASE(nsMIMEInfoBase)
michael@0 21
michael@0 22 NS_INTERFACE_MAP_BEGIN(nsMIMEInfoBase)
michael@0 23 NS_INTERFACE_MAP_ENTRY(nsIHandlerInfo)
michael@0 24 // This is only an nsIMIMEInfo if it's a MIME handler.
michael@0 25 NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIMIMEInfo, mClass == eMIMEInfo)
michael@0 26 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIHandlerInfo)
michael@0 27 NS_INTERFACE_MAP_END_THREADSAFE
michael@0 28
michael@0 29 // nsMIMEInfoImpl methods
michael@0 30
michael@0 31 // Constructors for a MIME handler.
michael@0 32 nsMIMEInfoBase::nsMIMEInfoBase(const char *aMIMEType) :
michael@0 33 mSchemeOrType(aMIMEType),
michael@0 34 mClass(eMIMEInfo),
michael@0 35 mPreferredAction(nsIMIMEInfo::saveToDisk),
michael@0 36 mAlwaysAskBeforeHandling(true)
michael@0 37 {
michael@0 38 }
michael@0 39
michael@0 40 nsMIMEInfoBase::nsMIMEInfoBase(const nsACString& aMIMEType) :
michael@0 41 mSchemeOrType(aMIMEType),
michael@0 42 mClass(eMIMEInfo),
michael@0 43 mPreferredAction(nsIMIMEInfo::saveToDisk),
michael@0 44 mAlwaysAskBeforeHandling(true)
michael@0 45 {
michael@0 46 }
michael@0 47
michael@0 48 // Constructor for a handler that lets the caller specify whether this is a
michael@0 49 // MIME handler or a protocol handler. In the long run, these will be distinct
michael@0 50 // classes (f.e. nsMIMEInfo and nsProtocolInfo), but for now we reuse this class
michael@0 51 // for both and distinguish between the two kinds of handlers via the aClass
michael@0 52 // argument to this method, which can be either eMIMEInfo or eProtocolInfo.
michael@0 53 nsMIMEInfoBase::nsMIMEInfoBase(const nsACString& aType, HandlerClass aClass) :
michael@0 54 mSchemeOrType(aType),
michael@0 55 mClass(aClass),
michael@0 56 mPreferredAction(nsIMIMEInfo::saveToDisk),
michael@0 57 mAlwaysAskBeforeHandling(true)
michael@0 58 {
michael@0 59 }
michael@0 60
michael@0 61 nsMIMEInfoBase::~nsMIMEInfoBase()
michael@0 62 {
michael@0 63 }
michael@0 64
michael@0 65 NS_IMETHODIMP
michael@0 66 nsMIMEInfoBase::GetFileExtensions(nsIUTF8StringEnumerator** aResult)
michael@0 67 {
michael@0 68 return NS_NewUTF8StringEnumerator(aResult, &mExtensions, this);
michael@0 69 }
michael@0 70
michael@0 71 NS_IMETHODIMP
michael@0 72 nsMIMEInfoBase::ExtensionExists(const nsACString& aExtension, bool *_retval)
michael@0 73 {
michael@0 74 NS_ASSERTION(!aExtension.IsEmpty(), "no extension");
michael@0 75 bool found = false;
michael@0 76 uint32_t extCount = mExtensions.Length();
michael@0 77 if (extCount < 1) return NS_OK;
michael@0 78
michael@0 79 for (uint8_t i=0; i < extCount; i++) {
michael@0 80 const nsCString& ext = mExtensions[i];
michael@0 81 if (ext.Equals(aExtension, nsCaseInsensitiveCStringComparator())) {
michael@0 82 found = true;
michael@0 83 break;
michael@0 84 }
michael@0 85 }
michael@0 86
michael@0 87 *_retval = found;
michael@0 88 return NS_OK;
michael@0 89 }
michael@0 90
michael@0 91 NS_IMETHODIMP
michael@0 92 nsMIMEInfoBase::GetPrimaryExtension(nsACString& _retval)
michael@0 93 {
michael@0 94 if (!mExtensions.Length())
michael@0 95 return NS_ERROR_NOT_INITIALIZED;
michael@0 96
michael@0 97 _retval = mExtensions[0];
michael@0 98 return NS_OK;
michael@0 99 }
michael@0 100
michael@0 101 NS_IMETHODIMP
michael@0 102 nsMIMEInfoBase::SetPrimaryExtension(const nsACString& aExtension)
michael@0 103 {
michael@0 104 NS_ASSERTION(!aExtension.IsEmpty(), "no extension");
michael@0 105 uint32_t extCount = mExtensions.Length();
michael@0 106 uint8_t i;
michael@0 107 bool found = false;
michael@0 108 for (i=0; i < extCount; i++) {
michael@0 109 const nsCString& ext = mExtensions[i];
michael@0 110 if (ext.Equals(aExtension, nsCaseInsensitiveCStringComparator())) {
michael@0 111 found = true;
michael@0 112 break;
michael@0 113 }
michael@0 114 }
michael@0 115 if (found) {
michael@0 116 mExtensions.RemoveElementAt(i);
michael@0 117 }
michael@0 118
michael@0 119 mExtensions.InsertElementAt(0, aExtension);
michael@0 120
michael@0 121 return NS_OK;
michael@0 122 }
michael@0 123
michael@0 124 NS_IMETHODIMP
michael@0 125 nsMIMEInfoBase::AppendExtension(const nsACString& aExtension)
michael@0 126 {
michael@0 127 mExtensions.AppendElement(aExtension);
michael@0 128 return NS_OK;
michael@0 129 }
michael@0 130
michael@0 131 NS_IMETHODIMP
michael@0 132 nsMIMEInfoBase::GetType(nsACString& aType)
michael@0 133 {
michael@0 134 if (mSchemeOrType.IsEmpty())
michael@0 135 return NS_ERROR_NOT_INITIALIZED;
michael@0 136
michael@0 137 aType = mSchemeOrType;
michael@0 138 return NS_OK;
michael@0 139 }
michael@0 140
michael@0 141 NS_IMETHODIMP
michael@0 142 nsMIMEInfoBase::GetMIMEType(nsACString& aMIMEType)
michael@0 143 {
michael@0 144 if (mSchemeOrType.IsEmpty())
michael@0 145 return NS_ERROR_NOT_INITIALIZED;
michael@0 146
michael@0 147 aMIMEType = mSchemeOrType;
michael@0 148 return NS_OK;
michael@0 149 }
michael@0 150
michael@0 151 NS_IMETHODIMP
michael@0 152 nsMIMEInfoBase::GetDescription(nsAString& aDescription)
michael@0 153 {
michael@0 154 aDescription = mDescription;
michael@0 155 return NS_OK;
michael@0 156 }
michael@0 157
michael@0 158 NS_IMETHODIMP
michael@0 159 nsMIMEInfoBase::SetDescription(const nsAString& aDescription)
michael@0 160 {
michael@0 161 mDescription = aDescription;
michael@0 162 return NS_OK;
michael@0 163 }
michael@0 164
michael@0 165 NS_IMETHODIMP
michael@0 166 nsMIMEInfoBase::Equals(nsIMIMEInfo *aMIMEInfo, bool *_retval)
michael@0 167 {
michael@0 168 if (!aMIMEInfo) return NS_ERROR_NULL_POINTER;
michael@0 169
michael@0 170 nsAutoCString type;
michael@0 171 nsresult rv = aMIMEInfo->GetMIMEType(type);
michael@0 172 if (NS_FAILED(rv)) return rv;
michael@0 173
michael@0 174 *_retval = mSchemeOrType.Equals(type);
michael@0 175
michael@0 176 return NS_OK;
michael@0 177 }
michael@0 178
michael@0 179 NS_IMETHODIMP
michael@0 180 nsMIMEInfoBase::SetFileExtensions(const nsACString& aExtensions)
michael@0 181 {
michael@0 182 mExtensions.Clear();
michael@0 183 nsCString extList( aExtensions );
michael@0 184
michael@0 185 int32_t breakLocation = -1;
michael@0 186 while ( (breakLocation= extList.FindChar(',') )!= -1)
michael@0 187 {
michael@0 188 mExtensions.AppendElement(Substring(extList.get(), extList.get() + breakLocation));
michael@0 189 extList.Cut(0, breakLocation+1 );
michael@0 190 }
michael@0 191 if ( !extList.IsEmpty() )
michael@0 192 mExtensions.AppendElement( extList );
michael@0 193 return NS_OK;
michael@0 194 }
michael@0 195
michael@0 196 NS_IMETHODIMP
michael@0 197 nsMIMEInfoBase::GetDefaultDescription(nsAString& aDefaultDescription)
michael@0 198 {
michael@0 199 aDefaultDescription = mDefaultAppDescription;
michael@0 200 return NS_OK;
michael@0 201 }
michael@0 202
michael@0 203 NS_IMETHODIMP
michael@0 204 nsMIMEInfoBase::GetPreferredApplicationHandler(nsIHandlerApp ** aPreferredAppHandler)
michael@0 205 {
michael@0 206 *aPreferredAppHandler = mPreferredApplication;
michael@0 207 NS_IF_ADDREF(*aPreferredAppHandler);
michael@0 208 return NS_OK;
michael@0 209 }
michael@0 210
michael@0 211 NS_IMETHODIMP
michael@0 212 nsMIMEInfoBase::SetPreferredApplicationHandler(nsIHandlerApp * aPreferredAppHandler)
michael@0 213 {
michael@0 214 mPreferredApplication = aPreferredAppHandler;
michael@0 215 return NS_OK;
michael@0 216 }
michael@0 217
michael@0 218 NS_IMETHODIMP
michael@0 219 nsMIMEInfoBase::GetPossibleApplicationHandlers(nsIMutableArray ** aPossibleAppHandlers)
michael@0 220 {
michael@0 221 if (!mPossibleApplications)
michael@0 222 mPossibleApplications = do_CreateInstance(NS_ARRAY_CONTRACTID);
michael@0 223
michael@0 224 if (!mPossibleApplications)
michael@0 225 return NS_ERROR_OUT_OF_MEMORY;
michael@0 226
michael@0 227 *aPossibleAppHandlers = mPossibleApplications;
michael@0 228 NS_IF_ADDREF(*aPossibleAppHandlers);
michael@0 229 return NS_OK;
michael@0 230 }
michael@0 231
michael@0 232 NS_IMETHODIMP
michael@0 233 nsMIMEInfoBase::GetPreferredAction(nsHandlerInfoAction * aPreferredAction)
michael@0 234 {
michael@0 235 *aPreferredAction = mPreferredAction;
michael@0 236 return NS_OK;
michael@0 237 }
michael@0 238
michael@0 239 NS_IMETHODIMP
michael@0 240 nsMIMEInfoBase::SetPreferredAction(nsHandlerInfoAction aPreferredAction)
michael@0 241 {
michael@0 242 mPreferredAction = aPreferredAction;
michael@0 243 return NS_OK;
michael@0 244 }
michael@0 245
michael@0 246 NS_IMETHODIMP
michael@0 247 nsMIMEInfoBase::GetAlwaysAskBeforeHandling(bool * aAlwaysAsk)
michael@0 248 {
michael@0 249 *aAlwaysAsk = mAlwaysAskBeforeHandling;
michael@0 250
michael@0 251 return NS_OK;
michael@0 252 }
michael@0 253
michael@0 254 NS_IMETHODIMP
michael@0 255 nsMIMEInfoBase::SetAlwaysAskBeforeHandling(bool aAlwaysAsk)
michael@0 256 {
michael@0 257 mAlwaysAskBeforeHandling = aAlwaysAsk;
michael@0 258 return NS_OK;
michael@0 259 }
michael@0 260
michael@0 261 /* static */
michael@0 262 nsresult
michael@0 263 nsMIMEInfoBase::GetLocalFileFromURI(nsIURI *aURI, nsIFile **aFile)
michael@0 264 {
michael@0 265 nsresult rv;
michael@0 266
michael@0 267 nsCOMPtr<nsIFileURL> fileUrl = do_QueryInterface(aURI, &rv);
michael@0 268 if (NS_FAILED(rv)) {
michael@0 269 return rv;
michael@0 270 }
michael@0 271
michael@0 272 nsCOMPtr<nsIFile> file;
michael@0 273 rv = fileUrl->GetFile(getter_AddRefs(file));
michael@0 274 if (NS_FAILED(rv)) {
michael@0 275 return rv;
michael@0 276 }
michael@0 277
michael@0 278 file.forget(aFile);
michael@0 279 return NS_OK;
michael@0 280 }
michael@0 281
michael@0 282 NS_IMETHODIMP
michael@0 283 nsMIMEInfoBase::LaunchWithFile(nsIFile* aFile)
michael@0 284 {
michael@0 285 nsresult rv;
michael@0 286
michael@0 287 // it doesn't make any sense to call this on protocol handlers
michael@0 288 NS_ASSERTION(mClass == eMIMEInfo,
michael@0 289 "nsMIMEInfoBase should have mClass == eMIMEInfo");
michael@0 290
michael@0 291 if (mPreferredAction == useSystemDefault) {
michael@0 292 return LaunchDefaultWithFile(aFile);
michael@0 293 }
michael@0 294
michael@0 295 if (mPreferredAction == useHelperApp) {
michael@0 296 if (!mPreferredApplication)
michael@0 297 return NS_ERROR_FILE_NOT_FOUND;
michael@0 298
michael@0 299 // at the moment, we only know how to hand files off to local handlers
michael@0 300 nsCOMPtr<nsILocalHandlerApp> localHandler =
michael@0 301 do_QueryInterface(mPreferredApplication, &rv);
michael@0 302 NS_ENSURE_SUCCESS(rv, rv);
michael@0 303
michael@0 304 nsCOMPtr<nsIFile> executable;
michael@0 305 rv = localHandler->GetExecutable(getter_AddRefs(executable));
michael@0 306 NS_ENSURE_SUCCESS(rv, rv);
michael@0 307
michael@0 308 nsAutoCString path;
michael@0 309 aFile->GetNativePath(path);
michael@0 310 return LaunchWithIProcess(executable, path);
michael@0 311 }
michael@0 312
michael@0 313 return NS_ERROR_INVALID_ARG;
michael@0 314 }
michael@0 315
michael@0 316 NS_IMETHODIMP
michael@0 317 nsMIMEInfoBase::LaunchWithURI(nsIURI* aURI,
michael@0 318 nsIInterfaceRequestor* aWindowContext)
michael@0 319 {
michael@0 320 // for now, this is only being called with protocol handlers; that
michael@0 321 // will change once we get to more general registerContentHandler
michael@0 322 // support
michael@0 323 NS_ASSERTION(mClass == eProtocolInfo,
michael@0 324 "nsMIMEInfoBase should be a protocol handler");
michael@0 325
michael@0 326 if (mPreferredAction == useSystemDefault) {
michael@0 327 return LoadUriInternal(aURI);
michael@0 328 }
michael@0 329
michael@0 330 if (mPreferredAction == useHelperApp) {
michael@0 331 if (!mPreferredApplication)
michael@0 332 return NS_ERROR_FILE_NOT_FOUND;
michael@0 333
michael@0 334 return mPreferredApplication->LaunchWithURI(aURI, aWindowContext);
michael@0 335 }
michael@0 336
michael@0 337 return NS_ERROR_INVALID_ARG;
michael@0 338 }
michael@0 339
michael@0 340 void
michael@0 341 nsMIMEInfoBase::CopyBasicDataTo(nsMIMEInfoBase* aOther)
michael@0 342 {
michael@0 343 aOther->mSchemeOrType = mSchemeOrType;
michael@0 344 aOther->mDefaultAppDescription = mDefaultAppDescription;
michael@0 345 aOther->mExtensions = mExtensions;
michael@0 346 }
michael@0 347
michael@0 348 /* static */
michael@0 349 already_AddRefed<nsIProcess>
michael@0 350 nsMIMEInfoBase::InitProcess(nsIFile* aApp, nsresult* aResult)
michael@0 351 {
michael@0 352 NS_ASSERTION(aApp, "Unexpected null pointer, fix caller");
michael@0 353
michael@0 354 nsCOMPtr<nsIProcess> process = do_CreateInstance(NS_PROCESS_CONTRACTID,
michael@0 355 aResult);
michael@0 356 if (NS_FAILED(*aResult))
michael@0 357 return nullptr;
michael@0 358
michael@0 359 *aResult = process->Init(aApp);
michael@0 360 if (NS_FAILED(*aResult))
michael@0 361 return nullptr;
michael@0 362
michael@0 363 return process.forget();
michael@0 364 }
michael@0 365
michael@0 366 /* static */
michael@0 367 nsresult
michael@0 368 nsMIMEInfoBase::LaunchWithIProcess(nsIFile* aApp, const nsCString& aArg)
michael@0 369 {
michael@0 370 nsresult rv;
michael@0 371 nsCOMPtr<nsIProcess> process = InitProcess(aApp, &rv);
michael@0 372 if (NS_FAILED(rv))
michael@0 373 return rv;
michael@0 374
michael@0 375 const char *string = aArg.get();
michael@0 376
michael@0 377 return process->Run(false, &string, 1);
michael@0 378 }
michael@0 379
michael@0 380 /* static */
michael@0 381 nsresult
michael@0 382 nsMIMEInfoBase::LaunchWithIProcess(nsIFile* aApp, const nsString& aArg)
michael@0 383 {
michael@0 384 nsresult rv;
michael@0 385 nsCOMPtr<nsIProcess> process = InitProcess(aApp, &rv);
michael@0 386 if (NS_FAILED(rv))
michael@0 387 return rv;
michael@0 388
michael@0 389 const char16_t *string = aArg.get();
michael@0 390
michael@0 391 return process->Runw(false, &string, 1);
michael@0 392 }
michael@0 393
michael@0 394 // nsMIMEInfoImpl implementation
michael@0 395 NS_IMETHODIMP
michael@0 396 nsMIMEInfoImpl::GetDefaultDescription(nsAString& aDefaultDescription)
michael@0 397 {
michael@0 398 if (mDefaultAppDescription.IsEmpty() && mDefaultApplication) {
michael@0 399 // Don't want to cache this, just in case someone resets the app
michael@0 400 // without changing the description....
michael@0 401 mDefaultApplication->GetLeafName(aDefaultDescription);
michael@0 402 } else {
michael@0 403 aDefaultDescription = mDefaultAppDescription;
michael@0 404 }
michael@0 405
michael@0 406 return NS_OK;
michael@0 407 }
michael@0 408
michael@0 409 NS_IMETHODIMP
michael@0 410 nsMIMEInfoImpl::GetHasDefaultHandler(bool * _retval)
michael@0 411 {
michael@0 412 *_retval = !mDefaultAppDescription.IsEmpty();
michael@0 413 if (mDefaultApplication) {
michael@0 414 bool exists;
michael@0 415 *_retval = NS_SUCCEEDED(mDefaultApplication->Exists(&exists)) && exists;
michael@0 416 }
michael@0 417 return NS_OK;
michael@0 418 }
michael@0 419
michael@0 420 nsresult
michael@0 421 nsMIMEInfoImpl::LaunchDefaultWithFile(nsIFile* aFile)
michael@0 422 {
michael@0 423 if (!mDefaultApplication)
michael@0 424 return NS_ERROR_FILE_NOT_FOUND;
michael@0 425
michael@0 426 nsAutoCString nativePath;
michael@0 427 aFile->GetNativePath(nativePath);
michael@0 428
michael@0 429 return LaunchWithIProcess(mDefaultApplication, nativePath);
michael@0 430 }
michael@0 431
michael@0 432 NS_IMETHODIMP
michael@0 433 nsMIMEInfoBase::GetPossibleLocalHandlers(nsIArray **_retval)
michael@0 434 {
michael@0 435 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 436 }

mercurial