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_BASE_NETWORK_CHANGE_NOTIFIER_POSIX_H_
6 #define NET_BASE_NETWORK_CHANGE_NOTIFIER_POSIX_H_
7 
8 #include <memory>
9 
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/scoped_refptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/sequence_checker.h"
14 #include "base/synchronization/lock.h"
15 #include "base/threading/thread.h"
16 #include "base/threading/thread_checker.h"
17 #include "net/base/net_export.h"
18 #include "net/base/network_change_notifier.h"
19 
20 namespace net {
21 
22 // A NetworkChangeNotifier that needs to be told about network changes by some
23 // other object. This class can't directly listen for network changes because on
24 // ChromeOS and Android only objects running in the browser process can listen
25 // for network state changes.
26 class NET_EXPORT NetworkChangeNotifierPosix : public NetworkChangeNotifier {
27  public:
28   NetworkChangeNotifierPosix(
29       NetworkChangeNotifier::ConnectionType initial_connection_type,
30       NetworkChangeNotifier::ConnectionSubtype initial_connection_subtype);
31   NetworkChangeNotifierPosix(const NetworkChangeNotifierPosix&) = delete;
32   NetworkChangeNotifierPosix& operator=(const NetworkChangeNotifierPosix&) =
33       delete;
34   ~NetworkChangeNotifierPosix() override;
35 
36   // These methods are used to notify this object that a network property has
37   // changed. These must be called from the thread that owns this object.
38   void OnDNSChanged();
39   void OnIPAddressChanged();
40   void OnConnectionChanged(
41       NetworkChangeNotifier::ConnectionType connection_type);
42   void OnConnectionSubtypeChanged(
43       NetworkChangeNotifier::ConnectionType connection_type,
44       NetworkChangeNotifier::ConnectionSubtype connection_subtype);
45 
46  protected:
47   // NetworkChangeNotifier overrides.
48   NetworkChangeNotifier::ConnectionType GetCurrentConnectionType()
49       const override;
50   void GetCurrentMaxBandwidthAndConnectionType(
51       double* max_bandwidth_mbps,
52       ConnectionType* connection_type) const override;
53 
54  private:
55   friend class NetworkChangeNotifierPosixTest;
56 
57   // For testing purposes, allows specifying a SystemDnsConfigChangeNotifier.
58   // If |system_dns_config_notifier| is nullptr, NetworkChangeNotifier create a
59   // global one.
60   NetworkChangeNotifierPosix(
61       NetworkChangeNotifier::ConnectionType initial_connection_type,
62       NetworkChangeNotifier::ConnectionSubtype initial_connection_subtype,
63       SystemDnsConfigChangeNotifier* system_dns_config_notifier);
64 
65   // Calculates parameters used for network change notifier online/offline
66   // signals.
67   static NetworkChangeNotifier::NetworkChangeCalculatorParams
68   NetworkChangeCalculatorParamsPosix();
69 
70   THREAD_CHECKER(thread_checker_);
71 
72   mutable base::Lock lock_;
73   NetworkChangeNotifier::ConnectionType
74       connection_type_;        // Guarded by |lock_|.
75   double max_bandwidth_mbps_;  // Guarded by |lock_|.
76 };
77 
78 }  // namespace net
79 
80 #endif  // NET_BASE_NETWORK_CHANGE_NOTIFIER_POSIX_H_
81