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