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
3 // found in the LICENSE file.
5 #ifndef NET_ANDROID_NETWORK_CHANGE_NOTIFIER_DELEGATE_ANDROID_H_
6 #define NET_ANDROID_NETWORK_CHANGE_NOTIFIER_DELEGATE_ANDROID_H_
8 #include "base/android/jni_android.h"
9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/observer_list_threadsafe.h"
12 #include "base/synchronization/lock.h"
13 #include "base/threading/thread_checker.h"
14 #include "net/base/network_change_notifier.h"
18 // Delegate used to thread-safely notify NetworkChangeNotifierAndroid whenever a
19 // network connection change notification is signaled by the Java side (on the
21 // All the methods exposed below must be called exclusively on the JNI thread
22 // unless otherwise stated (e.g. AddObserver()/RemoveObserver()).
23 class NET_EXPORT_PRIVATE NetworkChangeNotifierDelegateAndroid
{
25 typedef NetworkChangeNotifier::ConnectionType ConnectionType
;
27 // Observer interface implemented by NetworkChangeNotifierAndroid which
28 // subscribes to network change notifications fired by the delegate (and
29 // initiated by the Java side).
32 virtual ~Observer() {}
34 // Updates the current connection type.
35 virtual void OnConnectionTypeChanged() = 0;
37 // Updates the current max bandwidth.
38 virtual void OnMaxBandwidthChanged(double max_bandwidth_mbps
) = 0;
41 NetworkChangeNotifierDelegateAndroid();
42 ~NetworkChangeNotifierDelegateAndroid();
44 // Called from NetworkChangeNotifierAndroid.java on the JNI thread whenever
45 // the connection type changes. This updates the current connection type seen
46 // by this class and forwards the notification to the observers that
47 // subscribed through AddObserver().
48 void NotifyConnectionTypeChanged(JNIEnv
* env
,
50 jint new_connection_type
);
51 jint
GetConnectionType(JNIEnv
* env
, jobject obj
) const;
53 // Called from NetworkChangeNotifierAndroid.java on the JNI thread whenever
54 // the maximum bandwidth of the connection changes. This updates the current
55 // max bandwidth seen by this class and forwards the notification to the
56 // observers that subscribed through AddObserver().
57 void NotifyMaxBandwidthChanged(JNIEnv
* env
,
59 jdouble new_max_bandwidth
);
61 // These methods can be called on any thread. Note that the provided observer
62 // will be notified on the thread AddObserver() is called on.
63 void AddObserver(Observer
* observer
);
64 void RemoveObserver(Observer
* observer
);
66 // Can be called from any thread.
67 ConnectionType
GetCurrentConnectionType() const;
69 // Can be called from any thread.
70 double GetCurrentMaxBandwidth() const;
72 // Initializes JNI bindings.
73 static bool Register(JNIEnv
* env
);
76 friend class BaseNetworkChangeNotifierAndroidTest
;
78 void SetCurrentConnectionType(ConnectionType connection_type
);
79 void SetCurrentMaxBandwidth(double max_bandwidth
);
81 // Methods calling the Java side exposed for testing.
85 base::ThreadChecker thread_checker_
;
86 scoped_refptr
<base::ObserverListThreadSafe
<Observer
>> observers_
;
87 scoped_refptr
<base::SingleThreadTaskRunner
> jni_task_runner_
;
88 base::android::ScopedJavaGlobalRef
<jobject
> java_network_change_notifier_
;
89 mutable base::Lock connection_lock_
; // Protects the state below.
90 ConnectionType connection_type_
;
91 double connection_max_bandwidth_
;
93 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierDelegateAndroid
);
98 #endif // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_DELEGATE_H_