netwerk/wifi/nsWifiScannerWin.cpp
author Mike Hommey <mh+mozilla@glandium.org>
Fri, 11 Jan 2019 16:01:15 +0000
changeset 453570 daf50f25895db073e44d50fecf2e4f6fe873865d
parent 448947 6f3709b3878117466168c40affa7bca0b60cf75b
permissions -rw-r--r--
Bug 1519307 - Add a new project to build useful parts of breakpad independently. r=froydnj With `ac_add_options --enable-project=tools/crashreporter` in a mozconfig, `./mach build` builds minidump_stackwalk, dump_syms and fileid. One caveat is that due to limitation in how the build system works currently, it's cumbersome to keep dump_syms as a host program for Gecko, and to make it a target program for this project. For now, keep it as a host program. We're not going to use it on automation, but it's still convenient to have for quick local builds (I've had to resort to awful hacks downstream). Differential Revision: https://phabricator.services.mozilla.com/D16299

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsWifiMonitor.h"

// moz headers (alphabetical)
#include "nsAutoPtr.h"
#include "nsCOMArray.h"
#include "nsComponentManagerUtils.h"
#include "nsIMutableArray.h"
#include "nsServiceManagerUtils.h"
#include "nsWifiAccessPoint.h"
#include "win_wifiScanner.h"

using namespace mozilla;

/**
 * `nsWifiMonitor` is declared in the cross-platform nsWifiMonitor.h and
 * is mostly defined in the cross-platform nsWifiMonitor.cpp. This function
 * is implemented in various platform-specific files but the implementation
 * is almost identical in each file. We relegate the Windows-specific
 * work to the `WinWifiScanner` class and deal with non-Windows-specific
 * issues like calling listeners here. Hopefully this file can be merged
 * with the other implementations of `nsWifiMonitor::DoScan` since a lot
 * of the code is identical
 */
nsresult nsWifiMonitor::DoScan() {
  if (!mWinWifiScanner) {
    mWinWifiScanner = new WinWifiScanner();
    if (!mWinWifiScanner) {
      // TODO: Probably return OOM error
      return NS_ERROR_FAILURE;
    }
  }

  // Regularly get the access point data.

  nsCOMArray<nsWifiAccessPoint> lastAccessPoints;
  nsCOMArray<nsWifiAccessPoint> accessPoints;

  do {
    accessPoints.Clear();
    nsresult rv = mWinWifiScanner->GetAccessPointsFromWLAN(accessPoints);
    if (NS_FAILED(rv)) {
      return rv;
    }

    bool accessPointsChanged =
        !AccessPointsEqual(accessPoints, lastAccessPoints);
    ReplaceArray(lastAccessPoints, accessPoints);

    rv = CallWifiListeners(lastAccessPoints, accessPointsChanged);
    NS_ENSURE_SUCCESS(rv, rv);

    // wait for some reasonable amount of time.  pref?
    LOG(("waiting on monitor\n"));

    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
    if (mKeepGoing) {
      mon.Wait(PR_SecondsToInterval(kDefaultWifiScanInterval));
    }
  } while (mKeepGoing);

  return NS_OK;
}