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/memory/weak_ptr.h" | |
11 #include "base/observer_list_threadsafe.h" | 12 #include "base/observer_list_threadsafe.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 |
(...skipping 22 matching lines...) Expand all Loading... | |
43 | 44 |
44 // Called from NetworkChangeNotifierAndroid.java on the JNI thread whenever | 45 // Called from NetworkChangeNotifierAndroid.java on the JNI thread whenever |
45 // the connection type changes. This updates the current connection type seen | 46 // the connection type changes. This updates the current connection type seen |
46 // by this class and forwards the notification to the observers that | 47 // by this class and forwards the notification to the observers that |
47 // subscribed through AddObserver(). | 48 // subscribed through AddObserver(). |
48 void NotifyConnectionTypeChanged(JNIEnv* env, | 49 void NotifyConnectionTypeChanged(JNIEnv* env, |
49 jobject obj, | 50 jobject obj, |
50 jint new_connection_type); | 51 jint new_connection_type); |
51 jint GetConnectionType(JNIEnv* env, jobject obj) const; | 52 jint GetConnectionType(JNIEnv* env, jobject obj) const; |
52 | 53 |
53 // These methods can be called on any thread. Note that the provided observer | 54 // These methods can be called on any thread. The provided observer will be |
54 // will be notified on the thread AddObserver() is called on. | 55 // notified on the thread AddObserver() is called on. Note that calling this |
56 // method schedules an initial notification that lets the observer initialize | |
57 // its connection type. | |
55 void AddObserver(Observer* observer); | 58 void AddObserver(Observer* observer); |
Ryan Sleevi
2012/12/19 17:38:16
Can you not just have AddObserver return the initi
Philippe
2012/12/19 17:52:23
I might be missing something but AddObserver() can
Ryan Sleevi
2012/12/19 18:07:57
Yeah, you'd still have to have a lock.
| |
56 void RemoveObserver(Observer* observer); | 59 void RemoveObserver(Observer* observer); |
57 | 60 |
58 // Exposed for testing. | 61 // Exposed for testing. |
59 void ForceConnectivityState(ConnectivityState state); | 62 void ForceConnectivityState(ConnectivityState state); |
60 | 63 |
61 // Initializes JNI bindings. | 64 // Initializes JNI bindings. |
62 static bool Register(JNIEnv* env); | 65 static bool Register(JNIEnv* env); |
63 | 66 |
64 private: | 67 private: |
65 friend class NetworkChangeNotifierDelegateAndroidTest; | 68 friend class BaseNetworkChangeNotifierAndroidTest; |
66 | 69 |
67 typedef NetworkChangeNotifier::ConnectionType ConnectionType; | 70 typedef NetworkChangeNotifier::ConnectionType ConnectionType; |
68 | 71 |
72 // Provides the newly added observer with an initial connection type. | |
73 void SendInitialNotification(); | |
74 | |
69 base::ThreadChecker thread_checker_; | 75 base::ThreadChecker thread_checker_; |
76 scoped_refptr<base::SingleThreadTaskRunner> jni_task_runner_; | |
77 base::WeakPtrFactory<NetworkChangeNotifierDelegateAndroid> weak_ptr_factory_; | |
78 base::WeakPtr<NetworkChangeNotifierDelegateAndroid> weak_ptr_; | |
79 | |
70 scoped_refptr<ObserverListThreadSafe<Observer> > observers_; | 80 scoped_refptr<ObserverListThreadSafe<Observer> > observers_; |
71 scoped_refptr<base::SingleThreadTaskRunner> jni_task_runner_; | |
72 base::android::ScopedJavaGlobalRef<jobject> java_network_change_notifier_; | 81 base::android::ScopedJavaGlobalRef<jobject> java_network_change_notifier_; |
73 ConnectionType connection_type_; | 82 ConnectionType connection_type_; |
74 | 83 |
75 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierDelegateAndroid); | 84 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierDelegateAndroid); |
76 }; | 85 }; |
77 | 86 |
78 } // namespace net | 87 } // namespace net |
79 | 88 |
80 #endif // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_DELEGATE_H_ | 89 #endif // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_DELEGATE_H_ |
OLD | NEW |