1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef NET_HTTP_HTTP_PIPELINED_HOST_IMPL_H_
6#define NET_HTTP_HTTP_PIPELINED_HOST_IMPL_H_
7
8#include <map>
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/memory/scoped_ptr.h"
13#include "net/base/host_port_pair.h"
14#include "net/base/net_export.h"
15#include "net/http/http_pipelined_connection.h"
16#include "net/http/http_pipelined_host.h"
17#include "net/http/http_pipelined_host_capability.h"
18
19namespace base {
20class Value;
21}
22
23namespace net {
24
25class BoundNetLog;
26class ClientSocketHandle;
27class HttpPipelinedStream;
28class ProxyInfo;
29struct SSLConfig;
30
31// Manages all of the pipelining state for specific host with active pipelined
32// HTTP requests. Manages connection jobs, constructs pipelined streams, and
33// assigns requests to the least loaded pipelined connection.
34class NET_EXPORT_PRIVATE HttpPipelinedHostImpl
35    : public HttpPipelinedHost,
36      public HttpPipelinedConnection::Delegate {
37 public:
38  HttpPipelinedHostImpl(HttpPipelinedHost::Delegate* delegate,
39                        const HttpPipelinedHost::Key& key,
40                        HttpPipelinedConnection::Factory* factory,
41                        HttpPipelinedHostCapability capability);
42  virtual ~HttpPipelinedHostImpl();
43
44  // HttpPipelinedHost interface
45  virtual HttpPipelinedStream* CreateStreamOnNewPipeline(
46      ClientSocketHandle* connection,
47      const SSLConfig& used_ssl_config,
48      const ProxyInfo& used_proxy_info,
49      const BoundNetLog& net_log,
50      bool was_npn_negotiated,
51      NextProto protocol_negotiated) OVERRIDE;
52
53  virtual HttpPipelinedStream* CreateStreamOnExistingPipeline() OVERRIDE;
54
55  virtual bool IsExistingPipelineAvailable() const OVERRIDE;
56
57  // HttpPipelinedConnection::Delegate interface
58
59  // Called when a pipelined connection completes a request. Adds a pending
60  // request to the pipeline if the pipeline is still usable.
61  virtual void OnPipelineHasCapacity(
62      HttpPipelinedConnection* pipeline) OVERRIDE;
63
64  virtual void OnPipelineFeedback(
65      HttpPipelinedConnection* pipeline,
66      HttpPipelinedConnection::Feedback feedback) OVERRIDE;
67
68  virtual const Key& GetKey() const OVERRIDE;
69
70  // Creates a Value summary of this host's |pipelines_|. Caller assumes
71  // ownership of the returned Value.
72  virtual base::Value* PipelineInfoToValue() const OVERRIDE;
73
74  // Returns the maximum number of in-flight pipelined requests we'll allow on a
75  // single connection.
76  static int max_pipeline_depth() { return 3; }
77
78 private:
79  struct PipelineInfo {
80    PipelineInfo();
81
82    int num_successes;
83  };
84  typedef std::map<HttpPipelinedConnection*, PipelineInfo> PipelineInfoMap;
85
86  // Called when a pipeline is empty and there are no pending requests. Closes
87  // the connection.
88  void OnPipelineEmpty(HttpPipelinedConnection* pipeline);
89
90  // Adds the next pending request to the pipeline if it's still usuable.
91  void AddRequestToPipeline(HttpPipelinedConnection* pipeline);
92
93  // Returns the current pipeline capacity based on |capability_|. This should
94  // not be called if |capability_| is INCAPABLE.
95  int GetPipelineCapacity() const;
96
97  // Returns true if |pipeline| can handle a new request. This is true if the
98  // |pipeline| is active, usable, has capacity, and |capability_| is
99  // sufficient.
100  bool CanPipelineAcceptRequests(HttpPipelinedConnection* pipeline) const;
101
102  // Called when |this| moves from UNKNOWN |capability_| to PROBABLY_CAPABLE.
103  // Causes all pipelines to increase capacity to start pipelining.
104  void NotifyAllPipelinesHaveCapacity();
105
106  HttpPipelinedHost::Delegate* delegate_;
107  const Key key_;
108  PipelineInfoMap pipelines_;
109  scoped_ptr<HttpPipelinedConnection::Factory> factory_;
110  HttpPipelinedHostCapability capability_;
111
112  DISALLOW_COPY_AND_ASSIGN(HttpPipelinedHostImpl);
113};
114
115}  // namespace net
116
117#endif  // NET_HTTP_HTTP_PIPELINED_HOST_IMPL_H_
118