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 #ifndef COMPONENTS_PROXIMITY_AUTH_MESSENGER_IMPL_H | 5 #ifndef COMPONENTS_PROXIMITY_AUTH_MESSENGER_IMPL_H |
6 #define COMPONENTS_PROXIMITY_AUTH_MESSENGER_IMPL_H | 6 #define COMPONENTS_PROXIMITY_AUTH_MESSENGER_IMPL_H |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/observer_list.h" | 13 #include "base/observer_list.h" |
14 #include "components/proximity_auth/connection_observer.h" | 14 #include "components/cryptauth/connection.h" |
| 15 #include "components/cryptauth/connection_observer.h" |
15 #include "components/proximity_auth/messenger.h" | 16 #include "components/proximity_auth/messenger.h" |
16 | 17 |
17 namespace base { | 18 namespace base { |
18 class DictionaryValue; | 19 class DictionaryValue; |
19 } | 20 } |
20 | 21 |
21 namespace proximity_auth { | 22 namespace proximity_auth { |
22 | 23 |
23 class Connection; | |
24 class SecureContext; | 24 class SecureContext; |
25 | 25 |
26 // Concrete implementation of the Messenger interface. | 26 // Concrete implementation of the Messenger interface. |
27 class MessengerImpl : public Messenger, public ConnectionObserver { | 27 class MessengerImpl : public Messenger, public cryptauth::ConnectionObserver { |
28 public: | 28 public: |
29 // Constructs a messenger that sends and receives messages over the given | 29 // Constructs a messenger that sends and receives messages over the given |
30 // |connection|, using the |secure_context| to encrypt and decrypt the | 30 // |connection|, using the |secure_context| to encrypt and decrypt the |
31 // messages. The |connection| must be connected. The messenger begins | 31 // messages. The |connection| must be connected. The messenger begins |
32 // observing messages as soon as it is constructed. | 32 // observing messages as soon as it is constructed. |
33 MessengerImpl(std::unique_ptr<Connection> connection, | 33 MessengerImpl(std::unique_ptr<cryptauth::Connection> connection, |
34 std::unique_ptr<SecureContext> secure_context); | 34 std::unique_ptr<SecureContext> secure_context); |
35 ~MessengerImpl() override; | 35 ~MessengerImpl() override; |
36 | 36 |
37 // Messenger: | 37 // Messenger: |
38 void AddObserver(MessengerObserver* observer) override; | 38 void AddObserver(MessengerObserver* observer) override; |
39 void RemoveObserver(MessengerObserver* observer) override; | 39 void RemoveObserver(MessengerObserver* observer) override; |
40 bool SupportsSignIn() const override; | 40 bool SupportsSignIn() const override; |
41 void DispatchUnlockEvent() override; | 41 void DispatchUnlockEvent() override; |
42 void RequestDecryption(const std::string& challenge) override; | 42 void RequestDecryption(const std::string& challenge) override; |
43 void RequestUnlock() override; | 43 void RequestUnlock() override; |
44 SecureContext* GetSecureContext() const override; | 44 SecureContext* GetSecureContext() const override; |
45 | 45 |
46 // Exposed for testing. | 46 // Exposed for testing. |
47 Connection* connection() { return connection_.get(); } | 47 cryptauth::Connection* connection() { return connection_.get(); } |
48 | 48 |
49 private: | 49 private: |
50 // Internal data structure to represent a pending message that either hasn't | 50 // Internal data structure to represent a pending message that either hasn't |
51 // been sent yet or is waiting for a response from the remote device. | 51 // been sent yet or is waiting for a response from the remote device. |
52 struct PendingMessage { | 52 struct PendingMessage { |
53 PendingMessage(); | 53 PendingMessage(); |
54 PendingMessage(const base::DictionaryValue& message); | 54 PendingMessage(const base::DictionaryValue& message); |
55 PendingMessage(const std::string& message); | 55 PendingMessage(const std::string& message); |
56 ~PendingMessage(); | 56 ~PendingMessage(); |
57 | 57 |
(...skipping 23 matching lines...) Expand all Loading... |
81 void HandleDecryptResponseMessage(const base::DictionaryValue& message); | 81 void HandleDecryptResponseMessage(const base::DictionaryValue& message); |
82 | 82 |
83 // Handles an incoming "unlock_response" message, notifying observers of the | 83 // Handles an incoming "unlock_response" message, notifying observers of the |
84 // response. | 84 // response. |
85 void HandleUnlockResponseMessage(const base::DictionaryValue& message); | 85 void HandleUnlockResponseMessage(const base::DictionaryValue& message); |
86 | 86 |
87 // For iOS, we need to poll the phone every few seconds to keep the app alive | 87 // For iOS, we need to poll the phone every few seconds to keep the app alive |
88 // in the background. This function starts the poll loop. | 88 // in the background. This function starts the poll loop. |
89 void PollScreenStateForIOS(); | 89 void PollScreenStateForIOS(); |
90 | 90 |
91 // ConnectionObserver: | 91 // cryptauth::ConnectionObserver: |
92 void OnConnectionStatusChanged(Connection* connection, | 92 void OnConnectionStatusChanged( |
93 Connection::Status old_status, | 93 cryptauth::Connection* connection, |
94 Connection::Status new_status) override; | 94 cryptauth::Connection::Status old_status, |
95 void OnMessageReceived(const Connection& connection, | 95 cryptauth::Connection::Status new_status) override; |
96 const WireMessage& wire_message) override; | 96 void OnMessageReceived(const cryptauth::Connection& connection, |
97 void OnSendCompleted(const Connection& connection, | 97 const cryptauth::WireMessage& wire_message) override; |
98 const WireMessage& wire_message, | 98 void OnSendCompleted(const cryptauth::Connection& connection, |
| 99 const cryptauth::WireMessage& wire_message, |
99 bool success) override; | 100 bool success) override; |
100 | 101 |
101 // The connection used to send and receive events and status updates. | 102 // The connection used to send and receive events and status updates. |
102 std::unique_ptr<Connection> connection_; | 103 std::unique_ptr<cryptauth::Connection> connection_; |
103 | 104 |
104 // Used to encrypt and decrypt payloads sent and received over the | 105 // Used to encrypt and decrypt payloads sent and received over the |
105 // |connection_|. | 106 // |connection_|. |
106 std::unique_ptr<SecureContext> secure_context_; | 107 std::unique_ptr<SecureContext> secure_context_; |
107 | 108 |
108 // The registered observers of |this_| messenger. | 109 // The registered observers of |this_| messenger. |
109 base::ObserverList<MessengerObserver> observers_; | 110 base::ObserverList<MessengerObserver> observers_; |
110 | 111 |
111 // Queue of messages to send to the remote device. | 112 // Queue of messages to send to the remote device. |
112 std::deque<PendingMessage> queued_messages_; | 113 std::deque<PendingMessage> queued_messages_; |
113 | 114 |
114 // The current message being sent or waiting on the remote device for a | 115 // The current message being sent or waiting on the remote device for a |
115 // response. Null if there is no message currently in this state. | 116 // response. Null if there is no message currently in this state. |
116 std::unique_ptr<PendingMessage> pending_message_; | 117 std::unique_ptr<PendingMessage> pending_message_; |
117 | 118 |
118 base::WeakPtrFactory<MessengerImpl> weak_ptr_factory_; | 119 base::WeakPtrFactory<MessengerImpl> weak_ptr_factory_; |
119 | 120 |
120 DISALLOW_COPY_AND_ASSIGN(MessengerImpl); | 121 DISALLOW_COPY_AND_ASSIGN(MessengerImpl); |
121 }; | 122 }; |
122 | 123 |
123 } // namespace proximity_auth | 124 } // namespace proximity_auth |
124 | 125 |
125 #endif // COMPONENTS_PROXIMITY_AUTH_MESSENGER_IMPL_H | 126 #endif // COMPONENTS_PROXIMITY_AUTH_MESSENGER_IMPL_H |
OLD | NEW |