1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsISocketProvider.h"
9 #include "nsNSSComponent.h"
10 #include "nsSOCKSSocketProvider.h"
11 #include "nsSocketProviderService.h"
12 #include "nsSSLSocketProvider.h"
13 #include "nsTLSSocketProvider.h"
14 #include "nsUDPSocketProvider.h"
15 #include "mozilla/ClearOnShutdown.h"
16 #include "nsThreadUtils.h"
19 mozilla::StaticRefPtr
<nsSocketProviderService
>
20 nsSocketProviderService::gSingleton
;
22 ////////////////////////////////////////////////////////////////////////////////
24 already_AddRefed
<nsISocketProviderService
>
25 nsSocketProviderService::GetOrCreate() {
26 RefPtr
<nsSocketProviderService
> inst
;
30 inst
= new nsSocketProviderService();
32 if (NS_IsMainThread()) {
33 mozilla::ClearOnShutdown(&gSingleton
);
35 NS_DispatchToMainThread(NS_NewRunnableFunction(
36 "net::nsSocketProviderService::GetOrCreate",
37 []() -> void { mozilla::ClearOnShutdown(&gSingleton
); }));
43 NS_IMPL_ISUPPORTS(nsSocketProviderService
, nsISocketProviderService
)
45 ////////////////////////////////////////////////////////////////////////////////
48 nsSocketProviderService::GetSocketProvider(const char* type
,
49 nsISocketProvider
** result
) {
50 nsCOMPtr
<nsISocketProvider
> inst
;
51 if (!nsCRT::strcmp(type
, "ssl") &&
52 (XRE_IsParentProcess() || XRE_IsSocketProcess()) &&
53 EnsureNSSInitializedChromeOrContent()) {
54 inst
= new nsSSLSocketProvider();
55 } else if (!nsCRT::strcmp(type
, "starttls") &&
56 (XRE_IsParentProcess() || XRE_IsSocketProcess()) &&
57 EnsureNSSInitializedChromeOrContent()) {
58 inst
= new nsTLSSocketProvider();
59 } else if (!nsCRT::strcmp(type
, "socks")) {
60 inst
= new nsSOCKSSocketProvider(NS_SOCKS_VERSION_5
);
61 } else if (!nsCRT::strcmp(type
, "socks4")) {
62 inst
= new nsSOCKSSocketProvider(NS_SOCKS_VERSION_4
);
63 } else if (!nsCRT::strcmp(type
, "udp")) {
64 inst
= new nsUDPSocketProvider();
66 return NS_ERROR_UNKNOWN_SOCKET_TYPE
;
72 ////////////////////////////////////////////////////////////////////////////////