| 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 //////////////////////////////////////////////////////////////////////////////// | 5 //////////////////////////////////////////////////////////////////////////////// |
| 6 // Threading considerations: | 6 // Threading considerations: |
| 7 // | 7 // |
| 8 // This class is designed to meet various threading guarantees starting from the | 8 // This class is designed to meet various threading guarantees starting from the |
| 9 // ones imposed by NetworkChangeNotifier: | 9 // ones imposed by NetworkChangeNotifier: |
| 10 // - The notifier can be constructed on any thread. | 10 // - The notifier can be constructed on any thread. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 // state change, and notifies each of its observers on their threads. | 54 // state change, and notifies each of its observers on their threads. |
| 55 // | 55 // |
| 56 // This can also be seen as: | 56 // This can also be seen as: |
| 57 // Android platform -> NetworkChangeNotifier (Java) -> | 57 // Android platform -> NetworkChangeNotifier (Java) -> |
| 58 // NetworkChangeNotifierDelegateAndroid -> NetworkChangeNotifierAndroid. | 58 // NetworkChangeNotifierDelegateAndroid -> NetworkChangeNotifierAndroid. |
| 59 | 59 |
| 60 #include "net/android/network_change_notifier_android.h" | 60 #include "net/android/network_change_notifier_android.h" |
| 61 | 61 |
| 62 #include "base/threading/thread.h" | 62 #include "base/threading/thread.h" |
| 63 #include "net/base/address_tracker_linux.h" | 63 #include "net/base/address_tracker_linux.h" |
| 64 #include "net/dns/dns_config_service.h" | 64 #include "net/dns/dns_config_service_posix.h" |
| 65 | 65 |
| 66 namespace net { | 66 namespace net { |
| 67 | 67 |
| 68 // Thread on which we can run DnsConfigService, which requires a TYPE_IO | 68 // Thread on which we can run DnsConfigService, which requires a TYPE_IO |
| 69 // message loop to monitor /system/etc/hosts. | 69 // message loop to monitor /system/etc/hosts. |
| 70 class NetworkChangeNotifierAndroid::DnsConfigServiceThread | 70 class NetworkChangeNotifierAndroid::DnsConfigServiceThread |
| 71 : public base::Thread { | 71 : public base::Thread, |
| 72 public NetworkChangeNotifier::NetworkChangeObserver { |
| 72 public: | 73 public: |
| 73 DnsConfigServiceThread() | 74 DnsConfigServiceThread(const DnsConfig* dns_config_for_testing) |
| 74 : base::Thread("DnsConfigService"), | 75 : base::Thread("DnsConfigService"), |
| 76 dns_config_for_testing_(dns_config_for_testing), |
| 77 creation_time_(base::Time::Now()), |
| 75 address_tracker_(base::Bind(base::DoNothing), | 78 address_tracker_(base::Bind(base::DoNothing), |
| 76 base::Bind(base::DoNothing), | 79 base::Bind(base::DoNothing), |
| 77 // We're only interested in tunnel interface changes. | 80 // We're only interested in tunnel interface changes. |
| 78 base::Bind(NotifyNetworkChangeNotifierObservers)) {} | 81 base::Bind(NotifyNetworkChangeNotifierObservers)) {} |
| 79 | 82 |
| 80 ~DnsConfigServiceThread() override { Stop(); } | 83 ~DnsConfigServiceThread() override { |
| 84 NetworkChangeNotifier::RemoveNetworkChangeObserver(this); |
| 85 Stop(); |
| 86 } |
| 87 |
| 88 void InitAfterStart() { |
| 89 DCHECK(IsRunning()); |
| 90 NetworkChangeNotifier::AddNetworkChangeObserver(this); |
| 91 } |
| 81 | 92 |
| 82 void Init() override { | 93 void Init() override { |
| 83 address_tracker_.Init(); | 94 address_tracker_.Init(); |
| 84 dns_config_service_ = DnsConfigService::CreateSystemService(); | 95 dns_config_service_.reset(new internal::DnsConfigServicePosix()); |
| 96 if (dns_config_for_testing_) |
| 97 dns_config_service_->SetDnsConfigForTesting(dns_config_for_testing_); |
| 85 dns_config_service_->WatchConfig( | 98 dns_config_service_->WatchConfig( |
| 86 base::Bind(&NetworkChangeNotifier::SetDnsConfig)); | 99 base::Bind(&DnsConfigServiceThread::DnsConfigChangeCallback, |
| 100 base::Unretained(this))); |
| 87 } | 101 } |
| 88 | 102 |
| 89 void CleanUp() override { dns_config_service_.reset(); } | 103 void CleanUp() override { dns_config_service_.reset(); } |
| 90 | 104 |
| 91 static void NotifyNetworkChangeNotifierObservers() { | 105 static void NotifyNetworkChangeNotifierObservers() { |
| 92 NetworkChangeNotifier::NotifyObserversOfIPAddressChange(); | 106 NetworkChangeNotifier::NotifyObserversOfIPAddressChange(); |
| 93 NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange(); | 107 NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange(); |
| 94 } | 108 } |
| 95 | 109 |
| 96 private: | 110 private: |
| 97 scoped_ptr<DnsConfigService> dns_config_service_; | 111 void DnsConfigChangeCallback(const DnsConfig& config) { |
| 112 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 113 if (dns_config_service_->SeenChangeSince(creation_time_)) { |
| 114 NetworkChangeNotifier::SetDnsConfig(config); |
| 115 } else { |
| 116 NetworkChangeNotifier::SetInitialDnsConfig(config); |
| 117 } |
| 118 } |
| 119 |
| 120 // NetworkChangeNotifier::NetworkChangeObserver implementation: |
| 121 void OnNetworkChanged(NetworkChangeNotifier::ConnectionType type) override { |
| 122 task_runner()->PostTask( |
| 123 FROM_HERE, |
| 124 base::Bind(&internal::DnsConfigServicePosix::OnNetworkChanged, |
| 125 base::Unretained(dns_config_service_.get()), type)); |
| 126 } |
| 127 |
| 128 const DnsConfig* dns_config_for_testing_; |
| 129 const base::Time creation_time_; |
| 130 scoped_ptr<internal::DnsConfigServicePosix> dns_config_service_; |
| 98 // Used to detect tunnel state changes. | 131 // Used to detect tunnel state changes. |
| 99 internal::AddressTrackerLinux address_tracker_; | 132 internal::AddressTrackerLinux address_tracker_; |
| 100 | 133 |
| 101 DISALLOW_COPY_AND_ASSIGN(DnsConfigServiceThread); | 134 DISALLOW_COPY_AND_ASSIGN(DnsConfigServiceThread); |
| 102 }; | 135 }; |
| 103 | 136 |
| 104 NetworkChangeNotifierAndroid::~NetworkChangeNotifierAndroid() { | 137 NetworkChangeNotifierAndroid::~NetworkChangeNotifierAndroid() { |
| 105 delegate_->RemoveObserver(this); | 138 delegate_->RemoveObserver(this); |
| 106 } | 139 } |
| 107 | 140 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 123 NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChange( | 156 NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChange( |
| 124 max_bandwidth_mbps); | 157 max_bandwidth_mbps); |
| 125 } | 158 } |
| 126 | 159 |
| 127 // static | 160 // static |
| 128 bool NetworkChangeNotifierAndroid::Register(JNIEnv* env) { | 161 bool NetworkChangeNotifierAndroid::Register(JNIEnv* env) { |
| 129 return NetworkChangeNotifierDelegateAndroid::Register(env); | 162 return NetworkChangeNotifierDelegateAndroid::Register(env); |
| 130 } | 163 } |
| 131 | 164 |
| 132 NetworkChangeNotifierAndroid::NetworkChangeNotifierAndroid( | 165 NetworkChangeNotifierAndroid::NetworkChangeNotifierAndroid( |
| 133 NetworkChangeNotifierDelegateAndroid* delegate) | 166 NetworkChangeNotifierDelegateAndroid* delegate, |
| 167 const DnsConfig* dns_config_for_testing) |
| 134 : NetworkChangeNotifier(NetworkChangeCalculatorParamsAndroid()), | 168 : NetworkChangeNotifier(NetworkChangeCalculatorParamsAndroid()), |
| 135 delegate_(delegate), | 169 delegate_(delegate), |
| 136 dns_config_service_thread_(new DnsConfigServiceThread()) { | 170 dns_config_service_thread_( |
| 171 new DnsConfigServiceThread(dns_config_for_testing)) { |
| 137 delegate_->AddObserver(this); | 172 delegate_->AddObserver(this); |
| 138 dns_config_service_thread_->StartWithOptions( | 173 dns_config_service_thread_->StartWithOptions( |
| 139 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | 174 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| 175 dns_config_service_thread_->InitAfterStart(); |
| 140 } | 176 } |
| 141 | 177 |
| 142 // static | 178 // static |
| 143 NetworkChangeNotifier::NetworkChangeCalculatorParams | 179 NetworkChangeNotifier::NetworkChangeCalculatorParams |
| 144 NetworkChangeNotifierAndroid::NetworkChangeCalculatorParamsAndroid() { | 180 NetworkChangeNotifierAndroid::NetworkChangeCalculatorParamsAndroid() { |
| 145 NetworkChangeCalculatorParams params; | 181 NetworkChangeCalculatorParams params; |
| 146 // IPAddressChanged is produced immediately prior to ConnectionTypeChanged | 182 // IPAddressChanged is produced immediately prior to ConnectionTypeChanged |
| 147 // so delay IPAddressChanged so they get merged with the following | 183 // so delay IPAddressChanged so they get merged with the following |
| 148 // ConnectionTypeChanged signal. | 184 // ConnectionTypeChanged signal. |
| 149 params.ip_address_offline_delay_ = base::TimeDelta::FromSeconds(1); | 185 params.ip_address_offline_delay_ = base::TimeDelta::FromSeconds(1); |
| 150 params.ip_address_online_delay_ = base::TimeDelta::FromSeconds(1); | 186 params.ip_address_online_delay_ = base::TimeDelta::FromSeconds(1); |
| 151 params.connection_type_offline_delay_ = base::TimeDelta::FromSeconds(0); | 187 params.connection_type_offline_delay_ = base::TimeDelta::FromSeconds(0); |
| 152 params.connection_type_online_delay_ = base::TimeDelta::FromSeconds(0); | 188 params.connection_type_online_delay_ = base::TimeDelta::FromSeconds(0); |
| 153 return params; | 189 return params; |
| 154 } | 190 } |
| 155 | 191 |
| 156 } // namespace net | 192 } // namespace net |
| OLD | NEW |