Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / proximity_auth / client.h
blob5cd5107a1a55c5af8adc7980a5980282939cb8b3
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_H
6 #define COMPONENTS_PROXIMITY_AUTH_CLIENT_H
8 #include <deque>
10 #include "base/macros.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/observer_list.h"
13 #include "components/proximity_auth/connection_observer.h"
15 namespace base {
16 class DictionaryValue;
19 namespace proximity_auth {
21 class ClientObserver;
22 class Connection;
23 class SecureContext;
25 // A client handling the Easy Unlock protocol, capable of parsing events from
26 // the remote device and sending events for the local device.
27 class 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 Client(scoped_ptr<Connection> connection,
34 scoped_ptr<SecureContext> secure_context);
35 virtual ~Client();
37 // Adds or removes an observer for Client events.
38 void AddObserver(ClientObserver* observer);
39 void RemoveObserver(ClientObserver* observer);
41 // Returns true iff the remote device supports the v3.1 sign-in protocol.
42 bool SupportsSignIn() const;
44 // Sends an unlock event message to the remote device.
45 void DispatchUnlockEvent();
47 // Sends a serialized SecureMessage to the remote device to decrypt the
48 // |challenge|. OnDecryptResponse will be called for each observer when the
49 // decrypted response is received.
50 // TODO(isherman): Add params for the RSA private key and crypto delegate.
51 void RequestDecryption(const std::string& challenge);
53 // Sends a simple request to the remote device to unlock the screen.
54 // OnUnlockResponse is called for each observer when the response is returned.
55 void RequestUnlock();
57 protected:
58 // Exposed for testing.
59 Connection* connection() { return connection_.get(); }
60 SecureContext* secure_context() { return secure_context_.get(); }
62 private:
63 // Internal data structure to represent a pending message that either hasn't
64 // been sent yet or is waiting for a response from the remote device.
65 struct PendingMessage {
66 PendingMessage();
67 PendingMessage(const base::DictionaryValue& message);
68 ~PendingMessage();
70 // The message, serialized as JSON.
71 const std::string json_message;
73 // The message type. This is possible to parse from the |json_message|; it's
74 // stored redundantly for convenience.
75 const std::string type;
78 // Pops the first of the |queued_messages_| and sends it to the remote device.
79 void ProcessMessageQueue();
81 // Handles an incoming "status_update" |message|, parsing and notifying
82 // observers of the content.
83 void HandleRemoteStatusUpdateMessage(const base::DictionaryValue& message);
85 // Handles an incoming "decrypt_response" message, parsing and notifying
86 // observers of the decrypted content.
87 void HandleDecryptResponseMessage(const base::DictionaryValue& message);
89 // Handles an incoming "unlock_response" message, notifying observers of the
90 // response.
91 void HandleUnlockResponseMessage(const base::DictionaryValue& message);
93 // ConnectionObserver:
94 void OnConnectionStatusChanged(const Connection& connection,
95 Connection::Status old_status,
96 Connection::Status new_status) override;
97 void OnMessageReceived(const Connection& connection,
98 const WireMessage& wire_message) override;
99 void OnSendCompleted(const Connection& connection,
100 const WireMessage& wire_message,
101 bool success) override;
103 // The connection used to send and receive events and status updates.
104 scoped_ptr<Connection> connection_;
106 // Used to encrypt and decrypt payloads sent and received over the
107 // |connection_|.
108 scoped_ptr<SecureContext> secure_context_;
110 // The registered observers of |this_| client.
111 ObserverList<ClientObserver> observers_;
113 // Queue of messages to send to the remote device.
114 std::deque<PendingMessage> queued_messages_;
116 // The current message being sent or waiting on the remote device for a
117 // response. Null if there is no message currently in this state.
118 scoped_ptr<PendingMessage> pending_message_;
120 DISALLOW_COPY_AND_ASSIGN(Client);
123 } // namespace proximity_auth
125 #endif // COMPONENTS_PROXIMITY_AUTH_CLIENT_H