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