nsDNSServiceInfo.cpp 4.9 KB

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