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