1 // Copyright 2017 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 SERVICES_NETWORK_NETWORK_CHANGE_MANAGER_H_
6 #define SERVICES_NETWORK_NETWORK_CHANGE_MANAGER_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include "base/component_export.h"
13 #include "base/macros.h"
14 #include "build/build_config.h"
15 #include "mojo/public/cpp/bindings/receiver_set.h"
16 #include "mojo/public/cpp/bindings/remote.h"
17 #include "net/base/network_change_notifier.h"
18 #include "services/network/public/mojom/network_change_manager.mojom.h"
19 
20 namespace network {
21 
22 // Implementation of mojom::NetworkChangeManager. All accesses to this class are
23 // done through mojo on the main thread. This registers itself to receive
24 // broadcasts from net::NetworkChangeNotifier and rebroadcasts the notifications
25 // to mojom::NetworkChangeManagerClients through mojo pipes.
COMPONENT_EXPORT(NETWORK_SERVICE)26 class COMPONENT_EXPORT(NETWORK_SERVICE) NetworkChangeManager
27     : public mojom::NetworkChangeManager,
28       public net::NetworkChangeNotifier::NetworkChangeObserver {
29  public:
30   // If |network_change_notifier| is not null, |this| will take ownership of it.
31   // Otherwise, the global net::NetworkChangeNotifier will be used.
32   explicit NetworkChangeManager(
33       std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier);
34 
35   ~NetworkChangeManager() override;
36 
37   // Binds a NetworkChangeManager receiver to this object. Mojo messages
38   // coming through the associated pipe will be served by this object.
39   void AddReceiver(mojo::PendingReceiver<mojom::NetworkChangeManager> receiver);
40 
41   // mojom::NetworkChangeManager implementation:
42   void RequestNotifications(
43       mojo::PendingRemote<mojom::NetworkChangeManagerClient> client_remote)
44       override;
45 
46 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
47   void OnNetworkChanged(
48       bool dns_changed,
49       bool ip_address_changed,
50       bool connection_type_changed,
51       mojom::ConnectionType new_connection_type,
52       bool connection_subtype_changed,
53       mojom::ConnectionSubtype new_connection_subtype) override;
54 #endif
55 
56   size_t GetNumClientsForTesting() const;
57 
58  private:
59   // Handles connection errors on notification pipes.
60   void NotificationPipeBroken(mojom::NetworkChangeManagerClient* client);
61 
62   // net::NetworkChangeNotifier::NetworkChangeObserver implementation:
63   void OnNetworkChanged(
64       net::NetworkChangeNotifier::ConnectionType type) override;
65 
66   std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier_;
67   mojo::ReceiverSet<mojom::NetworkChangeManager> receivers_;
68   std::vector<mojo::Remote<mojom::NetworkChangeManagerClient>> clients_;
69   mojom::ConnectionType connection_type_;
70 
71   DISALLOW_COPY_AND_ASSIGN(NetworkChangeManager);
72 };
73 
74 }  // namespace network
75 
76 #endif  // SERVICES_NETWORK_NETWORK_CHANGE_MANAGER_H_
77