OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/network_config_watcher_mac.h" | 5 #include "net/base/network_config_watcher_mac.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 NetworkConfigWatcherMac::Delegate* const delegate_; | 47 NetworkConfigWatcherMac::Delegate* const delegate_; |
48 base::WeakPtrFactory<NetworkConfigWatcherMacThread> weak_factory_; | 48 base::WeakPtrFactory<NetworkConfigWatcherMacThread> weak_factory_; |
49 | 49 |
50 DISALLOW_COPY_AND_ASSIGN(NetworkConfigWatcherMacThread); | 50 DISALLOW_COPY_AND_ASSIGN(NetworkConfigWatcherMacThread); |
51 }; | 51 }; |
52 | 52 |
53 NetworkConfigWatcherMacThread::NetworkConfigWatcherMacThread( | 53 NetworkConfigWatcherMacThread::NetworkConfigWatcherMacThread( |
54 NetworkConfigWatcherMac::Delegate* delegate) | 54 NetworkConfigWatcherMac::Delegate* delegate) |
55 : base::Thread("NetworkConfigWatcher"), | 55 : base::Thread("NetworkConfigWatcher"), |
56 delegate_(delegate), | 56 delegate_(delegate), |
57 weak_factory_(this) {} | 57 weak_factory_(this) { |
| 58 } |
58 | 59 |
59 NetworkConfigWatcherMacThread::~NetworkConfigWatcherMacThread() { | 60 NetworkConfigWatcherMacThread::~NetworkConfigWatcherMacThread() { |
60 // Allow IO because Stop() calls PlatformThread::Join(), which is a blocking | 61 // Allow IO because Stop() calls PlatformThread::Join(), which is a blocking |
61 // operation. This is expected during shutdown. | 62 // operation. This is expected during shutdown. |
62 base::ThreadRestrictions::ScopedAllowIO allow_io; | 63 base::ThreadRestrictions::ScopedAllowIO allow_io; |
63 | 64 |
64 Stop(); | 65 Stop(); |
65 } | 66 } |
66 | 67 |
67 void NetworkConfigWatcherMacThread::Init() { | 68 void NetworkConfigWatcherMacThread::Init() { |
(...skipping 10 matching lines...) Expand all Loading... |
78 FROM_HERE, | 79 FROM_HERE, |
79 base::Bind(&NetworkConfigWatcherMacThread::InitNotifications, | 80 base::Bind(&NetworkConfigWatcherMacThread::InitNotifications, |
80 weak_factory_.GetWeakPtr()), | 81 weak_factory_.GetWeakPtr()), |
81 kInitializationDelay); | 82 kInitializationDelay); |
82 } | 83 } |
83 | 84 |
84 void NetworkConfigWatcherMacThread::CleanUp() { | 85 void NetworkConfigWatcherMacThread::CleanUp() { |
85 if (!run_loop_source_.get()) | 86 if (!run_loop_source_.get()) |
86 return; | 87 return; |
87 | 88 |
88 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), run_loop_source_.get(), | 89 CFRunLoopRemoveSource( |
89 kCFRunLoopCommonModes); | 90 CFRunLoopGetCurrent(), run_loop_source_.get(), kCFRunLoopCommonModes); |
90 run_loop_source_.reset(); | 91 run_loop_source_.reset(); |
91 } | 92 } |
92 | 93 |
93 void NetworkConfigWatcherMacThread::InitNotifications() { | 94 void NetworkConfigWatcherMacThread::InitNotifications() { |
94 #if !defined(OS_IOS) | 95 #if !defined(OS_IOS) |
95 // SCDynamicStore API does not exist on iOS. | 96 // SCDynamicStore API does not exist on iOS. |
96 // Add a run loop source for a dynamic store to the current run loop. | 97 // Add a run loop source for a dynamic store to the current run loop. |
97 SCDynamicStoreContext context = { | 98 SCDynamicStoreContext context = { |
98 0, // Version 0. | 99 0, // Version 0. |
99 delegate_, // User data. | 100 delegate_, // User data. |
100 NULL, // This is not reference counted. No retain function. | 101 NULL, // This is not reference counted. No retain function. |
101 NULL, // This is not reference counted. No release function. | 102 NULL, // This is not reference counted. No release function. |
102 NULL, // No description for this. | 103 NULL, // No description for this. |
103 }; | 104 }; |
104 base::ScopedCFTypeRef<SCDynamicStoreRef> store(SCDynamicStoreCreate( | 105 base::ScopedCFTypeRef<SCDynamicStoreRef> store(SCDynamicStoreCreate( |
105 NULL, CFSTR("org.chromium"), DynamicStoreCallback, &context)); | 106 NULL, CFSTR("org.chromium"), DynamicStoreCallback, &context)); |
106 run_loop_source_.reset(SCDynamicStoreCreateRunLoopSource( | 107 run_loop_source_.reset( |
107 NULL, store.get(), 0)); | 108 SCDynamicStoreCreateRunLoopSource(NULL, store.get(), 0)); |
108 CFRunLoopAddSource(CFRunLoopGetCurrent(), run_loop_source_.get(), | 109 CFRunLoopAddSource( |
109 kCFRunLoopCommonModes); | 110 CFRunLoopGetCurrent(), run_loop_source_.get(), kCFRunLoopCommonModes); |
110 #endif // !defined(OS_IOS) | 111 #endif // !defined(OS_IOS) |
111 | 112 |
112 // Set up notifications for interface and IP address changes. | 113 // Set up notifications for interface and IP address changes. |
113 delegate_->StartReachabilityNotifications(); | 114 delegate_->StartReachabilityNotifications(); |
114 #if !defined(OS_IOS) | 115 #if !defined(OS_IOS) |
115 delegate_->SetDynamicStoreNotificationKeys(store.get()); | 116 delegate_->SetDynamicStoreNotificationKeys(store.get()); |
116 #endif // !defined(OS_IOS) | 117 #endif // !defined(OS_IOS) |
117 } | 118 } |
118 | 119 |
119 } // namespace | 120 } // namespace |
120 | 121 |
121 NetworkConfigWatcherMac::NetworkConfigWatcherMac(Delegate* delegate) | 122 NetworkConfigWatcherMac::NetworkConfigWatcherMac(Delegate* delegate) |
122 : notifier_thread_(new NetworkConfigWatcherMacThread(delegate)) { | 123 : notifier_thread_(new NetworkConfigWatcherMacThread(delegate)) { |
123 // We create this notifier thread because the notification implementation | 124 // We create this notifier thread because the notification implementation |
124 // needs a thread with a CFRunLoop, and there's no guarantee that | 125 // needs a thread with a CFRunLoop, and there's no guarantee that |
125 // MessageLoop::current() meets that criterion. | 126 // MessageLoop::current() meets that criterion. |
126 base::Thread::Options thread_options(base::MessageLoop::TYPE_UI, 0); | 127 base::Thread::Options thread_options(base::MessageLoop::TYPE_UI, 0); |
127 notifier_thread_->StartWithOptions(thread_options); | 128 notifier_thread_->StartWithOptions(thread_options); |
128 } | 129 } |
129 | 130 |
130 NetworkConfigWatcherMac::~NetworkConfigWatcherMac() {} | 131 NetworkConfigWatcherMac::~NetworkConfigWatcherMac() { |
| 132 } |
131 | 133 |
132 } // namespace net | 134 } // namespace net |
OLD | NEW |