Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(363)

Side by Side Diff: remoting/protocol/chromium_port_allocator.cc

Issue 1571943002: Simplify PortAllocatorBase and make PortAllocator creation synchronous. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "remoting/protocol/chromium_port_allocator.h" 5 #include "remoting/protocol/chromium_port_allocator.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
13 #include "net/http/http_status_code.h" 13 #include "net/http/http_status_code.h"
14 #include "net/url_request/url_fetcher.h" 14 #include "net/url_request/url_fetcher.h"
15 #include "net/url_request/url_fetcher_delegate.h" 15 #include "net/url_request/url_fetcher_delegate.h"
16 #include "net/url_request/url_request_context_getter.h" 16 #include "net/url_request/url_request_context_getter.h"
17 #include "remoting/protocol/chromium_socket_factory.h" 17 #include "remoting/protocol/chromium_socket_factory.h"
18 #include "remoting/protocol/transport_context.h"
18 #include "url/gurl.h" 19 #include "url/gurl.h"
19 20
20 namespace remoting { 21 namespace remoting {
21 namespace protocol { 22 namespace protocol {
22 23
23 namespace { 24 namespace {
24 25
25 class ChromiumPortAllocatorSession : public PortAllocatorSessionBase, 26 class ChromiumPortAllocatorSession : public PortAllocatorSessionBase,
26 public net::URLFetcherDelegate { 27 public net::URLFetcherDelegate {
27 public: 28 public:
28 ChromiumPortAllocatorSession( 29 ChromiumPortAllocatorSession(PortAllocatorBase* allocator,
29 PortAllocatorBase* allocator, 30 const std::string& content_name,
30 const std::string& content_name, 31 int component,
31 int component, 32 const std::string& ice_username_fragment,
32 const std::string& ice_username_fragment, 33 const std::string& ice_password);
33 const std::string& ice_password,
34 const std::vector<rtc::SocketAddress>& stun_hosts,
35 const std::vector<std::string>& relay_hosts,
36 const std::string& relay,
37 const scoped_refptr<net::URLRequestContextGetter>& url_context);
38 ~ChromiumPortAllocatorSession() override; 34 ~ChromiumPortAllocatorSession() override;
39 35
40 // PortAllocatorBase overrides. 36 // PortAllocatorBase overrides.
41 void ConfigReady(cricket::PortConfiguration* config) override;
42 void SendSessionRequest(const std::string& host) override; 37 void SendSessionRequest(const std::string& host) override;
43 38
44 // net::URLFetcherDelegate interface. 39 // net::URLFetcherDelegate interface.
45 void OnURLFetchComplete(const net::URLFetcher* url_fetcher) override; 40 void OnURLFetchComplete(const net::URLFetcher* url_fetcher) override;
46 41
47 private: 42 private:
43 ChromiumPortAllocator* allocator() override {
44 return static_cast<ChromiumPortAllocator*>(
Jamie 2016/01/09 00:12:07 Similarly, consider saving the URL context in the
Sergey Ulanov 2016/01/11 20:01:04 Done. Also removed allocator() override from PortA
45 BasicPortAllocatorSession::allocator());
46 }
47
48 scoped_refptr<net::URLRequestContextGetter> url_context_; 48 scoped_refptr<net::URLRequestContextGetter> url_context_;
49 std::set<const net::URLFetcher*> url_fetchers_; 49 std::set<const net::URLFetcher*> url_fetchers_;
50 50
51 DISALLOW_COPY_AND_ASSIGN(ChromiumPortAllocatorSession); 51 DISALLOW_COPY_AND_ASSIGN(ChromiumPortAllocatorSession);
52 }; 52 };
53 53
54 ChromiumPortAllocatorSession::ChromiumPortAllocatorSession( 54 ChromiumPortAllocatorSession::ChromiumPortAllocatorSession(
55 PortAllocatorBase* allocator, 55 PortAllocatorBase* allocator,
56 const std::string& content_name, 56 const std::string& content_name,
57 int component, 57 int component,
58 const std::string& ice_username_fragment, 58 const std::string& ice_username_fragment,
59 const std::string& ice_password, 59 const std::string& ice_password)
60 const std::vector<rtc::SocketAddress>& stun_hosts,
61 const std::vector<std::string>& relay_hosts,
62 const std::string& relay,
63 const scoped_refptr<net::URLRequestContextGetter>& url_context)
64 : PortAllocatorSessionBase(allocator, 60 : PortAllocatorSessionBase(allocator,
65 content_name, 61 content_name,
66 component, 62 component,
67 ice_username_fragment, 63 ice_username_fragment,
68 ice_password, 64 ice_password) {}
69 stun_hosts,
70 relay_hosts,
71 relay),
72 url_context_(url_context) {}
73 65
74 ChromiumPortAllocatorSession::~ChromiumPortAllocatorSession() { 66 ChromiumPortAllocatorSession::~ChromiumPortAllocatorSession() {
75 STLDeleteElements(&url_fetchers_); 67 STLDeleteElements(&url_fetchers_);
76 } 68 }
77 69
78 void ChromiumPortAllocatorSession::ConfigReady(
79 cricket::PortConfiguration* config) {
80 // Filter out non-UDP relay ports, so that we don't try using TCP.
81 for (cricket::PortConfiguration::RelayList::iterator relay =
82 config->relays.begin(); relay != config->relays.end(); ++relay) {
83 cricket::PortList filtered_ports;
84 for (cricket::PortList::iterator port =
85 relay->ports.begin(); port != relay->ports.end(); ++port) {
86 if (port->proto == cricket::PROTO_UDP) {
87 filtered_ports.push_back(*port);
88 }
89 }
90 relay->ports = filtered_ports;
91 }
92 cricket::BasicPortAllocatorSession::ConfigReady(config);
93 }
94
95 void ChromiumPortAllocatorSession::SendSessionRequest(const std::string& host) { 70 void ChromiumPortAllocatorSession::SendSessionRequest(const std::string& host) {
96 GURL url("https://" + host + GetSessionRequestUrl() + "&sn=1"); 71 GURL url("https://" + host + GetSessionRequestUrl() + "&sn=1");
97 scoped_ptr<net::URLFetcher> url_fetcher = 72 scoped_ptr<net::URLFetcher> url_fetcher =
98 net::URLFetcher::Create(url, net::URLFetcher::GET, this); 73 net::URLFetcher::Create(url, net::URLFetcher::GET, this);
99 url_fetcher->SetRequestContext(url_context_.get()); 74 url_fetcher->SetRequestContext(allocator()->url_context().get());
100 url_fetcher->AddExtraRequestHeader("X-Talk-Google-Relay-Auth: " + 75 url_fetcher->AddExtraRequestHeader("X-Talk-Google-Relay-Auth: " +
101 relay_token()); 76 relay_token());
102 url_fetcher->AddExtraRequestHeader("X-Google-Relay-Auth: " + relay_token()); 77 url_fetcher->AddExtraRequestHeader("X-Google-Relay-Auth: " + relay_token());
103 url_fetcher->AddExtraRequestHeader("X-Stream-Type: chromoting"); 78 url_fetcher->AddExtraRequestHeader("X-Stream-Type: chromoting");
104 url_fetcher->Start(); 79 url_fetcher->Start();
105 url_fetchers_.insert(url_fetcher.release()); 80 url_fetchers_.insert(url_fetcher.release());
106 } 81 }
107 82
108 void ChromiumPortAllocatorSession::OnURLFetchComplete( 83 void ChromiumPortAllocatorSession::OnURLFetchComplete(
109 const net::URLFetcher* source) { 84 const net::URLFetcher* source) {
110 int response_code = source->GetResponseCode(); 85 int response_code = source->GetResponseCode();
111 std::string response; 86 std::string response;
112 source->GetResponseAsString(&response); 87 source->GetResponseAsString(&response);
113 88
114 url_fetchers_.erase(source); 89 url_fetchers_.erase(source);
115 delete source; 90 delete source;
116 91
117 if (response_code != net::HTTP_OK) { 92 if (response_code != net::HTTP_OK) {
118 LOG(WARNING) << "Received error when allocating relay session: " 93 LOG(WARNING) << "Received error when allocating relay session: "
119 << response_code; 94 << response_code;
120 TryCreateRelaySession(); 95 TryCreateRelaySession();
121 return; 96 return;
122 } 97 }
123 98
124 ReceiveSessionResponse(response); 99 ReceiveSessionResponse(response);
125 } 100 }
126 101
127 } // namespace 102 } // namespace
128 103
129 // static
130 scoped_ptr<ChromiumPortAllocator> ChromiumPortAllocator::Create(
131 const scoped_refptr<net::URLRequestContextGetter>& url_context) {
132 scoped_ptr<rtc::NetworkManager> network_manager(
133 new rtc::BasicNetworkManager());
134 scoped_ptr<rtc::PacketSocketFactory> socket_factory(
135 new ChromiumPacketSocketFactory());
136 return make_scoped_ptr(new ChromiumPortAllocator(
137 url_context, std::move(network_manager), std::move(socket_factory)));
138 }
139
140 ChromiumPortAllocator::ChromiumPortAllocator( 104 ChromiumPortAllocator::ChromiumPortAllocator(
141 const scoped_refptr<net::URLRequestContextGetter>& url_context,
142 scoped_ptr<rtc::NetworkManager> network_manager, 105 scoped_ptr<rtc::NetworkManager> network_manager,
143 scoped_ptr<rtc::PacketSocketFactory> socket_factory) 106 scoped_ptr<rtc::PacketSocketFactory> socket_factory,
144 : PortAllocatorBase(network_manager.get(), socket_factory.get()), 107 scoped_refptr<TransportContext> transport_context,
145 url_context_(url_context), 108 scoped_refptr<net::URLRequestContextGetter> url_context)
146 network_manager_(std::move(network_manager)), 109 : PortAllocatorBase(std::move(network_manager),
147 socket_factory_(std::move(socket_factory)) {} 110 std::move(socket_factory),
111 transport_context),
112 url_context_(url_context) {}
148 113
149 ChromiumPortAllocator::~ChromiumPortAllocator() {} 114 ChromiumPortAllocator::~ChromiumPortAllocator() {}
150 115
151 cricket::PortAllocatorSession* ChromiumPortAllocator::CreateSessionInternal( 116 cricket::PortAllocatorSession* ChromiumPortAllocator::CreateSessionInternal(
152 const std::string& content_name, 117 const std::string& content_name,
153 int component, 118 int component,
154 const std::string& ice_username_fragment, 119 const std::string& ice_username_fragment,
155 const std::string& ice_password) { 120 const std::string& ice_password) {
156 return new ChromiumPortAllocatorSession( 121 return new ChromiumPortAllocatorSession(this, content_name, component,
157 this, content_name, component, ice_username_fragment, ice_password, 122 ice_username_fragment, ice_password);
158 stun_hosts(), relay_hosts(), relay_token(), url_context_);
159 } 123 }
160 124
161 ChromiumPortAllocatorFactory::ChromiumPortAllocatorFactory( 125 ChromiumPortAllocatorFactory::ChromiumPortAllocatorFactory(
162 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) 126 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter)
163 : url_request_context_getter_(url_request_context_getter) {} 127 : url_request_context_getter_(url_request_context_getter) {}
164 128
165 ChromiumPortAllocatorFactory::~ChromiumPortAllocatorFactory() {} 129 ChromiumPortAllocatorFactory::~ChromiumPortAllocatorFactory() {}
166 130
167 scoped_ptr<PortAllocatorBase> 131 scoped_ptr<cricket::PortAllocator>
168 ChromiumPortAllocatorFactory::CreatePortAllocator() { 132 ChromiumPortAllocatorFactory::CreatePortAllocator(
169 return ChromiumPortAllocator::Create(url_request_context_getter_); 133 scoped_refptr<TransportContext> transport_context) {
134 scoped_ptr<rtc::NetworkManager> network_manager(
135 new rtc::BasicNetworkManager());
136 scoped_ptr<rtc::PacketSocketFactory> socket_factory(
137 new ChromiumPacketSocketFactory());
138 return make_scoped_ptr(new ChromiumPortAllocator(
139 std::move(network_manager), std::move(socket_factory), transport_context,
140 url_request_context_getter_));
170 } 141 }
171 142
172 } // namespace protocol 143 } // namespace protocol
173 } // namespace remoting 144 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698