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/memory/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "components/proximity_auth/client.h"
15 #include "components/proximity_auth/connection_observer.h"
18 class DictionaryValue
;
21 namespace proximity_auth
{
26 // Concrete implementation of the Client interface.
27 class ClientImpl
: public Client
, public ConnectionObserver
{
29 // Constructs a client that sends and receives messages over the given
30 // |connection|, using the |secure_context| to encrypt and decrypt the
31 // messages. The |connection| must be connected. The client begins observing
32 // messages as soon as it is constructed.
33 ClientImpl(scoped_ptr
<Connection
> connection
,
34 scoped_ptr
<SecureContext
> secure_context
);
35 ~ClientImpl() override
;
38 void AddObserver(ClientObserver
* observer
) override
;
39 void RemoveObserver(ClientObserver
* observer
) override
;
40 bool SupportsSignIn() const override
;
41 void DispatchUnlockEvent() override
;
42 void RequestDecryption(const std::string
& challenge
) override
;
43 void RequestUnlock() override
;
46 // Exposed for testing.
47 Connection
* connection() { return connection_
.get(); }
48 SecureContext
* secure_context() { return secure_context_
.get(); }
51 // Internal data structure to represent a pending message that either hasn't
52 // been sent yet or is waiting for a response from the remote device.
53 struct PendingMessage
{
55 PendingMessage(const base::DictionaryValue
& message
);
58 // The message, serialized as JSON.
59 const std::string json_message
;
61 // The message type. This is possible to parse from the |json_message|; it's
62 // stored redundantly for convenience.
63 const std::string type
;
66 // Pops the first of the |queued_messages_| and sends it to the remote device.
67 void ProcessMessageQueue();
69 // Called when the message is encoded so it can be sent over the connection.
70 void OnMessageEncoded(const std::string
& encoded_message
);
72 // Called when the message is decoded so it can be parsed.
73 void OnMessageDecoded(const std::string
& decoded_message
);
75 // Handles an incoming "status_update" |message|, parsing and notifying
76 // observers of the content.
77 void HandleRemoteStatusUpdateMessage(const base::DictionaryValue
& message
);
79 // Handles an incoming "decrypt_response" message, parsing and notifying
80 // observers of the decrypted content.
81 void HandleDecryptResponseMessage(const base::DictionaryValue
& message
);
83 // Handles an incoming "unlock_response" message, notifying observers of the
85 void HandleUnlockResponseMessage(const base::DictionaryValue
& message
);
87 // ConnectionObserver:
88 void OnConnectionStatusChanged(Connection
* connection
,
89 Connection::Status old_status
,
90 Connection::Status new_status
) override
;
91 void OnMessageReceived(const Connection
& connection
,
92 const WireMessage
& wire_message
) override
;
93 void OnSendCompleted(const Connection
& connection
,
94 const WireMessage
& wire_message
,
95 bool success
) override
;
97 // The connection used to send and receive events and status updates.
98 scoped_ptr
<Connection
> connection_
;
100 // Used to encrypt and decrypt payloads sent and received over the
102 scoped_ptr
<SecureContext
> secure_context_
;
104 // The registered observers of |this_| client.
105 base::ObserverList
<ClientObserver
> observers_
;
107 // Queue of messages to send to the remote device.
108 std::deque
<PendingMessage
> queued_messages_
;
110 // The current message being sent or waiting on the remote device for a
111 // response. Null if there is no message currently in this state.
112 scoped_ptr
<PendingMessage
> pending_message_
;
114 base::WeakPtrFactory
<ClientImpl
> weak_ptr_factory_
;
116 DISALLOW_COPY_AND_ASSIGN(ClientImpl
);
119 } // namespace proximity_auth
121 #endif // COMPONENTS_PROXIMITY_AUTH_CLIENT_IMPL_H