1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
////////////////////////////////////////////////////////////////////////////////
// Threading considerations:
//
// This class is designed to meet various threading guarantees starting from the
// ones imposed by NetworkChangeNotifier:
// - The notifier can be constructed on any thread.
// - GetCurrentConnectionType() can be called on any thread.
//
// The fact that this implementation of NetworkChangeNotifier is backed by a
// Java side singleton class (see NetworkChangeNotifier.java) adds another
// threading constraint:
// - The calls to the Java side (stateful) object must be performed from a
// single thread. This object happens to be a singleton which is used on the
// application side on the main thread. Therefore all the method calls from
// the native NetworkChangeNotifierAndroid class to its Java counterpart are
// performed on the main thread.
//
// This leads to a design involving the following native classes:
// 1) NetworkChangeNotifierFactoryAndroid ('factory')
// 2) NetworkChangeNotifierDelegateAndroid ('delegate')
// 3) NetworkChangeNotifierAndroid ('notifier')
//
// The factory constructs and owns the delegate. The factory is constructed and
// destroyed on the main thread which makes it construct and destroy the
// delegate on the main thread too. This guarantees that the calls to the Java
// side are performed on the main thread.
// Note that after the factory's construction, the factory's creation method can
// be called from any thread since the delegate's construction (performing the
// JNI calls) already happened on the main thread (when the factory was
// constructed).
//
////////////////////////////////////////////////////////////////////////////////
// Propagation of network change notifications:
//
// When the factory is requested to create a new instance of the notifier, the
// factory passes the delegate to the notifier (without transferring ownership).
// Note that there is a one-to-one mapping between the factory and the
// delegate as explained above. But the factory naturally creates multiple
// instances of the notifier. That means that there is a one-to-many mapping
// between delegate and notifier (i.e. a single delegate can be shared by
// multiple notifiers).
// At construction the notifier (which is also an observer) subscribes to
// notifications fired by the delegate. These notifications, received by the
// delegate (and forwarded to the notifier(s)), are sent by the Java side
// notifier (see NetworkChangeNotifier.java) and are initiated by the Android
// platform.
// Notifications from the Java side always arrive on the main thread. The
// delegate then forwards these notifications to the threads of each observer
// (network change notifier). The network change notifier then processes the
// state change, and notifies each of its observers on their threads.
//
// This can also be seen as:
// Android platform -> NetworkChangeNotifier (Java) ->
// NetworkChangeNotifierDelegateAndroid -> NetworkChangeNotifierAndroid.
#include "net/android/network_change_notifier_android.h"
#include <string>
#include <unordered_set>
#include "base/android/android_info.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/metrics/histogram_macros.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/threading/thread.h"
#include "net/base/address_tracker_linux.h"
namespace net {
// Expose handles::kInvalidNetworkHandle out to Java as NetId.INVALID. The
// notion of a NetID is an Android framework one, see android.net.Network.netId.
// NetworkChangeNotifierAndroid implements handles::NetworkHandle to simply be
// the NetID.
// GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net
enum NetId {
// Cannot use |handles::kInvalidNetworkHandle| here as the Java generator
// fails, instead enforce their equality with CHECK in
// NetworkChangeNotifierAndroid().
INVALID = -1
};
// Thread on which we can run DnsConfigService, which requires a TYPE_IO
// message loop to monitor /system/etc/hosts.
class NetworkChangeNotifierAndroid::BlockingThreadObjects {
public:
BlockingThreadObjects()
: address_tracker_(
base::DoNothing(),
base::DoNothing(),
// We're only interested in tunnel interface changes.
base::BindRepeating(NotifyNetworkChangeNotifierObservers),
std::unordered_set<std::string>()) {}
BlockingThreadObjects(const BlockingThreadObjects&) = delete;
BlockingThreadObjects& operator=(const BlockingThreadObjects&) = delete;
void Init() {
address_tracker_.Init();
}
static void NotifyNetworkChangeNotifierObservers() {
NetworkChangeNotifier::NotifyObserversOfIPAddressChange();
NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange();
}
private:
// Used to detect tunnel state changes.
internal::AddressTrackerLinux address_tracker_;
};
NetworkChangeNotifierAndroid::~NetworkChangeNotifierAndroid() {
ClearGlobalPointer();
delegate_->UnregisterObserver(this);
}
NetworkChangeNotifier::ConnectionType
NetworkChangeNotifierAndroid::GetCurrentConnectionType() const {
return delegate_->GetCurrentConnectionType();
}
NetworkChangeNotifier::ConnectionCost
NetworkChangeNotifierAndroid::GetCurrentConnectionCost() {
return delegate_->GetCurrentConnectionCost();
}
NetworkChangeNotifier::ConnectionSubtype
NetworkChangeNotifierAndroid::GetCurrentConnectionSubtype() const {
return delegate_->GetCurrentConnectionSubtype();
}
void NetworkChangeNotifierAndroid::GetCurrentMaxBandwidthAndConnectionType(
double* max_bandwidth_mbps,
ConnectionType* connection_type) const {
delegate_->GetCurrentMaxBandwidthAndConnectionType(max_bandwidth_mbps,
connection_type);
}
void NetworkChangeNotifierAndroid::ForceNetworkHandlesSupportedForTesting() {
force_network_handles_supported_for_testing_ = true;
}
bool NetworkChangeNotifierAndroid::AreNetworkHandlesCurrentlySupported() const {
// Notifications for API using handles::NetworkHandles and querying using
// handles::NetworkHandles only implemented for Android versions >= L.
return force_network_handles_supported_for_testing_ ||
!delegate_->RegisterNetworkCallbackFailed();
}
void NetworkChangeNotifierAndroid::GetCurrentConnectedNetworks(
NetworkChangeNotifier::NetworkList* networks) const {
delegate_->GetCurrentlyConnectedNetworks(networks);
}
NetworkChangeNotifier::ConnectionType
NetworkChangeNotifierAndroid::GetCurrentNetworkConnectionType(
handles::NetworkHandle network) const {
return delegate_->GetNetworkConnectionType(network);
}
handles::NetworkHandle NetworkChangeNotifierAndroid::GetCurrentDefaultNetwork()
const {
return delegate_->GetCurrentDefaultNetwork();
}
void NetworkChangeNotifierAndroid::OnConnectionTypeChanged() {
BlockingThreadObjects::NotifyNetworkChangeNotifierObservers();
}
void NetworkChangeNotifierAndroid::OnConnectionCostChanged() {
NetworkChangeNotifier::NotifyObserversOfConnectionCostChange();
}
void NetworkChangeNotifierAndroid::OnMaxBandwidthChanged(
double max_bandwidth_mbps,
ConnectionType type) {
NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChange(max_bandwidth_mbps,
type);
}
void NetworkChangeNotifierAndroid::OnNetworkConnected(
handles::NetworkHandle network) {
NetworkChangeNotifier::NotifyObserversOfSpecificNetworkChange(
NetworkChangeType::kConnected, network);
}
void NetworkChangeNotifierAndroid::OnNetworkSoonToDisconnect(
handles::NetworkHandle network) {
NetworkChangeNotifier::NotifyObserversOfSpecificNetworkChange(
NetworkChangeType::kSoonToDisconnect, network);
}
void NetworkChangeNotifierAndroid::OnNetworkDisconnected(
handles::NetworkHandle network) {
NetworkChangeNotifier::NotifyObserversOfSpecificNetworkChange(
NetworkChangeType::kDisconnected, network);
}
void NetworkChangeNotifierAndroid::OnNetworkMadeDefault(
handles::NetworkHandle network) {
NetworkChangeNotifier::NotifyObserversOfSpecificNetworkChange(
NetworkChangeType::kMadeDefault, network);
}
void NetworkChangeNotifierAndroid::OnDefaultNetworkActive() {
NetworkChangeNotifier::NotifyObserversOfDefaultNetworkActive();
}
NetworkChangeNotifierAndroid::NetworkChangeNotifierAndroid(
NetworkChangeNotifierDelegateAndroid* delegate)
: NetworkChangeNotifier(NetworkChangeCalculatorParamsAndroid()),
delegate_(delegate),
blocking_thread_objects_(nullptr, base::OnTaskRunnerDeleter(nullptr)) {
static_assert(NetId::INVALID == handles::kInvalidNetworkHandle,
"handles::kInvalidNetworkHandle doesn't match NetId::INVALID");
delegate_->RegisterObserver(this);
// Since Android P, ConnectivityManager's signals include VPNs so we don't
// need to use AddressTrackerLinux.
if (base::android::android_info::sdk_int() <
base::android::android_info::SDK_VERSION_P) {
// |blocking_thread_objects_| will live on this runner.
scoped_refptr<base::SequencedTaskRunner> blocking_thread_runner =
base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()});
blocking_thread_objects_ =
std::unique_ptr<BlockingThreadObjects, base::OnTaskRunnerDeleter>(
new BlockingThreadObjects(),
// Ensure |blocking_thread_objects_| lives on
// |blocking_thread_runner| to prevent races where
// NetworkChangeNotifierAndroid outlives
// TaskEnvironment. https://crbug.com/938126
base::OnTaskRunnerDeleter(blocking_thread_runner));
blocking_thread_runner->PostTask(
FROM_HERE,
base::BindOnce(&BlockingThreadObjects::Init,
// The Unretained pointer is safe here because it's
// posted before the deleter can post.
base::Unretained(blocking_thread_objects_.get())));
}
}
// static
NetworkChangeNotifier::NetworkChangeCalculatorParams
NetworkChangeNotifierAndroid::NetworkChangeCalculatorParamsAndroid() {
NetworkChangeCalculatorParams params;
// IPAddressChanged is produced immediately prior to ConnectionTypeChanged
// so delay IPAddressChanged so they get merged with the following
// ConnectionTypeChanged signal.
params.ip_address_offline_delay_ = base::Seconds(1);
params.ip_address_online_delay_ = base::Seconds(1);
params.connection_type_offline_delay_ = base::Seconds(0);
params.connection_type_online_delay_ = base::Seconds(0);
return params;
}
bool NetworkChangeNotifierAndroid::IsDefaultNetworkActiveInternal() {
return delegate_->IsDefaultNetworkActive();
}
void NetworkChangeNotifierAndroid::DefaultNetworkActiveObserverAdded() {
delegate_->DefaultNetworkActiveObserverAdded();
}
void NetworkChangeNotifierAndroid::DefaultNetworkActiveObserverRemoved() {
delegate_->DefaultNetworkActiveObserverRemoved();
}
} // namespace net
|