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