Clean up URLFetcher unit tests, part 8.
[chromium-blink-merge.git] / net / android / network_change_notifier_delegate_android.cc
blobf623b14798528f5e2ea7cd811893b3fe5b8a867e
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 jclass caller,
49 jint subtype) {
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(
93 JNIEnv* env,
94 jobject obj,
95 jint new_connection_type) {
96 DCHECK(thread_checker_.CalledOnValidThread());
97 const ConnectionType actual_connection_type = ConvertConnectionType(
98 new_connection_type);
99 SetCurrentConnectionType(actual_connection_type);
100 observers_->Notify(FROM_HERE, &Observer::OnConnectionTypeChanged);
103 jint NetworkChangeNotifierDelegateAndroid::GetConnectionType(JNIEnv*,
104 jobject) const {
105 DCHECK(thread_checker_.CalledOnValidThread());
106 return GetCurrentConnectionType();
109 void NetworkChangeNotifierDelegateAndroid::NotifyMaxBandwidthChanged(
110 JNIEnv* env,
111 jobject obj,
112 jdouble new_max_bandwidth) {
113 DCHECK(thread_checker_.CalledOnValidThread());
114 DCHECK(new_max_bandwidth != GetCurrentMaxBandwidth());
115 SetCurrentMaxBandwidth(new_max_bandwidth);
116 observers_->Notify(FROM_HERE, &Observer::OnMaxBandwidthChanged,
117 new_max_bandwidth);
120 void NetworkChangeNotifierDelegateAndroid::AddObserver(
121 Observer* observer) {
122 observers_->AddObserver(observer);
125 void NetworkChangeNotifierDelegateAndroid::RemoveObserver(
126 Observer* observer) {
127 observers_->RemoveObserver(observer);
130 // static
131 bool NetworkChangeNotifierDelegateAndroid::Register(JNIEnv* env) {
132 return RegisterNativesImpl(env);
135 void NetworkChangeNotifierDelegateAndroid::SetCurrentConnectionType(
136 ConnectionType new_connection_type) {
137 base::AutoLock auto_lock(connection_lock_);
138 connection_type_ = new_connection_type;
141 void NetworkChangeNotifierDelegateAndroid::SetCurrentMaxBandwidth(
142 double max_bandwidth) {
143 base::AutoLock auto_lock(connection_lock_);
144 connection_max_bandwidth_ = max_bandwidth;
147 void NetworkChangeNotifierDelegateAndroid::SetOnline() {
148 JNIEnv* env = base::android::AttachCurrentThread();
149 Java_NetworkChangeNotifier_forceConnectivityState(env, true);
152 void NetworkChangeNotifierDelegateAndroid::SetOffline() {
153 JNIEnv* env = base::android::AttachCurrentThread();
154 Java_NetworkChangeNotifier_forceConnectivityState(env, false);
157 } // namespace net