Convert raw pointers to scoped_ptr in net module.
[chromium-blink-merge.git] / net / socket / client_socket_handle.cc
blobd5c17005ad433cf3f2bfc48a14ea671721f978b0
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"
7 #include "base/bind.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"
15 namespace net {
17 ClientSocketHandle::ClientSocketHandle()
18 : is_initialized_(false),
19 pool_(NULL),
20 higher_pool_(NULL),
21 reuse_type_(ClientSocketHandle::UNUSED),
22 callback_(base::Bind(&ClientSocketHandle::OnIOComplete,
23 base::Unretained(this))),
24 is_ssl_error_(false),
25 ssl_failure_state_(SSL_FAILURE_NONE) {
28 ClientSocketHandle::~ClientSocketHandle() {
29 Reset();
32 void ClientSocketHandle::Reset() {
33 ResetInternal(true);
34 ResetErrorState();
37 void ClientSocketHandle::ResetInternal(bool cancel) {
38 // Was Init called?
39 if (!group_name_.empty()) {
40 // If so, we must have a pool.
41 CHECK(pool_);
42 if (is_initialized()) {
43 if (socket_) {
44 socket_->NetLog().EndEvent(NetLog::TYPE_SOCKET_IN_USE);
45 // Release the socket back to the ClientSocketPool so it can be
46 // deleted or reused.
47 pool_->ReleaseSocket(group_name_, socket_.Pass(), pool_id_);
48 } else {
49 // If the handle has been initialized, we should still have a
50 // socket.
51 NOTREACHED();
53 } else if (cancel) {
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;
60 socket_.reset();
61 group_name_.clear();
62 reuse_type_ = ClientSocketHandle::UNUSED;
63 user_callback_.Reset();
64 if (higher_pool_)
65 RemoveHigherLayeredPool(higher_pool_);
66 pool_ = NULL;
67 idle_time_ = base::TimeDelta();
68 init_time_ = base::TimeTicks();
69 setup_time_ = base::TimeDelta();
70 connect_timing_ = LoadTimingInfo::ConnectTiming();
71 pool_id_ = -1;
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
85 // just a raw socket.
86 if (!pool_)
87 return LOAD_STATE_IDLE;
88 return pool_->GetLoadState(group_name_, this);
91 bool ClientSocketHandle::IsPoolStalled() const {
92 if (!pool_)
93 return false;
94 return pool_->IsStalled();
97 void ClientSocketHandle::AddHigherLayeredPool(HigherLayeredPool* higher_pool) {
98 CHECK(higher_pool);
99 CHECK(!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?
103 if (pool_) {
104 pool_->AddHigherLayeredPool(higher_pool);
105 higher_pool_ = higher_pool;
109 void ClientSocketHandle::RemoveHigherLayeredPool(
110 HigherLayeredPool* higher_pool) {
111 CHECK(higher_pool_);
112 CHECK_EQ(higher_pool_, higher_pool);
113 if (pool_) {
114 pool_->RemoveHigherLayeredPool(higher_pool);
115 higher_pool_ = NULL;
119 bool ClientSocketHandle::GetLoadTimingInfo(
120 bool is_reused,
121 LoadTimingInfo* load_timing_info) const {
122 // Only return load timing information when there's a socket.
123 if (!socket_)
124 return false;
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.
130 if (is_reused)
131 return true;
133 load_timing_info->connect_timing = connect_timing_;
134 return true;
137 void ClientSocketHandle::SetSocket(scoped_ptr<StreamSocket> s) {
138 socket_ = s.Pass();
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);
154 if (result != OK) {
155 if (!socket_.get())
156 ResetInternal(false); // Nothing to cancel since the request failed.
157 else
158 is_initialized_ = true;
159 return;
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());
175 } // namespace net