Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 9328443b | 2010-07-30 06:09:40 | [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 | #include "net/base/network_config_watcher_mac.h" |
| 6 | |
[email protected] | 9328443b | 2010-07-30 06:09:40 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
[email protected] | f7b773c | 2010-10-23 14:50:27 | [diff] [blame] | 9 | #include "base/compiler_specific.h" |
Avi Drissman | 41c4a41 | 2023-01-11 22:45:37 | [diff] [blame] | 10 | #include "base/functional/bind.h" |
Hans Wennborg | 725d043 | 2020-06-18 13:54:16 | [diff] [blame] | 11 | #include "base/logging.h" |
Keishi Hattori | 7c3c718 | 2022-06-24 22:18:14 | [diff] [blame] | 12 | #include "base/memory/raw_ptr.h" |
[email protected] | 9be87ba | 2011-09-30 01:49:25 | [diff] [blame] | 13 | #include "base/memory/weak_ptr.h" |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 14 | #include "base/message_loop/message_pump_type.h" |
Helen Li | a4dda252 | 2018-04-03 15:09:12 | [diff] [blame] | 15 | #include "base/metrics/histogram_macros.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 16 | #include "base/task/single_thread_task_runner.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 17 | #include "base/threading/thread.h" |
[email protected] | 221c38d | 2011-01-20 22:55:27 | [diff] [blame] | 18 | #include "base/threading/thread_restrictions.h" |
Helen Li | a4dda252 | 2018-04-03 15:09:12 | [diff] [blame] | 19 | #include "build/build_config.h" |
[email protected] | 9328443b | 2010-07-30 06:09:40 | [diff] [blame] | 20 | |
[email protected] | 9328443b | 2010-07-30 06:09:40 | [diff] [blame] | 21 | namespace net { |
| 22 | |
[email protected] | 6688a496 | 2010-09-07 19:41:36 | [diff] [blame] | 23 | namespace { |
| 24 | |
Helen Li | a4dda252 | 2018-04-03 15:09:12 | [diff] [blame] | 25 | // SCDynamicStore API does not exist on iOS. |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 26 | #if !BUILDFLAG(IS_IOS) |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 27 | const base::TimeDelta kRetryInterval = base::Seconds(1); |
Helen Li | a4dda252 | 2018-04-03 15:09:12 | [diff] [blame] | 28 | const int kMaxRetry = 5; |
| 29 | |
[email protected] | 6688a496 | 2010-09-07 19:41:36 | [diff] [blame] | 30 | // Called back by OS. Calls OnNetworkConfigChange(). |
| 31 | void DynamicStoreCallback(SCDynamicStoreRef /* store */, |
| 32 | CFArrayRef changed_keys, |
| 33 | void* config_delegate) { |
| 34 | NetworkConfigWatcherMac::Delegate* net_config_delegate = |
| 35 | static_cast<NetworkConfigWatcherMac::Delegate*>(config_delegate); |
| 36 | net_config_delegate->OnNetworkConfigChange(changed_keys); |
| 37 | } |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 38 | #endif // !BUILDFLAG(IS_IOS) |
[email protected] | 6688a496 | 2010-09-07 19:41:36 | [diff] [blame] | 39 | |
Gabriel Charette | 888ce27 | 2018-12-20 04:34:49 | [diff] [blame] | 40 | } // namespace |
| 41 | |
[email protected] | f7b773c | 2010-10-23 14:50:27 | [diff] [blame] | 42 | class NetworkConfigWatcherMacThread : public base::Thread { |
| 43 | public: |
David Bienvenu | a03ac8c | 2020-11-06 15:55:39 | [diff] [blame] | 44 | explicit NetworkConfigWatcherMacThread( |
| 45 | NetworkConfigWatcherMac::Delegate* delegate); |
| 46 | NetworkConfigWatcherMacThread(const NetworkConfigWatcherMacThread&) = delete; |
| 47 | NetworkConfigWatcherMacThread& operator=( |
| 48 | const NetworkConfigWatcherMacThread&) = delete; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 49 | ~NetworkConfigWatcherMacThread() override; |
[email protected] | 6688a496 | 2010-09-07 19:41:36 | [diff] [blame] | 50 | |
[email protected] | f7b773c | 2010-10-23 14:50:27 | [diff] [blame] | 51 | protected: |
| 52 | // base::Thread |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 53 | void Init() override; |
| 54 | void CleanUp() override; |
[email protected] | f7b773c | 2010-10-23 14:50:27 | [diff] [blame] | 55 | |
| 56 | private: |
| 57 | // The SystemConfiguration calls in this function can lead to contention early |
| 58 | // on, so we invoke this function later on in startup to keep it fast. |
| 59 | void InitNotifications(); |
| 60 | |
Helen Li | a4dda252 | 2018-04-03 15:09:12 | [diff] [blame] | 61 | // Returns whether initializing notifications has succeeded. |
| 62 | bool InitNotificationsHelper(); |
| 63 | |
Avi Drissman | 28154a6 | 2023-08-22 04:06:45 | [diff] [blame] | 64 | base::apple::ScopedCFTypeRef<CFRunLoopSourceRef> run_loop_source_; |
Keishi Hattori | 7c3c718 | 2022-06-24 22:18:14 | [diff] [blame] | 65 | const raw_ptr<NetworkConfigWatcherMac::Delegate> delegate_; |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 66 | #if !BUILDFLAG(IS_IOS) |
Tsuyoshi Horo | 432981d5 | 2022-06-09 09:50:13 | [diff] [blame] | 67 | int num_retry_ = 0; |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 68 | #endif // !BUILDFLAG(IS_IOS) |
[email protected] | 9be87ba | 2011-09-30 01:49:25 | [diff] [blame] | 69 | base::WeakPtrFactory<NetworkConfigWatcherMacThread> weak_factory_; |
[email protected] | f7b773c | 2010-10-23 14:50:27 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | NetworkConfigWatcherMacThread::NetworkConfigWatcherMacThread( |
| 73 | NetworkConfigWatcherMac::Delegate* delegate) |
| 74 | : base::Thread("NetworkConfigWatcher"), |
| 75 | delegate_(delegate), |
Tsuyoshi Horo | 432981d5 | 2022-06-09 09:50:13 | [diff] [blame] | 76 | weak_factory_(this) {} |
[email protected] | f7b773c | 2010-10-23 14:50:27 | [diff] [blame] | 77 | |
| 78 | NetworkConfigWatcherMacThread::~NetworkConfigWatcherMacThread() { |
Gabriel Charette | 888ce27 | 2018-12-20 04:34:49 | [diff] [blame] | 79 | // This is expected to be invoked during shutdown. |
| 80 | base::ScopedAllowBaseSyncPrimitivesOutsideBlockingScope allow_thread_join; |
[email protected] | f7b773c | 2010-10-23 14:50:27 | [diff] [blame] | 81 | Stop(); |
| 82 | } |
| 83 | |
| 84 | void NetworkConfigWatcherMacThread::Init() { |
[email protected] | f671d79 | 2011-09-02 18:11:47 | [diff] [blame] | 85 | delegate_->Init(); |
| 86 | |
[email protected] | 9328443b | 2010-07-30 06:09:40 | [diff] [blame] | 87 | // TODO(willchan): Look to see if there's a better signal for when it's ok to |
| 88 | // initialize this, rather than just delaying it by a fixed time. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 89 | const base::TimeDelta kInitializationDelay = base::Seconds(1); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 90 | task_runner()->PostDelayedTask( |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 91 | FROM_HERE, |
| 92 | base::BindOnce(&NetworkConfigWatcherMacThread::InitNotifications, |
| 93 | weak_factory_.GetWeakPtr()), |
[email protected] | 4a9d9d14 | 2012-04-09 16:32:39 | [diff] [blame] | 94 | kInitializationDelay); |
[email protected] | 9328443b | 2010-07-30 06:09:40 | [diff] [blame] | 95 | } |
| 96 | |
[email protected] | f7b773c | 2010-10-23 14:50:27 | [diff] [blame] | 97 | void NetworkConfigWatcherMacThread::CleanUp() { |
| 98 | if (!run_loop_source_.get()) |
| 99 | return; |
[email protected] | 9328443b | 2010-07-30 06:09:40 | [diff] [blame] | 100 | |
[email protected] | 9328443b | 2010-07-30 06:09:40 | [diff] [blame] | 101 | CFRunLoopRemoveSource(CFRunLoopGetCurrent(), run_loop_source_.get(), |
| 102 | kCFRunLoopCommonModes); |
| 103 | run_loop_source_.reset(); |
| 104 | } |
| 105 | |
[email protected] | f7b773c | 2010-10-23 14:50:27 | [diff] [blame] | 106 | void NetworkConfigWatcherMacThread::InitNotifications() { |
Helen Li | a4dda252 | 2018-04-03 15:09:12 | [diff] [blame] | 107 | // If initialization fails, retry after a 1s delay. |
| 108 | bool success = InitNotificationsHelper(); |
| 109 | |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 110 | #if !BUILDFLAG(IS_IOS) |
Helen Li | a4dda252 | 2018-04-03 15:09:12 | [diff] [blame] | 111 | if (!success && num_retry_ < kMaxRetry) { |
| 112 | LOG(ERROR) << "Retrying SystemConfiguration registration in 1 second."; |
| 113 | task_runner()->PostDelayedTask( |
| 114 | FROM_HERE, |
| 115 | base::BindOnce(&NetworkConfigWatcherMacThread::InitNotifications, |
| 116 | weak_factory_.GetWeakPtr()), |
| 117 | kRetryInterval); |
| 118 | num_retry_++; |
| 119 | return; |
| 120 | } |
| 121 | |
Helen Li | a4dda252 | 2018-04-03 15:09:12 | [diff] [blame] | 122 | #else |
| 123 | DCHECK(success); |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 124 | #endif // !BUILDFLAG(IS_IOS) |
Helen Li | a4dda252 | 2018-04-03 15:09:12 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | bool NetworkConfigWatcherMacThread::InitNotificationsHelper() { |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 128 | #if !BUILDFLAG(IS_IOS) |
[email protected] | a27065b | 2012-08-23 14:32:56 | [diff] [blame] | 129 | // SCDynamicStore API does not exist on iOS. |
[email protected] | 9328443b | 2010-07-30 06:09:40 | [diff] [blame] | 130 | // Add a run loop source for a dynamic store to the current run loop. |
| 131 | SCDynamicStoreContext context = { |
Tsuyoshi Horo | 291961af | 2022-06-16 08:51:27 | [diff] [blame] | 132 | 0, // Version 0. |
| 133 | delegate_, // User data. |
| 134 | nullptr, // This is not reference counted. No retain function. |
| 135 | nullptr, // This is not reference counted. No release function. |
| 136 | nullptr, // No description for this. |
[email protected] | 9328443b | 2010-07-30 06:09:40 | [diff] [blame] | 137 | }; |
Avi Drissman | 28154a6 | 2023-08-22 04:06:45 | [diff] [blame] | 138 | base::apple::ScopedCFTypeRef<SCDynamicStoreRef> store(SCDynamicStoreCreate( |
Tsuyoshi Horo | 291961af | 2022-06-16 08:51:27 | [diff] [blame] | 139 | nullptr, CFSTR("org.chromium"), DynamicStoreCallback, &context)); |
Helen Li | a4dda252 | 2018-04-03 15:09:12 | [diff] [blame] | 140 | if (!store) { |
| 141 | int error = SCError(); |
| 142 | LOG(ERROR) << "SCDynamicStoreCreate failed with Error: " << error << " - " |
| 143 | << SCErrorString(error); |
Helen Li | a4dda252 | 2018-04-03 15:09:12 | [diff] [blame] | 144 | return false; |
| 145 | } |
Tsuyoshi Horo | 291961af | 2022-06-16 08:51:27 | [diff] [blame] | 146 | run_loop_source_.reset( |
| 147 | SCDynamicStoreCreateRunLoopSource(nullptr, store.get(), 0)); |
Helen Li | a4dda252 | 2018-04-03 15:09:12 | [diff] [blame] | 148 | if (!run_loop_source_) { |
| 149 | int error = SCError(); |
| 150 | LOG(ERROR) << "SCDynamicStoreCreateRunLoopSource failed with Error: " |
| 151 | << error << " - " << SCErrorString(error); |
Helen Li | a4dda252 | 2018-04-03 15:09:12 | [diff] [blame] | 152 | return false; |
| 153 | } |
[email protected] | 9328443b | 2010-07-30 06:09:40 | [diff] [blame] | 154 | CFRunLoopAddSource(CFRunLoopGetCurrent(), run_loop_source_.get(), |
| 155 | kCFRunLoopCommonModes); |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 156 | #endif // !BUILDFLAG(IS_IOS) |
[email protected] | 9328443b | 2010-07-30 06:09:40 | [diff] [blame] | 157 | |
| 158 | // Set up notifications for interface and IP address changes. |
[email protected] | a301055d | 2012-01-11 10:58:17 | [diff] [blame] | 159 | delegate_->StartReachabilityNotifications(); |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 160 | #if !BUILDFLAG(IS_IOS) |
[email protected] | 6688a496 | 2010-09-07 19:41:36 | [diff] [blame] | 161 | delegate_->SetDynamicStoreNotificationKeys(store.get()); |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 162 | #endif // !BUILDFLAG(IS_IOS) |
Helen Li | a4dda252 | 2018-04-03 15:09:12 | [diff] [blame] | 163 | return true; |
[email protected] | 9328443b | 2010-07-30 06:09:40 | [diff] [blame] | 164 | } |
| 165 | |
[email protected] | f7b773c | 2010-10-23 14:50:27 | [diff] [blame] | 166 | NetworkConfigWatcherMac::NetworkConfigWatcherMac(Delegate* delegate) |
Tsuyoshi Horo | f8861cb | 2022-07-05 23:50:20 | [diff] [blame] | 167 | : notifier_thread_( |
| 168 | std::make_unique<NetworkConfigWatcherMacThread>(delegate)) { |
[email protected] | f7b773c | 2010-10-23 14:50:27 | [diff] [blame] | 169 | // We create this notifier thread because the notification implementation |
| 170 | // needs a thread with a CFRunLoop, and there's no guarantee that |
Carlos Caballero | b25fe847 | 2020-07-17 10:27:17 | [diff] [blame] | 171 | // CurrentThread::Get() meets that criterion. |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 172 | base::Thread::Options thread_options(base::MessagePumpType::UI, 0); |
Olivier Li | 28a5247 | 2021-05-12 03:08:28 | [diff] [blame] | 173 | notifier_thread_->StartWithOptions(std::move(thread_options)); |
[email protected] | f7b773c | 2010-10-23 14:50:27 | [diff] [blame] | 174 | } |
| 175 | |
Tsuyoshi Horo | 07c3f0e | 2022-06-16 07:30:47 | [diff] [blame] | 176 | NetworkConfigWatcherMac::~NetworkConfigWatcherMac() = default; |
[email protected] | f7b773c | 2010-10-23 14:50:27 | [diff] [blame] | 177 | |
[email protected] | 9328443b | 2010-07-30 06:09:40 | [diff] [blame] | 178 | } // namespace net |