tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

MozSrcProtocolHandler.cpp (2260B)


      1 /* -*- Mode: C++; tab-width: 2; 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/ModuleUtils.h"
      7 #include "mozilla/ClearOnShutdown.h"
      8 #include "mozilla/Omnijar.h"
      9 
     10 #include "MozSrcProtocolHandler.h"
     11 
     12 #define MOZSRC_SCHEME "moz-src"
     13 
     14 namespace mozilla {
     15 namespace net {
     16 
     17 NS_IMPL_QUERY_INTERFACE(MozSrcProtocolHandler, nsISubstitutingProtocolHandler,
     18                        nsIProtocolHandler, nsISupportsWeakReference)
     19 NS_IMPL_ADDREF_INHERITED(MozSrcProtocolHandler, SubstitutingProtocolHandler)
     20 NS_IMPL_RELEASE_INHERITED(MozSrcProtocolHandler, SubstitutingProtocolHandler)
     21 
     22 mozilla::StaticRefPtr<MozSrcProtocolHandler> MozSrcProtocolHandler::sSingleton;
     23 
     24 already_AddRefed<MozSrcProtocolHandler> MozSrcProtocolHandler::GetSingleton() {
     25  if (!sSingleton) {
     26    RefPtr<MozSrcProtocolHandler> handler = new MozSrcProtocolHandler();
     27    if (NS_WARN_IF(NS_FAILED(handler->Init()))) {
     28      return nullptr;
     29    }
     30    sSingleton = handler;
     31    ClearOnShutdown(&sSingleton);
     32  }
     33  return do_AddRef(sSingleton);
     34 }
     35 
     36 MozSrcProtocolHandler::MozSrcProtocolHandler()
     37    : SubstitutingProtocolHandler(MOZSRC_SCHEME) {}
     38 
     39 nsresult MozSrcProtocolHandler::Init() {
     40  nsresult rv = mozilla::Omnijar::GetURIString(mozilla::Omnijar::GRE, mGREURI);
     41  NS_ENSURE_SUCCESS(rv, rv);
     42 
     43  mGREURI.AppendLiteral(MOZSRC_SCHEME);
     44 
     45  return NS_OK;
     46 }
     47 
     48 bool MozSrcProtocolHandler::ResolveSpecialCases(const nsACString& aHost,
     49                                                const nsACString& aPath,
     50                                                const nsACString& aPathname,
     51                                                nsACString& aResult) {
     52  aResult = mGREURI;
     53  aResult.Append(aPathname);
     54  return true;
     55 }
     56 
     57 nsresult MozSrcProtocolHandler::GetSubstitutionInternal(const nsACString& aRoot,
     58                                                        nsIURI** aResult) {
     59  nsAutoCString uri;
     60 
     61  if (!ResolveSpecialCases(aRoot, "/"_ns, "/"_ns, uri)) {
     62    return NS_ERROR_NOT_AVAILABLE;
     63  }
     64 
     65  return NS_NewURI(aResult, uri);
     66 }
     67 
     68 }  // namespace net
     69 }  // namespace mozilla