summaryrefslogtreecommitdiffstats
path: root/blimp/net/client_connection_manager.h
blob: 10644d066e6338bf9e5293d8f1b9f5d355b0737f (plain)
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
// Copyright 2015 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 BLIMP_NET_CLIENT_CONNECTION_MANAGER_H_
#define BLIMP_NET_CLIENT_CONNECTION_MANAGER_H_

#include <string>
#include <vector>

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "blimp/net/blimp_net_export.h"
#include "blimp/net/connection_error_observer.h"

namespace blimp {

class BlimpConnection;
class BlimpTransport;
class ConnectionHandler;

// Coordinates the channel creation and authentication workflows for
// outgoing (Client) network connections.
//
// TODO(haibinlu): cope with network changes that may potentially affect the
// endpoint that we're trying to connect to.
class BLIMP_NET_EXPORT ClientConnectionManager
    : public ConnectionErrorObserver {
 public:
  // Caller is responsible for ensuring that |connection_handler|
  // outlives |this|.
  explicit ClientConnectionManager(ConnectionHandler* connection_handler);

  ~ClientConnectionManager() override;

  // Adds a transport. All transports are expected to be added before invoking
  // |Connect|.
  void AddTransport(scoped_ptr<BlimpTransport> transport);

  // Attempts to create a connection using any of the BlimpTransports in
  // |transports_|.
  // This will result in the handler being called back at-most-once.
  //
  // This is also a placeholder for automatic reconnection logic for common
  // cases such as network switches, online/offline changes.
  void Connect();

  // Sets the client token to use in the authentication message.
  void set_client_token(const std::string& client_token) {
    client_token_ = client_token;
  }

 private:
  // Tries to connect using the BlimpTransport specified at |transport_index|.
  void Connect(int transport_index);

  // Callback invoked by transports_[transport_index] to indicate that it has a
  // connection ready to be authenticated or there is an error.
  void OnConnectResult(int transport_index, int result);

  // Sends authentication message to the engine via |connection|.
  void SendAuthenticationMessage(scoped_ptr<BlimpConnection> connection);

  // Invoked after the authentication message is sent to |connection|.
  // The result of the write operation is passed via |result|.
  void OnAuthenticationMessageSent(scoped_ptr<BlimpConnection> connection,
                                   int result);

  // ConnectionErrorObserver implementation.
  void OnConnectionError(int error) override;

  std::string client_token_;
  ConnectionHandler* connection_handler_;
  std::vector<scoped_ptr<BlimpTransport>> transports_;
  base::WeakPtrFactory<ClientConnectionManager> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(ClientConnectionManager);
};

}  // namespace blimp

#endif  // BLIMP_NET_CLIENT_CONNECTION_MANAGER_H_