wifi_data_provider.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2013 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_H_
  5. #define SERVICES_DEVICE_GEOLOCATION_WIFI_DATA_PROVIDER_H_
  6. #include <set>
  7. #include "base/callback_forward.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/task/single_thread_task_runner.h"
  11. #include "services/device/geolocation/wifi_data.h"
  12. namespace device {
  13. class WifiDataProvider : public base::RefCountedThreadSafe<WifiDataProvider> {
  14. public:
  15. WifiDataProvider();
  16. WifiDataProvider(const WifiDataProvider&) = delete;
  17. WifiDataProvider& operator=(const WifiDataProvider&) = delete;
  18. // Tells the provider to start looking for data. Callbacks will start
  19. // receiving notifications after this call.
  20. virtual void StartDataProvider() = 0;
  21. // Tells the provider to stop looking for data. Callbacks will stop
  22. // receiving notifications after this call.
  23. virtual void StopDataProvider() = 0;
  24. // Returns true if the provider is delayed due to scanning policy.
  25. virtual bool DelayedByPolicy() = 0;
  26. // Provides whatever data the provider has, which may be nothing. Return
  27. // value indicates whether this is all the data the provider could ever
  28. // obtain.
  29. virtual bool GetData(WifiData* data) = 0;
  30. virtual void ForceRescan() = 0;
  31. typedef base::RepeatingClosure WifiDataUpdateCallback;
  32. void AddCallback(WifiDataUpdateCallback* callback);
  33. bool RemoveCallback(WifiDataUpdateCallback* callback);
  34. bool has_callbacks() const;
  35. base::WeakPtr<WifiDataProvider> GetWeakPtr() {
  36. return weak_factory_.GetWeakPtr();
  37. }
  38. protected:
  39. friend class base::RefCountedThreadSafe<WifiDataProvider>;
  40. virtual ~WifiDataProvider();
  41. typedef std::set<WifiDataUpdateCallback*> CallbackSet;
  42. // Runs all callbacks via a posted task, so we can unwind callstack here and
  43. // avoid client reentrancy.
  44. void RunCallbacks();
  45. bool CalledOnClientThread() const;
  46. scoped_refptr<base::SingleThreadTaskRunner> client_task_runner() const {
  47. return client_task_runner_;
  48. }
  49. private:
  50. void DoRunCallbacks();
  51. // The task runner for the client thread, all callbacks should run on it.
  52. scoped_refptr<base::SingleThreadTaskRunner> client_task_runner_;
  53. CallbackSet callbacks_;
  54. base::WeakPtrFactory<WifiDataProvider> weak_factory_{this};
  55. };
  56. } // namespace device
  57. #endif // SERVICES_DEVICE_GEOLOCATION_WIFI_DATA_PROVIDER_H_