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