ipc/glue/URIUtils.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #include "URIUtils.h"
     7 #include "nsIIPCSerializableURI.h"
     9 #include "mozilla/ArrayUtils.h"
    10 #include "mozilla/Assertions.h"
    11 #include "nsComponentManagerUtils.h"
    12 #include "nsDebug.h"
    13 #include "nsID.h"
    14 #include "nsJARURI.h"
    15 #include "nsNetCID.h"
    16 #include "nsNetUtil.h"
    17 #include "nsThreadUtils.h"
    19 using namespace mozilla::ipc;
    20 using mozilla::ArrayLength;
    22 namespace {
    24 NS_DEFINE_CID(kSimpleURICID, NS_SIMPLEURI_CID);
    25 NS_DEFINE_CID(kStandardURLCID, NS_STANDARDURL_CID);
    26 NS_DEFINE_CID(kJARURICID, NS_JARURI_CID);
    28 struct StringWithLengh
    29 {
    30   const char* string;
    31   size_t length;
    32 };
    34 #define STRING_WITH_LENGTH(_str) \
    35   { _str, ArrayLength(_str) - 1 }
    37 const StringWithLengh kGenericURIAllowedSchemes[] = {
    38   STRING_WITH_LENGTH("about:"),
    39   STRING_WITH_LENGTH("javascript:"),
    40   STRING_WITH_LENGTH("javascript")
    41 };
    43 #undef STRING_WITH_LENGTH
    45 } // anonymous namespace
    47 namespace mozilla {
    48 namespace ipc {
    50 void
    51 SerializeURI(nsIURI* aURI,
    52              URIParams& aParams)
    53 {
    54   MOZ_ASSERT(NS_IsMainThread());
    55   MOZ_ASSERT(aURI);
    57   nsCOMPtr<nsIIPCSerializableURI> serializable = do_QueryInterface(aURI);
    58   if (serializable) {
    59     serializable->Serialize(aParams);
    60     if (aParams.type() == URIParams::T__None) {
    61       MOZ_CRASH("Serialize failed!");
    62     }
    63     return;
    64   }
    66   nsCString scheme;
    67   if (NS_FAILED(aURI->GetScheme(scheme))) {
    68     MOZ_CRASH("This must never fail!");
    69   }
    71   bool allowed = false;
    73   for (size_t i = 0; i < ArrayLength(kGenericURIAllowedSchemes); i++) {
    74     const StringWithLengh& entry = kGenericURIAllowedSchemes[i];
    75     if (scheme.EqualsASCII(entry.string, entry.length)) {
    76       allowed = true;
    77       break;
    78     }
    79   }
    81   if (!allowed) {
    82     MOZ_CRASH("All IPDL URIs must be serializable or an allowed "
    83               "scheme!");
    84   }
    86   GenericURIParams params;
    87   if (NS_FAILED(aURI->GetSpec(params.spec())) ||
    88       NS_FAILED(aURI->GetOriginCharset(params.charset()))) {
    89     MOZ_CRASH("This must never fail!");
    90   }
    92   aParams = params;
    93 }
    95 void
    96 SerializeURI(nsIURI* aURI,
    97              OptionalURIParams& aParams)
    98 {
    99   MOZ_ASSERT(NS_IsMainThread());
   101   if (aURI) {
   102     URIParams params;
   103     SerializeURI(aURI, params);
   104     aParams = params;
   105   }
   106   else {
   107     aParams = mozilla::void_t();
   108   }
   109 }
   111 already_AddRefed<nsIURI>
   112 DeserializeURI(const URIParams& aParams)
   113 {
   114   MOZ_ASSERT(NS_IsMainThread());
   116   nsCOMPtr<nsIURI> uri;
   118   if (aParams.type() != URIParams::TGenericURIParams) {
   119     nsCOMPtr<nsIIPCSerializableURI> serializable;
   121     switch (aParams.type()) {
   122       case URIParams::TSimpleURIParams:
   123         serializable = do_CreateInstance(kSimpleURICID);
   124         break;
   126       case URIParams::TStandardURLParams:
   127         serializable = do_CreateInstance(kStandardURLCID);
   128         break;
   130       case URIParams::TJARURIParams:
   131         serializable = do_CreateInstance(kJARURICID);
   132         break;
   134       default:
   135         MOZ_CRASH("Unknown params!");
   136     }
   138     MOZ_ASSERT(serializable);
   140     if (!serializable->Deserialize(aParams)) {
   141       MOZ_ASSERT(false, "Deserialize failed!");
   142       return nullptr;
   143     }
   145     uri = do_QueryInterface(serializable);
   146     MOZ_ASSERT(uri);
   148     return uri.forget();
   149   }
   151   MOZ_ASSERT(aParams.type() == URIParams::TGenericURIParams);
   153   const GenericURIParams& params = aParams.get_GenericURIParams();
   155   if (NS_FAILED(NS_NewURI(getter_AddRefs(uri), params.spec(),
   156                           params.charset().get()))) {
   157     NS_WARNING("Failed to make new URI!");
   158     return nullptr;
   159   }
   161   nsCString scheme;
   162   if (NS_FAILED(uri->GetScheme(scheme))) {
   163     MOZ_CRASH("This must never fail!");
   164   }
   166   bool allowed = false;
   168   for (size_t i = 0; i < ArrayLength(kGenericURIAllowedSchemes); i++) {
   169     const StringWithLengh& entry = kGenericURIAllowedSchemes[i];
   170     if (scheme.EqualsASCII(entry.string, entry.length)) {
   171       allowed = true;
   172       break;
   173     }
   174   }
   176   if (!allowed) {
   177     MOZ_ASSERT(false, "This type of URI is not allowed!");
   178     return nullptr;
   179   }
   181   return uri.forget();
   182 }
   184 already_AddRefed<nsIURI>
   185 DeserializeURI(const OptionalURIParams& aParams)
   186 {
   187   MOZ_ASSERT(NS_IsMainThread());
   189   nsCOMPtr<nsIURI> uri;
   191   switch (aParams.type()) {
   192     case OptionalURIParams::Tvoid_t:
   193       break;
   195     case OptionalURIParams::TURIParams:
   196       uri = DeserializeURI(aParams.get_URIParams());
   197       break;
   199     default:
   200       MOZ_CRASH("Unknown params!");
   201   }
   203   return uri.forget();
   204 }
   206 } // namespace ipc
   207 } // namespace mozilla

mercurial