| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_ANDROID_NETWORK_CHANGE_NOTIFIER_ANDROID_H_ | 5 #ifndef NET_ANDROID_NETWORK_CHANGE_NOTIFIER_ANDROID_H_ |
| 6 #define NET_ANDROID_NETWORK_CHANGE_NOTIFIER_ANDROID_H_ | 6 #define NET_ANDROID_NETWORK_CHANGE_NOTIFIER_ANDROID_H_ |
| 7 | 7 |
| 8 #include "base/android/scoped_java_ref.h" | 8 #include "base/android/scoped_java_ref.h" |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
| 12 #include "net/base/network_change_notifier.h" | 12 #include "net/base/network_change_notifier.h" |
| 13 | 13 |
| 14 namespace base { |
| 15 |
| 16 class TaskRunner; |
| 17 |
| 18 } // namespace base |
| 19 |
| 14 namespace net { | 20 namespace net { |
| 15 | 21 |
| 16 class NetworkChangeNotifierAndroidTest; | 22 class NetworkChangeNotifierAndroidTest; |
| 17 | 23 |
| 18 class NetworkChangeNotifierAndroid : public NetworkChangeNotifier { | 24 class NetworkChangeNotifierAndroid : public NetworkChangeNotifier { |
| 19 public: | 25 public: |
| 26 class Delegate { |
| 27 public: |
| 28 // Called from Java on the UI thread. |
| 29 virtual void NotifyObserversOfConnectionTypeChange( |
| 30 JNIEnv* env, jobject obj, jint new_connection_type) = 0; |
| 31 |
| 32 virtual jint GetConnectionType(JNIEnv* env, jobject obj) const = 0; |
| 33 |
| 34 protected: |
| 35 virtual ~Delegate() {} |
| 36 }; |
| 37 |
| 20 virtual ~NetworkChangeNotifierAndroid(); | 38 virtual ~NetworkChangeNotifierAndroid(); |
| 21 | 39 |
| 22 // Called from Java on the UI thread. | |
| 23 void NotifyObserversOfConnectionTypeChange( | |
| 24 JNIEnv* env, jobject obj, jint new_connection_type); | |
| 25 jint GetConnectionType(JNIEnv* env, jobject obj); | |
| 26 | |
| 27 static bool Register(JNIEnv* env); | 40 static bool Register(JNIEnv* env); |
| 28 | 41 |
| 29 private: | 42 private: |
| 43 class DelegateImpl; |
| 44 |
| 45 friend class DelegateImpl; |
| 30 friend class NetworkChangeNotifierAndroidTest; | 46 friend class NetworkChangeNotifierAndroidTest; |
| 31 friend class NetworkChangeNotifierFactoryAndroid; | 47 friend class NetworkChangeNotifierFactoryAndroid; |
| 32 | 48 |
| 33 NetworkChangeNotifierAndroid(); | 49 NetworkChangeNotifierAndroid(base::TaskRunner* ui_task_runner); |
| 34 | 50 |
| 35 void SetConnectionType(int connection_type); | 51 // Wrapper used to let |Delegate| call the protected |
| 52 // NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange() method. |
| 53 static void NotifyObservers(); |
| 36 | 54 |
| 37 void ForceConnectivityState(bool state); | 55 void ForceConnectivityState(bool state); |
| 38 | 56 |
| 39 // NetworkChangeNotifier: | 57 // NetworkChangeNotifier: |
| 40 virtual ConnectionType GetCurrentConnectionType() const OVERRIDE; | 58 virtual ConnectionType GetCurrentConnectionType() const OVERRIDE; |
| 41 | 59 |
| 42 base::android::ScopedJavaGlobalRef<jobject> java_network_change_notifier_; | 60 // The delegate deletes itself. |
| 43 // TODO(pliard): http://crbug.com/150867. Use an atomic integer for the | 61 DelegateImpl* const delegate_; |
| 44 // connection type without the lock once a non-subtle atomic integer is | |
| 45 // available under base/. That might never happen though. | |
| 46 mutable base::Lock lock_; // Protects the state below. | |
| 47 // Written from the UI thread, read from any thread. | |
| 48 int connection_type_; | |
| 49 | 62 |
| 50 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierAndroid); | 63 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierAndroid); |
| 51 }; | 64 }; |
| 52 | 65 |
| 53 } // namespace net | 66 } // namespace net |
| 54 | 67 |
| 55 #endif // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_ANDROID_H_ | 68 #endif // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_ANDROID_H_ |
| OLD | NEW |