Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 8 | #include "base/android/jni_android.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 9 | #include "base/basictypes.h" |
| 10 | #include "base/compiler_specific.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 11 | #include "net/android/network_change_notifier_delegate_android.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 12 | #include "net/base/network_change_notifier.h" |
| 13 | |
| 14 | namespace net { |
| 15 | |
| 16 | class NetworkChangeNotifierAndroidTest; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 17 | class NetworkChangeNotifierFactoryAndroid; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 18 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 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. |
| 41 | class NET_EXPORT_PRIVATE NetworkChangeNotifierAndroid |
| 42 | : public NetworkChangeNotifier, |
| 43 | public NetworkChangeNotifierDelegateAndroid::Observer { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 44 | public: |
| 45 | virtual ~NetworkChangeNotifierAndroid(); |
| 46 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 47 | // NetworkChangeNotifier: |
| 48 | virtual ConnectionType GetCurrentConnectionType() const OVERRIDE; |
| 49 | |
| 50 | // NetworkChangeNotifierDelegateAndroid::Observer: |
| 51 | virtual void OnConnectionTypeChanged() OVERRIDE; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 52 | |
| 53 | static bool Register(JNIEnv* env); |
| 54 | |
| 55 | private: |
| 56 | friend class NetworkChangeNotifierAndroidTest; |
| 57 | friend class NetworkChangeNotifierFactoryAndroid; |
| 58 | |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 59 | class DnsConfigServiceThread; |
| 60 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 61 | explicit NetworkChangeNotifierAndroid( |
| 62 | NetworkChangeNotifierDelegateAndroid* delegate); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 63 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 64 | static NetworkChangeCalculatorParams NetworkChangeCalculatorParamsAndroid(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 65 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 66 | NetworkChangeNotifierDelegateAndroid* const delegate_; |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 67 | scoped_ptr<DnsConfigServiceThread> dns_config_service_thread_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 68 | |
| 69 | DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierAndroid); |
| 70 | }; |
| 71 | |
| 72 | } // namespace net |
| 73 | |
| 74 | #endif // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_ANDROID_H_ |