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"
17 ClientSocketHandle::ClientSocketHandle()
18 : is_initialized_(false),
21 reuse_type_(ClientSocketHandle::UNUSED
),
22 callback_(base::Bind(&ClientSocketHandle::OnIOComplete
,
23 base::Unretained(this))),
25 ssl_failure_state_(SSL_FAILURE_NONE
) {
28 ClientSocketHandle::~ClientSocketHandle() {
32 void ClientSocketHandle::Reset() {
37 void ClientSocketHandle::ResetInternal(bool cancel
) {
39 if (!group_name_
.empty()) {
40 // If so, we must have a pool.
42 if (is_initialized()) {
44 socket_
->NetLog().EndEvent(NetLog::TYPE_SOCKET_IN_USE
);
45 // Release the socket back to the ClientSocketPool so it can be
47 pool_
->ReleaseSocket(group_name_
, socket_
.Pass(), pool_id_
);
49 // If the handle has been initialized, we should still have a
54 // If we did not get initialized yet and we have a socket
55 // request pending, cancel it.
56 pool_
->CancelRequest(group_name_
, this);
59 is_initialized_
= false;
62 reuse_type_
= ClientSocketHandle::UNUSED
;
63 user_callback_
.Reset();
65 RemoveHigherLayeredPool(higher_pool_
);
67 idle_time_
= base::TimeDelta();
68 init_time_
= base::TimeTicks();
69 setup_time_
= base::TimeDelta();
70 connect_timing_
= LoadTimingInfo::ConnectTiming();
74 void ClientSocketHandle::ResetErrorState() {
75 is_ssl_error_
= false;
76 ssl_error_response_info_
= HttpResponseInfo();
77 ssl_failure_state_
= SSL_FAILURE_NONE
;
78 pending_http_proxy_connection_
.reset();
81 LoadState
ClientSocketHandle::GetLoadState() const {
82 CHECK(!is_initialized());
83 CHECK(!group_name_
.empty());
84 // Because of http://crbug.com/37810 we may not have a pool, but have
87 return LOAD_STATE_IDLE
;
88 return pool_
->GetLoadState(group_name_
, this);
91 bool ClientSocketHandle::IsPoolStalled() const {
94 return pool_
->IsStalled();
97 void ClientSocketHandle::AddHigherLayeredPool(HigherLayeredPool
* higher_pool
) {
100 // TODO(mmenke): |pool_| should only be NULL in tests. Maybe stop doing that
101 // so this be be made into a DCHECK, and the same can be done in
102 // RemoveHigherLayeredPool?
104 pool_
->AddHigherLayeredPool(higher_pool
);
105 higher_pool_
= higher_pool
;
109 void ClientSocketHandle::RemoveHigherLayeredPool(
110 HigherLayeredPool
* higher_pool
) {
112 CHECK_EQ(higher_pool_
, higher_pool
);
114 pool_
->RemoveHigherLayeredPool(higher_pool
);
119 bool ClientSocketHandle::GetLoadTimingInfo(
121 LoadTimingInfo
* load_timing_info
) const {
122 // Only return load timing information when there's a socket.
126 load_timing_info
->socket_log_id
= socket_
->NetLog().source().id
;
127 load_timing_info
->socket_reused
= is_reused
;
129 // No times if the socket is reused.
133 load_timing_info
->connect_timing
= connect_timing_
;
137 void ClientSocketHandle::SetSocket(scoped_ptr
<StreamSocket
> s
) {
141 void ClientSocketHandle::OnIOComplete(int result
) {
142 CompletionCallback callback
= user_callback_
;
143 user_callback_
.Reset();
144 HandleInitCompletion(result
);
145 callback
.Run(result
);
148 scoped_ptr
<StreamSocket
> ClientSocketHandle::PassSocket() {
149 return socket_
.Pass();
152 void ClientSocketHandle::HandleInitCompletion(int result
) {
153 CHECK_NE(ERR_IO_PENDING
, 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 // Broadcast that the socket has been acquired.
166 // TODO(eroman): This logging is not complete, in particular set_socket() and
167 // release() socket. It ends up working though, since those methods are being
168 // used to layer sockets (and the destination sources are the same).
169 DCHECK(socket_
.get());
170 socket_
->NetLog().BeginEvent(
171 NetLog::TYPE_SOCKET_IN_USE
,
172 requesting_source_
.ToEventParametersCallback());