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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_PROXIMITY_AUTH_CLIENT_IMPL_H
6 #define COMPONENTS_PROXIMITY_AUTH_CLIENT_IMPL_H
10 #include "base/macros.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/observer_list.h"
13 #include "components/proximity_auth/client.h"
14 #include "components/proximity_auth/connection_observer.h"
17 class DictionaryValue
;
20 namespace proximity_auth
{
25 // Concrete implementation of the Client interface.
26 class ClientImpl
: public Client
, public ConnectionObserver
{
28 // Constructs a client that sends and receives messages over the given
29 // |connection|, using the |secure_context| to encrypt and decrypt the
30 // messages. The |connection| must be connected. The client begins observing
31 // messages as soon as it is constructed.
32 ClientImpl(scoped_ptr
<Connection
> connection
,
33 scoped_ptr
<SecureContext
> secure_context
);
34 ~ClientImpl() override
;
37 void AddObserver(ClientObserver
* observer
) override
;
38 void RemoveObserver(ClientObserver
* observer
) override
;
39 bool SupportsSignIn() const override
;
40 void DispatchUnlockEvent() override
;
41 void RequestDecryption(const std::string
& challenge
) override
;
42 void RequestUnlock() override
;
45 // Exposed for testing.
46 Connection
* connection() { return connection_
.get(); }
47 SecureContext
* secure_context() { return secure_context_
.get(); }
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.
52 struct PendingMessage
{
54 PendingMessage(const base::DictionaryValue
& message
);
57 // The message, serialized as JSON.
58 const std::string json_message
;
60 // The message type. This is possible to parse from the |json_message|; it's
61 // stored redundantly for convenience.
62 const std::string type
;
65 // Pops the first of the |queued_messages_| and sends it to the remote device.
66 void ProcessMessageQueue();
68 // Handles an incoming "status_update" |message|, parsing and notifying
69 // observers of the content.
70 void HandleRemoteStatusUpdateMessage(const base::DictionaryValue
& message
);
72 // Handles an incoming "decrypt_response" message, parsing and notifying
73 // observers of the decrypted content.
74 void HandleDecryptResponseMessage(const base::DictionaryValue
& message
);
76 // Handles an incoming "unlock_response" message, notifying observers of the
78 void HandleUnlockResponseMessage(const base::DictionaryValue
& message
);
80 // ConnectionObserver:
81 void OnConnectionStatusChanged(Connection
* connection
,
82 Connection::Status old_status
,
83 Connection::Status new_status
) override
;
84 void OnMessageReceived(const Connection
& connection
,
85 const WireMessage
& wire_message
) override
;
86 void OnSendCompleted(const Connection
& connection
,
87 const WireMessage
& wire_message
,
88 bool success
) override
;
90 // The connection used to send and receive events and status updates.
91 scoped_ptr
<Connection
> connection_
;
93 // Used to encrypt and decrypt payloads sent and received over the
95 scoped_ptr
<SecureContext
> secure_context_
;
97 // The registered observers of |this_| client.
98 base::ObserverList
<ClientObserver
> observers_
;
100 // Queue of messages to send to the remote device.
101 std::deque
<PendingMessage
> queued_messages_
;
103 // The current message being sent or waiting on the remote device for a
104 // response. Null if there is no message currently in this state.
105 scoped_ptr
<PendingMessage
> pending_message_
;
107 DISALLOW_COPY_AND_ASSIGN(ClientImpl
);
110 } // namespace proximity_auth
112 #endif // COMPONENTS_PROXIMITY_AUTH_CLIENT_IMPL_H