OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/components/tether/wifi_hotspot_connector.h" | 5 #include "chromeos/components/tether/wifi_hotspot_connector.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/guid.h" | 8 #include "base/guid.h" |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "chromeos/network/network_connect.h" | 10 #include "chromeos/network/network_connect.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 network_state_handler_->AddObserver(this, FROM_HERE); | 29 network_state_handler_->AddObserver(this, FROM_HERE); |
30 } | 30 } |
31 | 31 |
32 WifiHotspotConnector::~WifiHotspotConnector() { | 32 WifiHotspotConnector::~WifiHotspotConnector() { |
33 network_state_handler_->RemoveObserver(this, FROM_HERE); | 33 network_state_handler_->RemoveObserver(this, FROM_HERE); |
34 } | 34 } |
35 | 35 |
36 void WifiHotspotConnector::ConnectToWifiHotspot( | 36 void WifiHotspotConnector::ConnectToWifiHotspot( |
37 const std::string& ssid, | 37 const std::string& ssid, |
38 const std::string& password, | 38 const std::string& password, |
| 39 const std::string& tether_network_guid, |
39 const WifiConnectionCallback& callback) { | 40 const WifiConnectionCallback& callback) { |
40 DCHECK(!ssid.empty()); | 41 DCHECK(!ssid.empty()); |
41 // Note: |password| can be empty in some cases. | 42 // Note: |password| can be empty in some cases. |
42 | 43 |
43 if (!callback_.is_null()) { | 44 if (!callback_.is_null()) { |
44 DCHECK(timer_->IsRunning()); | 45 DCHECK(timer_->IsRunning()); |
45 | 46 |
46 // If another connection attempt was underway but had not yet completed, | 47 // If another connection attempt was underway but had not yet completed, |
47 // call the callback, passing an empty string to signal that the connection | 48 // disassociate that network from the Tether network and call the callback, |
48 // did not complete successfully. | 49 // passing an empty string to signal that the connection did not complete |
| 50 // successfully. |
| 51 bool successful_disassociation = |
| 52 network_state_handler_->DisassociateTetherNetworkStateFromWifiNetwork( |
| 53 tether_network_guid_); |
| 54 if (successful_disassociation) { |
| 55 PA_LOG(INFO) << "Wifi network with ID " << wifi_network_guid_ |
| 56 << " successfully disassociated from Tether network with ID " |
| 57 << tether_network_guid_ << "."; |
| 58 } else { |
| 59 PA_LOG(INFO) << "Wifi network with ID " << wifi_network_guid_ |
| 60 << " failed to disassociate from Tether network with ID " |
| 61 << tether_network_guid_ << "."; |
| 62 } |
| 63 |
49 InvokeWifiConnectionCallback(std::string()); | 64 InvokeWifiConnectionCallback(std::string()); |
50 } | 65 } |
51 | 66 |
52 ssid_ = ssid; | 67 ssid_ = ssid; |
53 password_ = password; | 68 password_ = password; |
54 wifi_guid_ = base::GenerateGUID(); | 69 tether_network_guid_ = tether_network_guid; |
| 70 wifi_network_guid_ = base::GenerateGUID(); |
55 callback_ = callback; | 71 callback_ = callback; |
56 timer_->Start(FROM_HERE, | 72 timer_->Start(FROM_HERE, |
57 base::TimeDelta::FromSeconds(kConnectionTimeoutSeconds), | 73 base::TimeDelta::FromSeconds(kConnectionTimeoutSeconds), |
58 base::Bind(&WifiHotspotConnector::OnConnectionTimeout, | 74 base::Bind(&WifiHotspotConnector::OnConnectionTimeout, |
59 weak_ptr_factory_.GetWeakPtr())); | 75 weak_ptr_factory_.GetWeakPtr())); |
60 | 76 |
61 base::DictionaryValue properties = | 77 base::DictionaryValue properties = |
62 CreateWifiPropertyDictionary(ssid, password); | 78 CreateWifiPropertyDictionary(ssid, password); |
63 network_connect_->CreateConfiguration(&properties, false /* shared */); | 79 network_connect_->CreateConfiguration(&properties, false /* shared */); |
64 } | 80 } |
65 | 81 |
66 void WifiHotspotConnector::NetworkPropertiesUpdated( | 82 void WifiHotspotConnector::NetworkPropertiesUpdated( |
67 const NetworkState* network) { | 83 const NetworkState* network) { |
68 if (network->guid() != wifi_guid_) { | 84 if (network->guid() != wifi_network_guid_) { |
69 // If a different network has been connected, return early and wait for the | 85 // If a different network has been connected, return early and wait for the |
70 // network with ID |wifi_guid_| is updated. | 86 // network with ID |wifi_network_guid_| is updated. |
71 return; | 87 return; |
72 } | 88 } |
73 | 89 |
74 if (network->IsConnectedState()) { | 90 if (network->IsConnectedState()) { |
75 // If a connection occurred, notify observers and exit early. | 91 // If a connection occurred, notify observers and exit early. |
76 InvokeWifiConnectionCallback(wifi_guid_); | 92 InvokeWifiConnectionCallback(wifi_network_guid_); |
77 return; | 93 return; |
78 } | 94 } |
79 | 95 |
80 if (network->connectable()) { | 96 if (network->connectable()) { |
81 // If the network is now connectable, initiate a connection to it. | 97 // If the network is now connectable, associate it with a Tether network |
82 network_connect_->ConnectToNetworkId(wifi_guid_); | 98 // ASAP so that the correct icon will be displayed in the tray while the |
| 99 // network is connecting. |
| 100 bool successful_association = |
| 101 network_state_handler_->AssociateTetherNetworkStateWithWifiNetwork( |
| 102 tether_network_guid_, wifi_network_guid_); |
| 103 if (successful_association) { |
| 104 PA_LOG(INFO) << "Wifi network with ID " << wifi_network_guid_ |
| 105 << " is connectable, and successfully associated " |
| 106 "with Tether network. Tether network ID: \"" |
| 107 << tether_network_guid_ << "\", Wi-Fi network ID: \"" |
| 108 << wifi_network_guid_ << "\""; |
| 109 } else { |
| 110 PA_LOG(INFO) << "Wifi network with ID " << wifi_network_guid_ |
| 111 << " is connectable, but failed to associate tether network " |
| 112 "with ID \"" |
| 113 << tether_network_guid_ << "\" to Wi-Fi network with ID: \"" |
| 114 << wifi_network_guid_ << "\""; |
| 115 } |
| 116 |
| 117 // Initiate a connection to the network. |
| 118 network_connect_->ConnectToNetworkId(wifi_network_guid_); |
83 } | 119 } |
84 } | 120 } |
85 | 121 |
86 void WifiHotspotConnector::InvokeWifiConnectionCallback( | 122 void WifiHotspotConnector::InvokeWifiConnectionCallback( |
87 const std::string& wifi_guid) { | 123 const std::string& wifi_guid) { |
88 DCHECK(!callback_.is_null()); | 124 DCHECK(!callback_.is_null()); |
89 | 125 |
90 // |wifi_guid| may be a reference to |wifi_guid_|, so make a copy of it first | 126 // |wifi_guid| may be a reference to |wifi_network_guid_|, so make a copy of |
91 // before clearing it below. | 127 // it first before clearing it below. |
92 std::string wifi_guid_copy = wifi_guid; | 128 std::string wifi_network_guid_copy = wifi_guid; |
93 | 129 |
94 ssid_.clear(); | 130 ssid_.clear(); |
95 password_.clear(); | 131 password_.clear(); |
96 wifi_guid_.clear(); | 132 wifi_network_guid_.clear(); |
97 | 133 |
98 timer_->Stop(); | 134 timer_->Stop(); |
99 | 135 |
100 callback_.Run(wifi_guid_copy); | 136 callback_.Run(wifi_network_guid_copy); |
101 callback_.Reset(); | 137 callback_.Reset(); |
102 } | 138 } |
103 | 139 |
104 base::DictionaryValue WifiHotspotConnector::CreateWifiPropertyDictionary( | 140 base::DictionaryValue WifiHotspotConnector::CreateWifiPropertyDictionary( |
105 const std::string& ssid, | 141 const std::string& ssid, |
106 const std::string& password) { | 142 const std::string& password) { |
107 PA_LOG(INFO) << "Creating network configuration. " | 143 PA_LOG(INFO) << "Creating network configuration. " |
108 << "SSID: " << ssid << ", " | 144 << "SSID: " << ssid << ", " |
109 << "Password: " << password << ", " | 145 << "Password: " << password << ", " |
110 << "Wi-Fi network GUID: " << wifi_guid_; | 146 << "Wi-Fi network GUID: " << wifi_network_guid_; |
111 | 147 |
112 base::DictionaryValue properties; | 148 base::DictionaryValue properties; |
113 | 149 |
114 shill_property_util::SetSSID(ssid, &properties); | 150 shill_property_util::SetSSID(ssid, &properties); |
115 properties.SetStringWithoutPathExpansion(shill::kGuidProperty, wifi_guid_); | 151 properties.SetStringWithoutPathExpansion(shill::kGuidProperty, |
| 152 wifi_network_guid_); |
116 properties.SetBooleanWithoutPathExpansion(shill::kAutoConnectProperty, false); | 153 properties.SetBooleanWithoutPathExpansion(shill::kAutoConnectProperty, false); |
117 properties.SetStringWithoutPathExpansion(shill::kTypeProperty, | 154 properties.SetStringWithoutPathExpansion(shill::kTypeProperty, |
118 shill::kTypeWifi); | 155 shill::kTypeWifi); |
119 properties.SetBooleanWithoutPathExpansion(shill::kSaveCredentialsProperty, | 156 properties.SetBooleanWithoutPathExpansion(shill::kSaveCredentialsProperty, |
120 true); | 157 true); |
121 | 158 |
122 if (password.empty()) { | 159 if (password.empty()) { |
123 properties.SetStringWithoutPathExpansion(shill::kSecurityClassProperty, | 160 properties.SetStringWithoutPathExpansion(shill::kSecurityClassProperty, |
124 shill::kSecurityNone); | 161 shill::kSecurityNone); |
125 } else { | 162 } else { |
(...skipping 10 matching lines...) Expand all Loading... |
136 InvokeWifiConnectionCallback(std::string()); | 173 InvokeWifiConnectionCallback(std::string()); |
137 } | 174 } |
138 | 175 |
139 void WifiHotspotConnector::SetTimerForTest(std::unique_ptr<base::Timer> timer) { | 176 void WifiHotspotConnector::SetTimerForTest(std::unique_ptr<base::Timer> timer) { |
140 timer_ = std::move(timer); | 177 timer_ = std::move(timer); |
141 } | 178 } |
142 | 179 |
143 } // namespace tether | 180 } // namespace tether |
144 | 181 |
145 } // namespace chromeos | 182 } // namespace chromeos |
OLD | NEW |