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/memory/ptr_util.h" |
15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "components/proximity_auth/connection.h" | 17 #include "components/cryptauth/connection.h" |
| 18 #include "components/cryptauth/wire_message.h" |
18 #include "components/proximity_auth/logging/logging.h" | 19 #include "components/proximity_auth/logging/logging.h" |
19 #include "components/proximity_auth/messenger_observer.h" | 20 #include "components/proximity_auth/messenger_observer.h" |
20 #include "components/proximity_auth/remote_status_update.h" | 21 #include "components/proximity_auth/remote_status_update.h" |
21 #include "components/proximity_auth/secure_context.h" | 22 #include "components/proximity_auth/secure_context.h" |
22 #include "components/proximity_auth/wire_message.h" | |
23 | 23 |
24 namespace proximity_auth { | 24 namespace proximity_auth { |
25 namespace { | 25 namespace { |
26 | 26 |
27 // The key names of JSON fields for messages sent between the devices. | 27 // The key names of JSON fields for messages sent between the devices. |
28 const char kTypeKey[] = "type"; | 28 const char kTypeKey[] = "type"; |
29 const char kNameKey[] = "name"; | 29 const char kNameKey[] = "name"; |
30 const char kDataKey[] = "data"; | 30 const char kDataKey[] = "data"; |
31 const char kEncryptedDataKey[] = "encrypted_data"; | 31 const char kEncryptedDataKey[] = "encrypted_data"; |
32 | 32 |
(...skipping 28 matching lines...) Expand all Loading... |
61 // 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 |
62 // 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. |
63 std::string GetMessageType(const base::DictionaryValue& message) { | 63 std::string GetMessageType(const base::DictionaryValue& message) { |
64 std::string type; | 64 std::string type; |
65 message.GetString(kTypeKey, &type); | 65 message.GetString(kTypeKey, &type); |
66 return type; | 66 return type; |
67 } | 67 } |
68 | 68 |
69 } // namespace | 69 } // namespace |
70 | 70 |
71 MessengerImpl::MessengerImpl(std::unique_ptr<Connection> connection, | 71 MessengerImpl::MessengerImpl(std::unique_ptr<cryptauth::Connection> connection, |
72 std::unique_ptr<SecureContext> secure_context) | 72 std::unique_ptr<SecureContext> secure_context) |
73 : connection_(std::move(connection)), | 73 : connection_(std::move(connection)), |
74 secure_context_(std::move(secure_context)), | 74 secure_context_(std::move(secure_context)), |
75 weak_ptr_factory_(this) { | 75 weak_ptr_factory_(this) { |
76 DCHECK(connection_->IsConnected()); | 76 DCHECK(connection_->IsConnected()); |
77 connection_->AddObserver(this); | 77 connection_->AddObserver(this); |
78 | 78 |
79 // 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, |
80 // rather than relying on this heuristic. | 80 // rather than relying on this heuristic. |
81 if (connection_->remote_device().bluetooth_type == | 81 if (connection_->remote_device().bluetooth_type == |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 | 172 |
173 pending_message_.reset(new PendingMessage(queued_messages_.front())); | 173 pending_message_.reset(new PendingMessage(queued_messages_.front())); |
174 queued_messages_.pop_front(); | 174 queued_messages_.pop_front(); |
175 | 175 |
176 secure_context_->Encode(pending_message_->json_message, | 176 secure_context_->Encode(pending_message_->json_message, |
177 base::Bind(&MessengerImpl::OnMessageEncoded, | 177 base::Bind(&MessengerImpl::OnMessageEncoded, |
178 weak_ptr_factory_.GetWeakPtr())); | 178 weak_ptr_factory_.GetWeakPtr())); |
179 } | 179 } |
180 | 180 |
181 void MessengerImpl::OnMessageEncoded(const std::string& encoded_message) { | 181 void MessengerImpl::OnMessageEncoded(const std::string& encoded_message) { |
182 connection_->SendMessage(base::MakeUnique<WireMessage>(encoded_message)); | 182 connection_->SendMessage( |
| 183 base::MakeUnique<cryptauth::WireMessage>(encoded_message)); |
183 } | 184 } |
184 | 185 |
185 void MessengerImpl::OnMessageDecoded(const std::string& decoded_message) { | 186 void MessengerImpl::OnMessageDecoded(const std::string& decoded_message) { |
186 // TODO(tengs): Unify the iOS status update protocol with the existing Android | 187 // TODO(tengs): Unify the iOS status update protocol with the existing Android |
187 // protocol, so we don't have this special case. | 188 // protocol, so we don't have this special case. |
188 if (decoded_message == kScreenUnlocked || decoded_message == kScreenLocked) { | 189 if (decoded_message == kScreenUnlocked || decoded_message == kScreenLocked) { |
189 RemoteStatusUpdate update; | 190 RemoteStatusUpdate update; |
190 update.user_presence = | 191 update.user_presence = |
191 (decoded_message == kScreenUnlocked ? USER_PRESENT : USER_ABSENT); | 192 (decoded_message == kScreenUnlocked ? USER_PRESENT : USER_ABSENT); |
192 update.secure_screen_lock_state = SECURE_SCREEN_LOCK_ENABLED; | 193 update.secure_screen_lock_state = SECURE_SCREEN_LOCK_ENABLED; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 queued_messages_.push_back(PendingMessage(std::string(kPollScreenState))); | 300 queued_messages_.push_back(PendingMessage(std::string(kPollScreenState))); |
300 ProcessMessageQueue(); | 301 ProcessMessageQueue(); |
301 | 302 |
302 // Schedules the next message in |kPollingIntervalSeconds|. | 303 // Schedules the next message in |kPollingIntervalSeconds|. |
303 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 304 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
304 FROM_HERE, base::Bind(&MessengerImpl::PollScreenStateForIOS, | 305 FROM_HERE, base::Bind(&MessengerImpl::PollScreenStateForIOS, |
305 weak_ptr_factory_.GetWeakPtr()), | 306 weak_ptr_factory_.GetWeakPtr()), |
306 base::TimeDelta::FromSeconds(kIOSPollingIntervalSeconds)); | 307 base::TimeDelta::FromSeconds(kIOSPollingIntervalSeconds)); |
307 } | 308 } |
308 | 309 |
309 void MessengerImpl::OnConnectionStatusChanged(Connection* connection, | 310 void MessengerImpl::OnConnectionStatusChanged( |
310 Connection::Status old_status, | 311 cryptauth::Connection* connection, |
311 Connection::Status new_status) { | 312 cryptauth::Connection::Status old_status, |
| 313 cryptauth::Connection::Status new_status) { |
312 DCHECK_EQ(connection, connection_.get()); | 314 DCHECK_EQ(connection, connection_.get()); |
313 if (new_status == Connection::DISCONNECTED) { | 315 if (new_status == cryptauth::Connection::DISCONNECTED) { |
314 PA_LOG(INFO) << "Secure channel disconnected..."; | 316 PA_LOG(INFO) << "Secure channel disconnected..."; |
315 connection_->RemoveObserver(this); | 317 connection_->RemoveObserver(this); |
316 for (auto& observer : observers_) | 318 for (auto& observer : observers_) |
317 observer.OnDisconnected(); | 319 observer.OnDisconnected(); |
318 // TODO(isherman): Determine whether it's also necessary/appropriate to fire | 320 // TODO(isherman): Determine whether it's also necessary/appropriate to fire |
319 // this notification from the destructor. | 321 // this notification from the destructor. |
320 } | 322 } |
321 } | 323 } |
322 | 324 |
323 void MessengerImpl::OnMessageReceived(const Connection& connection, | 325 void MessengerImpl::OnMessageReceived( |
324 const WireMessage& wire_message) { | 326 const cryptauth::Connection& connection, |
| 327 const cryptauth::WireMessage& wire_message) { |
325 secure_context_->Decode(wire_message.payload(), | 328 secure_context_->Decode(wire_message.payload(), |
326 base::Bind(&MessengerImpl::OnMessageDecoded, | 329 base::Bind(&MessengerImpl::OnMessageDecoded, |
327 weak_ptr_factory_.GetWeakPtr())); | 330 weak_ptr_factory_.GetWeakPtr())); |
328 } | 331 } |
329 | 332 |
330 void MessengerImpl::OnSendCompleted(const Connection& connection, | 333 void MessengerImpl::OnSendCompleted(const cryptauth::Connection& connection, |
331 const WireMessage& wire_message, | 334 const cryptauth::WireMessage& wire_message, |
332 bool success) { | 335 bool success) { |
333 if (!pending_message_) { | 336 if (!pending_message_) { |
334 PA_LOG(ERROR) << "Unexpected message sent."; | 337 PA_LOG(ERROR) << "Unexpected message sent."; |
335 return; | 338 return; |
336 } | 339 } |
337 | 340 |
338 // In the common case, wait for a response from the remote device. | 341 // In the common case, wait for a response from the remote device. |
339 // Don't wait if the message could not be sent, as there won't ever be a | 342 // Don't wait if the message could not be sent, as there won't ever be a |
340 // response in that case. Likewise, don't wait for a response to local | 343 // response in that case. Likewise, don't wait for a response to local |
341 // event messages, as there is no response for such messages. | 344 // event messages, as there is no response for such messages. |
(...skipping 15 matching lines...) Expand all Loading... |
357 } else { | 360 } else { |
358 PA_LOG(ERROR) << "Message of unknown type '" << pending_message_->type | 361 PA_LOG(ERROR) << "Message of unknown type '" << pending_message_->type |
359 << "' sent."; | 362 << "' sent."; |
360 } | 363 } |
361 | 364 |
362 pending_message_.reset(); | 365 pending_message_.reset(); |
363 ProcessMessageQueue(); | 366 ProcessMessageQueue(); |
364 } | 367 } |
365 | 368 |
366 } // namespace proximity_auth | 369 } // namespace proximity_auth |
OLD | NEW |