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. 4 5 #ifndef NET_SOCKET_STREAM_SOCKET_STREAM_JOB_H_ 6 #define NET_SOCKET_STREAM_SOCKET_STREAM_JOB_H_ 7 8 #include <string> 9 10 #include "base/memory/ref_counted.h" 11 #include "net/base/net_export.h" 12 #include "net/socket_stream/socket_stream.h" 13 14 class GURL; 15 16 namespace net { 17 18 class SSLConfigService; 19 class SSLInfo; 20 class TransportSecurityState; 21 22 // SocketStreamJob represents full-duplex communication over SocketStream. 23 // If a protocol (e.g. WebSocket protocol) needs to inspect/modify data 24 // over SocketStream, you can implement protocol specific job (e.g. 25 // WebSocketJob) to do some work on data over SocketStream. 26 // Registers the protocol specific SocketStreamJob by RegisterProtocolFactory 27 // and call CreateSocketStreamJob to create SocketStreamJob for the URL. 28 class NET_EXPORT SocketStreamJob 29 : public base::RefCountedThreadSafe<SocketStreamJob> { 30 public: 31 // Callback function implemented by protocol handlers to create new jobs. 32 typedef SocketStreamJob* (ProtocolFactory)(const GURL& url, 33 SocketStream::Delegate* delegate); 34 35 static ProtocolFactory* RegisterProtocolFactory(const std::string& scheme, 36 ProtocolFactory* factory); 37 38 static SocketStreamJob* CreateSocketStreamJob( 39 const GURL& url, 40 SocketStream::Delegate* delegate, 41 TransportSecurityState* sts, 42 SSLConfigService* ssl); 43 44 SocketStreamJob(); InitSocketStream(SocketStream * socket)45 void InitSocketStream(SocketStream* socket) { 46 socket_ = socket; 47 } 48 49 virtual SocketStream::UserData* GetUserData(const void* key) const; 50 virtual void SetUserData(const void* key, SocketStream::UserData* data); 51 context()52 URLRequestContext* context() const { 53 return socket_.get() ? socket_->context() : 0; 54 } set_context(URLRequestContext * context)55 void set_context(URLRequestContext* context) { 56 if (socket_.get()) 57 socket_->set_context(context); 58 } 59 60 virtual void Connect(); 61 62 virtual bool SendData(const char* data, int len); 63 64 virtual void Close(); 65 66 virtual void RestartWithAuth(const AuthCredentials& credentials); 67 68 virtual void CancelWithError(int error); 69 70 virtual void CancelWithSSLError(const net::SSLInfo& ssl_info); 71 72 virtual void ContinueDespiteError(); 73 74 virtual void DetachDelegate(); 75 76 protected: 77 friend class WebSocketJobTest; 78 friend class base::RefCountedThreadSafe<SocketStreamJob>; 79 virtual ~SocketStreamJob(); 80 81 scoped_refptr<SocketStream> socket_; 82 83 DISALLOW_COPY_AND_ASSIGN(SocketStreamJob); 84 }; 85 86 } // namespace net 87 88 #endif // NET_SOCKET_STREAM_SOCKET_STREAM_JOB_H_ 89