1 // Copyright 2014 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.
5 #ifndef COMPONENTS_METRICS_NET_NETWORK_METRICS_PROVIDER_H_
6 #define COMPONENTS_METRICS_NET_NETWORK_METRICS_PROVIDER_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "components/metrics/metrics_provider.h"
12 #include "components/metrics/net/wifi_access_point_info_provider.h"
13 #include "components/metrics/proto/system_profile.pb.h"
14 #include "net/base/net_util.h"
15 #include "net/base/network_change_notifier.h"
19 // Registers as observer with net::NetworkChangeNotifier and keeps track of
20 // the network environment.
21 class NetworkMetricsProvider
22 : public MetricsProvider
,
23 public net::NetworkChangeNotifier::ConnectionTypeObserver
{
25 // Creates a NetworkMetricsProvider, where |io_task_runner| is used to post
26 // network info collection tasks.
27 explicit NetworkMetricsProvider(base::TaskRunner
* io_task_runner
);
28 ~NetworkMetricsProvider() override
;
30 // Returns callback function bound to the weak pointer of the provider, which
31 // can be used to get whether current connection type is cellular.
32 base::Callback
<void(bool*)> GetConnectionCallback();
36 void OnDidCreateMetricsLog() override
;
37 void ProvideSystemProfileMetrics(SystemProfileProto
* system_profile
) override
;
39 // ConnectionTypeObserver:
40 void OnConnectionTypeChanged(
41 net::NetworkChangeNotifier::ConnectionType type
) override
;
43 SystemProfileProto::Network::ConnectionType
GetConnectionType() const;
44 SystemProfileProto::Network::WifiPHYLayerProtocol
GetWifiPHYLayerProtocol()
47 // Posts a call to net::GetWifiPHYLayerProtocol on the blocking pool.
48 void ProbeWifiPHYLayerProtocol();
49 // Callback from the blocking pool with the result of
50 // net::GetWifiPHYLayerProtocol.
51 void OnWifiPHYLayerProtocolResult(net::WifiPHYLayerProtocol mode
);
53 // Writes info about the wireless access points that this system is
55 void WriteWifiAccessPointProto(
56 const WifiAccessPointInfoProvider::WifiAccessPointInfo
& info
,
57 SystemProfileProto::Network
* network_proto
);
59 // Returns true if the connection type is 2G, 3G, or 4G.
60 bool IsCellularConnection();
62 // Assigns the passed |is_cellular_out| parameter based on whether current
63 // network connection is cellular.
64 void GetIsCellularConnection(bool* is_cellular_out
);
66 // Task runner used for blocking file I/O.
67 base::TaskRunner
* io_task_runner_
;
69 // True if |connection_type_| changed during the lifetime of the log.
70 bool connection_type_is_ambiguous_
;
71 // The connection type according to net::NetworkChangeNotifier.
72 net::NetworkChangeNotifier::ConnectionType connection_type_
;
74 // True if |wifi_phy_layer_protocol_| changed during the lifetime of the log.
75 bool wifi_phy_layer_protocol_is_ambiguous_
;
76 // The PHY mode of the currently associated access point obtained via
77 // net::GetWifiPHYLayerProtocol.
78 net::WifiPHYLayerProtocol wifi_phy_layer_protocol_
;
80 // Helper object for retrieving connected wifi access point information.
81 scoped_ptr
<WifiAccessPointInfoProvider
> wifi_access_point_info_provider_
;
83 base::WeakPtrFactory
<NetworkMetricsProvider
> weak_ptr_factory_
;
85 DISALLOW_COPY_AND_ASSIGN(NetworkMetricsProvider
);
88 } // namespace metrics
90 #endif // COMPONENTS_METRICS_NET_NETWORK_METRICS_PROVIDER_H_