client_socket.h revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Copyright (c) 2010 The Chromium Authors. All rights reserved. 2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be 3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file. 4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 5c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#ifndef NET_SOCKET_CLIENT_SOCKET_H_ 6c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#define NET_SOCKET_CLIENT_SOCKET_H_ 73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once 8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 9c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "net/socket/socket.h" 10c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 11c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottnamespace net { 12c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 13c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass AddressList; 14c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass BoundNetLog; 15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 16c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass ClientSocket : public Socket { 17c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public: 183345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick virtual ~ClientSocket() {} 193345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick 20c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Called to establish a connection. Returns OK if the connection could be 21c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // established synchronously. Otherwise, ERR_IO_PENDING is returned and the 22c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // given callback will run asynchronously when the connection is established 23c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // or when an error occurs. The result is some other error code if the 24c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // connection could not be established. 25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // The socket's Read and Write methods may not be called until Connect 27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // succeeds. 28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 29c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // It is valid to call Connect on an already connected socket, in which case 30c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // OK is simply returned. 31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 32c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Connect may also be called again after a call to the Disconnect method. 33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 34c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual int Connect(CompletionCallback* callback) = 0; 35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Called to disconnect a socket. Does nothing if the socket is already 37c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // disconnected. After calling Disconnect it is possible to call Connect 38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // again to establish a new connection. 39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 40c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // If IO (Connect, Read, or Write) is pending when the socket is 41c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // disconnected, the pending IO is cancelled, and the completion callback 42c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // will not be called. 43c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott virtual void Disconnect() = 0; 44c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 45c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Called to test if the connection is still alive. Returns false if a 46c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // connection wasn't established or the connection is dead. 47c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott virtual bool IsConnected() const = 0; 48c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 49c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Called to test if the connection is still alive and idle. Returns false 50c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // if a connection wasn't established, the connection is dead, or some data 51c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // have been received. 52c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott virtual bool IsConnectedAndIdle() const = 0; 53c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 54c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Copies the peer address to |address| and returns a network error code. 553345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // ERR_UNEXPECTED will be returned if the socket is not connected. 56c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual int GetPeerAddress(AddressList* address) const = 0; 57c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 58c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Gets the NetLog for this socket. 59c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual const BoundNetLog& NetLog() const = 0; 603345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick 613345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // Set the annotation to indicate this socket was created for speculative 623345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // reasons. This call is generally forwarded to a basic TCPClientSocket*, 633345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // where a UseHistory can be updated. 643345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick virtual void SetSubresourceSpeculation() = 0; 653345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick virtual void SetOmniboxSpeculation() = 0; 663345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick 673345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // Returns true if the underlying transport socket ever had any reads or 683345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // writes. ClientSockets layered on top of transport sockets should forward 693345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // this call to the transport socket. 703345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick virtual bool WasEverUsed() const = 0; 713345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick 723345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick protected: 733345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // The following class is only used to gather statistics about the history of 743345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // a socket. It is only instantiated and used in basic sockets, such as 753345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // TCPClientSocket* instances. Other classes that are derived from 763345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // ClientSocket should forward any potential settings to their underlying 773345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // transport sockets. 783345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick class UseHistory { 793345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick public: 803345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick UseHistory(); 813345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick ~UseHistory(); 823345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick 833345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick void set_was_ever_connected(); 843345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick void set_was_used_to_convey_data(); 853345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick 863345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // The next two setters only have any impact if the socket has not yet been 873345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // used to transmit data. If called later, we assume that the socket was 883345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // reused from the pool, and was NOT constructed to service a speculative 893345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // request. 903345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick void set_subresource_speculation(); 913345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick void set_omnibox_speculation(); 923345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick 933345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick bool was_used_to_convey_data() const; 943345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick 953345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick private: 963345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // Summarize the statistics for this socket. 973345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick void EmitPreconnectionHistograms() const; 983345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // Indicate if this was ever connected. 993345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick bool was_ever_connected_; 1003345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // Indicate if this socket was ever used to transmit or receive data. 1013345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick bool was_used_to_convey_data_; 1023345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick 1033345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // Indicate if this socket was first created for speculative use, and 1043345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // identify the motivation. 1053345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick bool omnibox_speculation_; 1063345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick bool subresource_speculation_; 1073345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick DISALLOW_COPY_AND_ASSIGN(UseHistory); 1083345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick }; 109c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 110c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 111c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} // namespace net 112c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 113c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif // NET_SOCKET_CLIENT_SOCKET_H_ 114