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.
5 #include "net/socket/client_socket_handle.h"
8 #include "base/bind_helpers.h"
9 #include "base/compiler_specific.h"
10 #include "base/metrics/histogram.h"
11 #include "base/logging.h"
12 #include "net/base/net_errors.h"
13 #include "net/socket/client_socket_pool.h"
14 #include "net/socket/client_socket_pool_histograms.h"
18 ClientSocketHandle::ClientSocketHandle()
19 : is_initialized_(false),
22 reuse_type_(ClientSocketHandle::UNUSED
),
23 callback_(base::Bind(&ClientSocketHandle::OnIOComplete
,
24 base::Unretained(this))),
25 is_ssl_error_(false) {}
27 ClientSocketHandle::~ClientSocketHandle() {
31 void ClientSocketHandle::Reset() {
36 void ClientSocketHandle::ResetInternal(bool cancel
) {
38 if (!group_name_
.empty()) {
39 // If so, we must have a pool.
41 if (is_initialized()) {
43 socket_
->NetLog().EndEvent(NetLog::TYPE_SOCKET_IN_USE
);
44 // Release the socket back to the ClientSocketPool so it can be
46 pool_
->ReleaseSocket(group_name_
, socket_
.Pass(), pool_id_
);
48 // If the handle has been initialized, we should still have a
53 // If we did not get initialized yet and we have a socket
54 // request pending, cancel it.
55 pool_
->CancelRequest(group_name_
, this);
58 is_initialized_
= false;
61 reuse_type_
= ClientSocketHandle::UNUSED
;
62 user_callback_
.Reset();
64 RemoveHigherLayeredPool(higher_pool_
);
66 idle_time_
= base::TimeDelta();
67 init_time_
= base::TimeTicks();
68 setup_time_
= base::TimeDelta();
69 connect_timing_
= LoadTimingInfo::ConnectTiming();
73 void ClientSocketHandle::ResetErrorState() {
74 is_ssl_error_
= false;
75 ssl_error_response_info_
= HttpResponseInfo();
76 pending_http_proxy_connection_
.reset();
79 LoadState
ClientSocketHandle::GetLoadState() const {
80 CHECK(!is_initialized());
81 CHECK(!group_name_
.empty());
82 // Because of http://crbug.com/37810 we may not have a pool, but have
85 return LOAD_STATE_IDLE
;
86 return pool_
->GetLoadState(group_name_
, this);
89 bool ClientSocketHandle::IsPoolStalled() const {
92 return pool_
->IsStalled();
95 void ClientSocketHandle::AddHigherLayeredPool(HigherLayeredPool
* higher_pool
) {
98 // TODO(mmenke): |pool_| should only be NULL in tests. Maybe stop doing that
99 // so this be be made into a DCHECK, and the same can be done in
100 // RemoveHigherLayeredPool?
102 pool_
->AddHigherLayeredPool(higher_pool
);
103 higher_pool_
= higher_pool
;
107 void ClientSocketHandle::RemoveHigherLayeredPool(
108 HigherLayeredPool
* higher_pool
) {
110 CHECK_EQ(higher_pool_
, higher_pool
);
112 pool_
->RemoveHigherLayeredPool(higher_pool
);
117 bool ClientSocketHandle::GetLoadTimingInfo(
119 LoadTimingInfo
* load_timing_info
) const {
120 // Only return load timing information when there's a socket.
124 load_timing_info
->socket_log_id
= socket_
->NetLog().source().id
;
125 load_timing_info
->socket_reused
= is_reused
;
127 // No times if the socket is reused.
131 load_timing_info
->connect_timing
= connect_timing_
;
135 void ClientSocketHandle::SetSocket(scoped_ptr
<StreamSocket
> s
) {
139 void ClientSocketHandle::OnIOComplete(int result
) {
140 CompletionCallback callback
= user_callback_
;
141 user_callback_
.Reset();
142 HandleInitCompletion(result
);
143 callback
.Run(result
);
146 scoped_ptr
<StreamSocket
> ClientSocketHandle::PassSocket() {
147 return socket_
.Pass();
150 void ClientSocketHandle::HandleInitCompletion(int result
) {
151 CHECK_NE(ERR_IO_PENDING
, result
);
152 ClientSocketPoolHistograms
* histograms
= pool_
->histograms();
153 histograms
->AddErrorCode(result
);
156 ResetInternal(false); // Nothing to cancel since the request failed.
158 is_initialized_
= true;
161 is_initialized_
= true;
162 CHECK_NE(-1, pool_id_
) << "Pool should have set |pool_id_| to a valid value.";
163 setup_time_
= base::TimeTicks::Now() - init_time_
;
165 histograms
->AddSocketType(reuse_type());
166 switch (reuse_type()) {
167 case ClientSocketHandle::UNUSED
:
168 histograms
->AddRequestTime(setup_time());
170 case ClientSocketHandle::UNUSED_IDLE
:
171 histograms
->AddUnusedIdleTime(idle_time());
173 case ClientSocketHandle::REUSED_IDLE
:
174 histograms
->AddReusedIdleTime(idle_time());
181 // Broadcast that the socket has been acquired.
182 // TODO(eroman): This logging is not complete, in particular set_socket() and
183 // release() socket. It ends up working though, since those methods are being
184 // used to layer sockets (and the destination sources are the same).
185 DCHECK(socket_
.get());
186 socket_
->NetLog().BeginEvent(
187 NetLog::TYPE_SOCKET_IN_USE
,
188 requesting_source_
.ToEventParametersCallback());