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_DELEGATE_ANDROID_H_ | 5 #ifndef NET_ANDROID_NETWORK_CHANGE_NOTIFIER_DELEGATE_ANDROID_H_ |
6 #define NET_ANDROID_NETWORK_CHANGE_NOTIFIER_DELEGATE_ANDROID_H_ | 6 #define NET_ANDROID_NETWORK_CHANGE_NOTIFIER_DELEGATE_ANDROID_H_ |
7 | 7 |
8 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/observer_list_threadsafe.h" | 11 #include "base/observer_list_threadsafe.h" |
| 12 #include "base/synchronization/lock.h" |
12 #include "base/threading/thread_checker.h" | 13 #include "base/threading/thread_checker.h" |
13 #include "net/base/network_change_notifier.h" | 14 #include "net/base/network_change_notifier.h" |
14 | 15 |
15 namespace net { | 16 namespace net { |
16 | 17 |
17 // Delegate used to thread-safely notify NetworkChangeNotifierAndroid whenever a | 18 // Delegate used to thread-safely notify NetworkChangeNotifierAndroid whenever a |
18 // network connection change notification is signaled by the Java side (on the | 19 // network connection change notification is signaled by the Java side (on the |
19 // JNI thread). | 20 // JNI thread). |
20 // All the methods exposed below must be called exclusively on the JNI thread | 21 // All the methods exposed below must be called exclusively on the JNI thread |
21 // unless otherwise stated (e.g. AddObserver()/RemoveObserver()). | 22 // unless otherwise stated (e.g. AddObserver()/RemoveObserver()). |
22 class NET_EXPORT_PRIVATE NetworkChangeNotifierDelegateAndroid { | 23 class NET_EXPORT_PRIVATE NetworkChangeNotifierDelegateAndroid { |
23 public: | 24 public: |
24 enum ConnectivityState { | 25 typedef NetworkChangeNotifier::ConnectionType ConnectionType; |
25 OFFLINE, | |
26 ONLINE, | |
27 }; | |
28 | 26 |
29 // Observer interface implemented by NetworkChangeNotifierAndroid which | 27 // Observer interface implemented by NetworkChangeNotifierAndroid which |
30 // subscribes to network change notifications fired by the delegate (and | 28 // subscribes to network change notifications fired by the delegate (and |
31 // initiated by the Java side). | 29 // initiated by the Java side). |
32 class Observer { | 30 class Observer { |
33 public: | 31 public: |
34 virtual ~Observer() {} | 32 virtual ~Observer() {} |
35 | 33 |
36 // Updates the current connection type. | 34 // Updates the current connection type. |
37 virtual void OnConnectionTypeChanged( | 35 virtual void OnConnectionTypeChanged() = 0; |
38 NetworkChangeNotifier::ConnectionType new_connection_type) = 0; | |
39 }; | 36 }; |
40 | 37 |
41 NetworkChangeNotifierDelegateAndroid(); | 38 NetworkChangeNotifierDelegateAndroid(); |
42 ~NetworkChangeNotifierDelegateAndroid(); | 39 ~NetworkChangeNotifierDelegateAndroid(); |
43 | 40 |
44 // Called from NetworkChangeNotifierAndroid.java on the JNI thread whenever | 41 // Called from NetworkChangeNotifierAndroid.java on the JNI thread whenever |
45 // the connection type changes. This updates the current connection type seen | 42 // the connection type changes. This updates the current connection type seen |
46 // by this class and forwards the notification to the observers that | 43 // by this class and forwards the notification to the observers that |
47 // subscribed through AddObserver(). | 44 // subscribed through AddObserver(). |
48 void NotifyConnectionTypeChanged(JNIEnv* env, | 45 void NotifyConnectionTypeChanged(JNIEnv* env, |
49 jobject obj, | 46 jobject obj, |
50 jint new_connection_type); | 47 jint new_connection_type); |
51 jint GetConnectionType(JNIEnv* env, jobject obj) const; | 48 jint GetConnectionType(JNIEnv* env, jobject obj) const; |
52 | 49 |
53 // These methods can be called on any thread. Note that the provided observer | 50 // These methods can be called on any thread. Note that the provided observer |
54 // will be notified on the thread AddObserver() is called on. | 51 // will be notified on the thread AddObserver() is called on. |
55 void AddObserver(Observer* observer); | 52 void AddObserver(Observer* observer); |
56 void RemoveObserver(Observer* observer); | 53 void RemoveObserver(Observer* observer); |
57 | 54 |
58 // Exposed for testing. | 55 // Can be called from any thread. |
59 void ForceConnectivityState(ConnectivityState state); | 56 ConnectionType GetCurrentConnectionType() const; |
60 | 57 |
61 // Initializes JNI bindings. | 58 // Initializes JNI bindings. |
62 static bool Register(JNIEnv* env); | 59 static bool Register(JNIEnv* env); |
63 | 60 |
64 private: | 61 private: |
65 friend class NetworkChangeNotifierDelegateAndroidTest; | 62 friend class BaseNetworkChangeNotifierAndroidTest; |
66 | 63 |
67 typedef NetworkChangeNotifier::ConnectionType ConnectionType; | 64 void SetCurrentConnectionType(ConnectionType connection_type); |
| 65 |
| 66 // Methods calling the Java side exposed for testing. |
| 67 void SetOnline(); |
| 68 void SetOffline(); |
68 | 69 |
69 base::ThreadChecker thread_checker_; | 70 base::ThreadChecker thread_checker_; |
70 scoped_refptr<ObserverListThreadSafe<Observer> > observers_; | 71 scoped_refptr<ObserverListThreadSafe<Observer> > observers_; |
71 scoped_refptr<base::SingleThreadTaskRunner> jni_task_runner_; | 72 scoped_refptr<base::SingleThreadTaskRunner> jni_task_runner_; |
72 base::android::ScopedJavaGlobalRef<jobject> java_network_change_notifier_; | 73 base::android::ScopedJavaGlobalRef<jobject> java_network_change_notifier_; |
| 74 mutable base::Lock connection_type_lock_; // Protects the state below. |
73 ConnectionType connection_type_; | 75 ConnectionType connection_type_; |
74 | 76 |
75 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierDelegateAndroid); | 77 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierDelegateAndroid); |
76 }; | 78 }; |
77 | 79 |
78 } // namespace net | 80 } // namespace net |
79 | 81 |
80 #endif // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_DELEGATE_H_ | 82 #endif // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_DELEGATE_H_ |
OLD | NEW |