Connection.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* -*- Mode: C++; tab-width: 8; 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 <limits>
  6. #include "mozilla/Hal.h"
  7. #include "mozilla/dom/network/Connection.h"
  8. #include "nsIDOMClassInfo.h"
  9. #include "mozilla/Preferences.h"
  10. #include "Constants.h"
  11. /**
  12. * We have to use macros here because our leak analysis tool things we are
  13. * leaking strings when we have |static const nsString|. Sad :(
  14. */
  15. #define CHANGE_EVENT_NAME NS_LITERAL_STRING("typechange")
  16. namespace mozilla {
  17. namespace dom {
  18. namespace network {
  19. NS_IMPL_QUERY_INTERFACE_INHERITED(Connection, DOMEventTargetHelper,
  20. nsINetworkProperties)
  21. // Don't use |Connection| alone, since that confuses nsTraceRefcnt since
  22. // we're not the only class with that name.
  23. NS_IMPL_ADDREF_INHERITED(dom::network::Connection, DOMEventTargetHelper)
  24. NS_IMPL_RELEASE_INHERITED(dom::network::Connection, DOMEventTargetHelper)
  25. Connection::Connection(nsPIDOMWindowInner* aWindow)
  26. : DOMEventTargetHelper(aWindow)
  27. , mType(static_cast<ConnectionType>(kDefaultType))
  28. , mIsWifi(kDefaultIsWifi)
  29. , mDHCPGateway(kDefaultDHCPGateway)
  30. {
  31. hal::RegisterNetworkObserver(this);
  32. hal::NetworkInformation networkInfo;
  33. hal::GetCurrentNetworkInformation(&networkInfo);
  34. UpdateFromNetworkInfo(networkInfo);
  35. }
  36. void
  37. Connection::Shutdown()
  38. {
  39. hal::UnregisterNetworkObserver(this);
  40. }
  41. NS_IMETHODIMP
  42. Connection::GetIsWifi(bool *aIsWifi)
  43. {
  44. *aIsWifi = mIsWifi;
  45. return NS_OK;
  46. }
  47. NS_IMETHODIMP
  48. Connection::GetDhcpGateway(uint32_t *aGW)
  49. {
  50. *aGW = mDHCPGateway;
  51. return NS_OK;
  52. }
  53. void
  54. Connection::UpdateFromNetworkInfo(const hal::NetworkInformation& aNetworkInfo)
  55. {
  56. mType = static_cast<ConnectionType>(aNetworkInfo.type());
  57. mIsWifi = aNetworkInfo.isWifi();
  58. mDHCPGateway = aNetworkInfo.dhcpGateway();
  59. }
  60. void
  61. Connection::Notify(const hal::NetworkInformation& aNetworkInfo)
  62. {
  63. ConnectionType previousType = mType;
  64. UpdateFromNetworkInfo(aNetworkInfo);
  65. if (previousType == mType) {
  66. return;
  67. }
  68. DispatchTrustedEvent(CHANGE_EVENT_NAME);
  69. }
  70. JSObject*
  71. Connection::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  72. {
  73. return NetworkInformationBinding::Wrap(aCx, this, aGivenProto);
  74. }
  75. } // namespace network
  76. } // namespace dom
  77. } // namespace mozilla