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/. */
5 
6 #include "nsDNSServiceInfo.h"
7 #include "nsHashPropertyBag.h"
8 #include "nsIProperty.h"
9 #include "nsISimpleEnumerator.h"
10 #include "nsISupportsImpl.h"
11 #include "mozilla/Unused.h"
12 
13 namespace mozilla {
14 namespace net {
15 
NS_IMPL_ISUPPORTS(nsDNSServiceInfo,nsIDNSServiceInfo)16 NS_IMPL_ISUPPORTS(nsDNSServiceInfo, nsIDNSServiceInfo)
17 
18 nsDNSServiceInfo::nsDNSServiceInfo(nsIDNSServiceInfo* aServiceInfo) {
19   if (NS_WARN_IF(!aServiceInfo)) {
20     return;
21   }
22 
23   nsAutoCString str;
24   uint16_t value;
25 
26   if (NS_SUCCEEDED(aServiceInfo->GetHost(str))) {
27     Unused << NS_WARN_IF(NS_FAILED(SetHost(str)));
28   }
29   if (NS_SUCCEEDED(aServiceInfo->GetAddress(str))) {
30     Unused << NS_WARN_IF(NS_FAILED(SetAddress(str)));
31   }
32   if (NS_SUCCEEDED(aServiceInfo->GetPort(&value))) {
33     Unused << NS_WARN_IF(NS_FAILED(SetPort(value)));
34   }
35   if (NS_SUCCEEDED(aServiceInfo->GetServiceName(str))) {
36     Unused << NS_WARN_IF(NS_FAILED(SetServiceName(str)));
37   }
38   if (NS_SUCCEEDED(aServiceInfo->GetServiceType(str))) {
39     Unused << NS_WARN_IF(NS_FAILED(SetServiceType(str)));
40   }
41   if (NS_SUCCEEDED(aServiceInfo->GetDomainName(str))) {
42     Unused << NS_WARN_IF(NS_FAILED(SetDomainName(str)));
43   }
44 
45   nsCOMPtr<nsIPropertyBag2> attributes;  // deep copy
46   if (NS_SUCCEEDED(aServiceInfo->GetAttributes(getter_AddRefs(attributes)))) {
47     RefPtr<nsHashPropertyBag> newAttributes = new nsHashPropertyBag();
48     newAttributes->CopyFrom(attributes);
49     Unused << NS_WARN_IF(NS_FAILED(SetAttributes(newAttributes)));
50   }
51 }
52 
53 NS_IMETHODIMP
GetHost(nsACString & aHost)54 nsDNSServiceInfo::GetHost(nsACString& aHost) {
55   if (!mIsHostSet) {
56     return NS_ERROR_NOT_INITIALIZED;
57   }
58   aHost = mHost;
59   return NS_OK;
60 }
61 
62 NS_IMETHODIMP
SetHost(const nsACString & aHost)63 nsDNSServiceInfo::SetHost(const nsACString& aHost) {
64   mHost = aHost;
65   mIsHostSet = true;
66   return NS_OK;
67 }
68 
69 NS_IMETHODIMP
GetAddress(nsACString & aAddress)70 nsDNSServiceInfo::GetAddress(nsACString& aAddress) {
71   if (!mIsAddressSet) {
72     return NS_ERROR_NOT_INITIALIZED;
73   }
74   aAddress = mAddress;
75   return NS_OK;
76 }
77 
78 NS_IMETHODIMP
SetAddress(const nsACString & aAddress)79 nsDNSServiceInfo::SetAddress(const nsACString& aAddress) {
80   mAddress = aAddress;
81   mIsAddressSet = true;
82   return NS_OK;
83 }
84 
85 NS_IMETHODIMP
GetPort(uint16_t * aPort)86 nsDNSServiceInfo::GetPort(uint16_t* aPort) {
87   if (NS_WARN_IF(!aPort)) {
88     return NS_ERROR_INVALID_ARG;
89   }
90   if (!mIsPortSet) {
91     return NS_ERROR_NOT_INITIALIZED;
92   }
93   *aPort = mPort;
94   return NS_OK;
95 }
96 
97 NS_IMETHODIMP
SetPort(uint16_t aPort)98 nsDNSServiceInfo::SetPort(uint16_t aPort) {
99   mPort = aPort;
100   mIsPortSet = true;
101   return NS_OK;
102 }
103 
104 NS_IMETHODIMP
GetServiceName(nsACString & aServiceName)105 nsDNSServiceInfo::GetServiceName(nsACString& aServiceName) {
106   if (!mIsServiceNameSet) {
107     return NS_ERROR_NOT_INITIALIZED;
108   }
109   aServiceName = mServiceName;
110   return NS_OK;
111 }
112 
113 NS_IMETHODIMP
SetServiceName(const nsACString & aServiceName)114 nsDNSServiceInfo::SetServiceName(const nsACString& aServiceName) {
115   mServiceName = aServiceName;
116   mIsServiceNameSet = true;
117   return NS_OK;
118 }
119 
120 NS_IMETHODIMP
GetServiceType(nsACString & aServiceType)121 nsDNSServiceInfo::GetServiceType(nsACString& aServiceType) {
122   if (!mIsServiceTypeSet) {
123     return NS_ERROR_NOT_INITIALIZED;
124   }
125   aServiceType = mServiceType;
126   return NS_OK;
127 }
128 
129 NS_IMETHODIMP
SetServiceType(const nsACString & aServiceType)130 nsDNSServiceInfo::SetServiceType(const nsACString& aServiceType) {
131   mServiceType = aServiceType;
132   mIsServiceTypeSet = true;
133   return NS_OK;
134 }
135 
136 NS_IMETHODIMP
GetDomainName(nsACString & aDomainName)137 nsDNSServiceInfo::GetDomainName(nsACString& aDomainName) {
138   if (!mIsDomainNameSet) {
139     return NS_ERROR_NOT_INITIALIZED;
140   }
141   aDomainName = mDomainName;
142   return NS_OK;
143 }
144 
145 NS_IMETHODIMP
SetDomainName(const nsACString & aDomainName)146 nsDNSServiceInfo::SetDomainName(const nsACString& aDomainName) {
147   mDomainName = aDomainName;
148   mIsDomainNameSet = true;
149   return NS_OK;
150 }
151 
152 NS_IMETHODIMP
GetAttributes(nsIPropertyBag2 ** aAttributes)153 nsDNSServiceInfo::GetAttributes(nsIPropertyBag2** aAttributes) {
154   if (!mIsAttributesSet) {
155     return NS_ERROR_NOT_INITIALIZED;
156   }
157   nsCOMPtr<nsIPropertyBag2> attributes(mAttributes);
158   attributes.forget(aAttributes);
159   return NS_OK;
160 }
161 
162 NS_IMETHODIMP
SetAttributes(nsIPropertyBag2 * aAttributes)163 nsDNSServiceInfo::SetAttributes(nsIPropertyBag2* aAttributes) {
164   mAttributes = aAttributes;
165   mIsAttributesSet = aAttributes != nullptr;
166   return NS_OK;
167 }
168 
169 }  // namespace net
170 }  // namespace mozilla
171