1/*
2 *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_P2P_CLIENT_HTTPPORTALLOCATOR_H_
12#define WEBRTC_P2P_CLIENT_HTTPPORTALLOCATOR_H_
13
14#include <list>
15#include <string>
16#include <vector>
17
18#include "webrtc/p2p/client/basicportallocator.h"
19
20class HttpPortAllocatorTest_TestSessionRequestUrl_Test;
21
22namespace rtc {
23class AsyncHttpRequest;
24class SignalThread;
25}
26
27namespace cricket {
28
29// TODO(pthatcher): Remove this.  It's only used by chromoting, so we
30// should just move this code there.  It's used in these places in
31// chromium:
32// src/remoting/protocol/chromium_port_allocator.cc
33// src/remoting/client/plugin/pepper_port_allocator.cc
34// src/remoting/protocol/libjingle_transport_factory.cc
35class HttpPortAllocatorBase : public BasicPortAllocator {
36 public:
37  // The number of HTTP requests we should attempt before giving up.
38  static const int kNumRetries;
39
40  // Records the URL that we will GET in order to create a session.
41  static const char kCreateSessionURL[];
42
43  HttpPortAllocatorBase(rtc::NetworkManager* network_manager,
44                        const std::string& user_agent);
45  HttpPortAllocatorBase(rtc::NetworkManager* network_manager,
46                        rtc::PacketSocketFactory* socket_factory,
47                        const std::string& user_agent);
48  virtual ~HttpPortAllocatorBase();
49
50  // CreateSession is defined in BasicPortAllocator but is
51  // redefined here as pure virtual.
52  virtual PortAllocatorSession* CreateSessionInternal(
53      const std::string& content_name,
54      int component,
55      const std::string& ice_ufrag,
56      const std::string& ice_pwd) = 0;
57
58  void SetStunHosts(const std::vector<rtc::SocketAddress>& hosts) {
59    if (!hosts.empty()) {
60      stun_hosts_ = hosts;
61    }
62  }
63  void SetRelayHosts(const std::vector<std::string>& hosts) {
64    if (!hosts.empty()) {
65      relay_hosts_ = hosts;
66    }
67  }
68  void SetRelayToken(const std::string& relay) { relay_token_ = relay; }
69
70  const std::vector<rtc::SocketAddress>& stun_hosts() const {
71    return stun_hosts_;
72  }
73
74  const std::vector<std::string>& relay_hosts() const {
75    return relay_hosts_;
76  }
77
78  const std::string& relay_token() const {
79    return relay_token_;
80  }
81
82  const std::string& user_agent() const {
83    return agent_;
84  }
85
86 private:
87  std::vector<rtc::SocketAddress> stun_hosts_;
88  std::vector<std::string> relay_hosts_;
89  std::string relay_token_;
90  std::string agent_;
91};
92
93class RequestData;
94
95class HttpPortAllocatorSessionBase : public BasicPortAllocatorSession {
96 public:
97  HttpPortAllocatorSessionBase(
98      HttpPortAllocatorBase* allocator,
99      const std::string& content_name,
100      int component,
101      const std::string& ice_ufrag,
102      const std::string& ice_pwd,
103      const std::vector<rtc::SocketAddress>& stun_hosts,
104      const std::vector<std::string>& relay_hosts,
105      const std::string& relay,
106      const std::string& agent);
107  virtual ~HttpPortAllocatorSessionBase();
108
109  const std::string& relay_token() const {
110    return relay_token_;
111  }
112
113  const std::string& user_agent() const {
114      return agent_;
115  }
116
117  virtual void SendSessionRequest(const std::string& host, int port) = 0;
118  virtual void ReceiveSessionResponse(const std::string& response);
119
120  // Made public for testing. Should be protected.
121  std::string GetSessionRequestUrl();
122
123 protected:
124  virtual void GetPortConfigurations();
125  void TryCreateRelaySession();
126  virtual HttpPortAllocatorBase* allocator() {
127    return static_cast<HttpPortAllocatorBase*>(
128        BasicPortAllocatorSession::allocator());
129  }
130
131 private:
132  std::vector<std::string> relay_hosts_;
133  std::vector<rtc::SocketAddress> stun_hosts_;
134  std::string relay_token_;
135  std::string agent_;
136  int attempts_;
137};
138
139}  // namespace cricket
140
141#endif  // WEBRTC_P2P_CLIENT_HTTPPORTALLOCATOR_H_
142