Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / net / android / network_change_notifier_delegate_android.h
blobe1f874ab267d30c7442205f7e3d83aa53833369c
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"
16 namespace net {
18 // Delegate used to thread-safely notify NetworkChangeNotifierAndroid whenever a
19 // network connection change notification is signaled by the Java side (on the
20 // JNI thread).
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 {
24 public:
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).
30 class Observer {
31 public:
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,
39 ConnectionType connection_type) = 0;
42 NetworkChangeNotifierDelegateAndroid();
43 ~NetworkChangeNotifierDelegateAndroid();
45 // Called from NetworkChangeNotifierAndroid.java on the JNI thread whenever
46 // the connection type changes. This updates the current connection type seen
47 // by this class and forwards the notification to the observers that
48 // subscribed through AddObserver().
49 void NotifyConnectionTypeChanged(JNIEnv* env,
50 jobject obj,
51 jint new_connection_type);
52 jint GetConnectionType(JNIEnv* env, jobject obj) const;
54 // Called from NetworkChangeNotifierAndroid.java on the JNI thread whenever
55 // the maximum bandwidth of the connection changes. This updates the current
56 // max bandwidth seen by this class and forwards the notification to the
57 // observers that subscribed through AddObserver().
58 void NotifyMaxBandwidthChanged(JNIEnv* env,
59 jobject obj,
60 jdouble new_max_bandwidth);
62 // These methods can be called on any thread. Note that the provided observer
63 // will be notified on the thread AddObserver() is called on.
64 void AddObserver(Observer* observer);
65 void RemoveObserver(Observer* observer);
67 // Can be called from any thread.
68 ConnectionType GetCurrentConnectionType() const;
70 // Can be called from any thread.
71 void GetCurrentMaxBandwidthAndConnectionType(
72 double* max_bandwidth_mbps,
73 ConnectionType* connection_type) const;
75 // Initializes JNI bindings.
76 static bool Register(JNIEnv* env);
78 private:
79 friend class BaseNetworkChangeNotifierAndroidTest;
81 void SetCurrentConnectionType(ConnectionType connection_type);
82 void SetCurrentMaxBandwidth(double max_bandwidth);
84 // Methods calling the Java side exposed for testing.
85 void SetOnline();
86 void SetOffline();
88 base::ThreadChecker thread_checker_;
89 scoped_refptr<base::ObserverListThreadSafe<Observer>> observers_;
90 scoped_refptr<base::SingleThreadTaskRunner> jni_task_runner_;
91 base::android::ScopedJavaGlobalRef<jobject> java_network_change_notifier_;
92 mutable base::Lock connection_lock_; // Protects the state below.
93 ConnectionType connection_type_;
94 double connection_max_bandwidth_;
96 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierDelegateAndroid);
99 } // namespace net
101 #endif // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_DELEGATE_H_