| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 CHROME_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_COMMON_H_ | |
| 6 #define CHROME_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_COMMON_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <assert.h> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "base/scoped_ptr.h" | |
| 13 #include "base/string16.h" | |
| 14 #include "base/task.h" | |
| 15 #include "base/threading/thread.h" | |
| 16 #include "chrome/browser/geolocation/device_data_provider.h" | |
| 17 | |
| 18 // Converts a MAC address stored as an array of uint8 to a string. | |
| 19 string16 MacAddressAsString16(const uint8 mac_as_int[6]); | |
| 20 | |
| 21 // Allows sharing and mocking of the update polling policy function. | |
| 22 class PollingPolicyInterface { | |
| 23 public: | |
| 24 virtual ~PollingPolicyInterface() {} | |
| 25 // Calculates the new polling interval for wiFi scans, given the previous | |
| 26 // interval and whether the last scan produced new results. | |
| 27 virtual void UpdatePollingInterval(bool scan_results_differ) = 0; | |
| 28 virtual int PollingInterval() = 0; | |
| 29 virtual int NoWifiInterval() = 0; | |
| 30 }; | |
| 31 | |
| 32 // Generic polling policy, constants are compile-time parameterized to allow | |
| 33 // tuning on a per-platform basis. | |
| 34 template<int DEFAULT_INTERVAL, | |
| 35 int NO_CHANGE_INTERVAL, | |
| 36 int TWO_NO_CHANGE_INTERVAL, | |
| 37 int NO_WIFI_INTERVAL> | |
| 38 class GenericPollingPolicy : public PollingPolicyInterface { | |
| 39 public: | |
| 40 GenericPollingPolicy() : polling_interval_(DEFAULT_INTERVAL) {} | |
| 41 // PollingPolicyInterface | |
| 42 virtual void UpdatePollingInterval(bool scan_results_differ) { | |
| 43 if (scan_results_differ) { | |
| 44 polling_interval_ = DEFAULT_INTERVAL; | |
| 45 } else if (polling_interval_ == DEFAULT_INTERVAL) { | |
| 46 polling_interval_ = NO_CHANGE_INTERVAL; | |
| 47 } else { | |
| 48 DCHECK(polling_interval_ == NO_CHANGE_INTERVAL || | |
| 49 polling_interval_ == TWO_NO_CHANGE_INTERVAL); | |
| 50 polling_interval_ = TWO_NO_CHANGE_INTERVAL; | |
| 51 } | |
| 52 } | |
| 53 virtual int PollingInterval() { return polling_interval_; } | |
| 54 virtual int NoWifiInterval() { return NO_WIFI_INTERVAL; } | |
| 55 | |
| 56 private: | |
| 57 int polling_interval_; | |
| 58 }; | |
| 59 | |
| 60 // Base class to promote code sharing between platform specific wifi data | |
| 61 // providers. It's optional for specific platforms to derive this, but if they | |
| 62 // do threading and polling is taken care of by this base class, and all the | |
| 63 // platform need do is provide the underlying WLAN access API and policy policy, | |
| 64 // both of which will be create & accessed in the worker thread (only). | |
| 65 // Also designed this way to promotes ease of testing the cross-platform | |
| 66 // behavior w.r.t. polling & threading. | |
| 67 class WifiDataProviderCommon | |
| 68 : public WifiDataProviderImplBase, | |
| 69 private base::Thread { | |
| 70 public: | |
| 71 // Interface to abstract the low level data OS library call, and to allow | |
| 72 // mocking (hence public). | |
| 73 class WlanApiInterface { | |
| 74 public: | |
| 75 virtual ~WlanApiInterface() {} | |
| 76 // Gets wifi data for all visible access points. | |
| 77 virtual bool GetAccessPointData(WifiData::AccessPointDataSet* data) = 0; | |
| 78 }; | |
| 79 | |
| 80 WifiDataProviderCommon(); | |
| 81 | |
| 82 // WifiDataProviderImplBase implementation | |
| 83 virtual bool StartDataProvider(); | |
| 84 virtual void StopDataProvider(); | |
| 85 virtual bool GetData(WifiData *data); | |
| 86 | |
| 87 protected: | |
| 88 virtual ~WifiDataProviderCommon(); | |
| 89 | |
| 90 // Returns ownership. Will be called from the worker thread. | |
| 91 virtual WlanApiInterface* NewWlanApi() = 0; | |
| 92 | |
| 93 // Returns ownership. Will be called from the worker thread. | |
| 94 virtual PollingPolicyInterface* NewPollingPolicy() = 0; | |
| 95 | |
| 96 private: | |
| 97 // Thread implementation | |
| 98 virtual void Init(); | |
| 99 virtual void CleanUp(); | |
| 100 | |
| 101 // Task which run in the child thread. | |
| 102 void DoWifiScanTask(); | |
| 103 | |
| 104 // Will schedule a scan; i.e. enqueue DoWifiScanTask deferred task. | |
| 105 void ScheduleNextScan(int interval); | |
| 106 | |
| 107 WifiData wifi_data_; | |
| 108 base::Lock data_mutex_; | |
| 109 | |
| 110 // Whether we've successfully completed a scan for WiFi data (or the polling | |
| 111 // thread has terminated early). | |
| 112 bool is_first_scan_complete_; | |
| 113 | |
| 114 // Underlying OS wifi API. | |
| 115 scoped_ptr<WlanApiInterface> wlan_api_; | |
| 116 | |
| 117 // Controls the polling update interval. | |
| 118 scoped_ptr<PollingPolicyInterface> polling_policy_; | |
| 119 | |
| 120 // Holder for the tasks which run on the thread; takes care of cleanup. | |
| 121 ScopedRunnableMethodFactory<WifiDataProviderCommon> task_factory_; | |
| 122 | |
| 123 DISALLOW_COPY_AND_ASSIGN(WifiDataProviderCommon); | |
| 124 }; | |
| 125 | |
| 126 #endif // CHROME_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_COMMON_H_ | |
| OLD | NEW |