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.
4
5#ifndef NET_ANDROID_NETWORK_CHANGE_NOTIFIER_DELEGATE_ANDROID_H_
6#define NET_ANDROID_NETWORK_CHANGE_NOTIFIER_DELEGATE_ANDROID_H_
7
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"
15
16namespace net {
17
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()).
23class NET_EXPORT_PRIVATE NetworkChangeNotifierDelegateAndroid {
24 public:
25  typedef NetworkChangeNotifier::ConnectionType ConnectionType;
26
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() {}
33
34    // Updates the current connection type.
35    virtual void OnConnectionTypeChanged() = 0;
36  };
37
38  NetworkChangeNotifierDelegateAndroid();
39  ~NetworkChangeNotifierDelegateAndroid();
40
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;
49
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);
54
55  // Can be called from any thread.
56  ConnectionType GetCurrentConnectionType() const;
57
58  // Initializes JNI bindings.
59  static bool Register(JNIEnv* env);
60
61 private:
62  friend class BaseNetworkChangeNotifierAndroidTest;
63
64  void SetCurrentConnectionType(ConnectionType connection_type);
65
66  // Methods calling the Java side exposed for testing.
67  void SetOnline();
68  void SetOffline();
69
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_;
76
77  DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierDelegateAndroid);
78};
79
80}  // namespace net
81
82#endif  // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_DELEGATE_H_
83