Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(56)

Unified Diff: net/android/network_change_notifier_delegate_android.cc

Issue 11628008: Provide NetworkChangeNotifierAndroid with the actual initial connection type. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address Dan's comments Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/android/network_change_notifier_delegate_android.cc
diff --git a/net/android/network_change_notifier_delegate_android.cc b/net/android/network_change_notifier_delegate_android.cc
index 91b0cb0465e65c7a7513b5a2fc80a1aa090231b8..3ce1eebf9efa669c7da5a35197d9b19fc5e377dd 100644
--- a/net/android/network_change_notifier_delegate_android.cc
+++ b/net/android/network_change_notifier_delegate_android.cc
@@ -11,8 +11,10 @@ namespace net {
namespace {
-// Returns whether the provided connection type is known.
-bool CheckConnectionType(int connection_type) {
+// 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:
@@ -21,29 +23,42 @@ bool CheckConnectionType(int connection_type) {
case NetworkChangeNotifier::CONNECTION_3G:
case NetworkChangeNotifier::CONNECTION_4G:
case NetworkChangeNotifier::CONNECTION_NONE:
- return true;
+ break;
default:
NOTREACHED() << "Unknown connection type received: " << connection_type;
- return false;
+ 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_createInstance(
- base::android::AttachCurrentThread(),
- base::android::GetApplicationContext(),
- reinterpret_cast<jint>(this)));
+ Java_NetworkChangeNotifier_init(
+ env, base::android::GetApplicationContext()));
+ Java_NetworkChangeNotifier_addNativeObserver(
+ env, java_network_change_notifier_.obj(), reinterpret_cast<jint>(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_destroyInstance(env);
+ Java_NetworkChangeNotifier_removeNativeObserver(
+ env, java_network_change_notifier_.obj(), reinterpret_cast<jint>(this));
+}
+
+NetworkChangeNotifier::ConnectionType
+NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType() const {
+ base::AutoLock auto_lock(connection_type_lock_);
+ return connection_type_;
}
void NetworkChangeNotifierDelegateAndroid::NotifyConnectionTypeChanged(
@@ -51,16 +66,17 @@ void NetworkChangeNotifierDelegateAndroid::NotifyConnectionTypeChanged(
jobject obj,
jint new_connection_type) {
DCHECK(thread_checker_.CalledOnValidThread());
- connection_type_ = CheckConnectionType(new_connection_type) ?
- static_cast<ConnectionType>(new_connection_type) :
- NetworkChangeNotifier::CONNECTION_UNKNOWN;
- observers_->Notify(&Observer::OnConnectionTypeChanged, connection_type_);
+ const ConnectionType actual_connection_type = ConvertConnectionType(
+ new_connection_type);
+ SetCurrentConnectionType(actual_connection_type);
+ observers_->Notify(
+ &Observer::OnConnectionTypeChanged, actual_connection_type);
Ryan Sleevi 2012/12/21 19:26:07 Considering that all of the NCNDA::Observers do no
Philippe 2012/12/26 10:33:25 Good point. Note that this implied a few changes i
}
jint NetworkChangeNotifierDelegateAndroid::GetConnectionType(JNIEnv*,
jobject) const {
DCHECK(thread_checker_.CalledOnValidThread());
- return connection_type_;
+ return GetCurrentConnectionType();
}
void NetworkChangeNotifierDelegateAndroid::AddObserver(
@@ -73,16 +89,28 @@ void NetworkChangeNotifierDelegateAndroid::RemoveObserver(
observers_->RemoveObserver(observer);
}
-void NetworkChangeNotifierDelegateAndroid::ForceConnectivityState(
+// 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::ForceJavaSideConnectivityState(
ConnectivityState state) {
- DCHECK(thread_checker_.CalledOnValidThread());
JNIEnv* env = base::android::AttachCurrentThread();
Java_NetworkChangeNotifier_forceConnectivityState(env, state == ONLINE);
}
-// static
-bool NetworkChangeNotifierDelegateAndroid::Register(JNIEnv* env) {
- return RegisterNativesImpl(env);
+NetworkChangeNotifierDelegateAndroid::ConnectivityState
+NetworkChangeNotifierDelegateAndroid::GetJavaSideConnectivityState() {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ return Java_NetworkChangeNotifier_getCurrentConnectionType(
+ env, java_network_change_notifier_.obj()) ? ONLINE : OFFLINE;
}
} // namespace net

Powered by Google App Engine
This is Rietveld 408576698