tor-browser

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

URIUtils.cpp (3567B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "URIUtils.h"
      8 
      9 #include "mozilla/Assertions.h"
     10 #include "mozilla/dom/BlobURL.h"
     11 #include "mozilla/net/DefaultURI.h"
     12 #include "mozilla/net/SubstitutingJARURI.h"
     13 #include "mozilla/net/SubstitutingURL.h"
     14 #include "nsAboutProtocolHandler.h"
     15 #include "nsComponentManagerUtils.h"
     16 #include "nsDebug.h"
     17 #include "nsID.h"
     18 #include "nsJARURI.h"
     19 #include "nsIIconURI.h"
     20 #include "nsJSProtocolHandler.h"
     21 #include "nsNetCID.h"
     22 #include "nsSimpleNestedURI.h"
     23 #include "nsThreadUtils.h"
     24 #include "nsIURIMutator.h"
     25 
     26 using namespace mozilla::ipc;
     27 
     28 namespace {
     29 
     30 NS_DEFINE_CID(kSimpleURIMutatorCID, NS_SIMPLEURIMUTATOR_CID);
     31 NS_DEFINE_CID(kStandardURLMutatorCID, NS_STANDARDURLMUTATOR_CID);
     32 NS_DEFINE_CID(kJARURIMutatorCID, NS_JARURIMUTATOR_CID);
     33 NS_DEFINE_CID(kIconURIMutatorCID, NS_MOZICONURIMUTATOR_CID);
     34 
     35 }  // namespace
     36 
     37 namespace mozilla {
     38 namespace ipc {
     39 
     40 void SerializeURI(nsIURI* aURI, URIParams& aParams) {
     41  MOZ_ASSERT(aURI);
     42 
     43  aURI->Serialize(aParams);
     44  if (aParams.type() == URIParams::T__None) {
     45    MOZ_CRASH("Serialize failed!");
     46  }
     47 }
     48 
     49 void SerializeURI(nsIURI* aURI, Maybe<URIParams>& aParams) {
     50  if (aURI) {
     51    URIParams params;
     52    SerializeURI(aURI, params);
     53    aParams = Some(std::move(params));
     54  } else {
     55    aParams = Nothing();
     56  }
     57 }
     58 
     59 already_AddRefed<nsIURI> DeserializeURI(const URIParams& aParams) {
     60  nsCOMPtr<nsIURIMutator> mutator;
     61 
     62  switch (aParams.type()) {
     63    case URIParams::TSimpleURIParams:
     64      mutator = do_CreateInstance(kSimpleURIMutatorCID);
     65      break;
     66 
     67    case URIParams::TStandardURLParams:
     68      if (aParams.get_StandardURLParams().isSubstituting()) {
     69        mutator = new net::SubstitutingURL::Mutator();
     70      } else {
     71        mutator = do_CreateInstance(kStandardURLMutatorCID);
     72      }
     73      break;
     74 
     75    case URIParams::TJARURIParams:
     76      mutator = do_CreateInstance(kJARURIMutatorCID);
     77      break;
     78 
     79    case URIParams::TJSURIParams:
     80      mutator = new nsJSURI::Mutator();
     81      break;
     82 
     83    case URIParams::TIconURIParams:
     84      mutator = do_CreateInstance(kIconURIMutatorCID);
     85      break;
     86 
     87    case URIParams::TSimpleNestedURIParams:
     88      mutator = new net::nsSimpleNestedURI::Mutator();
     89      break;
     90 
     91    case URIParams::THostObjectURIParams:
     92      mutator = new mozilla::dom::BlobURL::Mutator();
     93      break;
     94 
     95    case URIParams::TDefaultURIParams:
     96      mutator = new mozilla::net::DefaultURI::Mutator();
     97      break;
     98 
     99    case URIParams::TNestedAboutURIParams:
    100      mutator = new net::nsNestedAboutURI::Mutator();
    101      break;
    102 
    103    case URIParams::TSubstitutingJARURIParams:
    104      mutator = new net::SubstitutingJARURI::Mutator();
    105      break;
    106 
    107    default:
    108      MOZ_CRASH("Unknown params!");
    109  }
    110 
    111  MOZ_ASSERT(mutator);
    112 
    113  nsresult rv = mutator->Deserialize(aParams);
    114  if (NS_FAILED(rv)) {
    115    MOZ_ASSERT(false, "Deserialize failed!");
    116    return nullptr;
    117  }
    118 
    119  nsCOMPtr<nsIURI> uri;
    120  DebugOnly<nsresult> rv2 = mutator->Finalize(getter_AddRefs(uri));
    121  MOZ_ASSERT(uri);
    122  MOZ_ASSERT(NS_SUCCEEDED(rv2));
    123 
    124  return uri.forget();
    125 }
    126 
    127 already_AddRefed<nsIURI> DeserializeURI(const Maybe<URIParams>& aParams) {
    128  nsCOMPtr<nsIURI> uri;
    129 
    130  if (aParams.isSome()) {
    131    uri = DeserializeURI(aParams.ref());
    132  }
    133 
    134  return uri.forget();
    135 }
    136 
    137 }  // namespace ipc
    138 }  // namespace mozilla