1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/glue/URIUtils.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,207 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "URIUtils.h" 1.9 + 1.10 +#include "nsIIPCSerializableURI.h" 1.11 + 1.12 +#include "mozilla/ArrayUtils.h" 1.13 +#include "mozilla/Assertions.h" 1.14 +#include "nsComponentManagerUtils.h" 1.15 +#include "nsDebug.h" 1.16 +#include "nsID.h" 1.17 +#include "nsJARURI.h" 1.18 +#include "nsNetCID.h" 1.19 +#include "nsNetUtil.h" 1.20 +#include "nsThreadUtils.h" 1.21 + 1.22 +using namespace mozilla::ipc; 1.23 +using mozilla::ArrayLength; 1.24 + 1.25 +namespace { 1.26 + 1.27 +NS_DEFINE_CID(kSimpleURICID, NS_SIMPLEURI_CID); 1.28 +NS_DEFINE_CID(kStandardURLCID, NS_STANDARDURL_CID); 1.29 +NS_DEFINE_CID(kJARURICID, NS_JARURI_CID); 1.30 + 1.31 +struct StringWithLengh 1.32 +{ 1.33 + const char* string; 1.34 + size_t length; 1.35 +}; 1.36 + 1.37 +#define STRING_WITH_LENGTH(_str) \ 1.38 + { _str, ArrayLength(_str) - 1 } 1.39 + 1.40 +const StringWithLengh kGenericURIAllowedSchemes[] = { 1.41 + STRING_WITH_LENGTH("about:"), 1.42 + STRING_WITH_LENGTH("javascript:"), 1.43 + STRING_WITH_LENGTH("javascript") 1.44 +}; 1.45 + 1.46 +#undef STRING_WITH_LENGTH 1.47 + 1.48 +} // anonymous namespace 1.49 + 1.50 +namespace mozilla { 1.51 +namespace ipc { 1.52 + 1.53 +void 1.54 +SerializeURI(nsIURI* aURI, 1.55 + URIParams& aParams) 1.56 +{ 1.57 + MOZ_ASSERT(NS_IsMainThread()); 1.58 + MOZ_ASSERT(aURI); 1.59 + 1.60 + nsCOMPtr<nsIIPCSerializableURI> serializable = do_QueryInterface(aURI); 1.61 + if (serializable) { 1.62 + serializable->Serialize(aParams); 1.63 + if (aParams.type() == URIParams::T__None) { 1.64 + MOZ_CRASH("Serialize failed!"); 1.65 + } 1.66 + return; 1.67 + } 1.68 + 1.69 + nsCString scheme; 1.70 + if (NS_FAILED(aURI->GetScheme(scheme))) { 1.71 + MOZ_CRASH("This must never fail!"); 1.72 + } 1.73 + 1.74 + bool allowed = false; 1.75 + 1.76 + for (size_t i = 0; i < ArrayLength(kGenericURIAllowedSchemes); i++) { 1.77 + const StringWithLengh& entry = kGenericURIAllowedSchemes[i]; 1.78 + if (scheme.EqualsASCII(entry.string, entry.length)) { 1.79 + allowed = true; 1.80 + break; 1.81 + } 1.82 + } 1.83 + 1.84 + if (!allowed) { 1.85 + MOZ_CRASH("All IPDL URIs must be serializable or an allowed " 1.86 + "scheme!"); 1.87 + } 1.88 + 1.89 + GenericURIParams params; 1.90 + if (NS_FAILED(aURI->GetSpec(params.spec())) || 1.91 + NS_FAILED(aURI->GetOriginCharset(params.charset()))) { 1.92 + MOZ_CRASH("This must never fail!"); 1.93 + } 1.94 + 1.95 + aParams = params; 1.96 +} 1.97 + 1.98 +void 1.99 +SerializeURI(nsIURI* aURI, 1.100 + OptionalURIParams& aParams) 1.101 +{ 1.102 + MOZ_ASSERT(NS_IsMainThread()); 1.103 + 1.104 + if (aURI) { 1.105 + URIParams params; 1.106 + SerializeURI(aURI, params); 1.107 + aParams = params; 1.108 + } 1.109 + else { 1.110 + aParams = mozilla::void_t(); 1.111 + } 1.112 +} 1.113 + 1.114 +already_AddRefed<nsIURI> 1.115 +DeserializeURI(const URIParams& aParams) 1.116 +{ 1.117 + MOZ_ASSERT(NS_IsMainThread()); 1.118 + 1.119 + nsCOMPtr<nsIURI> uri; 1.120 + 1.121 + if (aParams.type() != URIParams::TGenericURIParams) { 1.122 + nsCOMPtr<nsIIPCSerializableURI> serializable; 1.123 + 1.124 + switch (aParams.type()) { 1.125 + case URIParams::TSimpleURIParams: 1.126 + serializable = do_CreateInstance(kSimpleURICID); 1.127 + break; 1.128 + 1.129 + case URIParams::TStandardURLParams: 1.130 + serializable = do_CreateInstance(kStandardURLCID); 1.131 + break; 1.132 + 1.133 + case URIParams::TJARURIParams: 1.134 + serializable = do_CreateInstance(kJARURICID); 1.135 + break; 1.136 + 1.137 + default: 1.138 + MOZ_CRASH("Unknown params!"); 1.139 + } 1.140 + 1.141 + MOZ_ASSERT(serializable); 1.142 + 1.143 + if (!serializable->Deserialize(aParams)) { 1.144 + MOZ_ASSERT(false, "Deserialize failed!"); 1.145 + return nullptr; 1.146 + } 1.147 + 1.148 + uri = do_QueryInterface(serializable); 1.149 + MOZ_ASSERT(uri); 1.150 + 1.151 + return uri.forget(); 1.152 + } 1.153 + 1.154 + MOZ_ASSERT(aParams.type() == URIParams::TGenericURIParams); 1.155 + 1.156 + const GenericURIParams& params = aParams.get_GenericURIParams(); 1.157 + 1.158 + if (NS_FAILED(NS_NewURI(getter_AddRefs(uri), params.spec(), 1.159 + params.charset().get()))) { 1.160 + NS_WARNING("Failed to make new URI!"); 1.161 + return nullptr; 1.162 + } 1.163 + 1.164 + nsCString scheme; 1.165 + if (NS_FAILED(uri->GetScheme(scheme))) { 1.166 + MOZ_CRASH("This must never fail!"); 1.167 + } 1.168 + 1.169 + bool allowed = false; 1.170 + 1.171 + for (size_t i = 0; i < ArrayLength(kGenericURIAllowedSchemes); i++) { 1.172 + const StringWithLengh& entry = kGenericURIAllowedSchemes[i]; 1.173 + if (scheme.EqualsASCII(entry.string, entry.length)) { 1.174 + allowed = true; 1.175 + break; 1.176 + } 1.177 + } 1.178 + 1.179 + if (!allowed) { 1.180 + MOZ_ASSERT(false, "This type of URI is not allowed!"); 1.181 + return nullptr; 1.182 + } 1.183 + 1.184 + return uri.forget(); 1.185 +} 1.186 + 1.187 +already_AddRefed<nsIURI> 1.188 +DeserializeURI(const OptionalURIParams& aParams) 1.189 +{ 1.190 + MOZ_ASSERT(NS_IsMainThread()); 1.191 + 1.192 + nsCOMPtr<nsIURI> uri; 1.193 + 1.194 + switch (aParams.type()) { 1.195 + case OptionalURIParams::Tvoid_t: 1.196 + break; 1.197 + 1.198 + case OptionalURIParams::TURIParams: 1.199 + uri = DeserializeURI(aParams.get_URIParams()); 1.200 + break; 1.201 + 1.202 + default: 1.203 + MOZ_CRASH("Unknown params!"); 1.204 + } 1.205 + 1.206 + return uri.forget(); 1.207 +} 1.208 + 1.209 +} // namespace ipc 1.210 +} // namespace mozilla