summaryrefslogtreecommitdiffstats
path: root/blimp/net/browser_connection_handler.cc
blob: 78009cc3343e81d4d7a9d3477f98e2cef65daefb (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
// 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.

#include "blimp/net/browser_connection_handler.h"

#include "base/logging.h"
#include "base/macros.h"
#include "blimp/net/blimp_connection.h"
#include "blimp/net/blimp_message_checkpointer.h"
#include "blimp/net/blimp_message_demultiplexer.h"
#include "blimp/net/blimp_message_multiplexer.h"
#include "blimp/net/blimp_message_output_buffer.h"
#include "blimp/net/blimp_message_processor.h"
#include "net/base/net_errors.h"

namespace blimp {
namespace {

// Maximum footprint of the output buffer.
// TODO(kmarshall): Use a value that's computed from the platform.
const int kMaxBufferSizeBytes = 1 << 24;

}  // namespace

BrowserConnectionHandler::BrowserConnectionHandler()
    : demultiplexer_(new BlimpMessageDemultiplexer),
      output_buffer_(new BlimpMessageOutputBuffer(kMaxBufferSizeBytes)),
      multiplexer_(new BlimpMessageMultiplexer(output_buffer_.get())),
      checkpointer_(new BlimpMessageCheckpointer(demultiplexer_.get(),
                                                 output_buffer_.get(),
                                                 output_buffer_.get())) {}

BrowserConnectionHandler::~BrowserConnectionHandler() {}

scoped_ptr<BlimpMessageProcessor> BrowserConnectionHandler::RegisterFeature(
    BlimpMessage::Type type,
    BlimpMessageProcessor* incoming_processor) {
  demultiplexer_->AddProcessor(type, incoming_processor);
  return multiplexer_->CreateSenderForType(type);
}

void BrowserConnectionHandler::HandleConnection(
    scoped_ptr<BlimpConnection> connection) {
  DCHECK(connection);
  VLOG(1) << "HandleConnection " << connection;

  if (connection_) {
    DropCurrentConnection();
  }
  connection_ = std::move(connection);

  // Hook up message streams to the connection.
  connection_->SetIncomingMessageProcessor(demultiplexer_.get());
  output_buffer_->SetOutputProcessor(
      connection_->GetOutgoingMessageProcessor());
  connection_->AddConnectionErrorObserver(this);
}

void BrowserConnectionHandler::OnConnectionError(int error) {
  DropCurrentConnection();
}

void BrowserConnectionHandler::DropCurrentConnection() {
  DCHECK(connection_);
  output_buffer_->SetOutputProcessor(nullptr);
  connection_.reset();
}

}  // namespace blimp