1 // Copyright (c) 2006-2008 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. 4 5 #include "net/socket/client_socket_handle.h" 6 7 #include "base/compiler_specific.h" 8 #include "base/logging.h" 9 #include "net/base/net_errors.h" 10 #include "net/socket/client_socket_pool.h" 11 12 namespace net { 13 14 ClientSocketHandle::ClientSocketHandle() 15 : socket_(NULL), 16 is_reused_(false), 17 ALLOW_THIS_IN_INITIALIZER_LIST( 18 callback_(this, &ClientSocketHandle::OnIOComplete)) {} 19 20 ClientSocketHandle::~ClientSocketHandle() { 21 Reset(); 22 } 23 24 void ClientSocketHandle::Reset() { 25 ResetInternal(true); 26 } 27 28 void ClientSocketHandle::ResetInternal(bool cancel) { 29 if (group_name_.empty()) // Was Init called? 30 return; 31 if (socket_.get()) { 32 // If we've still got a socket, release it back to the ClientSocketPool so 33 // it can be deleted or reused. 34 pool_->ReleaseSocket(group_name_, release_socket()); 35 } else if (cancel) { 36 // If we did not get initialized yet, so we've got a socket request pending. 37 // Cancel it. 38 pool_->CancelRequest(group_name_, this); 39 } 40 group_name_.clear(); 41 is_reused_ = false; 42 user_callback_ = NULL; 43 pool_ = NULL; 44 idle_time_ = base::TimeDelta(); 45 init_time_ = base::TimeTicks(); 46 } 47 48 LoadState ClientSocketHandle::GetLoadState() const { 49 CHECK(!is_initialized()); 50 CHECK(!group_name_.empty()); 51 return pool_->GetLoadState(group_name_, this); 52 } 53 54 void ClientSocketHandle::OnIOComplete(int result) { 55 CompletionCallback* callback = user_callback_; 56 user_callback_ = NULL; 57 HandleInitCompletion(result); 58 callback->Run(result); 59 } 60 61 void ClientSocketHandle::HandleInitCompletion(int result) { 62 CHECK(ERR_IO_PENDING != result); 63 if (result != OK) 64 ResetInternal(false); // The request failed, so there's nothing to cancel. 65 } 66 67 } // namespace net 68