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"
14 // Converts a Java side connection type (integer) to
15 // the native side NetworkChangeNotifier::ConnectionType.
16 NetworkChangeNotifier::ConnectionType
ConvertConnectionType(
17 jint connection_type
) {
18 switch (connection_type
) {
19 case NetworkChangeNotifier::CONNECTION_UNKNOWN
:
20 case NetworkChangeNotifier::CONNECTION_ETHERNET
:
21 case NetworkChangeNotifier::CONNECTION_WIFI
:
22 case NetworkChangeNotifier::CONNECTION_2G
:
23 case NetworkChangeNotifier::CONNECTION_3G
:
24 case NetworkChangeNotifier::CONNECTION_4G
:
25 case NetworkChangeNotifier::CONNECTION_NONE
:
26 case NetworkChangeNotifier::CONNECTION_BLUETOOTH
:
29 NOTREACHED() << "Unknown connection type received: " << connection_type
;
30 return NetworkChangeNotifier::CONNECTION_UNKNOWN
;
32 return static_cast<NetworkChangeNotifier::ConnectionType
>(connection_type
);
37 NetworkChangeNotifierDelegateAndroid::NetworkChangeNotifierDelegateAndroid()
38 : observers_(new ObserverListThreadSafe
<Observer
>()) {
39 JNIEnv
* env
= base::android::AttachCurrentThread();
40 java_network_change_notifier_
.Reset(
41 Java_NetworkChangeNotifier_init(
42 env
, base::android::GetApplicationContext()));
43 Java_NetworkChangeNotifier_addNativeObserver(
44 env
, java_network_change_notifier_
.obj(),
45 reinterpret_cast<intptr_t>(this));
46 SetCurrentConnectionType(
47 ConvertConnectionType(
48 Java_NetworkChangeNotifier_getCurrentConnectionType(
49 env
, java_network_change_notifier_
.obj())));
52 NetworkChangeNotifierDelegateAndroid::~NetworkChangeNotifierDelegateAndroid() {
53 DCHECK(thread_checker_
.CalledOnValidThread());
54 observers_
->AssertEmpty();
55 JNIEnv
* env
= base::android::AttachCurrentThread();
56 Java_NetworkChangeNotifier_removeNativeObserver(
57 env
, java_network_change_notifier_
.obj(),
58 reinterpret_cast<intptr_t>(this));
61 NetworkChangeNotifier::ConnectionType
62 NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType() const {
63 base::AutoLock
auto_lock(connection_type_lock_
);
64 return connection_type_
;
67 void NetworkChangeNotifierDelegateAndroid::NotifyConnectionTypeChanged(
70 jint new_connection_type
) {
71 DCHECK(thread_checker_
.CalledOnValidThread());
72 const ConnectionType actual_connection_type
= ConvertConnectionType(
74 SetCurrentConnectionType(actual_connection_type
);
75 observers_
->Notify(&Observer::OnConnectionTypeChanged
);
78 jint
NetworkChangeNotifierDelegateAndroid::GetConnectionType(JNIEnv
*,
80 DCHECK(thread_checker_
.CalledOnValidThread());
81 return GetCurrentConnectionType();
84 void NetworkChangeNotifierDelegateAndroid::AddObserver(
86 observers_
->AddObserver(observer
);
89 void NetworkChangeNotifierDelegateAndroid::RemoveObserver(
91 observers_
->RemoveObserver(observer
);
95 bool NetworkChangeNotifierDelegateAndroid::Register(JNIEnv
* env
) {
96 return RegisterNativesImpl(env
);
99 void NetworkChangeNotifierDelegateAndroid::SetCurrentConnectionType(
100 ConnectionType new_connection_type
) {
101 base::AutoLock
auto_lock(connection_type_lock_
);
102 connection_type_
= new_connection_type
;
105 void NetworkChangeNotifierDelegateAndroid::SetOnline() {
106 JNIEnv
* env
= base::android::AttachCurrentThread();
107 Java_NetworkChangeNotifier_forceConnectivityState(env
, true);
110 void NetworkChangeNotifierDelegateAndroid::SetOffline() {
111 JNIEnv
* env
= base::android::AttachCurrentThread();
112 Java_NetworkChangeNotifier_forceConnectivityState(env
, false);