Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / net / android / network_change_notifier_delegate_android.cc
blobbe4c6b18e1112853221016ba5a97d90591de13d6
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
3 // found in the LICENSE file.
5 #include "net/android/network_change_notifier_delegate_android.h"
7 #include "base/logging.h"
8 #include "jni/NetworkChangeNotifier_jni.h"
9 #include "net/android/network_change_notifier_android.h"
11 namespace net {
13 namespace {
15 // Converts a Java side connection type (integer) to
16 // the native side NetworkChangeNotifier::ConnectionType.
17 NetworkChangeNotifier::ConnectionType ConvertConnectionType(
18 jint connection_type) {
19 switch (connection_type) {
20 case NetworkChangeNotifier::CONNECTION_UNKNOWN:
21 case NetworkChangeNotifier::CONNECTION_ETHERNET:
22 case NetworkChangeNotifier::CONNECTION_WIFI:
23 case NetworkChangeNotifier::CONNECTION_2G:
24 case NetworkChangeNotifier::CONNECTION_3G:
25 case NetworkChangeNotifier::CONNECTION_4G:
26 case NetworkChangeNotifier::CONNECTION_NONE:
27 case NetworkChangeNotifier::CONNECTION_BLUETOOTH:
28 break;
29 default:
30 NOTREACHED() << "Unknown connection type received: " << connection_type;
31 return NetworkChangeNotifier::CONNECTION_UNKNOWN;
33 return static_cast<NetworkChangeNotifier::ConnectionType>(connection_type);
36 // Converts a Java side connection type (integer) to
37 // the native side NetworkChangeNotifier::ConnectionType.
38 NetworkChangeNotifier::ConnectionSubtype ConvertConnectionSubtype(
39 jint subtype) {
40 DCHECK(subtype >= 0 && subtype <= NetworkChangeNotifier::SUBTYPE_LAST);
42 return static_cast<NetworkChangeNotifier::ConnectionSubtype>(subtype);
45 } // namespace
47 jdouble GetMaxBandwidthForConnectionSubtype(JNIEnv* env,
48 const JavaParamRef<jclass>& caller,
49 jint subtype) {
50 return NetworkChangeNotifierAndroid::GetMaxBandwidthForConnectionSubtype(
51 ConvertConnectionSubtype(subtype));
54 NetworkChangeNotifierDelegateAndroid::NetworkChangeNotifierDelegateAndroid()
55 : observers_(new base::ObserverListThreadSafe<Observer>()) {
56 JNIEnv* env = base::android::AttachCurrentThread();
57 java_network_change_notifier_.Reset(
58 Java_NetworkChangeNotifier_init(
59 env, base::android::GetApplicationContext()));
60 Java_NetworkChangeNotifier_addNativeObserver(
61 env, java_network_change_notifier_.obj(),
62 reinterpret_cast<intptr_t>(this));
63 SetCurrentConnectionType(
64 ConvertConnectionType(
65 Java_NetworkChangeNotifier_getCurrentConnectionType(
66 env, java_network_change_notifier_.obj())));
67 SetCurrentMaxBandwidth(
68 Java_NetworkChangeNotifier_getCurrentMaxBandwidthInMbps(
69 env, java_network_change_notifier_.obj()));
72 NetworkChangeNotifierDelegateAndroid::~NetworkChangeNotifierDelegateAndroid() {
73 DCHECK(thread_checker_.CalledOnValidThread());
74 observers_->AssertEmpty();
75 JNIEnv* env = base::android::AttachCurrentThread();
76 Java_NetworkChangeNotifier_removeNativeObserver(
77 env, java_network_change_notifier_.obj(),
78 reinterpret_cast<intptr_t>(this));
81 NetworkChangeNotifier::ConnectionType
82 NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType() const {
83 base::AutoLock auto_lock(connection_lock_);
84 return connection_type_;
87 void NetworkChangeNotifierDelegateAndroid::
88 GetCurrentMaxBandwidthAndConnectionType(
89 double* max_bandwidth_mbps,
90 ConnectionType* connection_type) const {
91 base::AutoLock auto_lock(connection_lock_);
92 *connection_type = connection_type_;
93 *max_bandwidth_mbps = connection_max_bandwidth_;
96 void NetworkChangeNotifierDelegateAndroid::NotifyConnectionTypeChanged(
97 JNIEnv* env,
98 jobject obj,
99 jint new_connection_type) {
100 DCHECK(thread_checker_.CalledOnValidThread());
101 const ConnectionType actual_connection_type = ConvertConnectionType(
102 new_connection_type);
103 SetCurrentConnectionType(actual_connection_type);
104 observers_->Notify(FROM_HERE, &Observer::OnConnectionTypeChanged);
107 jint NetworkChangeNotifierDelegateAndroid::GetConnectionType(JNIEnv*,
108 jobject) const {
109 DCHECK(thread_checker_.CalledOnValidThread());
110 return GetCurrentConnectionType();
113 void NetworkChangeNotifierDelegateAndroid::NotifyMaxBandwidthChanged(
114 JNIEnv* env,
115 jobject obj,
116 jdouble new_max_bandwidth) {
117 DCHECK(thread_checker_.CalledOnValidThread());
119 SetCurrentMaxBandwidth(new_max_bandwidth);
120 observers_->Notify(FROM_HERE, &Observer::OnMaxBandwidthChanged,
121 new_max_bandwidth, GetCurrentConnectionType());
124 void NetworkChangeNotifierDelegateAndroid::AddObserver(
125 Observer* observer) {
126 observers_->AddObserver(observer);
129 void NetworkChangeNotifierDelegateAndroid::RemoveObserver(
130 Observer* observer) {
131 observers_->RemoveObserver(observer);
134 // static
135 bool NetworkChangeNotifierDelegateAndroid::Register(JNIEnv* env) {
136 return RegisterNativesImpl(env);
139 void NetworkChangeNotifierDelegateAndroid::SetCurrentConnectionType(
140 ConnectionType new_connection_type) {
141 base::AutoLock auto_lock(connection_lock_);
142 connection_type_ = new_connection_type;
145 void NetworkChangeNotifierDelegateAndroid::SetCurrentMaxBandwidth(
146 double max_bandwidth) {
147 base::AutoLock auto_lock(connection_lock_);
148 connection_max_bandwidth_ = max_bandwidth;
151 void NetworkChangeNotifierDelegateAndroid::SetOnline() {
152 JNIEnv* env = base::android::AttachCurrentThread();
153 Java_NetworkChangeNotifier_forceConnectivityState(env, true);
156 void NetworkChangeNotifierDelegateAndroid::SetOffline() {
157 JNIEnv* env = base::android::AttachCurrentThread();
158 Java_NetworkChangeNotifier_forceConnectivityState(env, false);
161 } // namespace net