Put screenshot.py back to work
[chromium-blink-merge.git] / net / android / network_change_notifier_delegate_android.h
blobf56188894f489ef483ed1fea54023113fca4fc52
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/basictypes.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;
38 NetworkChangeNotifierDelegateAndroid();
39 ~NetworkChangeNotifierDelegateAndroid();
41 // Called from NetworkChangeNotifierAndroid.java on the JNI thread whenever
42 // the connection type changes. This updates the current connection type seen
43 // by this class and forwards the notification to the observers that
44 // subscribed through AddObserver().
45 void NotifyConnectionTypeChanged(JNIEnv* env,
46 jobject obj,
47 jint new_connection_type);
48 jint GetConnectionType(JNIEnv* env, jobject obj) const;
50 // These methods can be called on any thread. Note that the provided observer
51 // will be notified on the thread AddObserver() is called on.
52 void AddObserver(Observer* observer);
53 void RemoveObserver(Observer* observer);
55 // Can be called from any thread.
56 ConnectionType GetCurrentConnectionType() const;
58 // Can be called from any thread.
59 double GetCurrentMaxBandwidth() const;
61 // Initializes JNI bindings.
62 static bool Register(JNIEnv* env);
64 private:
65 friend class BaseNetworkChangeNotifierAndroidTest;
67 void SetCurrentConnectionType(ConnectionType connection_type);
68 void SetCurrentMaxBandwidth(double max_bandwidth);
70 // Methods calling the Java side exposed for testing.
71 void SetOnline();
72 void SetOffline();
74 base::ThreadChecker thread_checker_;
75 scoped_refptr<ObserverListThreadSafe<Observer> > observers_;
76 scoped_refptr<base::SingleThreadTaskRunner> jni_task_runner_;
77 base::android::ScopedJavaGlobalRef<jobject> java_network_change_notifier_;
78 mutable base::Lock connection_lock_; // Protects the state below.
79 ConnectionType connection_type_;
80 double connection_max_bandwidth_;
82 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierDelegateAndroid);
85 } // namespace net
87 #endif // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_DELEGATE_H_