blob: 207f01b6c86866c9d48abdb98c774e4553b2e110 (
plain)
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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// 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/logging.h"
#include "jni/NetworkChangeNotifier_jni.h"
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_NONE:
break;
default:
NOTREACHED() << "Unknown connection type received: " << connection_type;
return NetworkChangeNotifier::CONNECTION_UNKNOWN;
}
return static_cast<NetworkChangeNotifier::ConnectionType>(connection_type);
}
} // namespace
NetworkChangeNotifierDelegateAndroid::NetworkChangeNotifierDelegateAndroid()
: observers_(new ObserverListThreadSafe<Observer>()) {
JNIEnv* env = base::android::AttachCurrentThread();
java_network_change_notifier_.Reset(
Java_NetworkChangeNotifier_init(
env, base::android::GetApplicationContext()));
Java_NetworkChangeNotifier_addNativeObserver(
env, java_network_change_notifier_.obj(),
reinterpret_cast<intptr_t>(this));
SetCurrentConnectionType(
ConvertConnectionType(
Java_NetworkChangeNotifier_getCurrentConnectionType(
env, java_network_change_notifier_.obj())));
}
NetworkChangeNotifierDelegateAndroid::~NetworkChangeNotifierDelegateAndroid() {
DCHECK(thread_checker_.CalledOnValidThread());
observers_->AssertEmpty();
JNIEnv* env = base::android::AttachCurrentThread();
Java_NetworkChangeNotifier_removeNativeObserver(
env, java_network_change_notifier_.obj(),
reinterpret_cast<intptr_t>(this));
}
NetworkChangeNotifier::ConnectionType
NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType() const {
base::AutoLock auto_lock(connection_type_lock_);
return connection_type_;
}
void NetworkChangeNotifierDelegateAndroid::NotifyConnectionTypeChanged(
JNIEnv* env,
jobject obj,
jint new_connection_type) {
DCHECK(thread_checker_.CalledOnValidThread());
const ConnectionType actual_connection_type = ConvertConnectionType(
new_connection_type);
SetCurrentConnectionType(actual_connection_type);
observers_->Notify(&Observer::OnConnectionTypeChanged);
}
jint NetworkChangeNotifierDelegateAndroid::GetConnectionType(JNIEnv*,
jobject) const {
DCHECK(thread_checker_.CalledOnValidThread());
return GetCurrentConnectionType();
}
void NetworkChangeNotifierDelegateAndroid::AddObserver(
Observer* observer) {
observers_->AddObserver(observer);
}
void NetworkChangeNotifierDelegateAndroid::RemoveObserver(
Observer* observer) {
observers_->RemoveObserver(observer);
}
// static
bool NetworkChangeNotifierDelegateAndroid::Register(JNIEnv* env) {
return RegisterNativesImpl(env);
}
void NetworkChangeNotifierDelegateAndroid::SetCurrentConnectionType(
ConnectionType new_connection_type) {
base::AutoLock auto_lock(connection_type_lock_);
connection_type_ = new_connection_type;
}
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);
}
} // namespace net
|