nsWifiScannerWin.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include "nsWifiMonitor.h"
  5. // moz headers (alphabetical)
  6. #include "nsAutoPtr.h"
  7. #include "nsCOMArray.h"
  8. #include "nsComponentManagerUtils.h"
  9. #include "nsIMutableArray.h"
  10. #include "nsServiceManagerUtils.h"
  11. #include "nsWifiAccessPoint.h"
  12. #include "win_wifiScanner.h"
  13. using namespace mozilla;
  14. /**
  15. * `nsWifiMonitor` is declared in the cross-platform nsWifiMonitor.h and
  16. * is mostly defined in the cross-platform nsWifiMonitor.cpp. This function
  17. * is implemented in various platform-specific files but the implementation
  18. * is almost identical in each file. We relegate the Windows-specific
  19. * work to the `WinWifiScanner` class and deal with non-Windows-specific
  20. * issues like calling listeners here. Hopefully this file can be merged
  21. * with the other implementations of `nsWifiMonitor::DoScan` since a lot
  22. * of the code is identical
  23. */
  24. nsresult
  25. nsWifiMonitor::DoScan()
  26. {
  27. if (!mWinWifiScanner) {
  28. mWinWifiScanner = new WinWifiScanner();
  29. if (!mWinWifiScanner) {
  30. // TODO: Probably return OOM error
  31. return NS_ERROR_FAILURE;
  32. }
  33. }
  34. // Regularly get the access point data.
  35. nsCOMArray<nsWifiAccessPoint> lastAccessPoints;
  36. nsCOMArray<nsWifiAccessPoint> accessPoints;
  37. do {
  38. accessPoints.Clear();
  39. nsresult rv = mWinWifiScanner->GetAccessPointsFromWLAN(accessPoints);
  40. if (NS_FAILED(rv)) {
  41. return rv;
  42. }
  43. bool accessPointsChanged = !AccessPointsEqual(accessPoints, lastAccessPoints);
  44. ReplaceArray(lastAccessPoints, accessPoints);
  45. rv = CallWifiListeners(lastAccessPoints, accessPointsChanged);
  46. NS_ENSURE_SUCCESS(rv, rv);
  47. // wait for some reasonable amount of time. pref?
  48. LOG(("waiting on monitor\n"));
  49. ReentrantMonitorAutoEnter mon(mReentrantMonitor);
  50. if (mKeepGoing) {
  51. mon.Wait(PR_SecondsToInterval(kDefaultWifiScanInterval));
  52. }
  53. }
  54. while (mKeepGoing);
  55. return NS_OK;
  56. }