blob: e82b852f0fb79ab0b8f521d8272f914f458bba5c [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00008#include "base/android/jni_android.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +00009#include "base/basictypes.h"
10#include "base/compiler_specific.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000011#include "net/android/network_change_notifier_delegate_android.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000012#include "net/base/network_change_notifier.h"
13
14namespace net {
15
16class NetworkChangeNotifierAndroidTest;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000017class NetworkChangeNotifierFactoryAndroid;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000018
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000019// 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 {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000044 public:
45 virtual ~NetworkChangeNotifierAndroid();
46
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000047 // NetworkChangeNotifier:
48 virtual ConnectionType GetCurrentConnectionType() const OVERRIDE;
49
50 // NetworkChangeNotifierDelegateAndroid::Observer:
51 virtual void OnConnectionTypeChanged() OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000052
53 static bool Register(JNIEnv* env);
54
55 private:
56 friend class NetworkChangeNotifierAndroidTest;
57 friend class NetworkChangeNotifierFactoryAndroid;
58
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000059 class DnsConfigServiceThread;
60
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000061 explicit NetworkChangeNotifierAndroid(
62 NetworkChangeNotifierDelegateAndroid* delegate);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000063
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000064 static NetworkChangeCalculatorParams NetworkChangeCalculatorParamsAndroid();
Torne (Richard Coles)58218062012-11-14 11:43:16 +000065
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000066 NetworkChangeNotifierDelegateAndroid* const delegate_;
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000067 scoped_ptr<DnsConfigServiceThread> dns_config_service_thread_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000068
69 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierAndroid);
70};
71
72} // namespace net
73
74#endif // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_ANDROID_H_