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"
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
:
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(
40 DCHECK(subtype
>= 0 && subtype
<= NetworkChangeNotifier::SUBTYPE_LAST
);
42 return static_cast<NetworkChangeNotifier::ConnectionSubtype
>(subtype
);
47 jdouble
GetMaxBandwidthForConnectionSubtype(JNIEnv
* env
,
50 return NetworkChangeNotifierAndroid::GetMaxBandwidthForConnectionSubtype(
51 ConvertConnectionSubtype(subtype
));
54 NetworkChangeNotifierDelegateAndroid::NetworkChangeNotifierDelegateAndroid()
55 : observers_(new 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 double NetworkChangeNotifierDelegateAndroid::GetCurrentMaxBandwidth() const {
88 base::AutoLock
auto_lock(connection_lock_
);
89 return connection_max_bandwidth_
;
92 void NetworkChangeNotifierDelegateAndroid::NotifyConnectionTypeChanged(
95 jint new_connection_type
) {
96 DCHECK(thread_checker_
.CalledOnValidThread());
97 const ConnectionType actual_connection_type
= ConvertConnectionType(
99 SetCurrentConnectionType(actual_connection_type
);
100 observers_
->Notify(&Observer::OnConnectionTypeChanged
);
103 jint
NetworkChangeNotifierDelegateAndroid::GetConnectionType(JNIEnv
*,
105 DCHECK(thread_checker_
.CalledOnValidThread());
106 return GetCurrentConnectionType();
109 void NetworkChangeNotifierDelegateAndroid::NotifyMaxBandwidthChanged(
112 jdouble new_max_bandwidth
) {
113 DCHECK(thread_checker_
.CalledOnValidThread());
114 DCHECK(new_max_bandwidth
!= GetCurrentMaxBandwidth());
115 SetCurrentMaxBandwidth(new_max_bandwidth
);
116 observers_
->Notify(&Observer::OnMaxBandwidthChanged
, new_max_bandwidth
);
119 void NetworkChangeNotifierDelegateAndroid::AddObserver(
120 Observer
* observer
) {
121 observers_
->AddObserver(observer
);
124 void NetworkChangeNotifierDelegateAndroid::RemoveObserver(
125 Observer
* observer
) {
126 observers_
->RemoveObserver(observer
);
130 bool NetworkChangeNotifierDelegateAndroid::Register(JNIEnv
* env
) {
131 return RegisterNativesImpl(env
);
134 void NetworkChangeNotifierDelegateAndroid::SetCurrentConnectionType(
135 ConnectionType new_connection_type
) {
136 base::AutoLock
auto_lock(connection_lock_
);
137 connection_type_
= new_connection_type
;
140 void NetworkChangeNotifierDelegateAndroid::SetCurrentMaxBandwidth(
141 double max_bandwidth
) {
142 base::AutoLock
auto_lock(connection_lock_
);
143 connection_max_bandwidth_
= max_bandwidth
;
146 void NetworkChangeNotifierDelegateAndroid::SetOnline() {
147 JNIEnv
* env
= base::android::AttachCurrentThread();
148 Java_NetworkChangeNotifier_forceConnectivityState(env
, true);
151 void NetworkChangeNotifierDelegateAndroid::SetOffline() {
152 JNIEnv
* env
= base::android::AttachCurrentThread();
153 Java_NetworkChangeNotifier_forceConnectivityState(env
, false);