[email protected] | 12bd2c0 | 2011-06-02 21:56:44 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 2 | // 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] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 8 | |
[email protected] | 88a59c8 | 2011-09-12 21:28:15 | [diff] [blame] | 9 | #include <SystemConfiguration/SystemConfiguration.h> |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 10 | |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 11 | #include "base/basictypes.h" |
[email protected] | 9be87ba | 2011-09-30 01:49:25 | [diff] [blame] | 12 | #include "base/compiler_specific.h" |
[email protected] | 12bd2c0 | 2011-06-02 21:56:44 | [diff] [blame] | 13 | #include "base/mac/scoped_cftyperef.h" |
[email protected] | 7f8f556 | 2011-09-18 20:59:06 | [diff] [blame] | 14 | #include "base/memory/scoped_ptr.h" |
[email protected] | f671d79 | 2011-09-02 18:11:47 | [diff] [blame] | 15 | #include "base/synchronization/condition_variable.h" |
[email protected] | d72b8a8 | 2011-06-08 20:43:25 | [diff] [blame] | 16 | #include "base/synchronization/lock.h" |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 17 | #include "net/base/network_change_notifier.h" |
[email protected] | 9328443b | 2010-07-30 06:09:40 | [diff] [blame] | 18 | #include "net/base/network_config_watcher_mac.h" |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 19 | |
20 | namespace net { | ||||
21 | |||||
[email protected] | 6688a496 | 2010-09-07 19:41:36 | [diff] [blame] | 22 | class NetworkChangeNotifierMac: public NetworkChangeNotifier { |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 23 | public: |
[email protected] | aa92ba2 | 2010-06-21 23:17:24 | [diff] [blame] | 24 | NetworkChangeNotifierMac(); |
[email protected] | 6688a496 | 2010-09-07 19:41:36 | [diff] [blame] | 25 | virtual ~NetworkChangeNotifierMac(); |
[email protected] | 9328443b | 2010-07-30 06:09:40 | [diff] [blame] | 26 | |
[email protected] | 2d3b776 | 2010-10-09 00:35:47 | [diff] [blame] | 27 | // NetworkChangeNotifier implementation: |
[email protected] | 9be87ba | 2011-09-30 01:49:25 | [diff] [blame] | 28 | virtual bool IsCurrentlyOffline() const OVERRIDE; |
[email protected] | 2d3b776 | 2010-10-09 00:35:47 | [diff] [blame] | 29 | |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 30 | private: |
[email protected] | f671d79 | 2011-09-02 18:11:47 | [diff] [blame] | 31 | enum OnlineState { |
32 | UNINITIALIZED = -1, | ||||
33 | OFFLINE = 0, | ||||
34 | ONLINE = 1 | ||||
35 | }; | ||||
36 | |||||
[email protected] | 6688a496 | 2010-09-07 19:41:36 | [diff] [blame] | 37 | // 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] | 9be87ba | 2011-09-30 01:49:25 | [diff] [blame] | 45 | virtual void Init() OVERRIDE { |
[email protected] | f671d79 | 2011-09-02 18:11:47 | [diff] [blame] | 46 | net_config_watcher_->SetInitialState(); |
47 | } | ||||
[email protected] | 9be87ba | 2011-09-30 01:49:25 | [diff] [blame] | 48 | virtual void SetDynamicStoreNotificationKeys( |
49 | SCDynamicStoreRef store) OVERRIDE { | ||||
[email protected] | 6688a496 | 2010-09-07 19:41:36 | [diff] [blame] | 50 | net_config_watcher_->SetDynamicStoreNotificationKeys(store); |
51 | } | ||||
[email protected] | 9be87ba | 2011-09-30 01:49:25 | [diff] [blame] | 52 | virtual void OnNetworkConfigChange(CFArrayRef changed_keys) OVERRIDE { |
[email protected] | 6688a496 | 2010-09-07 19:41:36 | [diff] [blame] | 53 | 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] | f671d79 | 2011-09-02 18:11:47 | [diff] [blame] | 65 | void SetInitialState(); |
66 | |||||
[email protected] | 12bd2c0 | 2011-06-02 21:56:44 | [diff] [blame] | 67 | static void ReachabilityCallback(SCNetworkReachabilityRef target, |
68 | SCNetworkConnectionFlags flags, | ||||
69 | void* notifier); | ||||
70 | |||||
[email protected] | f671d79 | 2011-09-02 18:11:47 | [diff] [blame] | 71 | // 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] | 12bd2c0 | 2011-06-02 21:56:44 | [diff] [blame] | 76 | base::mac::ScopedCFTypeRef<SCNetworkReachabilityRef> reachability_; |
[email protected] | f671d79 | 2011-09-02 18:11:47 | [diff] [blame] | 77 | base::mac::ScopedCFTypeRef<CFRunLoopRef> run_loop_; |
78 | |||||
79 | Forwarder forwarder_; | ||||
[email protected] | 88a59c8 | 2011-09-12 21:28:15 | [diff] [blame] | 80 | scoped_ptr<const NetworkConfigWatcherMac> config_watcher_; |
[email protected] | 6688a496 | 2010-09-07 19:41:36 | [diff] [blame] | 81 | |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 82 | DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierMac); |
83 | }; | ||||
84 | |||||
85 | } // namespace net | ||||
86 | |||||
87 | #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_MAC_H_ |