Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(722)

Side by Side Diff: net/android/network_change_notifier_delegate_android.h

Issue 11628008: Provide NetworkChangeNotifierAndroid with the actual initial connection type. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address Dan's comments Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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(
38 NetworkChangeNotifier::ConnectionType new_connection_type) = 0; 36 NetworkChangeNotifier::ConnectionType new_connection_type) = 0;
39 }; 37 };
40 38
41 NetworkChangeNotifierDelegateAndroid(); 39 NetworkChangeNotifierDelegateAndroid();
42 ~NetworkChangeNotifierDelegateAndroid(); 40 ~NetworkChangeNotifierDelegateAndroid();
43 41
42 // Can be called from any thread.
43 ConnectionType GetCurrentConnectionType() const;
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. Note that the provided observer
54 // will be notified on the thread AddObserver() is called on. 55 // will be notified on the thread AddObserver() is called on.
55 void AddObserver(Observer* observer); 56 void AddObserver(Observer* observer);
56 void RemoveObserver(Observer* observer); 57 void RemoveObserver(Observer* observer);
57 58
58 // Exposed for testing.
59 void ForceConnectivityState(ConnectivityState state);
60
61 // Initializes JNI bindings. 59 // Initializes JNI bindings.
62 static bool Register(JNIEnv* env); 60 static bool Register(JNIEnv* env);
63 61
64 private: 62 private:
65 friend class NetworkChangeNotifierDelegateAndroidTest; 63 friend class BaseNetworkChangeNotifierAndroidTest;
66 64
67 typedef NetworkChangeNotifier::ConnectionType ConnectionType; 65 enum ConnectivityState {
66 OFFLINE,
67 ONLINE,
68 };
69
70 void SetCurrentConnectionType(ConnectionType connection_type);
71
72 // Exposed for testing.
73 void ForceJavaSideConnectivityState(ConnectivityState state);
74 ConnectivityState GetJavaSideConnectivityState();
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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698