2009-03-17 03:30:58 +03:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Geolocation.
|
|
|
|
*
|
2009-12-22 00:50:30 +03:00
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation
|
2009-03-17 03:30:58 +03:00
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2008
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This is a derivative of work done by Google under a BSD style License.
|
|
|
|
* See: http://gears.googlecode.com/svn/trunk/gears/geolocation/
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Doug Turner <dougt@meer.net> (Original Author)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include <mach-o/dyld.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "osx_wifi.h"
|
|
|
|
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMArray.h"
|
|
|
|
#include "nsWifiMonitor.h"
|
|
|
|
#include "nsWifiAccessPoint.h"
|
|
|
|
|
|
|
|
#include "nsIProxyObjectManager.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsIMutableArray.h"
|
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2009-09-17 00:08:15 +04:00
|
|
|
// defined in osx_corewlan.mm
|
|
|
|
// basically relaces accesspoints in the passed reference
|
2010-05-13 16:19:50 +04:00
|
|
|
// it lives in a separate file so that we can use objective c.
|
2009-09-17 00:08:15 +04:00
|
|
|
extern nsresult GetAccessPointsFromWLAN(nsCOMArray<nsWifiAccessPoint> &accessPoints);
|
|
|
|
|
2009-03-17 03:30:58 +03:00
|
|
|
nsresult
|
2009-09-17 00:08:15 +04:00
|
|
|
nsWifiMonitor::DoScanWithCoreWLAN()
|
|
|
|
{
|
|
|
|
// Regularly get the access point data.
|
2010-03-02 08:56:06 +03:00
|
|
|
|
2009-09-17 00:08:15 +04:00
|
|
|
nsCOMArray<nsWifiAccessPoint> lastAccessPoints;
|
|
|
|
nsCOMArray<nsWifiAccessPoint> accessPoints;
|
|
|
|
|
2010-03-02 08:56:06 +03:00
|
|
|
do {
|
2009-09-17 00:08:15 +04:00
|
|
|
nsresult rv = GetAccessPointsFromWLAN(accessPoints);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
PRBool accessPointsChanged = !AccessPointsEqual(accessPoints, lastAccessPoints);
|
|
|
|
nsCOMArray<nsIWifiListener> currentListeners;
|
|
|
|
|
|
|
|
{
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-03-02 08:56:06 +03:00
|
|
|
|
2009-09-17 00:08:15 +04:00
|
|
|
for (PRUint32 i = 0; i < mListeners.Length(); i++) {
|
|
|
|
if (!mListeners[i].mHasSentData || accessPointsChanged) {
|
|
|
|
mListeners[i].mHasSentData = PR_TRUE;
|
|
|
|
currentListeners.AppendObject(mListeners[i].mListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-02 08:56:06 +03:00
|
|
|
|
2009-09-17 00:08:15 +04:00
|
|
|
ReplaceArray(lastAccessPoints, accessPoints);
|
|
|
|
|
|
|
|
if (currentListeners.Count() > 0)
|
|
|
|
{
|
|
|
|
PRUint32 resultCount = lastAccessPoints.Count();
|
|
|
|
nsIWifiAccessPoint** result = static_cast<nsIWifiAccessPoint**> (nsMemory::Alloc(sizeof(nsIWifiAccessPoint*) * resultCount));
|
|
|
|
if (!result) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < resultCount; i++)
|
|
|
|
result[i] = lastAccessPoints[i];
|
|
|
|
|
|
|
|
for (PRInt32 i = 0; i < currentListeners.Count(); i++) {
|
2010-03-02 08:56:06 +03:00
|
|
|
|
2009-09-17 00:08:15 +04:00
|
|
|
LOG(("About to send data to the wifi listeners\n"));
|
2010-03-02 08:56:06 +03:00
|
|
|
|
2009-09-17 00:08:15 +04:00
|
|
|
nsCOMPtr<nsIWifiListener> proxy;
|
|
|
|
nsCOMPtr<nsIProxyObjectManager> proxyObjMgr = do_GetService("@mozilla.org/xpcomproxy;1");
|
|
|
|
proxyObjMgr->GetProxyForObject(NS_PROXY_TO_MAIN_THREAD,
|
|
|
|
NS_GET_IID(nsIWifiListener),
|
|
|
|
currentListeners[i],
|
|
|
|
NS_PROXY_SYNC | NS_PROXY_ALWAYS,
|
|
|
|
getter_AddRefs(proxy));
|
|
|
|
if (!proxy) {
|
|
|
|
LOG(("There is no proxy available. this should never happen\n"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsresult rv = proxy->OnChange(result, resultCount);
|
|
|
|
LOG( ("... sent %d\n", rv));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsMemory::Free(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for some reasonable amount of time. pref?
|
|
|
|
LOG(("waiting on monitor\n"));
|
2010-03-02 08:56:06 +03:00
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2009-09-17 00:08:15 +04:00
|
|
|
mon.Wait(PR_SecondsToInterval(60));
|
|
|
|
}
|
2010-02-07 18:52:43 +03:00
|
|
|
while (mKeepGoing);
|
2010-03-02 08:56:06 +03:00
|
|
|
|
2009-09-17 00:08:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsWifiMonitor::DoScanOld()
|
2009-03-17 03:30:58 +03:00
|
|
|
{
|
|
|
|
void *apple_80211_library = dlopen(
|
|
|
|
"/System/Library/PrivateFrameworks/Apple80211.framework/Apple80211",
|
|
|
|
RTLD_LAZY);
|
2009-08-12 13:49:53 +04:00
|
|
|
if (!apple_80211_library)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2009-03-17 03:30:58 +03:00
|
|
|
|
|
|
|
WirelessContextPtr wifi_context_;
|
|
|
|
|
|
|
|
WirelessAttachFunction WirelessAttach_function_ = reinterpret_cast<WirelessAttachFunction>(dlsym(apple_80211_library, "WirelessAttach"));
|
|
|
|
WirelessScanSplitFunction WirelessScanSplit_function_ = reinterpret_cast<WirelessScanSplitFunction>(dlsym(apple_80211_library, "WirelessScanSplit"));
|
|
|
|
WirelessDetachFunction WirelessDetach_function_ = reinterpret_cast<WirelessDetachFunction>(dlsym(apple_80211_library, "WirelessDetach"));
|
2010-03-02 08:56:06 +03:00
|
|
|
|
2009-08-12 13:49:53 +04:00
|
|
|
if (!WirelessAttach_function_ || !WirelessScanSplit_function_ || !WirelessDetach_function_) {
|
|
|
|
dlclose(apple_80211_library);
|
2009-03-17 03:30:58 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2009-08-12 13:49:53 +04:00
|
|
|
}
|
2009-03-17 03:30:58 +03:00
|
|
|
|
2009-09-17 00:08:15 +04:00
|
|
|
WIErr err = 0;
|
|
|
|
|
|
|
|
err = (*WirelessAttach_function_)(&wifi_context_, 0);
|
|
|
|
if (err != noErr) {
|
|
|
|
printf("Error: WirelessAttach: %d\n", (int) err);
|
2009-08-12 13:49:53 +04:00
|
|
|
dlclose(apple_80211_library);
|
2009-03-17 03:30:58 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2009-08-12 13:49:53 +04:00
|
|
|
}
|
2009-03-17 03:30:58 +03:00
|
|
|
|
|
|
|
// Regularly get the access point data.
|
2010-03-02 08:56:06 +03:00
|
|
|
|
2009-03-17 03:30:58 +03:00
|
|
|
nsCOMArray<nsWifiAccessPoint> lastAccessPoints;
|
|
|
|
nsCOMArray<nsWifiAccessPoint> accessPoints;
|
|
|
|
|
|
|
|
do {
|
|
|
|
accessPoints.Clear();
|
|
|
|
|
|
|
|
CFArrayRef managed_access_points = NULL;
|
|
|
|
CFArrayRef adhoc_access_points = NULL;
|
2010-03-02 08:56:06 +03:00
|
|
|
|
2009-03-17 03:30:58 +03:00
|
|
|
if ((*WirelessScanSplit_function_)(wifi_context_,
|
|
|
|
&managed_access_points,
|
|
|
|
&adhoc_access_points,
|
|
|
|
0) != noErr) {
|
2010-09-15 20:39:18 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2009-03-17 03:30:58 +03:00
|
|
|
}
|
2010-03-02 08:56:06 +03:00
|
|
|
|
2009-03-17 03:30:58 +03:00
|
|
|
if (managed_access_points == NULL) {
|
2010-09-15 20:39:18 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2009-03-17 03:30:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int accessPointsCount = CFArrayGetCount(managed_access_points);
|
|
|
|
|
|
|
|
for (int i = 0; i < accessPointsCount; ++i) {
|
|
|
|
|
|
|
|
nsWifiAccessPoint* ap = new nsWifiAccessPoint();
|
|
|
|
if (!ap)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const WirelessNetworkInfo *access_point_info =
|
|
|
|
reinterpret_cast<const WirelessNetworkInfo*>(CFDataGetBytePtr(reinterpret_cast<const CFDataRef>(CFArrayGetValueAtIndex(managed_access_points, i))));
|
|
|
|
|
|
|
|
ap->setMac(access_point_info->macAddress);
|
|
|
|
|
|
|
|
// WirelessNetworkInfo::signal appears to be signal strength in dBm.
|
|
|
|
ap->setSignal(access_point_info->signal);
|
|
|
|
|
|
|
|
ap->setSSID(reinterpret_cast<const char*>(access_point_info->name),
|
|
|
|
access_point_info->nameLen);
|
|
|
|
|
|
|
|
accessPoints.AppendObject(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool accessPointsChanged = !AccessPointsEqual(accessPoints, lastAccessPoints);
|
|
|
|
nsCOMArray<nsIWifiListener> currentListeners;
|
|
|
|
|
|
|
|
{
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-03-02 08:56:06 +03:00
|
|
|
|
2009-03-17 03:30:58 +03:00
|
|
|
for (PRUint32 i = 0; i < mListeners.Length(); i++) {
|
|
|
|
if (!mListeners[i].mHasSentData || accessPointsChanged) {
|
|
|
|
mListeners[i].mHasSentData = PR_TRUE;
|
|
|
|
currentListeners.AppendObject(mListeners[i].mListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-02 08:56:06 +03:00
|
|
|
|
2009-03-17 03:30:58 +03:00
|
|
|
ReplaceArray(lastAccessPoints, accessPoints);
|
|
|
|
|
|
|
|
if (currentListeners.Count() > 0)
|
|
|
|
{
|
|
|
|
PRUint32 resultCount = lastAccessPoints.Count();
|
|
|
|
nsIWifiAccessPoint** result = static_cast<nsIWifiAccessPoint**> (nsMemory::Alloc(sizeof(nsIWifiAccessPoint*) * resultCount));
|
2009-08-12 13:49:53 +04:00
|
|
|
if (!result) {
|
|
|
|
dlclose(apple_80211_library);
|
2009-03-17 03:30:58 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2009-08-12 13:49:53 +04:00
|
|
|
}
|
|
|
|
|
2009-03-17 03:30:58 +03:00
|
|
|
for (PRUint32 i = 0; i < resultCount; i++)
|
|
|
|
result[i] = lastAccessPoints[i];
|
|
|
|
|
|
|
|
for (PRInt32 i = 0; i < currentListeners.Count(); i++) {
|
2010-03-02 08:56:06 +03:00
|
|
|
|
2009-03-17 03:30:58 +03:00
|
|
|
LOG(("About to send data to the wifi listeners\n"));
|
2010-03-02 08:56:06 +03:00
|
|
|
|
2009-03-17 03:30:58 +03:00
|
|
|
nsCOMPtr<nsIWifiListener> proxy;
|
|
|
|
nsCOMPtr<nsIProxyObjectManager> proxyObjMgr = do_GetService("@mozilla.org/xpcomproxy;1");
|
|
|
|
proxyObjMgr->GetProxyForObject(NS_PROXY_TO_MAIN_THREAD,
|
|
|
|
NS_GET_IID(nsIWifiListener),
|
|
|
|
currentListeners[i],
|
|
|
|
NS_PROXY_SYNC | NS_PROXY_ALWAYS,
|
|
|
|
getter_AddRefs(proxy));
|
|
|
|
if (!proxy) {
|
|
|
|
LOG(("There is no proxy available. this should never happen\n"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsresult rv = proxy->OnChange(result, resultCount);
|
|
|
|
LOG( ("... sent %d\n", rv));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsMemory::Free(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for some reasonable amount of time. pref?
|
|
|
|
LOG(("waiting on monitor\n"));
|
2010-03-02 08:56:06 +03:00
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2009-03-17 03:30:58 +03:00
|
|
|
mon.Wait(PR_SecondsToInterval(60));
|
|
|
|
}
|
2010-02-07 18:52:43 +03:00
|
|
|
while (mKeepGoing);
|
2010-03-02 08:56:06 +03:00
|
|
|
|
2009-03-17 03:30:58 +03:00
|
|
|
(*WirelessDetach_function_)(wifi_context_);
|
2010-03-02 08:56:06 +03:00
|
|
|
|
2009-03-17 03:30:58 +03:00
|
|
|
dlclose(apple_80211_library);
|
2010-03-02 08:56:06 +03:00
|
|
|
|
2009-03-17 03:30:58 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-09-17 00:08:15 +04:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsWifiMonitor::DoScan()
|
|
|
|
{
|
|
|
|
nsresult rv = DoScanWithCoreWLAN();
|
|
|
|
|
|
|
|
// we can remove this once we stop caring about 10.5.
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
rv = DoScanOld();
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|