1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 "ProxyConfigLookup.h"
8 #include "ProxyConfigLookupChild.h"
9 #include "mozilla/Unused.h"
10 #include "nsContentUtils.h"
11 #include "nsICancelable.h"
12 #include "nsIProtocolProxyService.h"
13 #include "nsIProtocolProxyService2.h"
14 #include "nsNetUtil.h"
15 #include "nsThreadUtils.h"
16 #include "nsIChannel.h"
22 nsresult
ProxyConfigLookup::Create(
23 std::function
<void(nsIProxyInfo
*, nsresult
)>&& aCallback
, nsIURI
* aURI
,
24 uint32_t aProxyResolveFlags
, nsICancelable
** aLookupCancellable
) {
25 MOZ_ASSERT(NS_IsMainThread());
27 RefPtr
<ProxyConfigLookup
> lookUp
=
28 new ProxyConfigLookup(std::move(aCallback
), aURI
, aProxyResolveFlags
);
29 return lookUp
->DoProxyResolve(aLookupCancellable
);
32 ProxyConfigLookup::ProxyConfigLookup(
33 std::function
<void(nsIProxyInfo
*, nsresult
)>&& aCallback
, nsIURI
* aURI
,
34 uint32_t aProxyResolveFlags
)
35 : mCallback(std::move(aCallback
)),
37 mProxyResolveFlags(aProxyResolveFlags
) {}
39 ProxyConfigLookup::~ProxyConfigLookup() = default;
41 nsresult
ProxyConfigLookup::DoProxyResolve(nsICancelable
** aLookupCancellable
) {
42 if (!XRE_IsParentProcess()) {
43 RefPtr
<ProxyConfigLookup
> self
= this;
44 bool result
= ProxyConfigLookupChild::Create(
45 mURI
, mProxyResolveFlags
,
46 [self
](nsIProxyInfo
* aProxyinfo
, nsresult aResult
) {
47 self
->OnProxyAvailable(nullptr, nullptr, aProxyinfo
, aResult
);
49 return result
? NS_OK
: NS_ERROR_FAILURE
;
53 nsCOMPtr
<nsIChannel
> channel
;
54 rv
= NS_NewChannel(getter_AddRefs(channel
), mURI
,
55 nsContentUtils::GetSystemPrincipal(),
56 nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL
,
57 nsIContentPolicy::TYPE_OTHER
);
62 nsCOMPtr
<nsIProtocolProxyService
> pps
=
63 do_GetService(NS_PROTOCOLPROXYSERVICE_CONTRACTID
, &rv
);
68 // using the nsIProtocolProxyService2 allows a minor performance
69 // optimization, but if an add-on has only provided the original interface
70 // then it is ok to use that version.
71 nsCOMPtr
<nsICancelable
> proxyRequest
;
72 nsCOMPtr
<nsIProtocolProxyService2
> pps2
= do_QueryInterface(pps
);
74 rv
= pps2
->AsyncResolve2(channel
, mProxyResolveFlags
, this, nullptr,
75 getter_AddRefs(proxyRequest
));
77 rv
= pps
->AsyncResolve(channel
, mProxyResolveFlags
, this, nullptr,
78 getter_AddRefs(proxyRequest
));
81 if (aLookupCancellable
) {
82 proxyRequest
.forget(aLookupCancellable
);
88 NS_IMETHODIMP
ProxyConfigLookup::OnProxyAvailable(nsICancelable
* aRequest
,
90 nsIProxyInfo
* aProxyinfo
,
92 mCallback(aProxyinfo
, aResult
);
96 NS_IMPL_ISUPPORTS(ProxyConfigLookup
, nsIProtocolProxyCallback
)
99 } // namespace mozilla