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.

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

mercurial