summaryrefslogtreecommitdiff
path: root/chromium/blimp/net/blimp_connection.h
blob: a662c4e10ef5d488ef5352ae3b2d1c555f66c030 (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
// 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_BLIMP_CONNECTION_H_
#define BLIMP_NET_BLIMP_CONNECTION_H_

#include <memory>

#include "base/macros.h"
#include "base/observer_list.h"
#include "blimp/net/blimp_net_export.h"
#include "blimp/net/connection_error_observer.h"

namespace blimp {

class BlimpMessageProcessor;
class BlimpMessagePump;
class PacketReader;
class PacketWriter;

// Encapsulates the state and logic used to exchange BlimpMessages over
// a network connection.
class BLIMP_NET_EXPORT BlimpConnection : public ConnectionErrorObserver {
 public:
  BlimpConnection(std::unique_ptr<PacketReader> reader,
                  std::unique_ptr<PacketWriter> writer);

  ~BlimpConnection() override;

  // Adds |observer| to the connection's error observer list.
  virtual void AddConnectionErrorObserver(ConnectionErrorObserver* observer);

  // Removes |observer| from the connection's error observer list.
  virtual void RemoveConnectionErrorObserver(ConnectionErrorObserver* observer);

  // Sets the processor which will take incoming messages for this connection.
  // Can be set multiple times, but previously set processors are discarded.
  // Caller retains the ownership of |processor|.
  virtual void SetIncomingMessageProcessor(BlimpMessageProcessor* processor);

  // Gets a processor for BrowserSession->BlimpConnection message routing.
  virtual BlimpMessageProcessor* GetOutgoingMessageProcessor();

 protected:
  BlimpConnection();

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

 private:
  std::unique_ptr<PacketReader> reader_;
  std::unique_ptr<BlimpMessagePump> message_pump_;
  std::unique_ptr<PacketWriter> writer_;
  std::unique_ptr<BlimpMessageProcessor> outgoing_msg_processor_;
  base::ObserverList<ConnectionErrorObserver> error_observers_;

  DISALLOW_COPY_AND_ASSIGN(BlimpConnection);
};

}  // namespace blimp

#endif  // BLIMP_NET_BLIMP_CONNECTION_H_