1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/protocol/wyciwyg/nsWyciwygProtocolHandler.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,150 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "nsWyciwyg.h" 1.11 +#include "nsWyciwygChannel.h" 1.12 +#include "nsWyciwygProtocolHandler.h" 1.13 +#include "nsNetCID.h" 1.14 +#include "nsServiceManagerUtils.h" 1.15 +#include "plstr.h" 1.16 +#include "nsNetUtil.h" 1.17 +#include "nsIObserverService.h" 1.18 +#include "mozIApplicationClearPrivateDataParams.h" 1.19 + 1.20 +#include "mozilla/net/NeckoChild.h" 1.21 + 1.22 +using namespace mozilla::net; 1.23 +#include "mozilla/net/WyciwygChannelChild.h" 1.24 + 1.25 +//////////////////////////////////////////////////////////////////////////////// 1.26 + 1.27 +nsWyciwygProtocolHandler::nsWyciwygProtocolHandler() 1.28 +{ 1.29 +#if defined(PR_LOGGING) 1.30 + if (!gWyciwygLog) 1.31 + gWyciwygLog = PR_NewLogModule("nsWyciwygChannel"); 1.32 +#endif 1.33 + 1.34 + LOG(("Creating nsWyciwygProtocolHandler [this=%p].\n", this)); 1.35 +} 1.36 + 1.37 +nsWyciwygProtocolHandler::~nsWyciwygProtocolHandler() 1.38 +{ 1.39 + LOG(("Deleting nsWyciwygProtocolHandler [this=%p]\n", this)); 1.40 +} 1.41 + 1.42 +NS_IMPL_ISUPPORTS(nsWyciwygProtocolHandler, 1.43 + nsIProtocolHandler) 1.44 + 1.45 +//////////////////////////////////////////////////////////////////////////////// 1.46 +// nsIProtocolHandler methods: 1.47 +//////////////////////////////////////////////////////////////////////////////// 1.48 + 1.49 +NS_IMETHODIMP 1.50 +nsWyciwygProtocolHandler::GetScheme(nsACString &result) 1.51 +{ 1.52 + result = "wyciwyg"; 1.53 + return NS_OK; 1.54 +} 1.55 + 1.56 +NS_IMETHODIMP 1.57 +nsWyciwygProtocolHandler::GetDefaultPort(int32_t *result) 1.58 +{ 1.59 + return NS_ERROR_NOT_AVAILABLE; 1.60 +} 1.61 + 1.62 +NS_IMETHODIMP 1.63 +nsWyciwygProtocolHandler::AllowPort(int32_t port, const char *scheme, bool *_retval) 1.64 +{ 1.65 + // don't override anything. 1.66 + *_retval = false; 1.67 + return NS_OK; 1.68 +} 1.69 + 1.70 +NS_IMETHODIMP 1.71 +nsWyciwygProtocolHandler::NewURI(const nsACString &aSpec, 1.72 + const char *aCharset, // ignored 1.73 + nsIURI *aBaseURI, 1.74 + nsIURI **result) 1.75 +{ 1.76 + nsresult rv; 1.77 + 1.78 + nsCOMPtr<nsIURI> url = do_CreateInstance(NS_SIMPLEURI_CONTRACTID, &rv); 1.79 + NS_ENSURE_SUCCESS(rv, rv); 1.80 + 1.81 + rv = url->SetSpec(aSpec); 1.82 + NS_ENSURE_SUCCESS(rv, rv); 1.83 + 1.84 + *result = url; 1.85 + NS_ADDREF(*result); 1.86 + 1.87 + return rv; 1.88 +} 1.89 + 1.90 +NS_IMETHODIMP 1.91 +nsWyciwygProtocolHandler::NewChannel(nsIURI* url, nsIChannel* *result) 1.92 +{ 1.93 + if (mozilla::net::IsNeckoChild()) 1.94 + mozilla::net::NeckoChild::InitNeckoChild(); 1.95 + 1.96 + NS_ENSURE_ARG_POINTER(url); 1.97 + nsresult rv; 1.98 + 1.99 + nsCOMPtr<nsIWyciwygChannel> channel; 1.100 + if (IsNeckoChild()) { 1.101 + NS_ENSURE_TRUE(gNeckoChild != nullptr, NS_ERROR_FAILURE); 1.102 + 1.103 + WyciwygChannelChild *wcc = static_cast<WyciwygChannelChild *>( 1.104 + gNeckoChild->SendPWyciwygChannelConstructor()); 1.105 + if (!wcc) 1.106 + return NS_ERROR_OUT_OF_MEMORY; 1.107 + 1.108 + channel = wcc; 1.109 + rv = wcc->Init(url); 1.110 + if (NS_FAILED(rv)) 1.111 + PWyciwygChannelChild::Send__delete__(wcc); 1.112 + } else 1.113 + { 1.114 + // If original channel used https, make sure PSM is initialized 1.115 + // (this may be first channel to load during a session restore) 1.116 + nsAutoCString path; 1.117 + rv = url->GetPath(path); 1.118 + NS_ENSURE_SUCCESS(rv, rv); 1.119 + int32_t slashIndex = path.FindChar('/', 2); 1.120 + if (slashIndex == kNotFound) 1.121 + return NS_ERROR_FAILURE; 1.122 + if (path.Length() < (uint32_t)slashIndex + 1 + 5) 1.123 + return NS_ERROR_FAILURE; 1.124 + if (!PL_strncasecmp(path.get() + slashIndex + 1, "https", 5)) 1.125 + net_EnsurePSMInit(); 1.126 + 1.127 + nsWyciwygChannel *wc = new nsWyciwygChannel(); 1.128 + channel = wc; 1.129 + rv = wc->Init(url); 1.130 + } 1.131 + 1.132 + if (NS_FAILED(rv)) 1.133 + return rv; 1.134 + 1.135 + channel.forget(result); 1.136 + return NS_OK; 1.137 +} 1.138 + 1.139 +NS_IMETHODIMP 1.140 +nsWyciwygProtocolHandler::GetProtocolFlags(uint32_t *result) 1.141 +{ 1.142 + // Should this be an an nsINestedURI? We don't really want random webpages 1.143 + // loading these URIs... 1.144 + 1.145 + // Note that using URI_INHERITS_SECURITY_CONTEXT here is OK -- untrusted code 1.146 + // is not allowed to link to wyciwyg URIs and users shouldn't be able to get 1.147 + // at them, and nsDocShell::InternalLoad forbids non-history loads of these 1.148 + // URIs. And when loading from history we end up using the principal from 1.149 + // the history entry, which we put there ourselves, so all is ok. 1.150 + *result = URI_NORELATIVE | URI_NOAUTH | URI_DANGEROUS_TO_LOAD | 1.151 + URI_INHERITS_SECURITY_CONTEXT; 1.152 + return NS_OK; 1.153 +}