This source file includes following definitions.
- capability_
- CreateStreamOnNewPipeline
- CreateStreamOnExistingPipeline
- IsExistingPipelineAvailable
- GetKey
- OnPipelineEmpty
- OnPipelineHasCapacity
- OnPipelineFeedback
- GetPipelineCapacity
- CanPipelineAcceptRequests
- NotifyAllPipelinesHaveCapacity
- PipelineInfoToValue
#include "net/http/http_pipelined_host_impl.h"
#include "base/stl_util.h"
#include "base/values.h"
#include "net/http/http_pipelined_connection_impl.h"
#include "net/http/http_pipelined_stream.h"
namespace net {
static const int kNumKnownSuccessesThreshold = 3;
HttpPipelinedHostImpl::HttpPipelinedHostImpl(
HttpPipelinedHost::Delegate* delegate,
const HttpPipelinedHost::Key& key,
HttpPipelinedConnection::Factory* factory,
HttpPipelinedHostCapability capability)
: delegate_(delegate),
key_(key),
factory_(factory),
capability_(capability) {
if (!factory) {
factory_.reset(new HttpPipelinedConnectionImpl::Factory());
}
}
HttpPipelinedHostImpl::~HttpPipelinedHostImpl() {
CHECK(pipelines_.empty());
}
HttpPipelinedStream* HttpPipelinedHostImpl::CreateStreamOnNewPipeline(
ClientSocketHandle* connection,
const SSLConfig& used_ssl_config,
const ProxyInfo& used_proxy_info,
const BoundNetLog& net_log,
bool was_npn_negotiated,
NextProto protocol_negotiated) {
if (capability_ == PIPELINE_INCAPABLE) {
return NULL;
}
HttpPipelinedConnection* pipeline = factory_->CreateNewPipeline(
connection, this, key_.origin(), used_ssl_config, used_proxy_info,
net_log, was_npn_negotiated, protocol_negotiated);
PipelineInfo info;
pipelines_.insert(std::make_pair(pipeline, info));
return pipeline->CreateNewStream();
}
HttpPipelinedStream* HttpPipelinedHostImpl::CreateStreamOnExistingPipeline() {
HttpPipelinedConnection* available_pipeline = NULL;
for (PipelineInfoMap::iterator it = pipelines_.begin();
it != pipelines_.end(); ++it) {
if (CanPipelineAcceptRequests(it->first) &&
(!available_pipeline ||
it->first->depth() < available_pipeline->depth())) {
available_pipeline = it->first;
}
}
if (!available_pipeline) {
return NULL;
}
return available_pipeline->CreateNewStream();
}
bool HttpPipelinedHostImpl::IsExistingPipelineAvailable() const {
for (PipelineInfoMap::const_iterator it = pipelines_.begin();
it != pipelines_.end(); ++it) {
if (CanPipelineAcceptRequests(it->first)) {
return true;
}
}
return false;
}
const HttpPipelinedHost::Key& HttpPipelinedHostImpl::GetKey() const {
return key_;
}
void HttpPipelinedHostImpl::OnPipelineEmpty(HttpPipelinedConnection* pipeline) {
CHECK(ContainsKey(pipelines_, pipeline));
pipelines_.erase(pipeline);
delete pipeline;
if (pipelines_.empty()) {
delegate_->OnHostIdle(this);
}
}
void HttpPipelinedHostImpl::OnPipelineHasCapacity(
HttpPipelinedConnection* pipeline) {
CHECK(ContainsKey(pipelines_, pipeline));
if (CanPipelineAcceptRequests(pipeline)) {
delegate_->OnHostHasAdditionalCapacity(this);
}
if (!pipeline->depth()) {
OnPipelineEmpty(pipeline);
}
}
void HttpPipelinedHostImpl::OnPipelineFeedback(
HttpPipelinedConnection* pipeline,
HttpPipelinedConnection::Feedback feedback) {
CHECK(ContainsKey(pipelines_, pipeline));
switch (feedback) {
case HttpPipelinedConnection::OK:
++pipelines_[pipeline].num_successes;
if (capability_ == PIPELINE_UNKNOWN) {
capability_ = PIPELINE_PROBABLY_CAPABLE;
NotifyAllPipelinesHaveCapacity();
} else if (capability_ == PIPELINE_PROBABLY_CAPABLE &&
pipelines_[pipeline].num_successes >=
kNumKnownSuccessesThreshold) {
capability_ = PIPELINE_CAPABLE;
delegate_->OnHostDeterminedCapability(this, PIPELINE_CAPABLE);
}
break;
case HttpPipelinedConnection::PIPELINE_SOCKET_ERROR:
if (pipelines_[pipeline].num_successes > 0 || pipeline->depth() > 1) {
capability_ = PIPELINE_INCAPABLE;
delegate_->OnHostDeterminedCapability(this, PIPELINE_INCAPABLE);
}
break;
case HttpPipelinedConnection::OLD_HTTP_VERSION:
case HttpPipelinedConnection::AUTHENTICATION_REQUIRED:
capability_ = PIPELINE_INCAPABLE;
delegate_->OnHostDeterminedCapability(this, PIPELINE_INCAPABLE);
break;
case HttpPipelinedConnection::MUST_CLOSE_CONNECTION:
break;
}
}
int HttpPipelinedHostImpl::GetPipelineCapacity() const {
int capacity = 0;
switch (capability_) {
case PIPELINE_CAPABLE:
case PIPELINE_PROBABLY_CAPABLE:
capacity = max_pipeline_depth();
break;
case PIPELINE_INCAPABLE:
CHECK(false);
case PIPELINE_UNKNOWN:
capacity = 1;
break;
default:
CHECK(false) << "Unkown pipeline capability: " << capability_;
}
return capacity;
}
bool HttpPipelinedHostImpl::CanPipelineAcceptRequests(
HttpPipelinedConnection* pipeline) const {
return capability_ != PIPELINE_INCAPABLE &&
pipeline->usable() &&
pipeline->active() &&
pipeline->depth() < GetPipelineCapacity();
}
void HttpPipelinedHostImpl::NotifyAllPipelinesHaveCapacity() {
PipelineInfoMap pipelines_to_notify = pipelines_;
for (PipelineInfoMap::iterator it = pipelines_to_notify.begin();
it != pipelines_to_notify.end(); ++it) {
if (pipelines_.find(it->first) != pipelines_.end()) {
OnPipelineHasCapacity(it->first);
}
}
}
base::Value* HttpPipelinedHostImpl::PipelineInfoToValue() const {
base::ListValue* list_value = new base::ListValue();
for (PipelineInfoMap::const_iterator it = pipelines_.begin();
it != pipelines_.end(); ++it) {
base::DictionaryValue* pipeline_dict = new base::DictionaryValue;
pipeline_dict->SetString("host", key_.origin().ToString());
pipeline_dict->SetBoolean("forced", false);
pipeline_dict->SetInteger("depth", it->first->depth());
pipeline_dict->SetInteger("capacity", GetPipelineCapacity());
pipeline_dict->SetBoolean("usable", it->first->usable());
pipeline_dict->SetBoolean("active", it->first->active());
pipeline_dict->SetInteger("source_id", it->first->net_log().source().id);
list_value->Append(pipeline_dict);
}
return list_value;
}
HttpPipelinedHostImpl::PipelineInfo::PipelineInfo()
: num_successes(0) {
}
}