wifi_data_provider_handle.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2014 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. // A wifi data provider provides wifi data from the device that is used by a
  5. // NetworkLocationProvider to obtain a position fix. We use a singleton
  6. // instance of the wifi data provider manager, which is used by multiple
  7. // NetworkLocationProvider objects.
  8. //
  9. // This file provides WifiDataProviderHandle, which provides static methods to
  10. // access the singleton instance. The singleton instance uses a private
  11. // implementation of WifiDataProvider to abstract across platforms and also to
  12. // allow mock providers to be used for testing.
  13. #ifndef SERVICES_DEVICE_GEOLOCATION_WIFI_DATA_PROVIDER_HANDLE_H_
  14. #define SERVICES_DEVICE_GEOLOCATION_WIFI_DATA_PROVIDER_HANDLE_H_
  15. #include "base/bind.h"
  16. #include "base/callback.h"
  17. #include "base/memory/ref_counted.h"
  18. #include "base/strings/string_util.h"
  19. #include "services/device/geolocation/wifi_data.h"
  20. namespace device {
  21. class WifiDataProvider;
  22. // A handle for using wifi data providers.
  23. //
  24. // We use a singleton instance of WifiDataProvider which is shared by multiple
  25. // network location providers.
  26. class WifiDataProviderHandle {
  27. public:
  28. typedef WifiDataProvider* (*ImplFactoryFunction)();
  29. typedef base::RepeatingClosure WifiDataUpdateCallback;
  30. // Sets the factory function which will be used by Register to create the
  31. // implementation used by the singleton instance. This factory approach is
  32. // used both to abstract across platform-specific implementations and to
  33. // inject mock implementations for testing.
  34. static void SetFactoryForTesting(ImplFactoryFunction factory_function_in);
  35. // Resets the factory function to the default.
  36. static void ResetFactoryForTesting();
  37. // Creates a handle on WifiDataProvider.
  38. static std::unique_ptr<WifiDataProviderHandle> CreateHandle(
  39. WifiDataUpdateCallback* callback);
  40. ~WifiDataProviderHandle();
  41. WifiDataProviderHandle(const WifiDataProviderHandle&) = delete;
  42. WifiDataProviderHandle& operator=(const WifiDataProviderHandle&) = delete;
  43. // Returns true if the data provider is currently delayed by polling policy.
  44. bool DelayedByPolicy();
  45. // Provides whatever data the provider has, which may be nothing. Return
  46. // value indicates whether this is all the data the provider could ever
  47. // obtain.
  48. bool GetData(WifiData* data);
  49. void ForceRescan();
  50. private:
  51. explicit WifiDataProviderHandle(WifiDataUpdateCallback* callback);
  52. void AddCallback(WifiDataUpdateCallback* callback);
  53. bool RemoveCallback(WifiDataUpdateCallback* callback);
  54. bool has_callbacks() const;
  55. void StartDataProvider();
  56. void StopDataProvider();
  57. static WifiDataProvider* DefaultFactoryFunction();
  58. static scoped_refptr<WifiDataProvider> GetOrCreateProvider();
  59. // The factory function used to create the singleton instance.
  60. static ImplFactoryFunction factory_function_;
  61. scoped_refptr<WifiDataProvider> impl_;
  62. WifiDataUpdateCallback* callback_;
  63. };
  64. } // namespace device
  65. #endif // SERVICES_DEVICE_GEOLOCATION_WIFI_DATA_PROVIDER_HANDLE_H_