summaryrefslogtreecommitdiffstats
path: root/net/base/network_change_notifier.cc
blob: a0bc6b5eebc57a7eb9c20a3c414e1cb5b9b7678d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/base/network_change_notifier.h"
#include "build/build_config.h"
#if defined(OS_WIN)
#include "net/base/network_change_notifier_win.h"
#elif defined(OS_LINUX)
#include "net/base/network_change_notifier_linux.h"
#elif defined(OS_MACOSX)
#include "net/base/network_change_notifier_mac.h"
#endif

namespace net {

namespace {

// The actual singleton notifier.  The class contract forbids usage of the API
// in ways that would require us to place locks around access to this object.
// (The prohibition on global non-POD objects makes it tricky to do such a thing
// anyway.)
NetworkChangeNotifier* g_network_change_notifier = NULL;

}  // namespace

NetworkChangeNotifier::~NetworkChangeNotifier() {
  DCHECK_EQ(this, g_network_change_notifier);
  g_network_change_notifier = NULL;
}

NetworkChangeNotifier* NetworkChangeNotifier::Create() {
#if defined(OS_WIN)
  return new NetworkChangeNotifierWin();
#elif defined(OS_LINUX)
  return new NetworkChangeNotifierLinux();
#elif defined(OS_MACOSX)
  return new NetworkChangeNotifierMac();
#else
  NOTIMPLEMENTED();
  return NULL;
#endif
}

void NetworkChangeNotifier::AddObserver(Observer* observer) {
  if (g_network_change_notifier)
    g_network_change_notifier->observer_list_->AddObserver(observer);
}

void NetworkChangeNotifier::RemoveObserver(Observer* observer) {
  if (g_network_change_notifier)
    g_network_change_notifier->observer_list_->RemoveObserver(observer);
}

NetworkChangeNotifier::NetworkChangeNotifier()
    : observer_list_(new ObserverListThreadSafe<Observer>()) {
  DCHECK(!g_network_change_notifier);
  g_network_change_notifier = this;
}

void NetworkChangeNotifier::NotifyObserversOfIPAddressChange() {
  if (g_network_change_notifier) {
    g_network_change_notifier->observer_list_->Notify(
        &Observer::OnIPAddressChanged);
  }
}

}  // namespace net