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/connect_tethering_operation.h" | 5 #include "chromeos/components/tether/connect_tethering_operation.h" |
6 | 6 |
7 #include "chromeos/components/tether/host_scan_device_prioritizer.h" | 7 #include "chromeos/components/tether/host_scan_device_prioritizer.h" |
8 #include "chromeos/components/tether/message_wrapper.h" | 8 #include "chromeos/components/tether/message_wrapper.h" |
9 #include "chromeos/components/tether/proto/tether.pb.h" | 9 #include "chromeos/components/tether/proto/tether.pb.h" |
10 #include "components/proximity_auth/logging/logging.h" | 10 #include "components/proximity_auth/logging/logging.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 device_to_connect, connection_manager, host_scan_device_prioritizer); | 45 device_to_connect, connection_manager, host_scan_device_prioritizer); |
46 } | 46 } |
47 | 47 |
48 ConnectTetheringOperation::ConnectTetheringOperation( | 48 ConnectTetheringOperation::ConnectTetheringOperation( |
49 const cryptauth::RemoteDevice& device_to_connect, | 49 const cryptauth::RemoteDevice& device_to_connect, |
50 BleConnectionManager* connection_manager, | 50 BleConnectionManager* connection_manager, |
51 HostScanDevicePrioritizer* host_scan_device_prioritizer) | 51 HostScanDevicePrioritizer* host_scan_device_prioritizer) |
52 : MessageTransferOperation( | 52 : MessageTransferOperation( |
53 std::vector<cryptauth::RemoteDevice>{device_to_connect}, | 53 std::vector<cryptauth::RemoteDevice>{device_to_connect}, |
54 connection_manager), | 54 connection_manager), |
| 55 remote_device_(device_to_connect), |
55 host_scan_device_prioritizer_(host_scan_device_prioritizer), | 56 host_scan_device_prioritizer_(host_scan_device_prioritizer), |
56 has_authenticated_(false) {} | 57 error_code_to_return_( |
| 58 ConnectTetheringResponse_ResponseCode:: |
| 59 ConnectTetheringResponse_ResponseCode_UNKNOWN_ERROR) {} |
57 | 60 |
58 ConnectTetheringOperation::~ConnectTetheringOperation() {} | 61 ConnectTetheringOperation::~ConnectTetheringOperation() {} |
59 | 62 |
60 void ConnectTetheringOperation::AddObserver(Observer* observer) { | 63 void ConnectTetheringOperation::AddObserver(Observer* observer) { |
61 observer_list_.AddObserver(observer); | 64 observer_list_.AddObserver(observer); |
62 } | 65 } |
63 | 66 |
64 void ConnectTetheringOperation::RemoveObserver(Observer* observer) { | 67 void ConnectTetheringOperation::RemoveObserver(Observer* observer) { |
65 observer_list_.RemoveObserver(observer); | 68 observer_list_.RemoveObserver(observer); |
66 } | 69 } |
67 | 70 |
68 void ConnectTetheringOperation::OnDeviceAuthenticated( | 71 void ConnectTetheringOperation::OnDeviceAuthenticated( |
69 const cryptauth::RemoteDevice& remote_device) { | 72 const cryptauth::RemoteDevice& remote_device) { |
70 DCHECK(remote_devices().size() == 1u && remote_devices()[0] == remote_device); | 73 DCHECK(remote_devices().size() == 1u && remote_devices()[0] == remote_device); |
71 has_authenticated_ = true; | |
72 | |
73 SendMessageToDevice(remote_device, base::MakeUnique<MessageWrapper>( | 74 SendMessageToDevice(remote_device, base::MakeUnique<MessageWrapper>( |
74 ConnectTetheringRequest())); | 75 ConnectTetheringRequest())); |
75 } | 76 } |
76 | 77 |
77 void ConnectTetheringOperation::OnMessageReceived( | 78 void ConnectTetheringOperation::OnMessageReceived( |
78 std::unique_ptr<MessageWrapper> message_wrapper, | 79 std::unique_ptr<MessageWrapper> message_wrapper, |
79 const cryptauth::RemoteDevice& remote_device) { | 80 const cryptauth::RemoteDevice& remote_device) { |
80 if (message_wrapper->GetMessageType() != | 81 if (message_wrapper->GetMessageType() != |
81 MessageType::CONNECT_TETHERING_RESPONSE) { | 82 MessageType::CONNECT_TETHERING_RESPONSE) { |
82 // If another type of message has been received, ignore it. | 83 // If another type of message has been received, ignore it. |
83 return; | 84 return; |
84 } | 85 } |
85 | 86 |
| 87 if (!(remote_device == remote_device_)) { |
| 88 // If the message came from another device, ignore it. |
| 89 return; |
| 90 } |
| 91 |
86 ConnectTetheringResponse* response = | 92 ConnectTetheringResponse* response = |
87 static_cast<ConnectTetheringResponse*>(message_wrapper->GetProto().get()); | 93 static_cast<ConnectTetheringResponse*>(message_wrapper->GetProto().get()); |
88 if (response->response_code() == | 94 if (response->response_code() == |
89 ConnectTetheringResponse_ResponseCode:: | 95 ConnectTetheringResponse_ResponseCode:: |
90 ConnectTetheringResponse_ResponseCode_SUCCESS) { | 96 ConnectTetheringResponse_ResponseCode_SUCCESS) { |
91 if (response->has_ssid() && response->has_password()) { | 97 if (response->has_ssid() && response->has_password()) { |
92 PA_LOG(INFO) << "Received ConnectTetheringResponse from device with ID " | 98 PA_LOG(INFO) << "Received ConnectTetheringResponse from device with ID " |
93 << remote_device.GetTruncatedDeviceIdForLogs() << " and " | 99 << remote_device.GetTruncatedDeviceIdForLogs() << " and " |
94 << "response_code == SUCCESS. Config: {ssid: \"" | 100 << "response_code == SUCCESS. Config: {ssid: \"" |
95 << response->ssid() << "\", password: \"" | 101 << response->ssid() << "\", password: \"" |
96 << response->password() << "\"}"; | 102 << response->password() << "\"}"; |
97 | 103 |
98 host_scan_device_prioritizer_->RecordSuccessfulConnectTetheringResponse( | 104 host_scan_device_prioritizer_->RecordSuccessfulConnectTetheringResponse( |
99 remote_device); | 105 remote_device); |
100 | 106 |
101 NotifyObserversOfSuccessfulResponse(response->ssid(), | 107 ssid_to_return_ = response->ssid(); |
102 response->password()); | 108 password_to_return_ = response->password(); |
103 } else { | 109 } else { |
104 PA_LOG(ERROR) << "Received ConnectTetheringResponse from device with ID " | 110 PA_LOG(ERROR) << "Received ConnectTetheringResponse from device with ID " |
105 << remote_device.GetTruncatedDeviceIdForLogs() << " and " | 111 << remote_device.GetTruncatedDeviceIdForLogs() << " and " |
106 << "response_code == SUCCESS, but the response did not " | 112 << "response_code == SUCCESS, but the response did not " |
107 << "contain a Wi-Fi SSID and/or password."; | 113 << "contain a Wi-Fi SSID and/or password."; |
108 NotifyObserversOfConnectionFailure( | |
109 ConnectTetheringResponse_ResponseCode:: | |
110 ConnectTetheringResponse_ResponseCode_UNKNOWN_ERROR); | |
111 } | 114 } |
112 } else { | 115 } else { |
113 PA_LOG(INFO) << "Received ConnectTetheringResponse from device with ID " | 116 PA_LOG(INFO) << "Received ConnectTetheringResponse from device with ID " |
114 << remote_device.GetTruncatedDeviceIdForLogs() << " and " | 117 << remote_device.GetTruncatedDeviceIdForLogs() << " and " |
115 << "response_code == " << response->response_code() << "."; | 118 << "response_code == " << response->response_code() << "."; |
116 NotifyObserversOfConnectionFailure(response->response_code()); | 119 error_code_to_return_ = response->response_code(); |
117 } | 120 } |
118 | 121 |
119 // Now that a response has been received, the device can be unregistered. | 122 // Now that a response has been received, the device can be unregistered. |
120 UnregisterDevice(remote_device); | 123 UnregisterDevice(remote_device); |
121 } | 124 } |
122 | 125 |
123 void ConnectTetheringOperation::OnOperationFinished() { | 126 void ConnectTetheringOperation::OnOperationFinished() { |
124 if (!has_authenticated_) { | 127 // Notify observers of the results of this operation in OnOperationFinished() |
125 // If the operation finished but the device never authenticated, there was | 128 // instead of in OnMessageReceived() because observers may delete this |
126 // some sort of problem connecting to the device. In this case, notify | 129 // ConnectTetheringOperation instance. If this happens, the UnregisterDevice() |
127 // observers of a failure. | 130 // call in OnMessageReceived() will cause a crash. |
128 NotifyObserversOfConnectionFailure( | 131 |
129 ConnectTetheringResponse_ResponseCode:: | 132 if (!ssid_to_return_.empty()) { |
130 ConnectTetheringResponse_ResponseCode_UNKNOWN_ERROR); | 133 NotifyObserversOfSuccessfulResponse(ssid_to_return_, password_to_return_); |
| 134 } else { |
| 135 // At this point, either the operation finished with a failed response or |
| 136 // no connection succeeded at all. In these cases, notify observers of a |
| 137 // failure. |
| 138 NotifyObserversOfConnectionFailure(error_code_to_return_); |
131 } | 139 } |
132 } | 140 } |
133 | 141 |
134 MessageType ConnectTetheringOperation::GetMessageTypeForConnection() { | 142 MessageType ConnectTetheringOperation::GetMessageTypeForConnection() { |
135 return MessageType::CONNECT_TETHERING_REQUEST; | 143 return MessageType::CONNECT_TETHERING_REQUEST; |
136 } | 144 } |
137 | 145 |
138 void ConnectTetheringOperation::NotifyObserversOfSuccessfulResponse( | 146 void ConnectTetheringOperation::NotifyObserversOfSuccessfulResponse( |
139 const std::string& ssid, | 147 const std::string& ssid, |
140 const std::string& password) { | 148 const std::string& password) { |
141 for (auto& observer : observer_list_) { | 149 for (auto& observer : observer_list_) { |
142 observer.OnSuccessfulConnectTetheringResponse(ssid, password); | 150 observer.OnSuccessfulConnectTetheringResponse(remote_device_, ssid, |
| 151 password); |
143 } | 152 } |
144 } | 153 } |
145 | 154 |
146 void ConnectTetheringOperation::NotifyObserversOfConnectionFailure( | 155 void ConnectTetheringOperation::NotifyObserversOfConnectionFailure( |
147 ConnectTetheringResponse_ResponseCode error_code) { | 156 ConnectTetheringResponse_ResponseCode error_code) { |
148 for (auto& observer : observer_list_) { | 157 for (auto& observer : observer_list_) { |
149 observer.OnConnectTetheringFailure(error_code); | 158 observer.OnConnectTetheringFailure(remote_device_, error_code); |
150 } | 159 } |
151 } | 160 } |
152 | 161 |
153 } // namespace tether | 162 } // namespace tether |
154 | 163 |
155 } // namespace chromeos | 164 } // namespace chromeos |
OLD | NEW |