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 "NativeDNSResolverOverrideParent.h"
9 #include "mozilla/ClearOnShutdown.h"
10 #include "mozilla/net/SocketProcessParent.h"
11 #include "nsIOService.h"
17 NS_IMPL_ISUPPORTS(NativeDNSResolverOverrideParent
, nsINativeDNSResolverOverride
)
19 static StaticRefPtr
<NativeDNSResolverOverrideParent
>
20 gNativeDNSResolverOverrideParent
;
23 already_AddRefed
<nsINativeDNSResolverOverride
>
24 NativeDNSResolverOverrideParent::GetSingleton() {
25 if (gNativeDNSResolverOverrideParent
) {
26 return do_AddRef(gNativeDNSResolverOverrideParent
);
33 gNativeDNSResolverOverrideParent
= new NativeDNSResolverOverrideParent();
34 ClearOnShutdown(&gNativeDNSResolverOverrideParent
);
36 auto initTask
= []() {
37 Unused
<< SocketProcessParent::GetSingleton()
38 ->SendPNativeDNSResolverOverrideConstructor(
39 gNativeDNSResolverOverrideParent
);
41 gIOService
->CallOrWaitForSocketProcess(initTask
);
42 return do_AddRef(gNativeDNSResolverOverrideParent
);
45 NS_IMETHODIMP
NativeDNSResolverOverrideParent::AddIPOverride(
46 const nsACString
& aHost
, const nsACString
& aIPLiteral
) {
48 if (!aIPLiteral
.Equals("N/A"_ns
) &&
49 NS_FAILED(tempAddr
.InitFromString(aIPLiteral
))) {
50 return NS_ERROR_UNEXPECTED
;
53 RefPtr
<NativeDNSResolverOverrideParent
> self
= this;
54 nsCString
host(aHost
);
55 nsCString
ip(aIPLiteral
);
56 auto task
= [self
{std::move(self
)}, host
, ip
]() {
57 Unused
<< self
->SendAddIPOverride(host
, ip
);
59 gIOService
->CallOrWaitForSocketProcess(task
);
63 NS_IMETHODIMP
NativeDNSResolverOverrideParent::SetCnameOverride(
64 const nsACString
& aHost
, const nsACString
& aCNAME
) {
65 if (aCNAME
.IsEmpty()) {
66 return NS_ERROR_UNEXPECTED
;
69 RefPtr
<NativeDNSResolverOverrideParent
> self
= this;
70 nsCString
host(aHost
);
71 nsCString
cname(aCNAME
);
72 auto task
= [self
{std::move(self
)}, host
, cname
]() {
73 Unused
<< self
->SendSetCnameOverride(host
, cname
);
75 gIOService
->CallOrWaitForSocketProcess(task
);
79 NS_IMETHODIMP
NativeDNSResolverOverrideParent::ClearHostOverride(
80 const nsACString
& aHost
) {
81 RefPtr
<NativeDNSResolverOverrideParent
> self
= this;
82 nsCString
host(aHost
);
83 auto task
= [self
{std::move(self
)}, host
]() {
84 Unused
<< self
->SendClearHostOverride(host
);
86 gIOService
->CallOrWaitForSocketProcess(task
);
90 NS_IMETHODIMP
NativeDNSResolverOverrideParent::ClearOverrides() {
91 RefPtr
<NativeDNSResolverOverrideParent
> self
= this;
92 auto task
= [self
{std::move(self
)}]() {
93 Unused
<< self
->SendClearOverrides();
95 gIOService
->CallOrWaitForSocketProcess(task
);
100 } // namespace mozilla