wifi_data_provider.cc 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. #include "services/device/geolocation/wifi_data_provider.h"
  5. #include "base/bind.h"
  6. #include "base/callback.h"
  7. #include "base/location.h"
  8. #include "base/threading/thread_task_runner_handle.h"
  9. namespace device {
  10. WifiDataProvider::WifiDataProvider()
  11. : client_task_runner_(base::ThreadTaskRunnerHandle::Get()) {
  12. DCHECK(client_task_runner_);
  13. }
  14. WifiDataProvider::~WifiDataProvider() = default;
  15. void WifiDataProvider::AddCallback(WifiDataUpdateCallback* callback) {
  16. callbacks_.insert(callback);
  17. }
  18. bool WifiDataProvider::RemoveCallback(WifiDataUpdateCallback* callback) {
  19. return callbacks_.erase(callback) == 1;
  20. }
  21. bool WifiDataProvider::has_callbacks() const {
  22. return !callbacks_.empty();
  23. }
  24. void WifiDataProvider::RunCallbacks() {
  25. client_task_runner_->PostTask(
  26. FROM_HERE, base::BindOnce(&WifiDataProvider::DoRunCallbacks, this));
  27. }
  28. bool WifiDataProvider::CalledOnClientThread() const {
  29. return client_task_runner()->BelongsToCurrentThread();
  30. }
  31. void WifiDataProvider::DoRunCallbacks() {
  32. // It's possible that all the callbacks went away whilst this task was
  33. // pending. This is fine; the loop will be a no-op.
  34. CallbackSet::const_iterator iter = callbacks_.begin();
  35. while (iter != callbacks_.end()) {
  36. WifiDataUpdateCallback* callback = *iter;
  37. ++iter; // Advance iter before running, in case callback unregisters.
  38. callback->Run();
  39. }
  40. }
  41. } // namespace device