File: client.h

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (125 lines) | stat: -rw-r--r-- 4,581 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_PROXIMITY_AUTH_CLIENT_H
#define COMPONENTS_PROXIMITY_AUTH_CLIENT_H

#include <deque>

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/observer_list.h"
#include "components/proximity_auth/connection_observer.h"

namespace base {
class DictionaryValue;
}

namespace proximity_auth {

class ClientObserver;
class Connection;
class SecureContext;

// A client handling the Easy Unlock protocol, capable of parsing events from
// the remote device and sending events for the local device.
class Client : public ConnectionObserver {
 public:
  // Constructs a client that sends and receives messages over the given
  // |connection|, using the |secure_context| to encrypt and decrypt the
  // messages. The |connection| must be connected. The client begins observing
  // messages as soon as it is constructed.
  Client(scoped_ptr<Connection> connection,
         scoped_ptr<SecureContext> secure_context);
  virtual ~Client();

  // Adds or removes an observer for Client events.
  void AddObserver(ClientObserver* observer);
  void RemoveObserver(ClientObserver* observer);

  // Returns true iff the remote device supports the v3.1 sign-in protocol.
  bool SupportsSignIn() const;

  // Sends an unlock event message to the remote device.
  void DispatchUnlockEvent();

  // Sends a serialized SecureMessage to the remote device to decrypt the
  // |challenge|. OnDecryptResponse will be called for each observer when the
  // decrypted response is received.
  // TODO(isherman): Add params for the RSA private key and crypto delegate.
  void RequestDecryption(const std::string& challenge);

  // Sends a simple request to the remote device to unlock the screen.
  // OnUnlockResponse is called for each observer when the response is returned.
  void RequestUnlock();

 protected:
  // Exposed for testing.
  Connection* connection() { return connection_.get(); }
  SecureContext* secure_context() { return secure_context_.get(); }

 private:
  // Internal data structure to represent a pending message that either hasn't
  // been sent yet or is waiting for a response from the remote device.
  struct PendingMessage {
    PendingMessage();
    PendingMessage(const base::DictionaryValue& message);
    ~PendingMessage();

    // The message, serialized as JSON.
    const std::string json_message;

    // The message type. This is possible to parse from the |json_message|; it's
    // stored redundantly for convenience.
    const std::string type;
  };

  // Pops the first of the |queued_messages_| and sends it to the remote device.
  void ProcessMessageQueue();

  // Handles an incoming "status_update" |message|, parsing and notifying
  // observers of the content.
  void HandleRemoteStatusUpdateMessage(const base::DictionaryValue& message);

  // Handles an incoming "decrypt_response" message, parsing and notifying
  // observers of the decrypted content.
  void HandleDecryptResponseMessage(const base::DictionaryValue& message);

  // Handles an incoming "unlock_response" message, notifying observers of the
  // response.
  void HandleUnlockResponseMessage(const base::DictionaryValue& message);

  // ConnectionObserver:
  void OnConnectionStatusChanged(const Connection& connection,
                                 Connection::Status old_status,
                                 Connection::Status new_status) override;
  void OnMessageReceived(const Connection& connection,
                         const WireMessage& wire_message) override;
  void OnSendCompleted(const Connection& connection,
                       const WireMessage& wire_message,
                       bool success) override;

  // The connection used to send and receive events and status updates.
  scoped_ptr<Connection> connection_;

  // Used to encrypt and decrypt payloads sent and received over the
  // |connection_|.
  scoped_ptr<SecureContext> secure_context_;

  // The registered observers of |this_| client.
  ObserverList<ClientObserver> observers_;

  // Queue of messages to send to the remote device.
  std::deque<PendingMessage> queued_messages_;

  // The current message being sent or waiting on the remote device for a
  // response. Null if there is no message currently in this state.
  scoped_ptr<PendingMessage> pending_message_;

  DISALLOW_COPY_AND_ASSIGN(Client);
};

}  // namespace proximity_auth

#endif  // COMPONENTS_PROXIMITY_AUTH_CLIENT_H