1// Copyright 2013 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_WEBSOCKETS_WEBSOCKET_HANDSHAKE_STREAM_BASE_H_
6#define NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_STREAM_BASE_H_
7
8// This file is included from net/http files.
9// Since net/http can be built without linking net/websockets code,
10// this file must not introduce any link-time dependencies on websockets.
11
12#include <string>
13
14#include "base/basictypes.h"
15#include "base/memory/scoped_ptr.h"
16#include "base/memory/weak_ptr.h"
17#include "base/supports_user_data.h"
18#include "net/http/http_stream_base.h"
19#include "net/url_request/websocket_handshake_userdata_key.h"
20#include "net/websockets/websocket_stream.h"
21
22namespace net {
23
24class ClientSocketHandle;
25class SpdySession;
26
27// WebSocketHandshakeStreamBase is the base class of
28// WebSocketBasicHandshakeStream.  net/http code uses this interface to handle
29// WebSocketBasicHandshakeStream when it needs to be treated differently from
30// HttpStreamBase.
31class NET_EXPORT WebSocketHandshakeStreamBase : public HttpStreamBase {
32 public:
33  // An object that stores data needed for the creation of a
34  // WebSocketBasicHandshakeStream object. A new CreateHelper is used for each
35  // WebSocket connection.
36  class NET_EXPORT_PRIVATE CreateHelper : public base::SupportsUserData::Data {
37   public:
38    // Returns a key to use to lookup this object in a URLRequest object. It is
39    // different from any other key that is supplied to
40    // URLRequest::SetUserData().
41    static const void* DataKey() { return kWebSocketHandshakeUserDataKey; }
42
43    virtual ~CreateHelper() {}
44
45    // Create a WebSocketBasicHandshakeStream. This is called after the
46    // underlying connection has been established but before any handshake data
47    // has been transferred. This can be called more than once in the case that
48    // HTTP authentication is needed.
49    virtual WebSocketHandshakeStreamBase* CreateBasicStream(
50        scoped_ptr<ClientSocketHandle> connection,
51        bool using_proxy) = 0;
52
53    // Create a WebSocketSpdyHandshakeStream (unimplemented as of October 2013)
54    virtual WebSocketHandshakeStreamBase* CreateSpdyStream(
55        const base::WeakPtr<SpdySession>& session,
56        bool use_relative_url) = 0;
57  };
58
59  // This has to have an inline implementation so that the net/url_request/
60  // tests do not fail on iOS.
61  virtual ~WebSocketHandshakeStreamBase() {}
62
63  // After the handshake has completed, this method creates a WebSocketStream
64  // (of the appropriate type) from the WebSocketHandshakeStreamBase object.
65  // The WebSocketHandshakeStreamBase object is unusable after Upgrade() has
66  // been called.
67  virtual scoped_ptr<WebSocketStream> Upgrade() = 0;
68
69 protected:
70  // As with the destructor, this must be inline.
71  WebSocketHandshakeStreamBase() {}
72
73 private:
74  DISALLOW_COPY_AND_ASSIGN(WebSocketHandshakeStreamBase);
75};
76
77}  // namespace net
78
79#endif  // NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_STREAM_BASE_H_
80