Home | History | Annotate | Download | only in android
      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.
      4 
      5 #include "net/android/network_change_notifier_delegate_android.h"
      6 
      7 #include "base/logging.h"
      8 #include "jni/NetworkChangeNotifier_jni.h"
      9 
     10 namespace net {
     11 
     12 namespace {
     13 
     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       break;
     27     default:
     28       NOTREACHED() << "Unknown connection type received: " << connection_type;
     29       return NetworkChangeNotifier::CONNECTION_UNKNOWN;
     30   }
     31   return static_cast<NetworkChangeNotifier::ConnectionType>(connection_type);
     32 }
     33 
     34 }  // namespace
     35 
     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(), reinterpret_cast<jint>(this));
     44   SetCurrentConnectionType(
     45       ConvertConnectionType(
     46           Java_NetworkChangeNotifier_getCurrentConnectionType(
     47               env, java_network_change_notifier_.obj())));
     48 }
     49 
     50 NetworkChangeNotifierDelegateAndroid::~NetworkChangeNotifierDelegateAndroid() {
     51   DCHECK(thread_checker_.CalledOnValidThread());
     52   observers_->AssertEmpty();
     53   JNIEnv* env = base::android::AttachCurrentThread();
     54   Java_NetworkChangeNotifier_removeNativeObserver(
     55       env, java_network_change_notifier_.obj(), reinterpret_cast<jint>(this));
     56 }
     57 
     58 NetworkChangeNotifier::ConnectionType
     59 NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType() const {
     60   base::AutoLock auto_lock(connection_type_lock_);
     61   return connection_type_;
     62 }
     63 
     64 void NetworkChangeNotifierDelegateAndroid::NotifyConnectionTypeChanged(
     65     JNIEnv* env,
     66     jobject obj,
     67     jint new_connection_type) {
     68   DCHECK(thread_checker_.CalledOnValidThread());
     69   const ConnectionType actual_connection_type = ConvertConnectionType(
     70       new_connection_type);
     71   SetCurrentConnectionType(actual_connection_type);
     72   observers_->Notify(&Observer::OnConnectionTypeChanged);
     73 }
     74 
     75 jint NetworkChangeNotifierDelegateAndroid::GetConnectionType(JNIEnv*,
     76                                                              jobject) const {
     77   DCHECK(thread_checker_.CalledOnValidThread());
     78   return GetCurrentConnectionType();
     79 }
     80 
     81 void NetworkChangeNotifierDelegateAndroid::AddObserver(
     82     Observer* observer) {
     83   observers_->AddObserver(observer);
     84 }
     85 
     86 void NetworkChangeNotifierDelegateAndroid::RemoveObserver(
     87     Observer* observer) {
     88   observers_->RemoveObserver(observer);
     89 }
     90 
     91 // static
     92 bool NetworkChangeNotifierDelegateAndroid::Register(JNIEnv* env) {
     93   return RegisterNativesImpl(env);
     94 }
     95 
     96 void NetworkChangeNotifierDelegateAndroid::SetCurrentConnectionType(
     97     ConnectionType new_connection_type) {
     98   base::AutoLock auto_lock(connection_type_lock_);
     99   connection_type_ = new_connection_type;
    100 }
    101 
    102 void NetworkChangeNotifierDelegateAndroid::SetOnline() {
    103   JNIEnv* env = base::android::AttachCurrentThread();
    104   Java_NetworkChangeNotifier_forceConnectivityState(env, true);
    105 }
    106 
    107 void NetworkChangeNotifierDelegateAndroid::SetOffline() {
    108   JNIEnv* env = base::android::AttachCurrentThread();
    109   Java_NetworkChangeNotifier_forceConnectivityState(env, false);
    110 }
    111 
    112 }  // namespace net
    113