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 15 matching lines...) Expand all Loading... | |
26 network_connect_(network_connect), | 26 network_connect_(network_connect), |
27 timer_(base::MakeUnique<base::OneShotTimer>()), | 27 timer_(base::MakeUnique<base::OneShotTimer>()), |
28 weak_ptr_factory_(this) { | 28 weak_ptr_factory_(this) { |
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( |
Kyle Horimoto
2017/05/01 17:09:26
You forgot to add the disassociation here when nec
| |
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 // call the callback, passing an empty string to signal that the connection |
48 // did not complete successfully. | 49 // did not complete successfully. |
49 InvokeWifiConnectionCallback(std::string()); | 50 InvokeWifiConnectionCallback(std::string()); |
50 } | 51 } |
51 | 52 |
52 ssid_ = ssid; | 53 ssid_ = ssid; |
53 password_ = password; | 54 password_ = password; |
54 wifi_guid_ = base::GenerateGUID(); | 55 tether_network_guid_ = tether_network_guid; |
56 wifi_network_guid_ = base::GenerateGUID(); | |
55 callback_ = callback; | 57 callback_ = callback; |
56 timer_->Start(FROM_HERE, | 58 timer_->Start(FROM_HERE, |
57 base::TimeDelta::FromSeconds(kConnectionTimeoutSeconds), | 59 base::TimeDelta::FromSeconds(kConnectionTimeoutSeconds), |
58 base::Bind(&WifiHotspotConnector::OnConnectionTimeout, | 60 base::Bind(&WifiHotspotConnector::OnConnectionTimeout, |
59 weak_ptr_factory_.GetWeakPtr())); | 61 weak_ptr_factory_.GetWeakPtr())); |
60 | 62 |
61 base::DictionaryValue properties = | 63 base::DictionaryValue properties = |
62 CreateWifiPropertyDictionary(ssid, password); | 64 CreateWifiPropertyDictionary(ssid, password); |
63 network_connect_->CreateConfiguration(&properties, false /* shared */); | 65 network_connect_->CreateConfiguration(&properties, false /* shared */); |
64 } | 66 } |
65 | 67 |
66 void WifiHotspotConnector::NetworkPropertiesUpdated( | 68 void WifiHotspotConnector::NetworkPropertiesUpdated( |
67 const NetworkState* network) { | 69 const NetworkState* network) { |
68 if (network->guid() != wifi_guid_) { | 70 if (network->guid() != wifi_network_guid_) { |
69 // If a different network has been connected, return early and wait for the | 71 // If a different network has been connected, return early and wait for the |
70 // network with ID |wifi_guid_| is updated. | 72 // network with ID |wifi_network_guid_| is updated. |
71 return; | 73 return; |
72 } | 74 } |
73 | 75 |
74 if (network->IsConnectedState()) { | 76 if (network->IsConnectedState()) { |
75 // If a connection occurred, notify observers and exit early. | 77 // If a connection occurred, notify observers and exit early. |
76 InvokeWifiConnectionCallback(wifi_guid_); | 78 InvokeWifiConnectionCallback(wifi_network_guid_); |
77 return; | 79 return; |
78 } | 80 } |
79 | 81 |
80 if (network->connectable()) { | 82 if (network->connectable()) { |
81 // If the network is now connectable, initiate a connection to it. | 83 bool successful_association = |
82 network_connect_->ConnectToNetworkId(wifi_guid_); | 84 network_state_handler_->AssociateTetherNetworkStateWithWifiNetwork( |
85 tether_network_guid_, wifi_network_guid_); | |
86 if (successful_association) { | |
87 PA_LOG(INFO) << "Wifi network with ID " << wifi_network_guid_ | |
88 << " is connectable, and successfully associated " | |
89 "with Tether network. Tether network ID: \"" | |
90 << tether_network_guid_ << "\", Wi-Fi network ID: \"" | |
91 << wifi_network_guid_ << "\""; | |
92 } else { | |
93 PA_LOG(INFO) << "Wifi network with ID " << wifi_network_guid_ | |
94 << " is connectable, but failed to associate tether network " | |
95 "with ID \"" | |
96 << tether_network_guid_ << "\" to Wi-Fi network with ID: \"" | |
97 << wifi_network_guid_ << "\""; | |
98 } | |
99 | |
100 // If the network is now connectable, associate it with a Tether network | |
101 // ASAP so that the correct icon will be displayed in the tray while the | |
102 // network is connecting. | |
103 network_connect_->ConnectToNetworkId(wifi_network_guid_); | |
83 } | 104 } |
84 } | 105 } |
85 | 106 |
86 void WifiHotspotConnector::InvokeWifiConnectionCallback( | 107 void WifiHotspotConnector::InvokeWifiConnectionCallback( |
87 const std::string& wifi_guid) { | 108 const std::string& wifi_guid) { |
88 DCHECK(!callback_.is_null()); | 109 DCHECK(!callback_.is_null()); |
89 | 110 |
90 // |wifi_guid| may be a reference to |wifi_guid_|, so make a copy of it first | 111 // |wifi_guid| may be a reference to |wifi_network_guid_|, so make a copy of |
91 // before clearing it below. | 112 // it first before clearing it below. |
92 std::string wifi_guid_copy = wifi_guid; | 113 std::string wifi_network_guid_copy = wifi_guid; |
93 | 114 |
94 ssid_.clear(); | 115 ssid_.clear(); |
95 password_.clear(); | 116 password_.clear(); |
96 wifi_guid_.clear(); | 117 wifi_network_guid_.clear(); |
97 | 118 |
98 timer_->Stop(); | 119 timer_->Stop(); |
99 | 120 |
100 callback_.Run(wifi_guid_copy); | 121 callback_.Run(wifi_network_guid_copy); |
101 callback_.Reset(); | 122 callback_.Reset(); |
102 } | 123 } |
103 | 124 |
104 base::DictionaryValue WifiHotspotConnector::CreateWifiPropertyDictionary( | 125 base::DictionaryValue WifiHotspotConnector::CreateWifiPropertyDictionary( |
105 const std::string& ssid, | 126 const std::string& ssid, |
106 const std::string& password) { | 127 const std::string& password) { |
107 PA_LOG(INFO) << "Creating network configuration. " | 128 PA_LOG(INFO) << "Creating network configuration. " |
108 << "SSID: " << ssid << ", " | 129 << "SSID: " << ssid << ", " |
109 << "Password: " << password << ", " | 130 << "Password: " << password << ", " |
110 << "Wi-Fi network GUID: " << wifi_guid_; | 131 << "Wi-Fi network GUID: " << wifi_network_guid_; |
111 | 132 |
112 base::DictionaryValue properties; | 133 base::DictionaryValue properties; |
113 | 134 |
114 shill_property_util::SetSSID(ssid, &properties); | 135 shill_property_util::SetSSID(ssid, &properties); |
115 properties.SetStringWithoutPathExpansion(shill::kGuidProperty, wifi_guid_); | 136 properties.SetStringWithoutPathExpansion(shill::kGuidProperty, |
137 wifi_network_guid_); | |
116 properties.SetBooleanWithoutPathExpansion(shill::kAutoConnectProperty, false); | 138 properties.SetBooleanWithoutPathExpansion(shill::kAutoConnectProperty, false); |
117 properties.SetStringWithoutPathExpansion(shill::kTypeProperty, | 139 properties.SetStringWithoutPathExpansion(shill::kTypeProperty, |
118 shill::kTypeWifi); | 140 shill::kTypeWifi); |
119 properties.SetBooleanWithoutPathExpansion(shill::kSaveCredentialsProperty, | 141 properties.SetBooleanWithoutPathExpansion(shill::kSaveCredentialsProperty, |
120 true); | 142 true); |
121 | 143 |
122 if (password.empty()) { | 144 if (password.empty()) { |
123 properties.SetStringWithoutPathExpansion(shill::kSecurityClassProperty, | 145 properties.SetStringWithoutPathExpansion(shill::kSecurityClassProperty, |
124 shill::kSecurityNone); | 146 shill::kSecurityNone); |
125 } else { | 147 } else { |
(...skipping 10 matching lines...) Expand all Loading... | |
136 InvokeWifiConnectionCallback(std::string()); | 158 InvokeWifiConnectionCallback(std::string()); |
137 } | 159 } |
138 | 160 |
139 void WifiHotspotConnector::SetTimerForTest(std::unique_ptr<base::Timer> timer) { | 161 void WifiHotspotConnector::SetTimerForTest(std::unique_ptr<base::Timer> timer) { |
140 timer_ = std::move(timer); | 162 timer_ = std::move(timer); |
141 } | 163 } |
142 | 164 |
143 } // namespace tether | 165 } // namespace tether |
144 | 166 |
145 } // namespace chromeos | 167 } // namespace chromeos |
OLD | NEW |