Home | History | Annotate | Download | only in geolocation
      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 #ifndef CONTENT_BROWSER_GEOLOCATION_NETWORK_LOCATION_PROVIDER_H_
      6 #define CONTENT_BROWSER_GEOLOCATION_NETWORK_LOCATION_PROVIDER_H_
      7 
      8 #include <list>
      9 #include <map>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/memory/weak_ptr.h"
     15 #include "base/strings/string16.h"
     16 #include "base/threading/non_thread_safe.h"
     17 #include "base/threading/thread.h"
     18 #include "content/browser/geolocation/device_data_provider.h"
     19 #include "content/browser/geolocation/location_provider_base.h"
     20 #include "content/browser/geolocation/network_location_request.h"
     21 #include "content/common/content_export.h"
     22 #include "content/public/common/geoposition.h"
     23 
     24 namespace content {
     25 class AccessTokenStore;
     26 
     27 
     28 class NetworkLocationProvider
     29     : public base::NonThreadSafe,
     30       public LocationProviderBase,
     31       public WifiDataProvider::ListenerInterface,
     32       public NetworkLocationRequest::ListenerInterface {
     33  public:
     34   // Cache of recently resolved locations. Public for tests.
     35   class CONTENT_EXPORT PositionCache {
     36    public:
     37     // The maximum size of the cache of positions for previously requested
     38     // device data.
     39     static const size_t kMaximumSize;
     40 
     41     PositionCache();
     42     ~PositionCache();
     43 
     44     // Caches the current position response for the current set of cell ID and
     45     // WiFi data. In the case of the cache exceeding kMaximumSize this will
     46     // evict old entries in FIFO orderer of being added.
     47     // Returns true on success, false otherwise.
     48     bool CachePosition(const WifiData& wifi_data,
     49                        const Geoposition& position);
     50 
     51     // Searches for a cached position response for the current set of device
     52     // data. Returns NULL if the position is not in the cache, or the cached
     53     // position if available. Ownership remains with the cache.
     54     const Geoposition* FindPosition(const WifiData& wifi_data);
     55 
     56    private:
     57     // Makes the key for the map of cached positions, using a set of
     58     // device data. Returns true if a good key was generated, false otherwise.
     59     static bool MakeKey(const WifiData& wifi_data,
     60                         string16* key);
     61 
     62     // The cache of positions. This is stored as a map keyed on a string that
     63     // represents a set of device data, and a list to provide
     64     // least-recently-added eviction.
     65     typedef std::map<string16, Geoposition> CacheMap;
     66     CacheMap cache_;
     67     typedef std::list<CacheMap::iterator> CacheAgeList;
     68     CacheAgeList cache_age_list_;  // Oldest first.
     69   };
     70 
     71   NetworkLocationProvider(AccessTokenStore* access_token_store,
     72                           net::URLRequestContextGetter* context,
     73                           const GURL& url,
     74                           const string16& access_token);
     75   virtual ~NetworkLocationProvider();
     76 
     77   // LocationProvider implementation
     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   // Satisfies a position request from cache or network.
     86   void RequestPosition();
     87 
     88   // Internal helper used by DeviceDataUpdateAvailable
     89   void OnDeviceDataUpdated();
     90 
     91   bool IsStarted() const;
     92 
     93   // DeviceDataProvider::ListenerInterface implementation.
     94   virtual void DeviceDataUpdateAvailable(WifiDataProvider* provider) OVERRIDE;
     95 
     96   // NetworkLocationRequest::ListenerInterface implementation.
     97   virtual void LocationResponseAvailable(const Geoposition& position,
     98                                          bool server_error,
     99                                          const string16& access_token,
    100                                          const WifiData& wifi_data) OVERRIDE;
    101 
    102   scoped_refptr<AccessTokenStore> access_token_store_;
    103 
    104   // The wifi data provider, acquired via global factories.
    105   WifiDataProvider* wifi_data_provider_;
    106 
    107   // The  wifi data, flags to indicate if the data set is complete.
    108   WifiData wifi_data_;
    109   bool is_wifi_data_complete_;
    110 
    111   // The timestamp for the latest device data update.
    112   base::Time device_data_updated_timestamp_;
    113 
    114   // Cached value loaded from the token store or set by a previous server
    115   // response, and sent in each subsequent network request.
    116   string16 access_token_;
    117 
    118   // The current best position estimate.
    119   Geoposition position_;
    120 
    121   // Whether permission has been granted for the provider to operate.
    122   bool is_permission_granted_;
    123 
    124   bool is_new_data_available_;
    125 
    126   // The network location request object, and the url it uses.
    127   scoped_ptr<NetworkLocationRequest> request_;
    128 
    129   base::WeakPtrFactory<NetworkLocationProvider> weak_factory_;
    130   // The cache of positions.
    131   scoped_ptr<PositionCache> position_cache_;
    132 
    133   DISALLOW_COPY_AND_ASSIGN(NetworkLocationProvider);
    134 };
    135 
    136 // Factory functions for the various types of location provider to abstract
    137 // over the platform-dependent implementations.
    138 CONTENT_EXPORT LocationProviderBase* NewNetworkLocationProvider(
    139     AccessTokenStore* access_token_store,
    140     net::URLRequestContextGetter* context,
    141     const GURL& url,
    142     const string16& access_token);
    143 
    144 }  // namespace content
    145 
    146 #endif  // CONTENT_BROWSER_GEOLOCATION_NETWORK_LOCATION_PROVIDER_H_
    147