1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // This file declares GPS providers that run on linux. Currently, just uses 6 // the libgps (gpsd) API. Public for testing only - for normal usage this 7 // header should not be required, as location_provider.h declares the needed 8 // factory function. 9 10 #ifndef CONTENT_BROWSER_GEOLOCATION_GPS_LOCATION_PROVIDER_LINUX_H_ 11 #define CONTENT_BROWSER_GEOLOCATION_GPS_LOCATION_PROVIDER_LINUX_H_ 12 13 #include "base/compiler_specific.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/weak_ptr.h" 16 #include "content/browser/geolocation/location_provider_base.h" 17 #include "content/common/content_export.h" 18 #include "content/public/common/geoposition.h" 19 20 #if defined(USE_LIBGPS) 21 #include "library_loaders/libgps.h" 22 #endif 23 24 struct gps_data_t; 25 26 namespace content { 27 28 // Defines a wrapper around the C libgps API (gps.h). Similar to the libgpsmm.h 29 // API provided by that package. 30 class CONTENT_EXPORT LibGps { 31 public: 32 virtual ~LibGps(); 33 // Attempts to dynamically load the libgps.so library and returns NULL on 34 // failure. 35 static LibGps* New(); 36 37 bool Start(); 38 void Stop(); 39 bool Read(content::Geoposition* position); 40 41 protected: 42 LibGps(); 43 44 // Returns false if there is no fix available. 45 virtual bool GetPositionIfFixed(content::Geoposition* position); 46 47 #if defined(USE_LIBGPS) 48 LibGpsLoader libgps_loader_; 49 50 scoped_ptr<gps_data_t> gps_data_; 51 bool is_open_; 52 #endif // defined(USE_LIBGPS) 53 54 private: 55 DISALLOW_COPY_AND_ASSIGN(LibGps); 56 }; 57 58 // Location provider for Linux, that uses libgps/gpsd to obtain position fixes. 59 class CONTENT_EXPORT GpsLocationProviderLinux : public LocationProviderBase { 60 public: 61 typedef LibGps* (*LibGpsFactory)(); 62 // |factory| will be used to create the gpsd client library wrapper. (Note 63 // NewSystemLocationProvider() will use the default factory). 64 explicit GpsLocationProviderLinux(LibGpsFactory libgps_factory); 65 virtual ~GpsLocationProviderLinux(); 66 67 void SetGpsdReconnectIntervalMillis(int value) { 68 gpsd_reconnect_interval_millis_ = value; 69 } 70 void SetPollPeriodMovingMillis(int value) { 71 poll_period_moving_millis_ = value; 72 } 73 void SetPollPeriodStationaryMillis(int value) { 74 poll_period_stationary_millis_ = value; 75 } 76 77 // LocationProvider 78 virtual bool StartProvider(bool high_accuracy) OVERRIDE; 79 virtual void StopProvider() OVERRIDE; 80 virtual void GetPosition(Geoposition* position) OVERRIDE; 81 virtual void RequestRefresh() OVERRIDE; 82 virtual void OnPermissionGranted() OVERRIDE; 83 84 private: 85 // Task which run in the child thread. 86 void DoGpsPollTask(); 87 88 // Will schedule a poll; i.e. enqueue DoGpsPollTask deferred task. 89 void ScheduleNextGpsPoll(int interval); 90 91 int gpsd_reconnect_interval_millis_; 92 int poll_period_moving_millis_; 93 int poll_period_stationary_millis_; 94 95 const LibGpsFactory libgps_factory_; 96 scoped_ptr<LibGps> gps_; 97 Geoposition position_; 98 99 // Holder for the tasks which run on the thread; takes care of cleanup. 100 base::WeakPtrFactory<GpsLocationProviderLinux> weak_factory_; 101 }; 102 103 } // namespace content 104 105 #endif // CONTENT_BROWSER_GEOLOCATION_GPS_LOCATION_PROVIDER_LINUX_H_ 106