netwerk/protocol/wyciwyg/nsWyciwygProtocolHandler.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 *
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "nsWyciwyg.h"
michael@0 8 #include "nsWyciwygChannel.h"
michael@0 9 #include "nsWyciwygProtocolHandler.h"
michael@0 10 #include "nsNetCID.h"
michael@0 11 #include "nsServiceManagerUtils.h"
michael@0 12 #include "plstr.h"
michael@0 13 #include "nsNetUtil.h"
michael@0 14 #include "nsIObserverService.h"
michael@0 15 #include "mozIApplicationClearPrivateDataParams.h"
michael@0 16
michael@0 17 #include "mozilla/net/NeckoChild.h"
michael@0 18
michael@0 19 using namespace mozilla::net;
michael@0 20 #include "mozilla/net/WyciwygChannelChild.h"
michael@0 21
michael@0 22 ////////////////////////////////////////////////////////////////////////////////
michael@0 23
michael@0 24 nsWyciwygProtocolHandler::nsWyciwygProtocolHandler()
michael@0 25 {
michael@0 26 #if defined(PR_LOGGING)
michael@0 27 if (!gWyciwygLog)
michael@0 28 gWyciwygLog = PR_NewLogModule("nsWyciwygChannel");
michael@0 29 #endif
michael@0 30
michael@0 31 LOG(("Creating nsWyciwygProtocolHandler [this=%p].\n", this));
michael@0 32 }
michael@0 33
michael@0 34 nsWyciwygProtocolHandler::~nsWyciwygProtocolHandler()
michael@0 35 {
michael@0 36 LOG(("Deleting nsWyciwygProtocolHandler [this=%p]\n", this));
michael@0 37 }
michael@0 38
michael@0 39 NS_IMPL_ISUPPORTS(nsWyciwygProtocolHandler,
michael@0 40 nsIProtocolHandler)
michael@0 41
michael@0 42 ////////////////////////////////////////////////////////////////////////////////
michael@0 43 // nsIProtocolHandler methods:
michael@0 44 ////////////////////////////////////////////////////////////////////////////////
michael@0 45
michael@0 46 NS_IMETHODIMP
michael@0 47 nsWyciwygProtocolHandler::GetScheme(nsACString &result)
michael@0 48 {
michael@0 49 result = "wyciwyg";
michael@0 50 return NS_OK;
michael@0 51 }
michael@0 52
michael@0 53 NS_IMETHODIMP
michael@0 54 nsWyciwygProtocolHandler::GetDefaultPort(int32_t *result)
michael@0 55 {
michael@0 56 return NS_ERROR_NOT_AVAILABLE;
michael@0 57 }
michael@0 58
michael@0 59 NS_IMETHODIMP
michael@0 60 nsWyciwygProtocolHandler::AllowPort(int32_t port, const char *scheme, bool *_retval)
michael@0 61 {
michael@0 62 // don't override anything.
michael@0 63 *_retval = false;
michael@0 64 return NS_OK;
michael@0 65 }
michael@0 66
michael@0 67 NS_IMETHODIMP
michael@0 68 nsWyciwygProtocolHandler::NewURI(const nsACString &aSpec,
michael@0 69 const char *aCharset, // ignored
michael@0 70 nsIURI *aBaseURI,
michael@0 71 nsIURI **result)
michael@0 72 {
michael@0 73 nsresult rv;
michael@0 74
michael@0 75 nsCOMPtr<nsIURI> url = do_CreateInstance(NS_SIMPLEURI_CONTRACTID, &rv);
michael@0 76 NS_ENSURE_SUCCESS(rv, rv);
michael@0 77
michael@0 78 rv = url->SetSpec(aSpec);
michael@0 79 NS_ENSURE_SUCCESS(rv, rv);
michael@0 80
michael@0 81 *result = url;
michael@0 82 NS_ADDREF(*result);
michael@0 83
michael@0 84 return rv;
michael@0 85 }
michael@0 86
michael@0 87 NS_IMETHODIMP
michael@0 88 nsWyciwygProtocolHandler::NewChannel(nsIURI* url, nsIChannel* *result)
michael@0 89 {
michael@0 90 if (mozilla::net::IsNeckoChild())
michael@0 91 mozilla::net::NeckoChild::InitNeckoChild();
michael@0 92
michael@0 93 NS_ENSURE_ARG_POINTER(url);
michael@0 94 nsresult rv;
michael@0 95
michael@0 96 nsCOMPtr<nsIWyciwygChannel> channel;
michael@0 97 if (IsNeckoChild()) {
michael@0 98 NS_ENSURE_TRUE(gNeckoChild != nullptr, NS_ERROR_FAILURE);
michael@0 99
michael@0 100 WyciwygChannelChild *wcc = static_cast<WyciwygChannelChild *>(
michael@0 101 gNeckoChild->SendPWyciwygChannelConstructor());
michael@0 102 if (!wcc)
michael@0 103 return NS_ERROR_OUT_OF_MEMORY;
michael@0 104
michael@0 105 channel = wcc;
michael@0 106 rv = wcc->Init(url);
michael@0 107 if (NS_FAILED(rv))
michael@0 108 PWyciwygChannelChild::Send__delete__(wcc);
michael@0 109 } else
michael@0 110 {
michael@0 111 // If original channel used https, make sure PSM is initialized
michael@0 112 // (this may be first channel to load during a session restore)
michael@0 113 nsAutoCString path;
michael@0 114 rv = url->GetPath(path);
michael@0 115 NS_ENSURE_SUCCESS(rv, rv);
michael@0 116 int32_t slashIndex = path.FindChar('/', 2);
michael@0 117 if (slashIndex == kNotFound)
michael@0 118 return NS_ERROR_FAILURE;
michael@0 119 if (path.Length() < (uint32_t)slashIndex + 1 + 5)
michael@0 120 return NS_ERROR_FAILURE;
michael@0 121 if (!PL_strncasecmp(path.get() + slashIndex + 1, "https", 5))
michael@0 122 net_EnsurePSMInit();
michael@0 123
michael@0 124 nsWyciwygChannel *wc = new nsWyciwygChannel();
michael@0 125 channel = wc;
michael@0 126 rv = wc->Init(url);
michael@0 127 }
michael@0 128
michael@0 129 if (NS_FAILED(rv))
michael@0 130 return rv;
michael@0 131
michael@0 132 channel.forget(result);
michael@0 133 return NS_OK;
michael@0 134 }
michael@0 135
michael@0 136 NS_IMETHODIMP
michael@0 137 nsWyciwygProtocolHandler::GetProtocolFlags(uint32_t *result)
michael@0 138 {
michael@0 139 // Should this be an an nsINestedURI? We don't really want random webpages
michael@0 140 // loading these URIs...
michael@0 141
michael@0 142 // Note that using URI_INHERITS_SECURITY_CONTEXT here is OK -- untrusted code
michael@0 143 // is not allowed to link to wyciwyg URIs and users shouldn't be able to get
michael@0 144 // at them, and nsDocShell::InternalLoad forbids non-history loads of these
michael@0 145 // URIs. And when loading from history we end up using the principal from
michael@0 146 // the history entry, which we put there ourselves, so all is ok.
michael@0 147 *result = URI_NORELATIVE | URI_NOAUTH | URI_DANGEROUS_TO_LOAD |
michael@0 148 URI_INHERITS_SECURITY_CONTEXT;
michael@0 149 return NS_OK;
michael@0 150 }

mercurial