Rollup of bug 786631: Get processes with non-default permissions on the prelaunch...
[gecko.git] / netwerk / wifi / osx_corewlan.mm
blobe292d3e28109e4f3a1c790c4c3dfecf98b85426b
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/. */
5 #import <Cocoa/Cocoa.h>
6 #import <CoreWLAN/CoreWLAN.h>
8 #include <mach-o/dyld.h>
9 #include <dlfcn.h>
10 #include <unistd.h>
12 #include <objc/objc.h>
13 #include <objc/objc-runtime.h>
15 #include "nsObjCExceptions.h"
16 #include "nsAutoPtr.h"
17 #include "nsCOMArray.h"
18 #include "nsWifiMonitor.h"
19 #include "nsWifiAccessPoint.h"
21 BOOL UsingSnowLeopard() {
22   static int32_t gOSXVersion = 0x0;
23   if (gOSXVersion == 0x0) {
24     Gestalt(gestaltSystemVersion, (SInt32*)&gOSXVersion);
25   }
26   return (gOSXVersion >= 0x00001060);
29 nsresult
30 GetAccessPointsFromWLAN(nsCOMArray<nsWifiAccessPoint> &accessPoints)
32   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
34   if (!UsingSnowLeopard())
35     return NS_ERROR_NOT_AVAILABLE;
37   accessPoints.Clear();
39   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
41   @try {
42     NSBundle * bundle = [[[NSBundle alloc] initWithPath:@"/System/Library/Frameworks/CoreWLAN.framework"] autorelease];
43     if (!bundle) {
44       [pool release];
45       return NS_ERROR_NOT_AVAILABLE;
46     }
48     Class CWI_class = [bundle classNamed:@"CWInterface"];
49     if (!CWI_class) {
50       [pool release];
51       return NS_ERROR_NOT_AVAILABLE;
52     }
54     id scanResult = [[CWI_class interface] scanForNetworksWithParameters:nil error:nil];
55     if (!scanResult) {
56       [pool release];
57       return NS_ERROR_NOT_AVAILABLE;
58     }
60     NSArray* scan = [NSMutableArray arrayWithArray:scanResult];
61     NSEnumerator *enumerator = [scan objectEnumerator];
63     while (id anObject = [enumerator nextObject]) {
64       nsWifiAccessPoint* ap = new nsWifiAccessPoint();
65       if (!ap) {
66         [pool release];
67         return NS_ERROR_OUT_OF_MEMORY;
68       }
70       // [CWInterface bssidData] is deprecated on OS X 10.7 and up.  Which is
71       // is a pain, so we'll use it for as long as it's available.
72       unsigned char macData[6] = {0};
73       if ([anObject respondsToSelector:@selector(bssidData)]) {
74         NSData* data = [anObject bssidData];
75         if (data) {
76           memcpy(macData, [data bytes], 6);
77         }
78       } else {
79         // [CWInterface bssid] returns a string formatted "00:00:00:00:00:00".
80         NSString* macString = [anObject bssid];
81         if (macString && ([macString length] == 17)) {
82           for (NSUInteger i = 0; i < 6; ++i) {
83             NSString* part = [macString substringWithRange:NSMakeRange(i * 3, 2)];
84             NSScanner* scanner = [NSScanner scannerWithString:part];
85             unsigned int data = 0;
86             if (![scanner scanHexInt:&data]) {
87               data = 0;
88             }
89             macData[i] = (unsigned char) data;
90           }
91         }
92       }
94       // [CWInterface rssiValue] is available on OS X 10.7 and up (and
95       // [CWInterface rssi] is deprecated).
96       int signal = 0;
97       if ([anObject respondsToSelector:@selector(rssiValue)]) {
98         signal = (int) ((NSInteger) [anObject rssiValue]);
99       } else {
100         signal = [[anObject rssi] intValue];
101       }
103       ap->setMac(macData);
104       ap->setSignal(signal);
105       ap->setSSID([[anObject ssid] UTF8String], 32);
107       accessPoints.AppendObject(ap);
108     }
109   }
110   @catch(NSException *_exn) {
111     [pool release];
112     return NS_ERROR_NOT_AVAILABLE;
113   }
115   [pool release];
117   return NS_OK;
119   NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NS_ERROR_NOT_AVAILABLE);