Implement finding BLE connections in chrome://proximity-auth.
[chromium-blink-merge.git] / components / proximity_auth / client_impl.h
blob11582c91a2da0948fa8347f196eb4dbe80d10c18
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
8 #include <deque>
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"
17 namespace base {
18 class DictionaryValue;
21 namespace proximity_auth {
23 class Connection;
24 class SecureContext;
26 // Concrete implementation of the Client interface.
27 class ClientImpl : public Client, public ConnectionObserver {
28 public:
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;
37 // Client:
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;
45 // Exposed for testing.
46 Connection* connection() { return connection_.get(); }
48 protected:
49 SecureContext* secure_context() { return secure_context_.get(); }
51 private:
52 // Internal data structure to represent a pending message that either hasn't
53 // been sent yet or is waiting for a response from the remote device.
54 struct PendingMessage {
55 PendingMessage();
56 PendingMessage(const base::DictionaryValue& message);
57 ~PendingMessage();
59 // The message, serialized as JSON.
60 const std::string json_message;
62 // The message type. This is possible to parse from the |json_message|; it's
63 // stored redundantly for convenience.
64 const std::string type;
67 // Pops the first of the |queued_messages_| and sends it to the remote device.
68 void ProcessMessageQueue();
70 // Called when the message is encoded so it can be sent over the connection.
71 void OnMessageEncoded(const std::string& encoded_message);
73 // Called when the message is decoded so it can be parsed.
74 void OnMessageDecoded(const std::string& decoded_message);
76 // Handles an incoming "status_update" |message|, parsing and notifying
77 // observers of the content.
78 void HandleRemoteStatusUpdateMessage(const base::DictionaryValue& message);
80 // Handles an incoming "decrypt_response" message, parsing and notifying
81 // observers of the decrypted content.
82 void HandleDecryptResponseMessage(const base::DictionaryValue& message);
84 // Handles an incoming "unlock_response" message, notifying observers of the
85 // response.
86 void HandleUnlockResponseMessage(const base::DictionaryValue& message);
88 // ConnectionObserver:
89 void OnConnectionStatusChanged(Connection* connection,
90 Connection::Status old_status,
91 Connection::Status new_status) override;
92 void OnMessageReceived(const Connection& connection,
93 const WireMessage& wire_message) override;
94 void OnSendCompleted(const Connection& connection,
95 const WireMessage& wire_message,
96 bool success) override;
98 // The connection used to send and receive events and status updates.
99 scoped_ptr<Connection> connection_;
101 // Used to encrypt and decrypt payloads sent and received over the
102 // |connection_|.
103 scoped_ptr<SecureContext> secure_context_;
105 // The registered observers of |this_| client.
106 base::ObserverList<ClientObserver> observers_;
108 // Queue of messages to send to the remote device.
109 std::deque<PendingMessage> queued_messages_;
111 // The current message being sent or waiting on the remote device for a
112 // response. Null if there is no message currently in this state.
113 scoped_ptr<PendingMessage> pending_message_;
115 base::WeakPtrFactory<ClientImpl> weak_ptr_factory_;
117 DISALLOW_COPY_AND_ASSIGN(ClientImpl);
120 } // namespace proximity_auth
122 #endif // COMPONENTS_PROXIMITY_AUTH_CLIENT_IMPL_H