- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / metrics / metrics_network_observer.cc
1 // Copyright (c) 2013 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 #include "chrome/browser/metrics/metrics_network_observer.h"
6
7 #include "base/compiler_specific.h"
8 #include "base/task_runner_util.h"
9 #include "base/threading/sequenced_worker_pool.h"
10 #include "content/public/browser/browser_thread.h"
11
12
13 MetricsNetworkObserver::MetricsNetworkObserver()
14     : weak_ptr_factory_(this),
15       connection_type_is_ambiguous_(false),
16       wifi_phy_layer_protocol_is_ambiguous_(false),
17       wifi_phy_layer_protocol_(net::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN) {
18   net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
19   connection_type_ = net::NetworkChangeNotifier::GetConnectionType();
20   ProbeWifiPHYLayerProtocol();
21 }
22
23 MetricsNetworkObserver::~MetricsNetworkObserver() {
24   net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
25 }
26
27 void MetricsNetworkObserver::Reset() {
28   connection_type_is_ambiguous_ = false;
29   connection_type_ = net::NetworkChangeNotifier::GetConnectionType();
30   wifi_phy_layer_protocol_is_ambiguous_ = false;
31 }
32
33 void MetricsNetworkObserver::OnConnectionTypeChanged(
34     net::NetworkChangeNotifier::ConnectionType type) {
35   if (type == net::NetworkChangeNotifier::CONNECTION_NONE)
36     return;
37   if (type != connection_type_ &&
38       connection_type_ != net::NetworkChangeNotifier::CONNECTION_NONE) {
39     connection_type_is_ambiguous_ = true;
40   }
41   connection_type_ = type;
42
43   ProbeWifiPHYLayerProtocol();
44 }
45
46 SystemProfileProto::Network::ConnectionType
47 MetricsNetworkObserver::connection_type() const {
48   switch (connection_type_) {
49     case net::NetworkChangeNotifier::CONNECTION_NONE:
50     case net::NetworkChangeNotifier::CONNECTION_UNKNOWN:
51       return SystemProfileProto::Network::CONNECTION_UNKNOWN;
52     case net::NetworkChangeNotifier::CONNECTION_ETHERNET:
53       return SystemProfileProto::Network::CONNECTION_ETHERNET;
54     case net::NetworkChangeNotifier::CONNECTION_WIFI:
55       return SystemProfileProto::Network::CONNECTION_WIFI;
56     case net::NetworkChangeNotifier::CONNECTION_2G:
57       return SystemProfileProto::Network::CONNECTION_2G;
58     case net::NetworkChangeNotifier::CONNECTION_3G:
59       return SystemProfileProto::Network::CONNECTION_3G;
60     case net::NetworkChangeNotifier::CONNECTION_4G:
61       return SystemProfileProto::Network::CONNECTION_4G;
62   }
63   NOTREACHED();
64   return SystemProfileProto::Network::CONNECTION_UNKNOWN;
65 }
66
67 SystemProfileProto::Network::WifiPHYLayerProtocol
68 MetricsNetworkObserver::wifi_phy_layer_protocol() const {
69   switch (wifi_phy_layer_protocol_) {
70     case net::WIFI_PHY_LAYER_PROTOCOL_NONE:
71       return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_NONE;
72     case net::WIFI_PHY_LAYER_PROTOCOL_ANCIENT:
73       return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_ANCIENT;
74     case net::WIFI_PHY_LAYER_PROTOCOL_A:
75       return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_A;
76     case net::WIFI_PHY_LAYER_PROTOCOL_B:
77       return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_B;
78     case net::WIFI_PHY_LAYER_PROTOCOL_G:
79       return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_G;
80     case net::WIFI_PHY_LAYER_PROTOCOL_N:
81       return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_N;
82     case net::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN:
83       return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN;
84   }
85   NOTREACHED();
86   return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN;
87 }
88
89 void MetricsNetworkObserver::ProbeWifiPHYLayerProtocol() {
90   PostTaskAndReplyWithResult(
91       content::BrowserThread::GetBlockingPool(),
92       FROM_HERE,
93       base::Bind(&net::GetWifiPHYLayerProtocol),
94       base::Bind(&MetricsNetworkObserver::OnWifiPHYLayerProtocolResult,
95                  weak_ptr_factory_.GetWeakPtr()));
96 }
97
98 void MetricsNetworkObserver::OnWifiPHYLayerProtocolResult(
99     net::WifiPHYLayerProtocol mode) {
100   if (wifi_phy_layer_protocol_ != net::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN &&
101       mode != wifi_phy_layer_protocol_) {
102     wifi_phy_layer_protocol_is_ambiguous_ = true;
103   }
104   wifi_phy_layer_protocol_ = mode;
105 }
106