OLD | NEW |
---|---|
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 "components/cronet/android/url_request_context_adapter.h" | 5 #include "components/cronet/android/url_request_context_adapter.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/message_loop/message_loop.h" | |
9 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
10 #include "components/cronet/url_request_context_config.h" | 11 #include "components/cronet/url_request_context_config.h" |
11 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
12 #include "net/base/net_log_logger.h" | 13 #include "net/base/net_log_logger.h" |
13 #include "net/cert/cert_verifier.h" | 14 #include "net/cert/cert_verifier.h" |
14 #include "net/http/http_auth_handler_factory.h" | 15 #include "net/http/http_auth_handler_factory.h" |
15 #include "net/http/http_network_layer.h" | 16 #include "net/http/http_network_layer.h" |
16 #include "net/http/http_server_properties.h" | 17 #include "net/http/http_server_properties.h" |
17 #include "net/proxy/proxy_config_service_fixed.h" | 18 #include "net/proxy/proxy_config_service.h" |
18 #include "net/proxy/proxy_service.h" | 19 #include "net/proxy/proxy_service.h" |
19 #include "net/ssl/ssl_config_service_defaults.h" | 20 #include "net/ssl/ssl_config_service_defaults.h" |
20 #include "net/url_request/static_http_user_agent_settings.h" | 21 #include "net/url_request/static_http_user_agent_settings.h" |
21 #include "net/url_request/url_request_context_builder.h" | 22 #include "net/url_request/url_request_context_builder.h" |
22 #include "net/url_request/url_request_context_storage.h" | 23 #include "net/url_request/url_request_context_storage.h" |
23 #include "net/url_request/url_request_job_factory_impl.h" | 24 #include "net/url_request/url_request_job_factory_impl.h" |
24 | 25 |
25 namespace { | 26 namespace { |
26 | 27 |
27 class BasicNetworkDelegate : public net::NetworkDelegate { | 28 class BasicNetworkDelegate : public net::NetworkDelegate { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 | 102 |
102 virtual int OnBeforeSocketStreamConnect( | 103 virtual int OnBeforeSocketStreamConnect( |
103 net::SocketStream* stream, | 104 net::SocketStream* stream, |
104 const net::CompletionCallback& callback) override { | 105 const net::CompletionCallback& callback) override { |
105 return net::OK; | 106 return net::OK; |
106 } | 107 } |
107 | 108 |
108 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); | 109 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); |
109 }; | 110 }; |
110 | 111 |
112 // Android specific UI MessageLoop. | |
mmenke
2014/10/15 17:44:17
Maybe "UI MessageLoop" -> "MessageLoop on main thr
xunjieli
2014/10/15 19:48:08
Done.
| |
113 base::MessageLoop* g_main_message_loop = nullptr; | |
mmenke
2014/10/15 17:44:17
optional: Suggest putting this at the top of the
xunjieli
2014/10/15 19:48:08
Done.
| |
114 | |
111 } // namespace | 115 } // namespace |
112 | 116 |
113 namespace cronet { | 117 namespace cronet { |
114 | 118 |
115 URLRequestContextAdapter::URLRequestContextAdapter( | 119 URLRequestContextAdapter::URLRequestContextAdapter( |
116 URLRequestContextAdapterDelegate* delegate, | 120 URLRequestContextAdapterDelegate* delegate, |
117 std::string user_agent) { | 121 std::string user_agent) { |
118 delegate_ = delegate; | 122 delegate_ = delegate; |
119 user_agent_ = user_agent; | 123 user_agent_ = user_agent; |
120 } | 124 } |
121 | 125 |
122 void URLRequestContextAdapter::Initialize( | 126 void URLRequestContextAdapter::Initialize( |
123 scoped_ptr<URLRequestContextConfig> config) { | 127 scoped_ptr<URLRequestContextConfig> config) { |
124 network_thread_ = new base::Thread("network"); | 128 network_thread_ = new base::Thread("network"); |
125 base::Thread::Options options; | 129 base::Thread::Options options; |
126 options.message_loop_type = base::MessageLoop::TYPE_IO; | 130 options.message_loop_type = base::MessageLoop::TYPE_IO; |
127 network_thread_->StartWithOptions(options); | 131 network_thread_->StartWithOptions(options); |
132 config_ = config.Pass(); | |
133 } | |
128 | 134 |
135 void URLRequestContextAdapter::InitRequestContextOnUIThread() { | |
mmenke
2014/10/15 17:44:18
DCHECK(config_)?
xunjieli
2014/10/15 19:48:09
Done.
| |
136 if (!base::MessageLoop::current()) { | |
mmenke
2014/10/15 17:44:17
We have no tests where this check is false, I beli
xunjieli
2014/10/15 19:48:08
Done.
| |
137 DCHECK(!g_main_message_loop); | |
138 g_main_message_loop = new base::MessageLoopForUI; | |
mmenke
2014/10/15 17:44:17
optional nit: Seems more common to use "()" with
xunjieli
2014/10/15 19:48:08
Done.
| |
139 base::MessageLoopForUI::current()->Start(); | |
140 } | |
mmenke
2014/10/15 17:44:17
DCHECK_EQ(g_main_message_loop, base::MessageLoop::
xunjieli
2014/10/15 19:48:08
Done.
| |
141 | |
142 net::ProxyConfigService* service = | |
mmenke
2014/10/15 17:44:17
Should generally avoid using raw pointers, except
xunjieli
2014/10/15 19:48:09
Done.
| |
143 net::ProxyService::CreateSystemProxyConfigService(GetNetworkTaskRunner(), | |
144 NULL); | |
145 proxy_config_service_.reset(service); | |
129 GetNetworkTaskRunner()->PostTask( | 146 GetNetworkTaskRunner()->PostTask( |
130 FROM_HERE, | 147 FROM_HERE, |
131 base::Bind(&URLRequestContextAdapter::InitializeURLRequestContext, | 148 base::Bind(&URLRequestContextAdapter::InitRequestContextOnNetworkThread, |
132 this, | 149 this, |
133 Passed(&config))); | 150 Passed(&config_))); |
mmenke
2014/10/15 17:44:17
I don't think we get anything from passing the con
xunjieli
2014/10/15 19:48:09
Done. Sounds good. I adopted the second approach.
| |
134 } | 151 } |
135 | 152 |
136 void URLRequestContextAdapter::InitializeURLRequestContext( | 153 void URLRequestContextAdapter::InitRequestContextOnNetworkThread( |
137 scoped_ptr<URLRequestContextConfig> config) { | 154 scoped_ptr<URLRequestContextConfig> config) { |
138 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 155 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
139 // TODO(mmenke): Add method to have the builder enable SPDY. | 156 // TODO(mmenke): Add method to have the builder enable SPDY. |
140 net::URLRequestContextBuilder context_builder; | 157 net::URLRequestContextBuilder context_builder; |
141 context_builder.set_network_delegate(new BasicNetworkDelegate()); | 158 context_builder.set_network_delegate(new BasicNetworkDelegate()); |
142 context_builder.set_proxy_config_service( | 159 context_builder.set_proxy_config_service(proxy_config_service_.get()); |
143 new net::ProxyConfigServiceFixed(net::ProxyConfig())); | |
144 config->ConfigureURLRequestContextBuilder(&context_builder); | 160 config->ConfigureURLRequestContextBuilder(&context_builder); |
145 | 161 |
146 context_.reset(context_builder.Build()); | 162 context_.reset(context_builder.Build()); |
147 | 163 |
148 // Currently (circa M39) enabling QUIC requires setting probability threshold. | 164 // Currently (circa M39) enabling QUIC requires setting probability threshold. |
149 if (config->enable_quic) { | 165 if (config->enable_quic) { |
150 context_->http_server_properties() | 166 context_->http_server_properties() |
151 ->SetAlternateProtocolProbabilityThreshold(0.0f); | 167 ->SetAlternateProtocolProbabilityThreshold(0.0f); |
152 for (size_t hint = 0; hint < config->quic_hints.size(); ++hint) { | 168 for (size_t hint = 0; hint < config->quic_hints.size(); ++hint) { |
153 const URLRequestContextConfig::QuicHint& quic_hint = | 169 const URLRequestContextConfig::QuicHint& quic_hint = |
(...skipping 18 matching lines...) Expand all Loading... | |
172 } | 188 } |
173 | 189 |
174 net::HostPortPair quic_hint_host_port_pair(quic_hint.host, | 190 net::HostPortPair quic_hint_host_port_pair(quic_hint.host, |
175 quic_hint.port); | 191 quic_hint.port); |
176 context_->http_server_properties()->SetAlternateProtocol( | 192 context_->http_server_properties()->SetAlternateProtocol( |
177 quic_hint_host_port_pair, | 193 quic_hint_host_port_pair, |
178 static_cast<uint16>(quic_hint.alternate_port), | 194 static_cast<uint16>(quic_hint.alternate_port), |
179 net::AlternateProtocol::QUIC, | 195 net::AlternateProtocol::QUIC, |
180 1.0f); | 196 1.0f); |
181 } | 197 } |
182 is_context_initialized_ = true; | |
183 while (!tasks_waiting_for_context_.empty()) { | |
184 tasks_waiting_for_context_.front().Run(); | |
185 tasks_waiting_for_context_.pop(); | |
186 } | |
187 } | 198 } |
188 | 199 |
189 if (VLOG_IS_ON(2)) { | 200 if (VLOG_IS_ON(2)) { |
190 net_log_observer_.reset(new NetLogObserver()); | 201 net_log_observer_.reset(new NetLogObserver()); |
191 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(), | 202 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(), |
192 net::NetLog::LOG_ALL_BUT_BYTES); | 203 net::NetLog::LOG_ALL_BUT_BYTES); |
193 } | 204 } |
194 | 205 |
206 is_context_initialized_ = true; | |
207 while (!tasks_waiting_for_context_.empty()) { | |
208 tasks_waiting_for_context_.front().Run(); | |
209 tasks_waiting_for_context_.pop(); | |
210 } | |
211 | |
195 delegate_->OnContextInitialized(this); | 212 delegate_->OnContextInitialized(this); |
196 } | 213 } |
197 | 214 |
198 void URLRequestContextAdapter::PostTaskToNetworkThread( | 215 void URLRequestContextAdapter::PostTaskToNetworkThread( |
199 const tracked_objects::Location& posted_from, | 216 const tracked_objects::Location& posted_from, |
200 const RunAfterContextInitTask& callback) { | 217 const RunAfterContextInitTask& callback) { |
201 GetNetworkTaskRunner()->PostTask( | 218 GetNetworkTaskRunner()->PostTask( |
202 posted_from, | 219 posted_from, |
203 base::Bind( | 220 base::Bind( |
204 &URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread, | 221 &URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread, |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
280 net_log_logger_.reset(); | 297 net_log_logger_.reset(); |
281 } | 298 } |
282 } | 299 } |
283 | 300 |
284 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { | 301 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { |
285 VLOG(2) << "Net log entry: type=" << entry.type() | 302 VLOG(2) << "Net log entry: type=" << entry.type() |
286 << ", source=" << entry.source().type << ", phase=" << entry.phase(); | 303 << ", source=" << entry.source().type << ", phase=" << entry.phase(); |
287 } | 304 } |
288 | 305 |
289 } // namespace cronet | 306 } // namespace cronet |
OLD | NEW |