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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/base64url.h" | 9 #include "base/base64url.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
12 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/memory/ptr_util.h" |
14 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
15 #include "base/values.h" | 16 #include "base/values.h" |
16 #include "components/proximity_auth/connection.h" | 17 #include "components/proximity_auth/connection.h" |
17 #include "components/proximity_auth/logging/logging.h" | 18 #include "components/proximity_auth/logging/logging.h" |
18 #include "components/proximity_auth/messenger_observer.h" | 19 #include "components/proximity_auth/messenger_observer.h" |
19 #include "components/proximity_auth/remote_status_update.h" | 20 #include "components/proximity_auth/remote_status_update.h" |
20 #include "components/proximity_auth/secure_context.h" | 21 #include "components/proximity_auth/secure_context.h" |
21 #include "components/proximity_auth/wire_message.h" | 22 #include "components/proximity_auth/wire_message.h" |
22 | 23 |
23 namespace proximity_auth { | 24 namespace proximity_auth { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 // wrapper that should only be called when the |message| is known to specify its | 61 // wrapper that should only be called when the |message| is known to specify its |
61 // message type, i.e. this should not be called for untrusted input. | 62 // message type, i.e. this should not be called for untrusted input. |
62 std::string GetMessageType(const base::DictionaryValue& message) { | 63 std::string GetMessageType(const base::DictionaryValue& message) { |
63 std::string type; | 64 std::string type; |
64 message.GetString(kTypeKey, &type); | 65 message.GetString(kTypeKey, &type); |
65 return type; | 66 return type; |
66 } | 67 } |
67 | 68 |
68 } // namespace | 69 } // namespace |
69 | 70 |
70 MessengerImpl::MessengerImpl(scoped_ptr<Connection> connection, | 71 MessengerImpl::MessengerImpl(std::unique_ptr<Connection> connection, |
71 scoped_ptr<SecureContext> secure_context) | 72 std::unique_ptr<SecureContext> secure_context) |
72 : connection_(std::move(connection)), | 73 : connection_(std::move(connection)), |
73 secure_context_(std::move(secure_context)), | 74 secure_context_(std::move(secure_context)), |
74 weak_ptr_factory_(this) { | 75 weak_ptr_factory_(this) { |
75 DCHECK(connection_->IsConnected()); | 76 DCHECK(connection_->IsConnected()); |
76 connection_->AddObserver(this); | 77 connection_->AddObserver(this); |
77 | 78 |
78 // TODO(tengs): We need CryptAuth to report if the phone runs iOS or Android, | 79 // TODO(tengs): We need CryptAuth to report if the phone runs iOS or Android, |
79 // rather than relying on this heuristic. | 80 // rather than relying on this heuristic. |
80 if (connection_->remote_device().bluetooth_type == RemoteDevice::BLUETOOTH_LE) | 81 if (connection_->remote_device().bluetooth_type == RemoteDevice::BLUETOOTH_LE) |
81 PollScreenStateForIOS(); | 82 PollScreenStateForIOS(); |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 | 170 |
170 pending_message_.reset(new PendingMessage(queued_messages_.front())); | 171 pending_message_.reset(new PendingMessage(queued_messages_.front())); |
171 queued_messages_.pop_front(); | 172 queued_messages_.pop_front(); |
172 | 173 |
173 secure_context_->Encode(pending_message_->json_message, | 174 secure_context_->Encode(pending_message_->json_message, |
174 base::Bind(&MessengerImpl::OnMessageEncoded, | 175 base::Bind(&MessengerImpl::OnMessageEncoded, |
175 weak_ptr_factory_.GetWeakPtr())); | 176 weak_ptr_factory_.GetWeakPtr())); |
176 } | 177 } |
177 | 178 |
178 void MessengerImpl::OnMessageEncoded(const std::string& encoded_message) { | 179 void MessengerImpl::OnMessageEncoded(const std::string& encoded_message) { |
179 connection_->SendMessage(make_scoped_ptr(new WireMessage(encoded_message))); | 180 connection_->SendMessage(base::WrapUnique(new WireMessage(encoded_message))); |
180 } | 181 } |
181 | 182 |
182 void MessengerImpl::OnMessageDecoded(const std::string& decoded_message) { | 183 void MessengerImpl::OnMessageDecoded(const std::string& decoded_message) { |
183 // TODO(tengs): Unify the iOS status update protocol with the existing Android | 184 // TODO(tengs): Unify the iOS status update protocol with the existing Android |
184 // protocol, so we don't have this special case. | 185 // protocol, so we don't have this special case. |
185 if (decoded_message == kScreenUnlocked || decoded_message == kScreenLocked) { | 186 if (decoded_message == kScreenUnlocked || decoded_message == kScreenLocked) { |
186 RemoteStatusUpdate update; | 187 RemoteStatusUpdate update; |
187 update.user_presence = | 188 update.user_presence = |
188 (decoded_message == kScreenUnlocked ? USER_PRESENT : USER_ABSENT); | 189 (decoded_message == kScreenUnlocked ? USER_PRESENT : USER_ABSENT); |
189 update.secure_screen_lock_state = SECURE_SCREEN_LOCK_ENABLED; | 190 update.secure_screen_lock_state = SECURE_SCREEN_LOCK_ENABLED; |
190 update.trust_agent_state = TRUST_AGENT_ENABLED; | 191 update.trust_agent_state = TRUST_AGENT_ENABLED; |
191 FOR_EACH_OBSERVER(MessengerObserver, observers_, | 192 FOR_EACH_OBSERVER(MessengerObserver, observers_, |
192 OnRemoteStatusUpdate(update)); | 193 OnRemoteStatusUpdate(update)); |
193 pending_message_.reset(); | 194 pending_message_.reset(); |
194 ProcessMessageQueue(); | 195 ProcessMessageQueue(); |
195 return; | 196 return; |
196 } | 197 } |
197 | 198 |
198 // The decoded message should be a JSON string. | 199 // The decoded message should be a JSON string. |
199 scoped_ptr<base::Value> message_value = | 200 std::unique_ptr<base::Value> message_value = |
200 base::JSONReader::Read(decoded_message); | 201 base::JSONReader::Read(decoded_message); |
201 if (!message_value || !message_value->IsType(base::Value::TYPE_DICTIONARY)) { | 202 if (!message_value || !message_value->IsType(base::Value::TYPE_DICTIONARY)) { |
202 PA_LOG(ERROR) << "Unable to parse message as JSON:\n" << decoded_message; | 203 PA_LOG(ERROR) << "Unable to parse message as JSON:\n" << decoded_message; |
203 return; | 204 return; |
204 } | 205 } |
205 | 206 |
206 base::DictionaryValue* message; | 207 base::DictionaryValue* message; |
207 bool success = message_value->GetAsDictionary(&message); | 208 bool success = message_value->GetAsDictionary(&message); |
208 DCHECK(success); | 209 DCHECK(success); |
209 | 210 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 HandleUnlockResponseMessage(*message); | 249 HandleUnlockResponseMessage(*message); |
249 else | 250 else |
250 NOTREACHED(); // There are no other message types that expect a response. | 251 NOTREACHED(); // There are no other message types that expect a response. |
251 | 252 |
252 pending_message_.reset(); | 253 pending_message_.reset(); |
253 ProcessMessageQueue(); | 254 ProcessMessageQueue(); |
254 } | 255 } |
255 | 256 |
256 void MessengerImpl::HandleRemoteStatusUpdateMessage( | 257 void MessengerImpl::HandleRemoteStatusUpdateMessage( |
257 const base::DictionaryValue& message) { | 258 const base::DictionaryValue& message) { |
258 scoped_ptr<RemoteStatusUpdate> status_update = | 259 std::unique_ptr<RemoteStatusUpdate> status_update = |
259 RemoteStatusUpdate::Deserialize(message); | 260 RemoteStatusUpdate::Deserialize(message); |
260 if (!status_update) { | 261 if (!status_update) { |
261 PA_LOG(ERROR) << "Unexpected remote status update: " << message; | 262 PA_LOG(ERROR) << "Unexpected remote status update: " << message; |
262 return; | 263 return; |
263 } | 264 } |
264 | 265 |
265 FOR_EACH_OBSERVER(MessengerObserver, observers_, | 266 FOR_EACH_OBSERVER(MessengerObserver, observers_, |
266 OnRemoteStatusUpdate(*status_update)); | 267 OnRemoteStatusUpdate(*status_update)); |
267 } | 268 } |
268 | 269 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 } else { | 352 } else { |
352 PA_LOG(ERROR) << "Message of unknown type '" << pending_message_->type | 353 PA_LOG(ERROR) << "Message of unknown type '" << pending_message_->type |
353 << "' sent."; | 354 << "' sent."; |
354 } | 355 } |
355 | 356 |
356 pending_message_.reset(); | 357 pending_message_.reset(); |
357 ProcessMessageQueue(); | 358 ProcessMessageQueue(); |
358 } | 359 } |
359 | 360 |
360 } // namespace proximity_auth | 361 } // namespace proximity_auth |
OLD | NEW |