Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / browser / geolocation / network_location_provider.h
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/location_provider_base.h"
19 #include "content/browser/geolocation/network_location_request.h"
20 #include "content/browser/geolocation/wifi_data_provider.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:
32   // Cache of recently resolved locations. Public for tests.
33   class CONTENT_EXPORT PositionCache {
34    public:
35     // The maximum size of the cache of positions.
36     static const size_t kMaximumSize;
37
38     PositionCache();
39     ~PositionCache();
40
41     // Caches the current position response for the current set of cell ID and
42     // WiFi data. In the case of the cache exceeding kMaximumSize this will
43     // evict old entries in FIFO orderer of being added.
44     // Returns true on success, false otherwise.
45     bool CachePosition(const WifiData& wifi_data,
46                        const Geoposition& position);
47
48     // Searches for a cached position response for the current set of data.
49     // Returns NULL if the position is not in the cache, or the cached
50     // position if available. Ownership remains with the cache.
51     const Geoposition* FindPosition(const WifiData& wifi_data);
52
53    private:
54     // Makes the key for the map of cached positions, using a set of
55     // data. Returns true if a good key was generated, false otherwise.
56     static bool MakeKey(const WifiData& wifi_data,
57                         base::string16* key);
58
59     // The cache of positions. This is stored as a map keyed on a string that
60     // represents a set of data, and a list to provide
61     // least-recently-added eviction.
62     typedef std::map<base::string16, Geoposition> CacheMap;
63     CacheMap cache_;
64     typedef std::list<CacheMap::iterator> CacheAgeList;
65     CacheAgeList cache_age_list_;  // Oldest first.
66   };
67
68   NetworkLocationProvider(AccessTokenStore* access_token_store,
69                           net::URLRequestContextGetter* context,
70                           const GURL& url,
71                           const base::string16& access_token);
72   virtual ~NetworkLocationProvider();
73
74   // LocationProvider implementation
75   virtual bool StartProvider(bool high_accuracy) OVERRIDE;
76   virtual void StopProvider() OVERRIDE;
77   virtual void GetPosition(Geoposition *position) OVERRIDE;
78   virtual void RequestRefresh() OVERRIDE;
79   virtual void OnPermissionGranted() OVERRIDE;
80
81  private:
82   // Satisfies a position request from cache or network.
83   void RequestPosition();
84
85   // Gets called when new wifi data is available.
86   void OnWifiDataUpdate(WifiDataProvider* provider);
87
88   // Internal helper used by OnWifiDataUpdate.
89   void OnWifiDataUpdated();
90
91   bool IsStarted() const;
92
93   void OnLocationResponse(const Geoposition& position,
94                           bool server_error,
95                           const base::string16& access_token,
96                           const WifiData& wifi_data);
97
98   scoped_refptr<AccessTokenStore> access_token_store_;
99
100   // The wifi data provider, acquired via global factories.
101   WifiDataProvider* wifi_data_provider_;
102
103   WifiDataProvider::WifiDataUpdateCallback wifi_data_update_callback_;
104
105   // The  wifi data and a flag to indicate if the data set is complete.
106   WifiData wifi_data_;
107   bool is_wifi_data_complete_;
108
109   // The timestamp for the latest wifi data update.
110   base::Time wifi_data_updated_timestamp_;
111
112   // Cached value loaded from the token store or set by a previous server
113   // response, and sent in each subsequent network request.
114   base::string16 access_token_;
115
116   // The current best position estimate.
117   Geoposition position_;
118
119   // Whether permission has been granted for the provider to operate.
120   bool is_permission_granted_;
121
122   bool is_new_data_available_;
123
124   // The network location request object, and the url it uses.
125   scoped_ptr<NetworkLocationRequest> request_;
126
127   // The cache of positions.
128   scoped_ptr<PositionCache> position_cache_;
129
130   base::WeakPtrFactory<NetworkLocationProvider> weak_factory_;
131
132   DISALLOW_COPY_AND_ASSIGN(NetworkLocationProvider);
133 };
134
135 // Factory functions for the various types of location provider to abstract
136 // over the platform-dependent implementations.
137 CONTENT_EXPORT LocationProviderBase* NewNetworkLocationProvider(
138     AccessTokenStore* access_token_store,
139     net::URLRequestContextGetter* context,
140     const GURL& url,
141     const base::string16& access_token);
142
143 }  // namespace content
144
145 #endif  // CONTENT_BROWSER_GEOLOCATION_NETWORK_LOCATION_PROVIDER_H_