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"
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;
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
,
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 // Initializes JNI bindings.
59 static bool Register(JNIEnv
* env
);
62 friend class BaseNetworkChangeNotifierAndroidTest
;
64 void SetCurrentConnectionType(ConnectionType connection_type
);
66 // Methods calling the Java side exposed for testing.
70 base::ThreadChecker thread_checker_
;
71 scoped_refptr
<ObserverListThreadSafe
<Observer
> > observers_
;
72 scoped_refptr
<base::SingleThreadTaskRunner
> jni_task_runner_
;
73 base::android::ScopedJavaGlobalRef
<jobject
> java_network_change_notifier_
;
74 mutable base::Lock connection_type_lock_
; // Protects the state below.
75 ConnectionType connection_type_
;
77 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierDelegateAndroid
);
82 #endif // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_DELEGATE_H_