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
:
28 NOTREACHED() << "Unknown connection type received: " << connection_type
;
29 return NetworkChangeNotifier::CONNECTION_UNKNOWN
;
31 return static_cast<NetworkChangeNotifier::ConnectionType
>(connection_type
);
36 NetworkChangeNotifierDelegateAndroid::NetworkChangeNotifierDelegateAndroid()
37 : observers_(new ObserverListThreadSafe
<Observer
>()) {
38 JNIEnv
* env
= base::android::AttachCurrentThread();
39 java_network_change_notifier_
.Reset(
40 Java_NetworkChangeNotifier_init(
41 env
, base::android::GetApplicationContext()));
42 Java_NetworkChangeNotifier_addNativeObserver(
43 env
, java_network_change_notifier_
.obj(),
44 reinterpret_cast<intptr_t>(this));
45 SetCurrentConnectionType(
46 ConvertConnectionType(
47 Java_NetworkChangeNotifier_getCurrentConnectionType(
48 env
, java_network_change_notifier_
.obj())));
51 NetworkChangeNotifierDelegateAndroid::~NetworkChangeNotifierDelegateAndroid() {
52 DCHECK(thread_checker_
.CalledOnValidThread());
53 observers_
->AssertEmpty();
54 JNIEnv
* env
= base::android::AttachCurrentThread();
55 Java_NetworkChangeNotifier_removeNativeObserver(
56 env
, java_network_change_notifier_
.obj(),
57 reinterpret_cast<intptr_t>(this));
60 NetworkChangeNotifier::ConnectionType
61 NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType() const {
62 base::AutoLock
auto_lock(connection_type_lock_
);
63 return connection_type_
;
66 void NetworkChangeNotifierDelegateAndroid::NotifyConnectionTypeChanged(
69 jint new_connection_type
) {
70 DCHECK(thread_checker_
.CalledOnValidThread());
71 const ConnectionType actual_connection_type
= ConvertConnectionType(
73 SetCurrentConnectionType(actual_connection_type
);
74 observers_
->Notify(&Observer::OnConnectionTypeChanged
);
77 jint
NetworkChangeNotifierDelegateAndroid::GetConnectionType(JNIEnv
*,
79 DCHECK(thread_checker_
.CalledOnValidThread());
80 return GetCurrentConnectionType();
83 void NetworkChangeNotifierDelegateAndroid::AddObserver(
85 observers_
->AddObserver(observer
);
88 void NetworkChangeNotifierDelegateAndroid::RemoveObserver(
90 observers_
->RemoveObserver(observer
);
94 bool NetworkChangeNotifierDelegateAndroid::Register(JNIEnv
* env
) {
95 return RegisterNativesImpl(env
);
98 void NetworkChangeNotifierDelegateAndroid::SetCurrentConnectionType(
99 ConnectionType new_connection_type
) {
100 base::AutoLock
auto_lock(connection_type_lock_
);
101 connection_type_
= new_connection_type
;
104 void NetworkChangeNotifierDelegateAndroid::SetOnline() {
105 JNIEnv
* env
= base::android::AttachCurrentThread();
106 Java_NetworkChangeNotifier_forceConnectivityState(env
, true);
109 void NetworkChangeNotifierDelegateAndroid::SetOffline() {
110 JNIEnv
* env
= base::android::AttachCurrentThread();
111 Java_NetworkChangeNotifier_forceConnectivityState(env
, false);