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/logging.h"
11 #include "net/base/net_errors.h"
12 #include "net/socket/client_socket_pool.h"
16 ClientSocketHandle::ClientSocketHandle()
17 : is_initialized_(false),
20 reuse_type_(ClientSocketHandle::UNUSED
),
21 callback_(base::Bind(&ClientSocketHandle::OnIOComplete
,
22 base::Unretained(this))),
24 ssl_failure_state_(SSL_FAILURE_NONE
) {
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 ssl_failure_state_
= SSL_FAILURE_NONE
;
77 pending_http_proxy_connection_
.reset();
80 LoadState
ClientSocketHandle::GetLoadState() const {
81 CHECK(!is_initialized());
82 CHECK(!group_name_
.empty());
83 // Because of http://crbug.com/37810 we may not have a pool, but have
86 return LOAD_STATE_IDLE
;
87 return pool_
->GetLoadState(group_name_
, this);
90 bool ClientSocketHandle::IsPoolStalled() const {
93 return pool_
->IsStalled();
96 void ClientSocketHandle::AddHigherLayeredPool(HigherLayeredPool
* higher_pool
) {
99 // TODO(mmenke): |pool_| should only be NULL in tests. Maybe stop doing that
100 // so this be be made into a DCHECK, and the same can be done in
101 // RemoveHigherLayeredPool?
103 pool_
->AddHigherLayeredPool(higher_pool
);
104 higher_pool_
= higher_pool
;
108 void ClientSocketHandle::RemoveHigherLayeredPool(
109 HigherLayeredPool
* higher_pool
) {
111 CHECK_EQ(higher_pool_
, higher_pool
);
113 pool_
->RemoveHigherLayeredPool(higher_pool
);
118 bool ClientSocketHandle::GetLoadTimingInfo(
120 LoadTimingInfo
* load_timing_info
) const {
121 // Only return load timing information when there's a socket.
125 load_timing_info
->socket_log_id
= socket_
->NetLog().source().id
;
126 load_timing_info
->socket_reused
= is_reused
;
128 // No times if the socket is reused.
132 load_timing_info
->connect_timing
= connect_timing_
;
136 void ClientSocketHandle::SetSocket(scoped_ptr
<StreamSocket
> s
) {
140 void ClientSocketHandle::OnIOComplete(int result
) {
141 CompletionCallback callback
= user_callback_
;
142 user_callback_
.Reset();
143 HandleInitCompletion(result
);
144 callback
.Run(result
);
147 scoped_ptr
<StreamSocket
> ClientSocketHandle::PassSocket() {
148 return socket_
.Pass();
151 void ClientSocketHandle::HandleInitCompletion(int result
) {
152 CHECK_NE(ERR_IO_PENDING
, result
);
155 ResetInternal(false); // Nothing to cancel since the request failed.
157 is_initialized_
= true;
160 is_initialized_
= true;
161 CHECK_NE(-1, pool_id_
) << "Pool should have set |pool_id_| to a valid value.";
162 setup_time_
= base::TimeTicks::Now() - init_time_
;
164 // Broadcast that the socket has been acquired.
165 // TODO(eroman): This logging is not complete, in particular set_socket() and
166 // release() socket. It ends up working though, since those methods are being
167 // used to layer sockets (and the destination sources are the same).
168 DCHECK(socket_
.get());
169 socket_
->NetLog().BeginEvent(
170 NetLog::TYPE_SOCKET_IN_USE
,
171 requesting_source_
.ToEventParametersCallback());