1// Copyright 2014 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_WEBSOCKET_TRANSPORT_CONNECT_SUB_JOB_H_
6#define NET_SOCKET_WEBSOCKET_TRANSPORT_CONNECT_SUB_JOB_H_
7
8#include "base/compiler_specific.h"
9#include "base/macros.h"
10#include "base/memory/scoped_ptr.h"
11#include "net/base/address_list.h"
12#include "net/base/load_states.h"
13#include "net/socket/websocket_endpoint_lock_manager.h"
14#include "net/socket/websocket_transport_client_socket_pool.h"
15
16namespace net {
17
18class BoundNetLog;
19class ClientSocketFactory;
20class IPEndPoint;
21class StreamSocket;
22
23// Attempts to connect to a subset of the addresses required by a
24// WebSocketTransportConnectJob, specifically either the IPv4 or IPv6
25// addresses. Each address is tried in turn, and parent_job->OnSubJobComplete()
26// is called when the first address succeeds or the last address fails.
27class WebSocketTransportConnectSubJob
28    : public WebSocketEndpointLockManager::Waiter {
29 public:
30  typedef WebSocketTransportConnectJob::SubJobType SubJobType;
31
32  WebSocketTransportConnectSubJob(const AddressList& addresses,
33                                  WebSocketTransportConnectJob* parent_job,
34                                  SubJobType type);
35
36  virtual ~WebSocketTransportConnectSubJob();
37
38  // Start connecting.
39  int Start();
40
41  bool started() { return next_state_ != STATE_NONE; }
42
43  LoadState GetLoadState() const;
44
45  SubJobType type() const { return type_; }
46
47  scoped_ptr<StreamSocket> PassSocket() { return transport_socket_.Pass(); }
48
49  // Implementation of WebSocketEndpointLockManager::EndpointWaiter.
50  virtual void GotEndpointLock() OVERRIDE;
51
52 private:
53  enum State {
54    STATE_NONE,
55    STATE_OBTAIN_LOCK,
56    STATE_OBTAIN_LOCK_COMPLETE,
57    STATE_TRANSPORT_CONNECT,
58    STATE_TRANSPORT_CONNECT_COMPLETE,
59    STATE_DONE,
60  };
61
62  ClientSocketFactory* client_socket_factory() const;
63
64  const BoundNetLog& net_log() const;
65
66  const IPEndPoint& CurrentAddress() const;
67
68  void OnIOComplete(int result);
69  int DoLoop(int result);
70  int DoEndpointLock();
71  int DoEndpointLockComplete();
72  int DoTransportConnect();
73  int DoTransportConnectComplete(int result);
74
75  WebSocketTransportConnectJob* const parent_job_;
76
77  const AddressList addresses_;
78  size_t current_address_index_;
79
80  State next_state_;
81  const SubJobType type_;
82
83  scoped_ptr<StreamSocket> transport_socket_;
84
85  DISALLOW_COPY_AND_ASSIGN(WebSocketTransportConnectSubJob);
86};
87
88}  // namespace net
89
90#endif  // NET_SOCKET_WEBSOCKET_TRANSPORT_CONNECT_SUB_JOB_H_
91