wifi_data_provider_common.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #ifndef SERVICES_DEVICE_GEOLOCATION_WIFI_DATA_PROVIDER_COMMON_H_
  5. #define SERVICES_DEVICE_GEOLOCATION_WIFI_DATA_PROVIDER_COMMON_H_
  6. #include <assert.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <string>
  10. #include "base/memory/weak_ptr.h"
  11. #include "services/device/geolocation/wifi_data_provider.h"
  12. #include "services/device/geolocation/wifi_polling_policy.h"
  13. namespace device {
  14. // Converts a MAC address stored as an array of uint8_t to a string.
  15. std::u16string MacAddressAsString16(const uint8_t mac_as_int[6]);
  16. // Base class to promote code sharing between platform specific wifi data
  17. // providers. It's optional for specific platforms to derive this, but if they
  18. // do polling behavior is taken care of by this base class, and all the platform
  19. // need do is provide the underlying WLAN access API and polling policy.
  20. // Also designed this way for ease of testing the cross-platform behavior.
  21. class WifiDataProviderCommon : public WifiDataProvider {
  22. public:
  23. // Interface to abstract the low level data OS library call, and to allow
  24. // mocking (hence public).
  25. class WlanApiInterface {
  26. public:
  27. virtual ~WlanApiInterface() {}
  28. // Gets wifi data for all visible access points.
  29. virtual bool GetAccessPointData(WifiData::AccessPointDataSet* data) = 0;
  30. };
  31. WifiDataProviderCommon();
  32. WifiDataProviderCommon(const WifiDataProviderCommon&) = delete;
  33. WifiDataProviderCommon& operator=(const WifiDataProviderCommon&) = delete;
  34. // WifiDataProvider implementation
  35. void StartDataProvider() override;
  36. void StopDataProvider() override;
  37. bool DelayedByPolicy() override;
  38. bool GetData(WifiData* data) override;
  39. void ForceRescan() override;
  40. protected:
  41. ~WifiDataProviderCommon() override;
  42. // Returns ownership.
  43. virtual std::unique_ptr<WlanApiInterface> CreateWlanApi() = 0;
  44. // Returns ownership.
  45. virtual std::unique_ptr<WifiPollingPolicy> CreatePollingPolicy() = 0;
  46. private:
  47. // Runs a scan. Calls the callbacks if new data is found.
  48. void DoWifiScanTask();
  49. // Will schedule a scan; i.e. enqueue DoWifiScanTask deferred task.
  50. void ScheduleNextScan(int interval);
  51. WifiData wifi_data_;
  52. // Whether we've successfully completed a scan for WiFi data.
  53. bool is_first_scan_complete_ = false;
  54. // Whether our first scan was delayed due to polling policy.
  55. bool first_scan_delayed_ = false;
  56. // Underlying OS wifi API.
  57. std::unique_ptr<WlanApiInterface> wlan_api_;
  58. // Holder for delayed tasks; takes care of cleanup.
  59. base::WeakPtrFactory<WifiDataProviderCommon> weak_factory_{this};
  60. };
  61. } // namespace device
  62. #endif // SERVICES_DEVICE_GEOLOCATION_WIFI_DATA_PROVIDER_COMMON_H_