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_ANDROID_H_
6#define NET_ANDROID_NETWORK_CHANGE_NOTIFIER_ANDROID_H_
7
8#include "base/android/jni_android.h"
9#include "base/basictypes.h"
10#include "base/compiler_specific.h"
11#include "net/android/network_change_notifier_delegate_android.h"
12#include "net/base/network_change_notifier.h"
13
14namespace net {
15
16class NetworkChangeNotifierAndroidTest;
17class NetworkChangeNotifierFactoryAndroid;
18
19// NetworkChangeNotifierAndroid observes network events from the Android
20// notification system and forwards them to observers.
21//
22// The implementation is complicated by the differing lifetime and thread
23// affinity requirements of Android notifications and of NetworkChangeNotifier.
24//
25// High-level overview:
26// NetworkChangeNotifier.java - Receives notifications from Android system, and
27// notifies native code via JNI (on the main application thread).
28// NetworkChangeNotifierDelegateAndroid ('Delegate') - Listens for notifications
29//   sent via JNI on the main application thread, and forwards them to observers
30//   on their threads. Owned by Factory, lives exclusively on main application
31//   thread.
32// NetworkChangeNotifierFactoryAndroid ('Factory') - Creates the Delegate on the
33//   main thread to receive JNI events, and vends Notifiers. Lives exclusively
34//   on main application thread, and outlives all other classes.
35// NetworkChangeNotifierAndroid ('Notifier') - Receives event notifications from
36//   the Delegate. Processes and forwards these events to the
37//   NetworkChangeNotifier observers on their threads. May live on any thread
38//   and be called by any thread.
39//
40// For more details, see the implementation file.
41class NET_EXPORT_PRIVATE NetworkChangeNotifierAndroid
42    : public NetworkChangeNotifier,
43      public NetworkChangeNotifierDelegateAndroid::Observer {
44 public:
45  virtual ~NetworkChangeNotifierAndroid();
46
47  // NetworkChangeNotifier:
48  virtual ConnectionType GetCurrentConnectionType() const OVERRIDE;
49
50  // NetworkChangeNotifierDelegateAndroid::Observer:
51  virtual void OnConnectionTypeChanged() OVERRIDE;
52
53  static bool Register(JNIEnv* env);
54
55 private:
56  friend class NetworkChangeNotifierAndroidTest;
57  friend class NetworkChangeNotifierFactoryAndroid;
58
59  explicit NetworkChangeNotifierAndroid(
60      NetworkChangeNotifierDelegateAndroid* delegate);
61
62  static NetworkChangeCalculatorParams NetworkChangeCalculatorParamsAndroid();
63
64  NetworkChangeNotifierDelegateAndroid* const delegate_;
65
66  DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierAndroid);
67};
68
69}  // namespace net
70
71#endif  // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_ANDROID_H_
72