| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/socket/client_socket_handle.h" | 5 #include "net/socket/client_socket_handle.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/histogram.h" | 8 #include "base/histogram.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 11 #include "net/socket/client_socket_pool.h" | 11 #include "net/socket/client_socket_pool.h" |
| 12 #include "net/socket/client_socket_pool_histograms.h" | 12 #include "net/socket/client_socket_pool_histograms.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 ClientSocketHandle::ClientSocketHandle() | 16 ClientSocketHandle::ClientSocketHandle() |
| 17 : socket_(NULL), | 17 : is_initialized_(false), |
| 18 is_reused_(false), | 18 is_reused_(false), |
| 19 ALLOW_THIS_IN_INITIALIZER_LIST( | 19 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 20 callback_(this, &ClientSocketHandle::OnIOComplete)), | 20 callback_(this, &ClientSocketHandle::OnIOComplete)), |
| 21 is_ssl_error_(false) {} | 21 is_ssl_error_(false) {} |
| 22 | 22 |
| 23 ClientSocketHandle::~ClientSocketHandle() { | 23 ClientSocketHandle::~ClientSocketHandle() { |
| 24 Reset(); | 24 Reset(); |
| 25 } | 25 } |
| 26 | 26 |
| 27 void ClientSocketHandle::Reset() { | 27 void ClientSocketHandle::Reset() { |
| 28 ResetInternal(true); | 28 ResetInternal(true); |
| 29 ResetErrorState(); | 29 ResetErrorState(); |
| 30 } | 30 } |
| 31 | 31 |
| 32 void ClientSocketHandle::ResetInternal(bool cancel) { | 32 void ClientSocketHandle::ResetInternal(bool cancel) { |
| 33 if (group_name_.empty()) // Was Init called? | 33 if (group_name_.empty()) // Was Init called? |
| 34 return; | 34 return; |
| 35 if (socket_.get()) { | 35 if (is_initialized()) { |
| 36 // Because of http://crbug.com/37810 we may not have a pool, but have | 36 // Because of http://crbug.com/37810 we may not have a pool, but have |
| 37 // just a raw socket. | 37 // just a raw socket. |
| 38 socket_->NetLog().EndEvent(NetLog::TYPE_SOCKET_IN_USE, NULL); | 38 socket_->NetLog().EndEvent(NetLog::TYPE_SOCKET_IN_USE, NULL); |
| 39 if (pool_) | 39 if (pool_) |
| 40 // If we've still got a socket, release it back to the ClientSocketPool so | 40 // If we've still got a socket, release it back to the ClientSocketPool so |
| 41 // it can be deleted or reused. | 41 // it can be deleted or reused. |
| 42 pool_->ReleaseSocket(group_name_, release_socket(), pool_id_); | 42 pool_->ReleaseSocket(group_name_, release_socket(), pool_id_); |
| 43 } else if (cancel) { | 43 } else if (cancel) { |
| 44 // If we did not get initialized yet, so we've got a socket request pending. | 44 // If we did not get initialized yet, we've got a socket request pending. |
| 45 // Cancel it. | 45 // Cancel it. |
| 46 pool_->CancelRequest(group_name_, this); | 46 pool_->CancelRequest(group_name_, this); |
| 47 } | 47 } |
| 48 is_initialized_ = false; |
| 48 group_name_.clear(); | 49 group_name_.clear(); |
| 49 is_reused_ = false; | 50 is_reused_ = false; |
| 50 user_callback_ = NULL; | 51 user_callback_ = NULL; |
| 51 pool_ = NULL; | 52 pool_ = NULL; |
| 52 idle_time_ = base::TimeDelta(); | 53 idle_time_ = base::TimeDelta(); |
| 53 init_time_ = base::TimeTicks(); | 54 init_time_ = base::TimeTicks(); |
| 54 setup_time_ = base::TimeDelta(); | 55 setup_time_ = base::TimeDelta(); |
| 55 pool_id_ = -1; | 56 pool_id_ = -1; |
| 56 } | 57 } |
| 57 | 58 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 75 user_callback_ = NULL; | 76 user_callback_ = NULL; |
| 76 HandleInitCompletion(result); | 77 HandleInitCompletion(result); |
| 77 callback->Run(result); | 78 callback->Run(result); |
| 78 } | 79 } |
| 79 | 80 |
| 80 void ClientSocketHandle::HandleInitCompletion(int result) { | 81 void ClientSocketHandle::HandleInitCompletion(int result) { |
| 81 CHECK_NE(ERR_IO_PENDING, result); | 82 CHECK_NE(ERR_IO_PENDING, result); |
| 82 if (result != OK) { | 83 if (result != OK) { |
| 83 if (!socket_.get()) | 84 if (!socket_.get()) |
| 84 ResetInternal(false); // Nothing to cancel since the request failed. | 85 ResetInternal(false); // Nothing to cancel since the request failed. |
| 86 else |
| 87 is_initialized_ = true; |
| 85 return; | 88 return; |
| 86 } | 89 } |
| 90 is_initialized_ = true; |
| 87 CHECK_NE(-1, pool_id_) << "Pool should have set |pool_id_| to a valid value."; | 91 CHECK_NE(-1, pool_id_) << "Pool should have set |pool_id_| to a valid value."; |
| 88 setup_time_ = base::TimeTicks::Now() - init_time_; | 92 setup_time_ = base::TimeTicks::Now() - init_time_; |
| 89 | 93 |
| 90 scoped_refptr<ClientSocketPoolHistograms> histograms = pool_->histograms(); | 94 scoped_refptr<ClientSocketPoolHistograms> histograms = pool_->histograms(); |
| 91 histograms->AddSocketType(reuse_type()); | 95 histograms->AddSocketType(reuse_type()); |
| 92 switch (reuse_type()) { | 96 switch (reuse_type()) { |
| 93 case ClientSocketHandle::UNUSED: | 97 case ClientSocketHandle::UNUSED: |
| 94 histograms->AddRequestTime(setup_time()); | 98 histograms->AddRequestTime(setup_time()); |
| 95 break; | 99 break; |
| 96 case ClientSocketHandle::UNUSED_IDLE: | 100 case ClientSocketHandle::UNUSED_IDLE: |
| (...skipping 11 matching lines...) Expand all Loading... |
| 108 // TODO(eroman): This logging is not complete, in particular set_socket() and | 112 // TODO(eroman): This logging is not complete, in particular set_socket() and |
| 109 // release() socket. It ends up working though, since those methods are being | 113 // release() socket. It ends up working though, since those methods are being |
| 110 // used to layer sockets (and the destination sources are the same). | 114 // used to layer sockets (and the destination sources are the same). |
| 111 DCHECK(socket_.get()); | 115 DCHECK(socket_.get()); |
| 112 socket_->NetLog().BeginEvent( | 116 socket_->NetLog().BeginEvent( |
| 113 NetLog::TYPE_SOCKET_IN_USE, | 117 NetLog::TYPE_SOCKET_IN_USE, |
| 114 new NetLogSourceParameter("source_dependency", requesting_source_)); | 118 new NetLogSourceParameter("source_dependency", requesting_source_)); |
| 115 } | 119 } |
| 116 | 120 |
| 117 } // namespace net | 121 } // namespace net |
| OLD | NEW |