blob: 54ca27d0797f8447ad25f86bea13de2e5e8c1788 [file] [log] [blame]
[email protected]12bd2c02011-06-02 21:56:441// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]100d5fb92009-12-21 21:08:352// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef NET_BASE_NETWORK_CHANGE_NOTIFIER_MAC_H_
6#define NET_BASE_NETWORK_CHANGE_NOTIFIER_MAC_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]100d5fb92009-12-21 21:08:358
[email protected]88a59c82011-09-12 21:28:159#include <SystemConfiguration/SystemConfiguration.h>
[email protected]66761b952010-06-25 21:30:3810
[email protected]100d5fb92009-12-21 21:08:3511#include "base/basictypes.h"
[email protected]9be87ba2011-09-30 01:49:2512#include "base/compiler_specific.h"
[email protected]12bd2c02011-06-02 21:56:4413#include "base/mac/scoped_cftyperef.h"
[email protected]7f8f5562011-09-18 20:59:0614#include "base/memory/scoped_ptr.h"
[email protected]f671d792011-09-02 18:11:4715#include "base/synchronization/condition_variable.h"
[email protected]d72b8a82011-06-08 20:43:2516#include "base/synchronization/lock.h"
[email protected]100d5fb92009-12-21 21:08:3517#include "net/base/network_change_notifier.h"
[email protected]9328443b2010-07-30 06:09:4018#include "net/base/network_config_watcher_mac.h"
[email protected]100d5fb92009-12-21 21:08:3519
20namespace net {
21
[email protected]6688a4962010-09-07 19:41:3622class NetworkChangeNotifierMac: public NetworkChangeNotifier {
[email protected]100d5fb92009-12-21 21:08:3523 public:
[email protected]aa92ba22010-06-21 23:17:2424 NetworkChangeNotifierMac();
[email protected]6688a4962010-09-07 19:41:3625 virtual ~NetworkChangeNotifierMac();
[email protected]9328443b2010-07-30 06:09:4026
[email protected]2d3b7762010-10-09 00:35:4727 // NetworkChangeNotifier implementation:
[email protected]9be87ba2011-09-30 01:49:2528 virtual bool IsCurrentlyOffline() const OVERRIDE;
[email protected]2d3b7762010-10-09 00:35:4729
[email protected]100d5fb92009-12-21 21:08:3530 private:
[email protected]f671d792011-09-02 18:11:4731 enum OnlineState {
32 UNINITIALIZED = -1,
33 OFFLINE = 0,
34 ONLINE = 1
35 };
36
[email protected]6688a4962010-09-07 19:41:3637 // Forwarder just exists to keep the NetworkConfigWatcherMac API out of
38 // NetworkChangeNotifierMac's public API.
39 class Forwarder : public NetworkConfigWatcherMac::Delegate {
40 public:
41 explicit Forwarder(NetworkChangeNotifierMac* net_config_watcher)
42 : net_config_watcher_(net_config_watcher) {}
43
44 // NetworkConfigWatcherMac::Delegate implementation:
[email protected]9be87ba2011-09-30 01:49:2545 virtual void Init() OVERRIDE {
[email protected]f671d792011-09-02 18:11:4746 net_config_watcher_->SetInitialState();
47 }
[email protected]9be87ba2011-09-30 01:49:2548 virtual void SetDynamicStoreNotificationKeys(
49 SCDynamicStoreRef store) OVERRIDE {
[email protected]6688a4962010-09-07 19:41:3650 net_config_watcher_->SetDynamicStoreNotificationKeys(store);
51 }
[email protected]9be87ba2011-09-30 01:49:2552 virtual void OnNetworkConfigChange(CFArrayRef changed_keys) OVERRIDE {
[email protected]6688a4962010-09-07 19:41:3653 net_config_watcher_->OnNetworkConfigChange(changed_keys);
54 }
55
56 private:
57 NetworkChangeNotifierMac* const net_config_watcher_;
58 DISALLOW_COPY_AND_ASSIGN(Forwarder);
59 };
60
61 // NetworkConfigWatcherMac::Delegate implementation:
62 void SetDynamicStoreNotificationKeys(SCDynamicStoreRef store);
63 void OnNetworkConfigChange(CFArrayRef changed_keys);
64
[email protected]f671d792011-09-02 18:11:4765 void SetInitialState();
66
[email protected]12bd2c02011-06-02 21:56:4467 static void ReachabilityCallback(SCNetworkReachabilityRef target,
68 SCNetworkConnectionFlags flags,
69 void* notifier);
70
[email protected]f671d792011-09-02 18:11:4771 // These must be constructed before config_watcher_ to ensure
72 // the lock is in a valid state when Forwarder::Init is called.
73 OnlineState online_state_;
74 mutable base::Lock online_state_lock_;
75 mutable base::ConditionVariable initial_state_cv_;
[email protected]12bd2c02011-06-02 21:56:4476 base::mac::ScopedCFTypeRef<SCNetworkReachabilityRef> reachability_;
[email protected]f671d792011-09-02 18:11:4777 base::mac::ScopedCFTypeRef<CFRunLoopRef> run_loop_;
78
79 Forwarder forwarder_;
[email protected]88a59c82011-09-12 21:28:1580 scoped_ptr<const NetworkConfigWatcherMac> config_watcher_;
[email protected]6688a4962010-09-07 19:41:3681
[email protected]100d5fb92009-12-21 21:08:3582 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierMac);
83};
84
85} // namespace net
86
87#endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_MAC_H_