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_WIFI_DATA_PROVIDER_COMMON_H_ | 5 #ifndef CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_COMMON_H_ |
6 #define CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_COMMON_H_ | 6 #define CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_COMMON_H_ |
7 | 7 |
8 #include <assert.h> | 8 #include <assert.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
14 #include "content/browser/geolocation/wifi_data_provider.h" | 14 #include "content/browser/geolocation/wifi_data_provider.h" |
| 15 #include "content/browser/geolocation/wifi_polling_policy.h" |
15 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
16 | 17 |
17 namespace content { | 18 namespace content { |
18 | 19 |
19 // Converts a MAC address stored as an array of uint8 to a string. | 20 // Converts a MAC address stored as an array of uint8 to a string. |
20 string16 MacAddressAsString16(const uint8 mac_as_int[6]); | 21 string16 MacAddressAsString16(const uint8 mac_as_int[6]); |
21 | 22 |
22 // Allows sharing and mocking of the update polling policy function. | |
23 class PollingPolicyInterface { | |
24 public: | |
25 virtual ~PollingPolicyInterface() {} | |
26 // Calculates the new polling interval for wiFi scans, given the previous | |
27 // interval and whether the last scan produced new results. | |
28 virtual void UpdatePollingInterval(bool scan_results_differ) = 0; | |
29 virtual int PollingInterval() = 0; | |
30 virtual int NoWifiInterval() = 0; | |
31 }; | |
32 | |
33 // Generic polling policy, constants are compile-time parameterized to allow | |
34 // tuning on a per-platform basis. | |
35 template<int DEFAULT_INTERVAL, | |
36 int NO_CHANGE_INTERVAL, | |
37 int TWO_NO_CHANGE_INTERVAL, | |
38 int NO_WIFI_INTERVAL> | |
39 class GenericPollingPolicy : public PollingPolicyInterface { | |
40 public: | |
41 GenericPollingPolicy() : polling_interval_(DEFAULT_INTERVAL) {} | |
42 // PollingPolicyInterface | |
43 virtual void UpdatePollingInterval(bool scan_results_differ) { | |
44 if (scan_results_differ) { | |
45 polling_interval_ = DEFAULT_INTERVAL; | |
46 } else if (polling_interval_ == DEFAULT_INTERVAL) { | |
47 polling_interval_ = NO_CHANGE_INTERVAL; | |
48 } else { | |
49 DCHECK(polling_interval_ == NO_CHANGE_INTERVAL || | |
50 polling_interval_ == TWO_NO_CHANGE_INTERVAL); | |
51 polling_interval_ = TWO_NO_CHANGE_INTERVAL; | |
52 } | |
53 } | |
54 virtual int PollingInterval() { return polling_interval_; } | |
55 virtual int NoWifiInterval() { return NO_WIFI_INTERVAL; } | |
56 | |
57 private: | |
58 int polling_interval_; | |
59 }; | |
60 | |
61 // Base class to promote code sharing between platform specific wifi data | 23 // Base class to promote code sharing between platform specific wifi data |
62 // providers. It's optional for specific platforms to derive this, but if they | 24 // providers. It's optional for specific platforms to derive this, but if they |
63 // do polling behavior is taken care of by this base class, and all the platform | 25 // do polling behavior is taken care of by this base class, and all the platform |
64 // need do is provide the underlying WLAN access API and polling policy. | 26 // need do is provide the underlying WLAN access API and polling policy. |
65 // Also designed this way for ease of testing the cross-platform behavior. | 27 // Also designed this way for ease of testing the cross-platform behavior. |
66 class CONTENT_EXPORT WifiDataProviderCommon : public WifiDataProviderImplBase { | 28 class CONTENT_EXPORT WifiDataProviderCommon : public WifiDataProviderImplBase { |
67 public: | 29 public: |
68 // Interface to abstract the low level data OS library call, and to allow | 30 // Interface to abstract the low level data OS library call, and to allow |
69 // mocking (hence public). | 31 // mocking (hence public). |
70 class WlanApiInterface { | 32 class WlanApiInterface { |
(...skipping 10 matching lines...) Expand all Loading... |
81 virtual void StopDataProvider() OVERRIDE; | 43 virtual void StopDataProvider() OVERRIDE; |
82 virtual bool GetData(WifiData* data) OVERRIDE; | 44 virtual bool GetData(WifiData* data) OVERRIDE; |
83 | 45 |
84 protected: | 46 protected: |
85 virtual ~WifiDataProviderCommon(); | 47 virtual ~WifiDataProviderCommon(); |
86 | 48 |
87 // Returns ownership. | 49 // Returns ownership. |
88 virtual WlanApiInterface* NewWlanApi() = 0; | 50 virtual WlanApiInterface* NewWlanApi() = 0; |
89 | 51 |
90 // Returns ownership. | 52 // Returns ownership. |
91 virtual PollingPolicyInterface* NewPollingPolicy() = 0; | 53 virtual WifiPollingPolicy* NewPollingPolicy() = 0; |
92 | 54 |
93 private: | 55 private: |
94 // Runs a scan. Calls the callbacks if new data is found. | 56 // Runs a scan. Calls the callbacks if new data is found. |
95 void DoWifiScanTask(); | 57 void DoWifiScanTask(); |
96 | 58 |
97 // Will schedule a scan; i.e. enqueue DoWifiScanTask deferred task. | 59 // Will schedule a scan; i.e. enqueue DoWifiScanTask deferred task. |
98 void ScheduleNextScan(int interval); | 60 void ScheduleNextScan(int interval); |
99 | 61 |
100 WifiData wifi_data_; | 62 WifiData wifi_data_; |
101 | 63 |
102 // Whether we've successfully completed a scan for WiFi data. | 64 // Whether we've successfully completed a scan for WiFi data. |
103 bool is_first_scan_complete_; | 65 bool is_first_scan_complete_; |
104 | 66 |
105 // Underlying OS wifi API. | 67 // Underlying OS wifi API. |
106 scoped_ptr<WlanApiInterface> wlan_api_; | 68 scoped_ptr<WlanApiInterface> wlan_api_; |
107 | 69 |
108 // Controls the polling update interval. | 70 // Controls the polling update interval. |
109 scoped_ptr<PollingPolicyInterface> polling_policy_; | 71 scoped_ptr<WifiPollingPolicy> polling_policy_; |
110 | 72 |
111 // Holder for delayed tasks; takes care of cleanup. | 73 // Holder for delayed tasks; takes care of cleanup. |
112 base::WeakPtrFactory<WifiDataProviderCommon> weak_factory_; | 74 base::WeakPtrFactory<WifiDataProviderCommon> weak_factory_; |
113 | 75 |
114 DISALLOW_COPY_AND_ASSIGN(WifiDataProviderCommon); | 76 DISALLOW_COPY_AND_ASSIGN(WifiDataProviderCommon); |
115 }; | 77 }; |
116 | 78 |
117 } // namespace content | 79 } // namespace content |
118 | 80 |
119 #endif // CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_COMMON_H_ | 81 #endif // CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_COMMON_H_ |
OLD | NEW |