summaryrefslogtreecommitdiffstats
path: root/net/http/http_pipelined_host_forced.cc
blob: 000936c973f31d844f67e98794aa54b32ab533d3 (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
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
// Copyright (c) 2012 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 "net/http/http_pipelined_host_forced.h"

#include "base/values.h"
#include "net/http/http_pipelined_connection_impl.h"
#include "net/http/http_pipelined_stream.h"
#include "net/socket/buffered_write_stream_socket.h"
#include "net/socket/client_socket_handle.h"

using base::DictionaryValue;
using base::ListValue;
using base::Value;

namespace net {

HttpPipelinedHostForced::HttpPipelinedHostForced(
    HttpPipelinedHost::Delegate* delegate,
    const Key& key,
    HttpPipelinedConnection::Factory* factory)
    : delegate_(delegate),
      key_(key),
      factory_(factory) {
  if (!factory) {
    factory_.reset(new HttpPipelinedConnectionImpl::Factory());
  }
}

HttpPipelinedHostForced::~HttpPipelinedHostForced() {
  CHECK(!pipeline_.get());
}

HttpPipelinedStream* HttpPipelinedHostForced::CreateStreamOnNewPipeline(
    ClientSocketHandle* connection,
    const SSLConfig& used_ssl_config,
    const ProxyInfo& used_proxy_info,
    const BoundNetLog& net_log,
    bool was_npn_negotiated,
    NextProto protocol_negotiated) {
  CHECK(!pipeline_.get());
  StreamSocket* wrapped_socket = connection->release_socket();
  BufferedWriteStreamSocket* buffered_socket = new BufferedWriteStreamSocket(
      wrapped_socket);
  connection->set_socket(buffered_socket);
  pipeline_.reset(factory_->CreateNewPipeline(
      connection, this, key_.origin(), used_ssl_config, used_proxy_info,
      net_log, was_npn_negotiated, protocol_negotiated));
  return pipeline_->CreateNewStream();
}

HttpPipelinedStream* HttpPipelinedHostForced::CreateStreamOnExistingPipeline() {
  if (!pipeline_.get()) {
    return NULL;
  }
  return pipeline_->CreateNewStream();
}

bool HttpPipelinedHostForced::IsExistingPipelineAvailable() const {
  return pipeline_.get() != NULL;
}

const HttpPipelinedHost::Key& HttpPipelinedHostForced::GetKey() const {
  return key_;
}

void HttpPipelinedHostForced::OnPipelineEmpty(
    HttpPipelinedConnection* pipeline) {
  CHECK_EQ(pipeline_.get(), pipeline);
  pipeline_.reset();
  delegate_->OnHostIdle(this);
  // WARNING: We'll probably be deleted here.
}

void HttpPipelinedHostForced::OnPipelineHasCapacity(
    HttpPipelinedConnection* pipeline) {
  CHECK_EQ(pipeline_.get(), pipeline);
  delegate_->OnHostHasAdditionalCapacity(this);
  if (!pipeline->depth()) {
    OnPipelineEmpty(pipeline);
    // WARNING: We might be deleted here.
  }
}

void HttpPipelinedHostForced::OnPipelineFeedback(
    HttpPipelinedConnection* pipeline,
    HttpPipelinedConnection::Feedback feedback) {
  // We don't care. We always pipeline.
}

Value* HttpPipelinedHostForced::PipelineInfoToValue() const {
  ListValue* list_value = new ListValue();
  if (pipeline_.get()) {
    DictionaryValue* pipeline_dict = new DictionaryValue;
    pipeline_dict->SetString("host", key_.origin().ToString());
    pipeline_dict->SetBoolean("forced", true);
    pipeline_dict->SetInteger("depth", pipeline_->depth());
    pipeline_dict->SetInteger("capacity", 1000);
    pipeline_dict->SetBoolean("usable", pipeline_->usable());
    pipeline_dict->SetBoolean("active", pipeline_->active());
    pipeline_dict->SetInteger("source_id", pipeline_->net_log().source().id);
    list_value->Append(pipeline_dict);
  }
  return list_value;
}

}  // namespace net