OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "components/proximity_auth/messenger_impl.h" | 5 #include "components/proximity_auth/messenger_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 void MessengerImpl::AddObserver(MessengerObserver* observer) { | 87 void MessengerImpl::AddObserver(MessengerObserver* observer) { |
88 observers_.AddObserver(observer); | 88 observers_.AddObserver(observer); |
89 } | 89 } |
90 | 90 |
91 void MessengerImpl::RemoveObserver(MessengerObserver* observer) { | 91 void MessengerImpl::RemoveObserver(MessengerObserver* observer) { |
92 observers_.RemoveObserver(observer); | 92 observers_.RemoveObserver(observer); |
93 } | 93 } |
94 | 94 |
95 bool MessengerImpl::SupportsSignIn() const { | 95 bool MessengerImpl::SupportsSignIn() const { |
96 return (secure_context_->GetProtocolVersion() == | 96 return (secure_context_->GetProtocolVersion() == |
97 SecureContext::PROTOCOL_VERSION_THREE_ONE); | 97 SecureContext::PROTOCOL_VERSION_THREE_ONE) && |
| 98 connection_->remote_device().bluetooth_type != |
| 99 RemoteDevice::BLUETOOTH_LE; |
98 } | 100 } |
99 | 101 |
100 void MessengerImpl::DispatchUnlockEvent() { | 102 void MessengerImpl::DispatchUnlockEvent() { |
101 base::DictionaryValue message; | 103 base::DictionaryValue message; |
102 message.SetString(kTypeKey, kMessageTypeLocalEvent); | 104 message.SetString(kTypeKey, kMessageTypeLocalEvent); |
103 message.SetString(kNameKey, kUnlockEventName); | 105 message.SetString(kNameKey, kUnlockEventName); |
104 queued_messages_.push_back(PendingMessage(message)); | 106 queued_messages_.push_back(PendingMessage(message)); |
105 ProcessMessageQueue(); | 107 ProcessMessageQueue(); |
106 } | 108 } |
107 | 109 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 ProcessMessageQueue(); | 142 ProcessMessageQueue(); |
141 } | 143 } |
142 | 144 |
143 MessengerImpl::PendingMessage::PendingMessage() {} | 145 MessengerImpl::PendingMessage::PendingMessage() {} |
144 | 146 |
145 MessengerImpl::PendingMessage::PendingMessage( | 147 MessengerImpl::PendingMessage::PendingMessage( |
146 const base::DictionaryValue& message) | 148 const base::DictionaryValue& message) |
147 : json_message(SerializeValueToJson(message)), | 149 : json_message(SerializeValueToJson(message)), |
148 type(GetMessageType(message)) {} | 150 type(GetMessageType(message)) {} |
149 | 151 |
| 152 MessengerImpl::PendingMessage::PendingMessage(const std::string& message) |
| 153 : json_message(message), type("") {} |
| 154 |
150 MessengerImpl::PendingMessage::~PendingMessage() {} | 155 MessengerImpl::PendingMessage::~PendingMessage() {} |
151 | 156 |
152 void MessengerImpl::ProcessMessageQueue() { | 157 void MessengerImpl::ProcessMessageQueue() { |
153 if (pending_message_ || queued_messages_.empty() || | 158 if (pending_message_ || queued_messages_.empty() || |
154 connection_->is_sending_message()) | 159 connection_->is_sending_message()) |
155 return; | 160 return; |
156 | 161 |
157 pending_message_.reset(new PendingMessage(queued_messages_.front())); | 162 pending_message_.reset(new PendingMessage(queued_messages_.front())); |
158 queued_messages_.pop_front(); | 163 queued_messages_.pop_front(); |
159 | 164 |
160 secure_context_->Encode(pending_message_->json_message, | 165 secure_context_->Encode(pending_message_->json_message, |
161 base::Bind(&MessengerImpl::OnMessageEncoded, | 166 base::Bind(&MessengerImpl::OnMessageEncoded, |
162 weak_ptr_factory_.GetWeakPtr())); | 167 weak_ptr_factory_.GetWeakPtr())); |
163 } | 168 } |
164 | 169 |
165 void MessengerImpl::OnMessageEncoded(const std::string& encoded_message) { | 170 void MessengerImpl::OnMessageEncoded(const std::string& encoded_message) { |
166 connection_->SendMessage(make_scoped_ptr(new WireMessage(encoded_message))); | 171 connection_->SendMessage(make_scoped_ptr(new WireMessage(encoded_message))); |
167 } | 172 } |
168 | 173 |
169 void MessengerImpl::OnMessageDecoded(const std::string& decoded_message) { | 174 void MessengerImpl::OnMessageDecoded(const std::string& decoded_message) { |
| 175 // TODO(tengs): Unify the iOS status update protocol with the existing Android |
| 176 // protocol, so we don't have this special case. |
| 177 if (decoded_message == kScreenUnlocked || decoded_message == kScreenLocked) { |
| 178 RemoteStatusUpdate update; |
| 179 update.user_presence = |
| 180 (decoded_message == kScreenUnlocked ? USER_PRESENT : USER_ABSENT); |
| 181 update.secure_screen_lock_state = SECURE_SCREEN_LOCK_ENABLED; |
| 182 update.trust_agent_state = TRUST_AGENT_ENABLED; |
| 183 FOR_EACH_OBSERVER(MessengerObserver, observers_, |
| 184 OnRemoteStatusUpdate(update)); |
| 185 pending_message_.reset(); |
| 186 ProcessMessageQueue(); |
| 187 return; |
| 188 } |
| 189 |
170 // The decoded message should be a JSON string. | 190 // The decoded message should be a JSON string. |
171 scoped_ptr<base::Value> message_value = | 191 scoped_ptr<base::Value> message_value = |
172 base::JSONReader::Read(decoded_message); | 192 base::JSONReader::Read(decoded_message); |
173 if (!message_value || !message_value->IsType(base::Value::TYPE_DICTIONARY)) { | 193 if (!message_value || !message_value->IsType(base::Value::TYPE_DICTIONARY)) { |
174 PA_LOG(ERROR) << "Unable to parse message as JSON:\n" << decoded_message; | 194 PA_LOG(ERROR) << "Unable to parse message as JSON:\n" << decoded_message; |
175 return; | 195 return; |
176 } | 196 } |
177 | 197 |
178 base::DictionaryValue* message; | 198 base::DictionaryValue* message; |
179 bool success = message_value->GetAsDictionary(&message); | 199 bool success = message_value->GetAsDictionary(&message); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 void MessengerImpl::HandleUnlockResponseMessage( | 277 void MessengerImpl::HandleUnlockResponseMessage( |
258 const base::DictionaryValue& message) { | 278 const base::DictionaryValue& message) { |
259 FOR_EACH_OBSERVER(MessengerObserver, observers_, OnUnlockResponse(true)); | 279 FOR_EACH_OBSERVER(MessengerObserver, observers_, OnUnlockResponse(true)); |
260 } | 280 } |
261 | 281 |
262 void MessengerImpl::PollScreenStateForIOS() { | 282 void MessengerImpl::PollScreenStateForIOS() { |
263 if (!connection_->IsConnected()) | 283 if (!connection_->IsConnected()) |
264 return; | 284 return; |
265 | 285 |
266 // Sends message requesting screen state. | 286 // Sends message requesting screen state. |
267 connection_->SendMessage(make_scoped_ptr(new WireMessage(kPollScreenState))); | 287 queued_messages_.push_back(PendingMessage(std::string(kPollScreenState))); |
| 288 ProcessMessageQueue(); |
268 | 289 |
269 // Schedules the next message in |kPollingIntervalSeconds|. | 290 // Schedules the next message in |kPollingIntervalSeconds|. |
270 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 291 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
271 FROM_HERE, base::Bind(&MessengerImpl::PollScreenStateForIOS, | 292 FROM_HERE, base::Bind(&MessengerImpl::PollScreenStateForIOS, |
272 weak_ptr_factory_.GetWeakPtr()), | 293 weak_ptr_factory_.GetWeakPtr()), |
273 base::TimeDelta::FromSeconds(kIOSPollingIntervalSeconds)); | 294 base::TimeDelta::FromSeconds(kIOSPollingIntervalSeconds)); |
274 } | 295 } |
275 | 296 |
276 void MessengerImpl::OnConnectionStatusChanged(Connection* connection, | 297 void MessengerImpl::OnConnectionStatusChanged(Connection* connection, |
277 Connection::Status old_status, | 298 Connection::Status old_status, |
278 Connection::Status new_status) { | 299 Connection::Status new_status) { |
279 DCHECK_EQ(connection, connection_.get()); | 300 DCHECK_EQ(connection, connection_.get()); |
280 if (new_status == Connection::DISCONNECTED) { | 301 if (new_status == Connection::DISCONNECTED) { |
281 PA_LOG(INFO) << "Secure channel disconnected..."; | 302 PA_LOG(INFO) << "Secure channel disconnected..."; |
282 connection_->RemoveObserver(this); | 303 connection_->RemoveObserver(this); |
283 FOR_EACH_OBSERVER(MessengerObserver, observers_, OnDisconnected()); | 304 FOR_EACH_OBSERVER(MessengerObserver, observers_, OnDisconnected()); |
284 // TODO(isherman): Determine whether it's also necessary/appropriate to fire | 305 // TODO(isherman): Determine whether it's also necessary/appropriate to fire |
285 // this notification from the destructor. | 306 // this notification from the destructor. |
286 } | 307 } |
287 } | 308 } |
288 | 309 |
289 void MessengerImpl::OnMessageReceived(const Connection& connection, | 310 void MessengerImpl::OnMessageReceived(const Connection& connection, |
290 const WireMessage& wire_message) { | 311 const WireMessage& wire_message) { |
291 // TODO(tengs): Unify the iOS status update protocol with the existing Android | |
292 // protocol, so we don't have this special case. | |
293 std::string payload = wire_message.payload(); | |
294 if (payload == kScreenUnlocked || payload == kScreenLocked) { | |
295 RemoteStatusUpdate update; | |
296 update.user_presence = | |
297 (payload == kScreenUnlocked ? USER_PRESENT : USER_ABSENT); | |
298 update.secure_screen_lock_state = SECURE_SCREEN_LOCK_ENABLED; | |
299 update.trust_agent_state = TRUST_AGENT_ENABLED; | |
300 FOR_EACH_OBSERVER(MessengerObserver, observers_, | |
301 OnRemoteStatusUpdate(update)); | |
302 return; | |
303 } | |
304 | |
305 secure_context_->Decode(wire_message.payload(), | 312 secure_context_->Decode(wire_message.payload(), |
306 base::Bind(&MessengerImpl::OnMessageDecoded, | 313 base::Bind(&MessengerImpl::OnMessageDecoded, |
307 weak_ptr_factory_.GetWeakPtr())); | 314 weak_ptr_factory_.GetWeakPtr())); |
308 } | 315 } |
309 | 316 |
310 void MessengerImpl::OnSendCompleted(const Connection& connection, | 317 void MessengerImpl::OnSendCompleted(const Connection& connection, |
311 const WireMessage& wire_message, | 318 const WireMessage& wire_message, |
312 bool success) { | 319 bool success) { |
313 if (!pending_message_) { | 320 if (!pending_message_) { |
314 PA_LOG(ERROR) << "Unexpected message sent."; | 321 PA_LOG(ERROR) << "Unexpected message sent."; |
(...skipping 21 matching lines...) Expand all Loading... |
336 } else { | 343 } else { |
337 PA_LOG(ERROR) << "Message of unknown type '" << pending_message_->type | 344 PA_LOG(ERROR) << "Message of unknown type '" << pending_message_->type |
338 << "' sent."; | 345 << "' sent."; |
339 } | 346 } |
340 | 347 |
341 pending_message_.reset(); | 348 pending_message_.reset(); |
342 ProcessMessageQueue(); | 349 ProcessMessageQueue(); |
343 } | 350 } |
344 | 351 |
345 } // namespace proximity_auth | 352 } // namespace proximity_auth |
OLD | NEW |