Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:set ts=4 sw=4 sts=4 et: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsViewSourceHandler.h"
8 #include "nsViewSourceChannel.h"
9 #include "nsNetUtil.h"
10 #include "nsSimpleNestedURI.h"
12 #define VIEW_SOURCE "view-source"
14 ////////////////////////////////////////////////////////////////////////////////
16 NS_IMPL_ISUPPORTS(nsViewSourceHandler, nsIProtocolHandler)
18 ////////////////////////////////////////////////////////////////////////////////
19 // nsIProtocolHandler methods:
21 NS_IMETHODIMP
22 nsViewSourceHandler::GetScheme(nsACString &result)
23 {
24 result.AssignLiteral(VIEW_SOURCE);
25 return NS_OK;
26 }
28 NS_IMETHODIMP
29 nsViewSourceHandler::GetDefaultPort(int32_t *result)
30 {
31 *result = -1;
32 return NS_OK;
33 }
35 NS_IMETHODIMP
36 nsViewSourceHandler::GetProtocolFlags(uint32_t *result)
37 {
38 *result = URI_NORELATIVE | URI_NOAUTH | URI_LOADABLE_BY_ANYONE |
39 URI_NON_PERSISTABLE;
40 return NS_OK;
41 }
43 NS_IMETHODIMP
44 nsViewSourceHandler::NewURI(const nsACString &aSpec,
45 const char *aCharset,
46 nsIURI *aBaseURI,
47 nsIURI **aResult)
48 {
49 *aResult = nullptr;
51 // Extract inner URL and normalize to ASCII. This is done to properly
52 // support IDN in cases like "view-source:http://www.szalagavató.hu/"
54 int32_t colon = aSpec.FindChar(':');
55 if (colon == kNotFound)
56 return NS_ERROR_MALFORMED_URI;
58 nsCOMPtr<nsIURI> innerURI;
59 nsresult rv = NS_NewURI(getter_AddRefs(innerURI),
60 Substring(aSpec, colon + 1), aCharset, aBaseURI);
61 if (NS_FAILED(rv))
62 return rv;
64 nsAutoCString asciiSpec;
65 rv = innerURI->GetAsciiSpec(asciiSpec);
66 if (NS_FAILED(rv))
67 return rv;
69 // put back our scheme and construct a simple-uri wrapper
71 asciiSpec.Insert(VIEW_SOURCE ":", 0);
73 // We can't swap() from an nsRefPtr<nsSimpleNestedURI> to an nsIURI**,
74 // sadly.
75 nsSimpleNestedURI* ourURI = new nsSimpleNestedURI(innerURI);
76 nsCOMPtr<nsIURI> uri = ourURI;
77 if (!uri)
78 return NS_ERROR_OUT_OF_MEMORY;
80 rv = ourURI->SetSpec(asciiSpec);
81 if (NS_FAILED(rv))
82 return rv;
84 // Make the URI immutable so it's impossible to get it out of sync
85 // with its inner URI.
86 ourURI->SetMutable(false);
88 uri.swap(*aResult);
89 return rv;
90 }
92 NS_IMETHODIMP
93 nsViewSourceHandler::NewChannel(nsIURI* uri, nsIChannel* *result)
94 {
95 NS_ENSURE_ARG_POINTER(uri);
96 nsViewSourceChannel *channel = new nsViewSourceChannel();
97 if (!channel)
98 return NS_ERROR_OUT_OF_MEMORY;
99 NS_ADDREF(channel);
101 nsresult rv = channel->Init(uri);
102 if (NS_FAILED(rv)) {
103 NS_RELEASE(channel);
104 return rv;
105 }
107 *result = static_cast<nsIViewSourceChannel*>(channel);
108 return NS_OK;
109 }
111 nsresult
112 nsViewSourceHandler::NewSrcdocChannel(nsIURI* uri, const nsAString &srcdoc,
113 nsIURI* baseURI, nsIChannel* *result)
114 {
115 NS_ENSURE_ARG_POINTER(uri);
116 nsViewSourceChannel *channel = new nsViewSourceChannel();
117 if (!channel)
118 return NS_ERROR_OUT_OF_MEMORY;
119 NS_ADDREF(channel);
121 nsresult rv = channel->InitSrcdoc(uri, srcdoc, baseURI);
122 if (NS_FAILED(rv)) {
123 NS_RELEASE(channel);
124 return rv;
125 }
127 *result = static_cast<nsIViewSourceChannel*>(channel);
128 return NS_OK;
129 }
131 NS_IMETHODIMP
132 nsViewSourceHandler::AllowPort(int32_t port, const char *scheme, bool *_retval)
133 {
134 // don't override anything.
135 *_retval = false;
136 return NS_OK;
137 }
139 nsViewSourceHandler::nsViewSourceHandler()
140 {
141 gInstance = this;
142 }
144 nsViewSourceHandler::~nsViewSourceHandler()
145 {
146 gInstance = nullptr;
147 }
149 nsViewSourceHandler* nsViewSourceHandler::gInstance = nullptr;
151 nsViewSourceHandler*
152 nsViewSourceHandler::GetInstance()
153 {
154 return gInstance;
155 }