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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
|
// 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.
#include "net/android/network_change_notifier_delegate_android.h"
#include "base/android/build_info.h"
#include "base/android/jni_array.h"
#include "base/check.h"
#include "base/notreached.h"
#include "net/android/network_change_notifier_android.h"
#include "net/base/features.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "net/net_jni_headers/NetworkActiveNotifier_jni.h"
#include "net/net_jni_headers/NetworkChangeNotifier_jni.h"
using base::android::JavaParamRef;
using base::android::JavaRef;
using base::android::ScopedJavaLocalRef;
namespace net {
namespace {
// Converts a Java side connection type (integer) to
// the native side NetworkChangeNotifier::ConnectionType.
NetworkChangeNotifier::ConnectionType ConvertConnectionType(
jint connection_type) {
switch (connection_type) {
case NetworkChangeNotifier::CONNECTION_UNKNOWN:
case NetworkChangeNotifier::CONNECTION_ETHERNET:
case NetworkChangeNotifier::CONNECTION_WIFI:
case NetworkChangeNotifier::CONNECTION_2G:
case NetworkChangeNotifier::CONNECTION_3G:
case NetworkChangeNotifier::CONNECTION_4G:
case NetworkChangeNotifier::CONNECTION_5G:
case NetworkChangeNotifier::CONNECTION_NONE:
case NetworkChangeNotifier::CONNECTION_BLUETOOTH:
break;
default:
NOTREACHED() << "Unknown connection type received: " << connection_type;
}
return static_cast<NetworkChangeNotifier::ConnectionType>(connection_type);
}
// Converts a Java side connection cost (integer) to
// the native side NetworkChangeNotifier::ConnectionCost.
NetworkChangeNotifier::ConnectionCost ConvertConnectionCost(
jint connection_cost) {
switch (connection_cost) {
case NetworkChangeNotifier::CONNECTION_COST_UNKNOWN:
case NetworkChangeNotifier::CONNECTION_COST_UNMETERED:
case NetworkChangeNotifier::CONNECTION_COST_METERED:
break;
default:
NOTREACHED() << "Unknown connection cost received: " << connection_cost;
}
return static_cast<NetworkChangeNotifier::ConnectionCost>(connection_cost);
}
// Converts a Java side connection type (integer) to
// the native side NetworkChangeNotifier::ConnectionType.
NetworkChangeNotifier::ConnectionSubtype ConvertConnectionSubtype(
jint subtype) {
DCHECK(subtype >= 0 && subtype <= NetworkChangeNotifier::SUBTYPE_LAST);
return static_cast<NetworkChangeNotifier::ConnectionSubtype>(subtype);
}
} // namespace
// static
void NetworkChangeNotifierDelegateAndroid::JavaLongArrayToNetworkMap(
JNIEnv* env,
const JavaRef<jlongArray>& long_array,
NetworkMap* network_map) {
std::vector<int64_t> int64_list;
base::android::JavaLongArrayToInt64Vector(env, long_array, &int64_list);
network_map->clear();
for (auto i = int64_list.begin(); i != int64_list.end(); ++i) {
handles::NetworkHandle network_handle = *i;
CHECK(++i != int64_list.end());
(*network_map)[network_handle] = static_cast<ConnectionType>(*i);
}
}
NetworkChangeNotifierDelegateAndroid::NetworkChangeNotifierDelegateAndroid()
: NetworkChangeNotifierDelegateAndroid(ForceUpdateNetworkState::kEnabled) {}
NetworkChangeNotifierDelegateAndroid::NetworkChangeNotifierDelegateAndroid(
net::NetworkChangeNotifierDelegateAndroid::ForceUpdateNetworkState
force_update_network_state)
: java_network_change_notifier_(Java_NetworkChangeNotifier_init(
base::android::AttachCurrentThread())),
register_network_callback_failed_(
Java_NetworkChangeNotifier_registerNetworkCallbackFailed(
base::android::AttachCurrentThread(),
java_network_change_notifier_)) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_NetworkChangeNotifier_addNativeObserver(
env, java_network_change_notifier_, reinterpret_cast<intptr_t>(this));
SetCurrentConnectionType(
ConvertConnectionType(Java_NetworkChangeNotifier_getCurrentConnectionType(
env, java_network_change_notifier_)));
SetCurrentConnectionCost(
ConvertConnectionCost(Java_NetworkChangeNotifier_getCurrentConnectionCost(
env, java_network_change_notifier_)));
auto connection_subtype = ConvertConnectionSubtype(
Java_NetworkChangeNotifier_getCurrentConnectionSubtype(
env, java_network_change_notifier_,
force_update_network_state == ForceUpdateNetworkState::kEnabled));
SetCurrentConnectionSubtype(connection_subtype);
SetCurrentMaxBandwidth(
NetworkChangeNotifierAndroid::GetMaxBandwidthMbpsForConnectionSubtype(
connection_subtype));
SetCurrentDefaultNetwork(Java_NetworkChangeNotifier_getCurrentDefaultNetId(
env, java_network_change_notifier_));
NetworkMap network_map;
ScopedJavaLocalRef<jlongArray> networks_and_types =
Java_NetworkChangeNotifier_getCurrentNetworksAndTypes(
env, java_network_change_notifier_);
JavaLongArrayToNetworkMap(env, networks_and_types, &network_map);
SetCurrentNetworksAndTypes(network_map);
java_network_active_notifier_ = Java_NetworkActiveNotifier_build(
base::android::AttachCurrentThread(), reinterpret_cast<intptr_t>(this));
}
NetworkChangeNotifierDelegateAndroid::~NetworkChangeNotifierDelegateAndroid() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DCHECK_EQ(default_network_active_observers_, 0);
{
base::AutoLock auto_lock(observer_lock_);
DCHECK(!observer_);
}
JNIEnv* env = base::android::AttachCurrentThread();
Java_NetworkChangeNotifier_removeNativeObserver(
env, java_network_change_notifier_, reinterpret_cast<intptr_t>(this));
}
NetworkChangeNotifier::ConnectionType
NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType() const {
base::AutoLock auto_lock(connection_lock_);
return connection_type_;
}
NetworkChangeNotifier::ConnectionCost
NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionCost() {
base::AutoLock auto_lock(connection_lock_);
return connection_cost_;
}
NetworkChangeNotifier::ConnectionSubtype
NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionSubtype() const {
base::AutoLock auto_lock(connection_lock_);
return connection_subtype_;
}
void NetworkChangeNotifierDelegateAndroid::
GetCurrentMaxBandwidthAndConnectionType(
double* max_bandwidth_mbps,
ConnectionType* connection_type) const {
base::AutoLock auto_lock(connection_lock_);
*connection_type = connection_type_;
*max_bandwidth_mbps = connection_max_bandwidth_;
}
NetworkChangeNotifier::ConnectionType
NetworkChangeNotifierDelegateAndroid::GetNetworkConnectionType(
handles::NetworkHandle network) const {
base::AutoLock auto_lock(connection_lock_);
auto network_entry = network_map_.find(network);
if (network_entry == network_map_.end())
return ConnectionType::CONNECTION_UNKNOWN;
return network_entry->second;
}
handles::NetworkHandle
NetworkChangeNotifierDelegateAndroid::GetCurrentDefaultNetwork() const {
base::AutoLock auto_lock(connection_lock_);
return default_network_;
}
void NetworkChangeNotifierDelegateAndroid::GetCurrentlyConnectedNetworks(
NetworkList* network_list) const {
network_list->clear();
base::AutoLock auto_lock(connection_lock_);
for (auto i : network_map_)
network_list->push_back(i.first);
}
bool NetworkChangeNotifierDelegateAndroid::IsDefaultNetworkActive() {
JNIEnv* env = base::android::AttachCurrentThread();
return Java_NetworkActiveNotifier_isDefaultNetworkActive(
env, java_network_active_notifier_);
}
void NetworkChangeNotifierDelegateAndroid::NotifyConnectionCostChanged(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
jint new_connection_cost) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
const ConnectionCost actual_connection_cost =
ConvertConnectionCost(new_connection_cost);
SetCurrentConnectionCost(actual_connection_cost);
base::AutoLock auto_lock(observer_lock_);
if (observer_)
observer_->OnConnectionCostChanged();
}
void NetworkChangeNotifierDelegateAndroid::NotifyConnectionTypeChanged(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
jint new_connection_type,
jlong default_netid) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
const ConnectionType actual_connection_type = ConvertConnectionType(
new_connection_type);
SetCurrentConnectionType(actual_connection_type);
handles::NetworkHandle default_network = default_netid;
if (default_network != GetCurrentDefaultNetwork()) {
SetCurrentDefaultNetwork(default_network);
bool default_exists;
{
base::AutoLock auto_lock(connection_lock_);
// |default_network| may be an invalid value (i.e. -1) in cases where
// the device is disconnected or when run on Android versions prior to L,
// in which case |default_exists| will correctly be false and no
// OnNetworkMadeDefault notification will be sent.
default_exists = network_map_.find(default_network) != network_map_.end();
}
// Android Lollipop had race conditions where CONNECTIVITY_ACTION intents
// were sent out before the network was actually made the default.
// Delay sending the OnNetworkMadeDefault notification until we are
// actually notified that the network connected in NotifyOfNetworkConnect.
if (default_exists) {
base::AutoLock auto_lock(observer_lock_);
if (observer_)
observer_->OnNetworkMadeDefault(default_network);
}
}
base::AutoLock auto_lock(observer_lock_);
if (observer_)
observer_->OnConnectionTypeChanged();
}
jint NetworkChangeNotifierDelegateAndroid::GetConnectionType(JNIEnv*,
jobject) const {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
return GetCurrentConnectionType();
}
jint NetworkChangeNotifierDelegateAndroid::GetConnectionCost(JNIEnv*, jobject) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
return GetCurrentConnectionCost();
}
void NetworkChangeNotifierDelegateAndroid::NotifyConnectionSubtypeChanged(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
jint subtype) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
double new_max_bandwidth =
NetworkChangeNotifierAndroid::GetMaxBandwidthMbpsForConnectionSubtype(
ConvertConnectionSubtype(subtype));
SetCurrentConnectionSubtype(ConvertConnectionSubtype(subtype));
SetCurrentMaxBandwidth(new_max_bandwidth);
const ConnectionType connection_type = GetCurrentConnectionType();
base::AutoLock auto_lock(observer_lock_);
if (observer_) {
observer_->OnMaxBandwidthChanged(new_max_bandwidth, connection_type);
}
}
void NetworkChangeNotifierDelegateAndroid::NotifyOfNetworkConnect(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
jlong net_id,
jint connection_type) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
handles::NetworkHandle network = net_id;
bool already_exists;
bool is_default_network;
{
base::AutoLock auto_lock(connection_lock_);
already_exists = network_map_.find(network) != network_map_.end();
network_map_[network] = static_cast<ConnectionType>(connection_type);
is_default_network = (network == default_network_);
}
// Android Lollipop would send many duplicate notifications.
// This was later fixed in Android Marshmallow.
// Deduplicate them here by avoiding sending duplicate notifications.
if (!already_exists) {
base::AutoLock auto_lock(observer_lock_);
if (observer_) {
observer_->OnNetworkConnected(network);
if (is_default_network)
observer_->OnNetworkMadeDefault(network);
}
}
}
void NetworkChangeNotifierDelegateAndroid::NotifyOfNetworkSoonToDisconnect(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
jlong net_id) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
handles::NetworkHandle network = net_id;
{
base::AutoLock auto_lock(connection_lock_);
if (network_map_.find(network) == network_map_.end())
return;
}
base::AutoLock auto_lock(observer_lock_);
if (observer_)
observer_->OnNetworkSoonToDisconnect(network);
}
void NetworkChangeNotifierDelegateAndroid::NotifyOfNetworkDisconnect(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
jlong net_id) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
handles::NetworkHandle network = net_id;
{
base::AutoLock auto_lock(connection_lock_);
if (network == default_network_)
default_network_ = handles::kInvalidNetworkHandle;
if (network_map_.erase(network) == 0)
return;
}
base::AutoLock auto_lock(observer_lock_);
if (observer_)
observer_->OnNetworkDisconnected(network);
}
void NetworkChangeNotifierDelegateAndroid::NotifyPurgeActiveNetworkList(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
const JavaParamRef<jlongArray>& active_networks) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
NetworkList active_network_list;
base::android::JavaLongArrayToInt64Vector(env, active_networks,
&active_network_list);
NetworkList disconnected_networks;
{
base::AutoLock auto_lock(connection_lock_);
for (auto i : network_map_) {
bool found = false;
for (auto j : active_network_list) {
if (j == i.first) {
found = true;
break;
}
}
if (!found) {
disconnected_networks.push_back(i.first);
}
}
}
for (auto disconnected_network : disconnected_networks)
NotifyOfNetworkDisconnect(env, obj, disconnected_network);
}
void NetworkChangeNotifierDelegateAndroid::NotifyOfDefaultNetworkActive(
JNIEnv* env) {
base::AutoLock auto_lock(observer_lock_);
if (observer_)
observer_->OnDefaultNetworkActive();
}
void NetworkChangeNotifierDelegateAndroid::RegisterObserver(
Observer* observer) {
base::AutoLock auto_lock(observer_lock_);
DCHECK(!observer_);
observer_ = observer;
}
void NetworkChangeNotifierDelegateAndroid::UnregisterObserver(
Observer* observer) {
base::AutoLock auto_lock(observer_lock_);
DCHECK_EQ(observer_, observer);
observer_ = nullptr;
}
void NetworkChangeNotifierDelegateAndroid::DefaultNetworkActiveObserverAdded() {
if (default_network_active_observers_.fetch_add(1) == 0)
EnableDefaultNetworkActiveNotifications();
}
void NetworkChangeNotifierDelegateAndroid::
DefaultNetworkActiveObserverRemoved() {
if (default_network_active_observers_.fetch_sub(1) == 1)
DisableDefaultNetworkActiveNotifications();
}
void NetworkChangeNotifierDelegateAndroid::
EnableDefaultNetworkActiveNotifications() {
JNIEnv* env = base::android::AttachCurrentThread();
Java_NetworkActiveNotifier_enableNotifications(env,
java_network_active_notifier_);
}
void NetworkChangeNotifierDelegateAndroid::
DisableDefaultNetworkActiveNotifications() {
JNIEnv* env = base::android::AttachCurrentThread();
Java_NetworkActiveNotifier_disableNotifications(
env, java_network_active_notifier_);
}
void NetworkChangeNotifierDelegateAndroid::SetCurrentConnectionType(
ConnectionType new_connection_type) {
base::AutoLock auto_lock(connection_lock_);
connection_type_ = new_connection_type;
}
void NetworkChangeNotifierDelegateAndroid::SetCurrentConnectionSubtype(
ConnectionSubtype new_connection_subtype) {
base::AutoLock auto_lock(connection_lock_);
connection_subtype_ = new_connection_subtype;
}
void NetworkChangeNotifierDelegateAndroid::SetCurrentConnectionCost(
ConnectionCost new_connection_cost) {
base::AutoLock auto_lock(connection_lock_);
connection_cost_ = new_connection_cost;
}
void NetworkChangeNotifierDelegateAndroid::SetCurrentMaxBandwidth(
double max_bandwidth) {
base::AutoLock auto_lock(connection_lock_);
connection_max_bandwidth_ = max_bandwidth;
}
void NetworkChangeNotifierDelegateAndroid::SetCurrentDefaultNetwork(
handles::NetworkHandle default_network) {
base::AutoLock auto_lock(connection_lock_);
default_network_ = default_network;
}
void NetworkChangeNotifierDelegateAndroid::SetCurrentNetworksAndTypes(
NetworkMap network_map) {
base::AutoLock auto_lock(connection_lock_);
network_map_ = network_map;
}
void NetworkChangeNotifierDelegateAndroid::SetOnline() {
JNIEnv* env = base::android::AttachCurrentThread();
Java_NetworkChangeNotifier_forceConnectivityState(env, true);
}
void NetworkChangeNotifierDelegateAndroid::SetOffline() {
JNIEnv* env = base::android::AttachCurrentThread();
Java_NetworkChangeNotifier_forceConnectivityState(env, false);
}
void NetworkChangeNotifierDelegateAndroid::FakeNetworkConnected(
handles::NetworkHandle network,
ConnectionType type) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_NetworkChangeNotifier_fakeNetworkConnected(env, network, type);
}
void NetworkChangeNotifierDelegateAndroid::FakeNetworkSoonToBeDisconnected(
handles::NetworkHandle network) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_NetworkChangeNotifier_fakeNetworkSoonToBeDisconnected(env, network);
}
void NetworkChangeNotifierDelegateAndroid::FakeNetworkDisconnected(
handles::NetworkHandle network) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_NetworkChangeNotifier_fakeNetworkDisconnected(env, network);
}
void NetworkChangeNotifierDelegateAndroid::FakePurgeActiveNetworkList(
NetworkChangeNotifier::NetworkList networks) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_NetworkChangeNotifier_fakePurgeActiveNetworkList(
env, base::android::ToJavaLongArray(env, networks));
}
void NetworkChangeNotifierDelegateAndroid::FakeDefaultNetwork(
handles::NetworkHandle network,
ConnectionType type) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_NetworkChangeNotifier_fakeDefaultNetwork(env, network, type);
}
void NetworkChangeNotifierDelegateAndroid::FakeConnectionCostChanged(
ConnectionCost cost) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_NetworkChangeNotifier_fakeConnectionCostChanged(env, cost);
}
void NetworkChangeNotifierDelegateAndroid::FakeConnectionSubtypeChanged(
ConnectionSubtype subtype) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_NetworkChangeNotifier_fakeConnectionSubtypeChanged(env, subtype);
}
void NetworkChangeNotifierDelegateAndroid::FakeDefaultNetworkActive() {
JNIEnv* env = base::android::AttachCurrentThread();
Java_NetworkActiveNotifier_fakeDefaultNetworkActive(
env, java_network_active_notifier_);
}
void NetworkChangeNotifierDelegateAndroid::
EnableNetworkChangeNotifierAutoDetectForTest() {
JNIEnv* env = base::android::AttachCurrentThread();
Java_NetworkChangeNotifier_setAutoDetectConnectivityState(env, true);
}
} // namespace net
|