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