Bug 928217 - Enable core location geolocation conditionally. r=jdm/smichaud
authorDoug Turner <doug.turner@gmail.com>
Sun, 20 Oct 2013 17:53:09 -0700
changeset 166248 a98a45e180f600f980690123b9916fbb0558c145
parent 166246 48717bae9d9675ec71e6e562e2413ef524748746
child 166249 2e3900ae8b35fee7e11d45a2eb1e4332221c4968
push id428
push userbbajaj@mozilla.com
push dateTue, 28 Jan 2014 00:16:25 +0000
treeherdermozilla-release@cd72a7ff3a75 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjdm, smichaud
bugs928217
milestone27.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 928217 - Enable core location geolocation conditionally. r=jdm/smichaud
dom/src/geolocation/nsGeolocation.cpp
dom/system/mac/CoreLocationLocationProvider.h
dom/system/mac/CoreLocationLocationProvider.mm
--- a/dom/src/geolocation/nsGeolocation.cpp
+++ b/dom/src/geolocation/nsGeolocation.cpp
@@ -670,17 +670,18 @@ nsresult nsGeolocationService::Init()
   mProvider = new AndroidLocationProvider();
 #endif
 
 #ifdef MOZ_WIDGET_GONK
   mProvider = do_GetService(GONK_GPS_GEOLOCATION_PROVIDER_CONTRACTID);
 #endif
 
 #ifdef MOZ_WIDGET_COCOA
-  if (Preferences::GetBool("geo.provider.use_corelocation", false)) {
+  if (Preferences::GetBool("geo.provider.use_corelocation", true) &&
+      CoreLocationLocationProvider::IsCoreLocationAvailable()) {
     mProvider = new CoreLocationLocationProvider();
   }
 #endif
 
   // Override platform-specific providers with the default (network)
   // provider while testing. Our tests are currently not meant to exercise
   // the provider, and some tests rely on the network provider being used.
   // "geo.provider.testing" is always set for all plain and browser chrome
--- a/dom/system/mac/CoreLocationLocationProvider.h
+++ b/dom/system/mac/CoreLocationLocationProvider.h
@@ -27,14 +27,16 @@ class CoreLocationLocationProvider
   , public nsIGeolocationUpdate
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIGEOLOCATIONUPDATE
   NS_DECL_NSIGEOLOCATIONPROVIDER
 
   CoreLocationLocationProvider();
+  static bool IsCoreLocationAvailable();
+
 private:
   virtual ~CoreLocationLocationProvider() {};
 
   CoreLocationObjects* mCLObjects;
   nsCOMPtr<nsIGeolocationUpdate> mCallback;
 };
--- a/dom/system/mac/CoreLocationLocationProvider.mm
+++ b/dom/system/mac/CoreLocationLocationProvider.mm
@@ -12,16 +12,22 @@
 #include "CoreLocationLocationProvider.h"
 #include "prtime.h"
 
 #include <CoreLocation/CLError.h>
 #include <CoreLocation/CLLocation.h>
 #include <CoreLocation/CLLocationManager.h>
 #include <CoreLocation/CLLocationManagerDelegate.h>
 
+#include <objc/objc.h>
+#include <objc/objc-runtime.h>
+
+#include "nsObjCExceptions.h"
+#import <CoreWLAN/CoreWLAN.h>
+
 using namespace mozilla;
 
 static const CLLocationAccuracy kHIGH_ACCURACY = kCLLocationAccuracyBest;
 static const CLLocationAccuracy kDEFAULT_ACCURACY = kCLLocationAccuracyNearestTenMeters;
 
 @interface LocationDelegate : NSObject <CLLocationManagerDelegate>
 {
   CoreLocationLocationProvider* mProvider;
@@ -120,16 +126,45 @@ public:
 
 NS_IMPL_ISUPPORTS1(CoreLocationLocationProvider, nsIGeolocationProvider)
 
 CoreLocationLocationProvider::CoreLocationLocationProvider()
   : mCLObjects(nullptr)
 {
 }
 
+bool
+CoreLocationLocationProvider::IsCoreLocationAvailable()
+{
+  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+
+  @try {
+    NSBundle * bundle = [[[NSBundle alloc] initWithPath:@"/System/Library/Frameworks/CoreWLAN.framework"] autorelease];
+    if (!bundle) {
+      [pool release];
+      return false;
+    }
+
+    Class CWI_class = [bundle classNamed:@"CWInterface"];
+    if (!CWI_class) {
+      [pool release];
+      return false;
+    }
+
+    if ([[[CWI_class interface] interfaceState] intValue] == kCWInterfaceStateRunning) {
+      [pool release];
+      return true;
+    }
+  }
+  @catch(NSException *e) {
+  }
+  [pool release];
+  return false;
+}
+
 NS_IMETHODIMP
 CoreLocationLocationProvider::Startup()
 {
   if (!mCLObjects) {
     nsAutoPtr<CoreLocationObjects> clObjs(new CoreLocationObjects());
 
     nsresult rv = clObjs->Init(this);
     NS_ENSURE_SUCCESS(rv, rv);