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: |
25 typedef NetworkChangeNotifier::ConnectionType ConnectionType; | |
26 | |
24 enum ConnectivityState { | 27 enum ConnectivityState { |
25 OFFLINE, | 28 OFFLINE, |
26 ONLINE, | 29 ONLINE, |
27 }; | 30 }; |
28 | 31 |
29 // Observer interface implemented by NetworkChangeNotifierAndroid which | 32 // Observer interface implemented by NetworkChangeNotifierAndroid which |
30 // subscribes to network change notifications fired by the delegate (and | 33 // subscribes to network change notifications fired by the delegate (and |
31 // initiated by the Java side). | 34 // initiated by the Java side). |
32 class Observer { | 35 class Observer { |
33 public: | 36 public: |
34 virtual ~Observer() {} | 37 virtual ~Observer() {} |
35 | 38 |
36 // Updates the current connection type. | 39 // Updates the current connection type. |
37 virtual void OnConnectionTypeChanged( | 40 virtual void OnConnectionTypeChanged( |
38 NetworkChangeNotifier::ConnectionType new_connection_type) = 0; | 41 NetworkChangeNotifier::ConnectionType new_connection_type) = 0; |
39 }; | 42 }; |
40 | 43 |
41 NetworkChangeNotifierDelegateAndroid(); | 44 NetworkChangeNotifierDelegateAndroid(); |
42 ~NetworkChangeNotifierDelegateAndroid(); | 45 ~NetworkChangeNotifierDelegateAndroid(); |
43 | 46 |
44 // Called from NetworkChangeNotifierAndroid.java on the JNI thread whenever | 47 // Called from NetworkChangeNotifierAndroid.java on the JNI thread whenever |
45 // the connection type changes. This updates the current connection type seen | 48 // the connection type changes. This updates the current connection type seen |
46 // by this class and forwards the notification to the observers that | 49 // by this class and forwards the notification to the observers that |
47 // subscribed through AddObserver(). | 50 // subscribed through AddObserver(). |
48 void NotifyConnectionTypeChanged(JNIEnv* env, | 51 void NotifyConnectionTypeChanged(JNIEnv* env, |
49 jobject obj, | 52 jobject obj, |
50 jint new_connection_type); | 53 jint new_connection_type); |
51 jint GetConnectionType(JNIEnv* env, jobject obj) const; | 54 jint GetConnectionType(JNIEnv* env, jobject obj) const; |
52 | 55 |
56 // Same as above but meant to be called by NetworkChangeNotifierAndroid from | |
57 // any thread. | |
Ryan Sleevi
2012/12/19 18:07:57
This comment is confusing and would need to be re-
| |
58 ConnectionType GetCurrentConnectionType() const; | |
59 | |
53 // These methods can be called on any thread. Note that the provided observer | 60 // These methods can be called on any thread. Note that the provided observer |
54 // will be notified on the thread AddObserver() is called on. | 61 // will be notified on the thread AddObserver() is called on. |
55 void AddObserver(Observer* observer); | 62 void AddObserver(Observer* observer); |
56 void RemoveObserver(Observer* observer); | 63 void RemoveObserver(Observer* observer); |
57 | 64 |
58 // Exposed for testing. | 65 // Exposed for testing. |
59 void ForceConnectivityState(ConnectivityState state); | 66 void ForceConnectivityState(ConnectivityState state); |
60 | 67 |
61 // Initializes JNI bindings. | 68 // Initializes JNI bindings. |
62 static bool Register(JNIEnv* env); | 69 static bool Register(JNIEnv* env); |
63 | 70 |
64 private: | 71 private: |
65 friend class NetworkChangeNotifierDelegateAndroidTest; | 72 friend class NetworkChangeNotifierDelegateAndroidTest; |
66 | 73 |
67 typedef NetworkChangeNotifier::ConnectionType ConnectionType; | 74 void SetConnectionType(ConnectionType new_connection_type); |
68 | 75 |
69 base::ThreadChecker thread_checker_; | 76 base::ThreadChecker thread_checker_; |
70 scoped_refptr<ObserverListThreadSafe<Observer> > observers_; | 77 scoped_refptr<ObserverListThreadSafe<Observer> > observers_; |
71 scoped_refptr<base::SingleThreadTaskRunner> jni_task_runner_; | 78 scoped_refptr<base::SingleThreadTaskRunner> jni_task_runner_; |
72 base::android::ScopedJavaGlobalRef<jobject> java_network_change_notifier_; | 79 base::android::ScopedJavaGlobalRef<jobject> java_network_change_notifier_; |
80 mutable base::Lock connection_type_lock_; // Protects the state below. | |
73 ConnectionType connection_type_; | 81 ConnectionType connection_type_; |
74 | 82 |
75 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierDelegateAndroid); | 83 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierDelegateAndroid); |
76 }; | 84 }; |
77 | 85 |
78 } // namespace net | 86 } // namespace net |
79 | 87 |
80 #endif // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_DELEGATE_H_ | 88 #endif // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_DELEGATE_H_ |
OLD | NEW |