Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / netwerk / wifi / osx_corewlan.mm
blob461d0f44539fba8fb136ad03082b0a51d0b06c36
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Geolocation.
15  *
16  * The Initial Developer of the Original Code is Mozilla Foundation
17  * Portions created by the Initial Developer are Copyright (C) 2009
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  *  Doug Turner <dougt@meer.net>  (Original Author)
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */
37 #import <Cocoa/Cocoa.h>
39 #include <mach-o/dyld.h>
40 #include <dlfcn.h>
41 #include <unistd.h>
43 #include <objc/objc.h>
44 #include <objc/objc-runtime.h>
46 #include "nsObjCExceptions.h"
47 #include "nsAutoPtr.h"
48 #include "nsCOMArray.h"
49 #include "nsWifiMonitor.h"
50 #include "nsWifiAccessPoint.h"
52 BOOL UsingSnowLeopard() {
53   static PRInt32 gOSXVersion = 0x0;
54   if (gOSXVersion == 0x0) {
55     Gestalt(gestaltSystemVersion, (SInt32*)&gOSXVersion);
56   }
57   return (gOSXVersion >= 0x00001060);
60 nsresult
61 GetAccessPointsFromWLAN(nsCOMArray<nsWifiAccessPoint> &accessPoints)
63   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
65   if (!UsingSnowLeopard())
66     return NS_ERROR_NOT_AVAILABLE;
68   accessPoints.Clear();
70   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
72   @try {
73     NSBundle * bundle = [[[NSBundle alloc] initWithPath:@"/System/Library/Frameworks/CoreWLAN.framework"] autorelease];
74     if (!bundle) {
75       [pool release];
76       return NS_ERROR_NOT_AVAILABLE;
77     }
79     Class CWI_class = [bundle classNamed:@"CWInterface"];
80     if (!CWI_class) {
81       [pool release];
82       return NS_ERROR_NOT_AVAILABLE;
83     }
85     id scanResult = [[CWI_class interface] scanForNetworksWithParameters:nil error:nil];
86     if (!scanResult) {
87       [pool release];
88       return NS_ERROR_NOT_AVAILABLE;
89     }
91     NSArray* scan = [NSMutableArray arrayWithArray:scanResult];
92     NSEnumerator *enumerator = [scan objectEnumerator];
94     while (id anObject = [enumerator nextObject]) {
95       nsWifiAccessPoint* ap = new nsWifiAccessPoint();
96       if (!ap) {
97         [pool release];
98         return NS_ERROR_OUT_OF_MEMORY;
99       }
100       NSData* data = [anObject bssidData];
101       ap->setMac((unsigned char*)[data bytes]);
102       ap->setSignal([[anObject rssi] intValue]);
103       ap->setSSID([[anObject ssid] UTF8String], 32);
105       accessPoints.AppendObject(ap);
106     }
107   }
108   @catch(NSException *_exn) {
109     [pool release];
110     return NS_ERROR_NOT_AVAILABLE;
111   }
113   [pool release];
115   return NS_OK;
117   NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NS_ERROR_NOT_AVAILABLE);