| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <SystemConfiguration/SCDynamicStoreKey.h> | 7 #include <SystemConfiguration/SCDynamicStoreKey.h> |
| 8 #include <SystemConfiguration/SCSchemaDefinitions.h> | 8 #include <SystemConfiguration/SCSchemaDefinitions.h> |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| 11 #include "base/compiler_specific.h" |
| 11 #include "base/thread.h" | 12 #include "base/thread.h" |
| 12 #include "base/mac/scoped_cftyperef.h" | 13 #include "base/mac/scoped_cftyperef.h" |
| 13 | 14 |
| 14 // We only post tasks to a child thread we own, so we don't need refcounting. | |
| 15 DISABLE_RUNNABLE_METHOD_REFCOUNT(net::NetworkConfigWatcherMac); | |
| 16 | |
| 17 namespace net { | 15 namespace net { |
| 18 | 16 |
| 19 namespace { | 17 namespace { |
| 20 | 18 |
| 21 // Called back by OS. Calls OnNetworkConfigChange(). | 19 // Called back by OS. Calls OnNetworkConfigChange(). |
| 22 void DynamicStoreCallback(SCDynamicStoreRef /* store */, | 20 void DynamicStoreCallback(SCDynamicStoreRef /* store */, |
| 23 CFArrayRef changed_keys, | 21 CFArrayRef changed_keys, |
| 24 void* config_delegate) { | 22 void* config_delegate) { |
| 25 NetworkConfigWatcherMac::Delegate* net_config_delegate = | 23 NetworkConfigWatcherMac::Delegate* net_config_delegate = |
| 26 static_cast<NetworkConfigWatcherMac::Delegate*>(config_delegate); | 24 static_cast<NetworkConfigWatcherMac::Delegate*>(config_delegate); |
| 27 net_config_delegate->OnNetworkConfigChange(changed_keys); | 25 net_config_delegate->OnNetworkConfigChange(changed_keys); |
| 28 } | 26 } |
| 29 | 27 |
| 30 } // namespace | 28 class NetworkConfigWatcherMacThread : public base::Thread { |
| 29 public: |
| 30 NetworkConfigWatcherMacThread(NetworkConfigWatcherMac::Delegate* delegate); |
| 31 virtual ~NetworkConfigWatcherMacThread(); |
| 31 | 32 |
| 32 NetworkConfigWatcherMac::NetworkConfigWatcherMac( | 33 protected: |
| 33 Delegate* delegate) | 34 // base::Thread |
| 34 : notifier_thread_(new base::Thread("NetworkConfigWatcher")), | 35 virtual void Init(); |
| 35 delegate_(delegate) { | 36 virtual void CleanUp(); |
| 36 // We create this notifier thread because the notification implementation | 37 |
| 37 // needs a thread with a CFRunLoop, and there's no guarantee that | 38 private: |
| 38 // MessageLoop::current() meets that criterion. | 39 // The SystemConfiguration calls in this function can lead to contention early |
| 39 base::Thread::Options thread_options(MessageLoop::TYPE_UI, 0); | 40 // on, so we invoke this function later on in startup to keep it fast. |
| 40 notifier_thread_->StartWithOptions(thread_options); | 41 void InitNotifications(); |
| 42 |
| 43 base::mac::ScopedCFTypeRef<CFRunLoopSourceRef> run_loop_source_; |
| 44 NetworkConfigWatcherMac::Delegate* const delegate_; |
| 45 ScopedRunnableMethodFactory<NetworkConfigWatcherMacThread> method_factory_; |
| 46 |
| 47 DISALLOW_COPY_AND_ASSIGN(NetworkConfigWatcherMacThread); |
| 48 }; |
| 49 |
| 50 NetworkConfigWatcherMacThread::NetworkConfigWatcherMacThread( |
| 51 NetworkConfigWatcherMac::Delegate* delegate) |
| 52 : base::Thread("NetworkConfigWatcher"), |
| 53 delegate_(delegate), |
| 54 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} |
| 55 |
| 56 NetworkConfigWatcherMacThread::~NetworkConfigWatcherMacThread() { |
| 57 Stop(); |
| 58 } |
| 59 |
| 60 void NetworkConfigWatcherMacThread::Init() { |
| 41 // TODO(willchan): Look to see if there's a better signal for when it's ok to | 61 // TODO(willchan): Look to see if there's a better signal for when it's ok to |
| 42 // initialize this, rather than just delaying it by a fixed time. | 62 // initialize this, rather than just delaying it by a fixed time. |
| 43 const int kNotifierThreadInitializationDelayMS = 1000; | 63 const int kInitializationDelayMS = 1000; |
| 44 notifier_thread_->message_loop()->PostDelayedTask( | 64 message_loop()->PostDelayedTask( |
| 45 FROM_HERE, | 65 FROM_HERE, |
| 46 NewRunnableMethod(this, &NetworkConfigWatcherMac::Init), | 66 method_factory_.NewRunnableMethod( |
| 47 kNotifierThreadInitializationDelayMS); | 67 &NetworkConfigWatcherMacThread::InitNotifications), |
| 68 kInitializationDelayMS); |
| 48 } | 69 } |
| 49 | 70 |
| 50 NetworkConfigWatcherMac::~NetworkConfigWatcherMac() { | 71 void NetworkConfigWatcherMacThread::CleanUp() { |
| 51 // We don't need to explicitly Stop(), but doing so allows us to sanity- | 72 if (!run_loop_source_.get()) |
| 52 // check that the notifier thread shut down properly. | 73 return; |
| 53 notifier_thread_->Stop(); | |
| 54 DCHECK(run_loop_source_ == NULL); | |
| 55 } | |
| 56 | 74 |
| 57 void NetworkConfigWatcherMac::WillDestroyCurrentMessageLoop() { | |
| 58 DCHECK(notifier_thread_ != NULL); | |
| 59 // We can't check the notifier_thread_'s message_loop(), as it's now 0. | |
| 60 // DCHECK_EQ(notifier_thread_->message_loop(), MessageLoop::current()); | |
| 61 | |
| 62 DCHECK(run_loop_source_ != NULL); | |
| 63 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), run_loop_source_.get(), | 75 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), run_loop_source_.get(), |
| 64 kCFRunLoopCommonModes); | 76 kCFRunLoopCommonModes); |
| 65 run_loop_source_.reset(); | 77 run_loop_source_.reset(); |
| 66 } | 78 } |
| 67 | 79 |
| 68 void NetworkConfigWatcherMac::Init() { | 80 void NetworkConfigWatcherMacThread::InitNotifications() { |
| 69 DCHECK(notifier_thread_ != NULL); | |
| 70 DCHECK_EQ(notifier_thread_->message_loop(), MessageLoop::current()); | |
| 71 | |
| 72 // Add a run loop source for a dynamic store to the current run loop. | 81 // Add a run loop source for a dynamic store to the current run loop. |
| 73 SCDynamicStoreContext context = { | 82 SCDynamicStoreContext context = { |
| 74 0, // Version 0. | 83 0, // Version 0. |
| 75 delegate_, // User data. | 84 delegate_, // User data. |
| 76 NULL, // This is not reference counted. No retain function. | 85 NULL, // This is not reference counted. No retain function. |
| 77 NULL, // This is not reference counted. No release function. | 86 NULL, // This is not reference counted. No release function. |
| 78 NULL, // No description for this. | 87 NULL, // No description for this. |
| 79 }; | 88 }; |
| 80 base::mac::ScopedCFTypeRef<SCDynamicStoreRef> store(SCDynamicStoreCreate( | 89 base::mac::ScopedCFTypeRef<SCDynamicStoreRef> store(SCDynamicStoreCreate( |
| 81 NULL, CFSTR("org.chromium"), DynamicStoreCallback, &context)); | 90 NULL, CFSTR("org.chromium"), DynamicStoreCallback, &context)); |
| 82 run_loop_source_.reset(SCDynamicStoreCreateRunLoopSource( | 91 run_loop_source_.reset(SCDynamicStoreCreateRunLoopSource( |
| 83 NULL, store.get(), 0)); | 92 NULL, store.get(), 0)); |
| 84 CFRunLoopAddSource(CFRunLoopGetCurrent(), run_loop_source_.get(), | 93 CFRunLoopAddSource(CFRunLoopGetCurrent(), run_loop_source_.get(), |
| 85 kCFRunLoopCommonModes); | 94 kCFRunLoopCommonModes); |
| 86 | 95 |
| 87 // Set up notifications for interface and IP address changes. | 96 // Set up notifications for interface and IP address changes. |
| 88 delegate_->SetDynamicStoreNotificationKeys(store.get()); | 97 delegate_->SetDynamicStoreNotificationKeys(store.get()); |
| 89 | |
| 90 MessageLoop::current()->AddDestructionObserver(this); | |
| 91 } | 98 } |
| 92 | 99 |
| 100 } // namespace |
| 101 |
| 102 NetworkConfigWatcherMac::NetworkConfigWatcherMac(Delegate* delegate) |
| 103 : notifier_thread_(new NetworkConfigWatcherMacThread(delegate)) { |
| 104 // We create this notifier thread because the notification implementation |
| 105 // needs a thread with a CFRunLoop, and there's no guarantee that |
| 106 // MessageLoop::current() meets that criterion. |
| 107 base::Thread::Options thread_options(MessageLoop::TYPE_UI, 0); |
| 108 notifier_thread_->StartWithOptions(thread_options); |
| 109 } |
| 110 |
| 111 NetworkConfigWatcherMac::~NetworkConfigWatcherMac() {} |
| 112 |
| 93 } // namespace net | 113 } // namespace net |
| OLD | NEW |