Bug 1880273 [wpt PR 44583] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / netwerk / dns / DNSUtils.cpp
blob8101f281160eabe6784993f07f7cdbdb79f6402d
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=4 sw=2 sts=2 et cin: */
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 "DNSUtils.h"
9 #include "nsContentUtils.h"
10 #include "nsHttpHandler.h"
11 #include "nsIHttpChannel.h"
12 #include "nsIHttpChannelInternal.h"
13 #include "nsIIOService.h"
14 #include "mozilla/SyncRunnable.h"
15 #include "TRRServiceChannel.h"
16 #include "TRRLoadInfo.h"
18 namespace mozilla {
19 namespace net {
21 static void InitHttpHandler() {
22 nsresult rv;
23 nsCOMPtr<nsIIOService> ios = do_GetIOService(&rv);
24 if (NS_FAILED(rv)) {
25 return;
28 nsCOMPtr<nsIProtocolHandler> handler;
29 rv = ios->GetProtocolHandler("http", getter_AddRefs(handler));
30 if (NS_FAILED(rv)) {
31 return;
35 // static
36 nsresult DNSUtils::CreateChannelHelper(nsIURI* aUri, nsIChannel** aResult) {
37 *aResult = nullptr;
39 if (NS_IsMainThread() && !XRE_IsSocketProcess()) {
40 nsresult rv;
41 nsCOMPtr<nsIIOService> ios(do_GetIOService(&rv));
42 NS_ENSURE_SUCCESS(rv, rv);
44 return NS_NewChannel(
45 aResult, aUri, nsContentUtils::GetSystemPrincipal(),
46 nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
47 nsIContentPolicy::TYPE_OTHER,
48 nullptr, // nsICookieJarSettings
49 nullptr, // PerformanceStorage
50 nullptr, // aLoadGroup
51 nullptr, // aCallbacks
52 nsIRequest::LOAD_NORMAL, ios);
55 // Unfortunately, we can only initialize gHttpHandler on main thread.
56 if (!gHttpHandler) {
57 nsCOMPtr<nsIEventTarget> main = GetMainThreadSerialEventTarget();
58 if (main) {
59 // Forward to the main thread synchronously.
60 SyncRunnable::DispatchToThread(
61 main, NS_NewRunnableFunction("InitHttpHandler",
62 []() { InitHttpHandler(); }));
66 if (!gHttpHandler) {
67 return NS_ERROR_UNEXPECTED;
70 RefPtr<TRRLoadInfo> loadInfo =
71 new TRRLoadInfo(aUri, nsIContentPolicy::TYPE_OTHER);
72 return gHttpHandler->CreateTRRServiceChannel(aUri,
73 nullptr, // givenProxyInfo
74 0, // proxyResolveFlags
75 nullptr, // proxyURI
76 loadInfo, // aLoadInfo
77 aResult);
80 } // namespace net
81 } // namespace mozilla