summaryrefslogtreecommitdiffstats
path: root/blimp/net/browser_connection_handler.h
blob: 0304ae15c6e960dab4fefcd701bdb29107c3c986 (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
// 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_BROWSER_CONNECTION_HANDLER_H_
#define BLIMP_NET_BROWSER_CONNECTION_HANDLER_H_

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "blimp/common/proto/blimp_message.pb.h"
#include "blimp/net/blimp_net_export.h"
#include "blimp/net/connection_error_observer.h"
#include "blimp/net/connection_handler.h"

namespace blimp {

class BlimpConnection;
class BlimpMessageCheckpointer;
class BlimpMessageDemultiplexer;
class BlimpMessageMultiplexer;
class BlimpMessageOutputBuffer;
class BlimpMessageProcessor;

// Routes incoming messages to their respective features, and buffers and sends
// messages out via underlying BlimpConnection.
// A BrowserConnectionHandler is created on browser startup, and persists for
// the lifetime of the application.
// BrowserConnectionHandler is created on the UI thread, and then used and
// destroyed on the IO thread.
class BLIMP_NET_EXPORT BrowserConnectionHandler
    : public ConnectionHandler,
      public ConnectionErrorObserver {
 public:
  BrowserConnectionHandler();
  ~BrowserConnectionHandler() override;

  // Registers a message processor which will receive all messages of the |type|
  // specified. Only one handler may be added per type.
  // That caller must ensure |incoming_processor| remains valid while
  // this object is in-use.
  //
  // Returns a BlimpMessageProcessor object for sending messages of type |type|.
  virtual scoped_ptr<BlimpMessageProcessor> RegisterFeature(
      BlimpMessage::Type type,
      BlimpMessageProcessor* incoming_processor);

  // ConnectionHandler implementation.
  void HandleConnection(scoped_ptr<BlimpConnection> connection) override;

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

 private:
  void DropCurrentConnection();

  // Routes incoming messages to the relevant feature-specific handlers.
  scoped_ptr<BlimpMessageDemultiplexer> demultiplexer_;

  // Provides buffering of outgoing messages, for use in session-recovery.
  scoped_ptr<BlimpMessageOutputBuffer> output_buffer_;

  // Routes outgoing messages from feature-specific handlers to a single
  // message stream.
  scoped_ptr<BlimpMessageMultiplexer> multiplexer_;

  // Dispatches checkpoint/ACK messages to the outgoing processor, as the
  // incoming processor completes processing them.
  scoped_ptr<BlimpMessageCheckpointer> checkpointer_;

  // Holds network resources while there is a Client connected.
  scoped_ptr<BlimpConnection> connection_;

  DISALLOW_COPY_AND_ASSIGN(BrowserConnectionHandler);
};

}  // namespace blimp

#endif  // BLIMP_NET_BROWSER_CONNECTION_HANDLER_H_