author Nicholas Nethercote <>
Fri, 26 Aug 2016 16:40:57 +1000
changeset 313299 fbe1cc85a7e6b96ba60f8b61f5721f18ab2ee2d6
parent 306047 36b524e69dc5250a87d8f6c42a369c817d508f39
child 327001 c28b3afaa5cd081a073ff2ae933548f9350ed862
permissions -rw-r--r--
Bug 1297300 - Add missing checks to GetSpec() calls in netwerk/. r=hurley,dragana.

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at */

#ifndef SubstitutingProtocolHandler_h___
#define SubstitutingProtocolHandler_h___

#include "nsISubstitutingProtocolHandler.h"

#include "nsInterfaceHashtable.h"
#include "nsIOService.h"
#include "nsStandardURL.h"
#include "mozilla/chrome/RegistryMessageUtils.h"
#include "mozilla/Maybe.h"

class nsIIOService;

namespace mozilla {
namespace net {

// Base class for resource://-like substitution protocols.
// If you add a new protocol, make sure to change nsChromeRegistryChrome
// to properly invoke CollectSubstitutions at the right time.
class SubstitutingProtocolHandler
  SubstitutingProtocolHandler(const char* aScheme, uint32_t aFlags, bool aEnforceFileOrJar = true);
  explicit SubstitutingProtocolHandler(const char* aScheme);


  bool HasSubstitution(const nsACString& aRoot) const { return mSubstitutions.Get(aRoot, nullptr); }

  nsresult CollectSubstitutions(InfallibleTArray<SubstitutionMapping>& aResources);

  virtual ~SubstitutingProtocolHandler() {}
  void ConstructInternal();

  nsresult SendSubstitution(const nsACString& aRoot, nsIURI* aBaseURI);

  // Override this in the subclass to try additional lookups after checking
  // mSubstitutions.
  virtual nsresult GetSubstitutionInternal(const nsACString& aRoot, nsIURI** aResult)
    *aResult = nullptr;

  // Override this in the subclass to check for special case when resolving URIs
  // _before_ checking substitutions.
  virtual bool ResolveSpecialCases(const nsACString& aHost,
                                   const nsACString& aPath,
                                   const nsACString& aPathname,
                                   nsACString& aResult)
    return false;

  // Override this in the subclass to check for special case when opening
  // channels.
  virtual nsresult SubstituteChannel(nsIURI* uri, nsILoadInfo* aLoadInfo, nsIChannel** result)
    return NS_OK;

  nsIIOService* IOService() { return mIOService; }

  nsCString mScheme;
  Maybe<uint32_t> mFlags;
  nsInterfaceHashtable<nsCStringHashKey,nsIURI> mSubstitutions;
  nsCOMPtr<nsIIOService> mIOService;

  // In general, we expect the principal of a document loaded from a
  // substituting URI to be a codebase principal for that URI (rather than
  // a principal for whatever is underneath). However, this only works if
  // the protocol handler for the underlying URI doesn't set an explicit
  // owner (which chrome:// does, for example). So we want to require that
  // substituting URIs only map to other URIs of the same type, or to
  // file:// and jar:// URIs.
  // Enforcing this for ye olde resource:// URIs could carry compat risks, so
  // we just try to enforce it on new protocols going forward.
  bool mEnforceFileOrJar;

// SubstitutingURL : overrides nsStandardURL::GetFile to provide nsIFile resolution
class SubstitutingURL : public nsStandardURL
  SubstitutingURL() : nsStandardURL(true) {}
  virtual nsStandardURL* StartClone();
  virtual nsresult EnsureFile();
  NS_IMETHOD GetClassIDNoAlloc(nsCID *aCID);

} // namespace net
} // namespace mozilla

#endif /* SubstitutingProtocolHandler_h___ */