Chromium Code Reviews| 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 <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/message_loop/message_loop.h" | |
| 11 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 12 #include "components/cronet/url_request_context_config.h" | 13 #include "components/cronet/url_request_context_config.h" |
| 13 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 14 #include "net/base/net_log_logger.h" | 15 #include "net/base/net_log_logger.h" |
| 15 #include "net/cert/cert_verifier.h" | 16 #include "net/cert/cert_verifier.h" |
| 16 #include "net/http/http_auth_handler_factory.h" | 17 #include "net/http/http_auth_handler_factory.h" |
| 17 #include "net/http/http_network_layer.h" | 18 #include "net/http/http_network_layer.h" |
| 18 #include "net/http/http_server_properties.h" | 19 #include "net/http/http_server_properties.h" |
| 19 #include "net/proxy/proxy_config_service_fixed.h" | |
| 20 #include "net/proxy/proxy_service.h" | 20 #include "net/proxy/proxy_service.h" |
| 21 #include "net/ssl/ssl_config_service_defaults.h" | 21 #include "net/ssl/ssl_config_service_defaults.h" |
| 22 #include "net/url_request/static_http_user_agent_settings.h" | 22 #include "net/url_request/static_http_user_agent_settings.h" |
| 23 #include "net/url_request/url_request_context_builder.h" | 23 #include "net/url_request/url_request_context_builder.h" |
| 24 #include "net/url_request/url_request_context_storage.h" | 24 #include "net/url_request/url_request_context_storage.h" |
| 25 #include "net/url_request/url_request_job_factory_impl.h" | 25 #include "net/url_request/url_request_job_factory_impl.h" |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 // MessageLoop on the main thread, which is where objects that receive Java | |
| 30 // notifications generally live. | |
| 31 base::MessageLoop* g_main_message_loop = nullptr; | |
| 32 | |
| 29 class BasicNetworkDelegate : public net::NetworkDelegate { | 33 class BasicNetworkDelegate : public net::NetworkDelegate { |
| 30 public: | 34 public: |
| 31 BasicNetworkDelegate() {} | 35 BasicNetworkDelegate() {} |
| 32 virtual ~BasicNetworkDelegate() {} | 36 virtual ~BasicNetworkDelegate() {} |
| 33 | 37 |
| 34 private: | 38 private: |
| 35 // net::NetworkDelegate implementation. | 39 // net::NetworkDelegate implementation. |
| 36 int OnBeforeURLRequest(net::URLRequest* request, | 40 int OnBeforeURLRequest(net::URLRequest* request, |
| 37 const net::CompletionCallback& callback, | 41 const net::CompletionCallback& callback, |
| 38 GURL* new_url) override { | 42 GURL* new_url) override { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 120 delegate_ = delegate; | 124 delegate_ = delegate; |
| 121 user_agent_ = user_agent; | 125 user_agent_ = user_agent; |
| 122 } | 126 } |
| 123 | 127 |
| 124 void URLRequestContextAdapter::Initialize( | 128 void URLRequestContextAdapter::Initialize( |
| 125 scoped_ptr<URLRequestContextConfig> config) { | 129 scoped_ptr<URLRequestContextConfig> config) { |
| 126 network_thread_ = new base::Thread("network"); | 130 network_thread_ = new base::Thread("network"); |
| 127 base::Thread::Options options; | 131 base::Thread::Options options; |
| 128 options.message_loop_type = base::MessageLoop::TYPE_IO; | 132 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 129 network_thread_->StartWithOptions(options); | 133 network_thread_->StartWithOptions(options); |
| 134 config_ = config.Pass(); | |
| 135 } | |
| 130 | 136 |
| 137 void URLRequestContextAdapter::InitRequestContextOnMainThread() { | |
| 138 if (!base::MessageLoop::current()) { | |
| 139 DCHECK(!g_main_message_loop); | |
| 140 g_main_message_loop = new base::MessageLoopForUI(); | |
| 141 base::MessageLoopForUI::current()->Start(); | |
| 142 } | |
| 143 DCHECK_EQ(g_main_message_loop, base::MessageLoop::current()); | |
| 144 | |
| 145 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( | |
| 146 GetNetworkTaskRunner(), NULL)); | |
| 131 GetNetworkTaskRunner()->PostTask( | 147 GetNetworkTaskRunner()->PostTask( |
| 132 FROM_HERE, | 148 FROM_HERE, |
| 133 base::Bind(&URLRequestContextAdapter::InitializeURLRequestContext, | 149 base::Bind(&URLRequestContextAdapter::InitRequestContextOnNetworkThread, |
| 134 this, | 150 this)); |
| 135 Passed(&config))); | |
| 136 } | 151 } |
| 137 | 152 |
| 138 void URLRequestContextAdapter::InitializeURLRequestContext( | 153 void URLRequestContextAdapter::InitRequestContextOnNetworkThread() { |
| 139 scoped_ptr<URLRequestContextConfig> config) { | |
| 140 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 154 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 155 DCHECK(config_); | |
| 141 // TODO(mmenke): Add method to have the builder enable SPDY. | 156 // TODO(mmenke): Add method to have the builder enable SPDY. |
| 142 net::URLRequestContextBuilder context_builder; | 157 net::URLRequestContextBuilder context_builder; |
| 143 context_builder.set_network_delegate(new BasicNetworkDelegate()); | 158 context_builder.set_network_delegate(new BasicNetworkDelegate()); |
| 144 context_builder.set_proxy_config_service( | 159 context_builder.set_proxy_config_service(proxy_config_service_.get()); |
|
pauljensen
2015/08/18 12:04:58
Hey, this looks like a double-free? We keep a sco
| |
| 145 new net::ProxyConfigServiceFixed(net::ProxyConfig())); | 160 config_->ConfigureURLRequestContextBuilder(&context_builder); |
| 146 config->ConfigureURLRequestContextBuilder(&context_builder); | |
| 147 | 161 |
| 148 context_.reset(context_builder.Build()); | 162 context_.reset(context_builder.Build()); |
| 149 | 163 |
| 150 // Currently (circa M39) enabling QUIC requires setting probability threshold. | 164 // Currently (circa M39) enabling QUIC requires setting probability threshold. |
| 151 if (config->enable_quic) { | 165 if (config_->enable_quic) { |
| 152 context_->http_server_properties() | 166 context_->http_server_properties() |
| 153 ->SetAlternateProtocolProbabilityThreshold(0.0f); | 167 ->SetAlternateProtocolProbabilityThreshold(0.0f); |
| 154 for (size_t hint = 0; hint < config->quic_hints.size(); ++hint) { | 168 for (size_t hint = 0; hint < config_->quic_hints.size(); ++hint) { |
| 155 const URLRequestContextConfig::QuicHint& quic_hint = | 169 const URLRequestContextConfig::QuicHint& quic_hint = |
| 156 *config->quic_hints[hint]; | 170 *config_->quic_hints[hint]; |
| 157 if (quic_hint.host.empty()) { | 171 if (quic_hint.host.empty()) { |
| 158 LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host; | 172 LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host; |
| 159 continue; | 173 continue; |
| 160 } | 174 } |
| 161 | 175 |
| 162 if (quic_hint.port <= std::numeric_limits<uint16>::min() || | 176 if (quic_hint.port <= std::numeric_limits<uint16>::min() || |
| 163 quic_hint.port > std::numeric_limits<uint16>::max()) { | 177 quic_hint.port > std::numeric_limits<uint16>::max()) { |
| 164 LOG(ERROR) << "Invalid QUIC hint port: " | 178 LOG(ERROR) << "Invalid QUIC hint port: " |
| 165 << quic_hint.port; | 179 << quic_hint.port; |
| 166 continue; | 180 continue; |
| 167 } | 181 } |
| 168 | 182 |
| 169 if (quic_hint.alternate_port <= std::numeric_limits<uint16>::min() || | 183 if (quic_hint.alternate_port <= std::numeric_limits<uint16>::min() || |
| 170 quic_hint.alternate_port > std::numeric_limits<uint16>::max()) { | 184 quic_hint.alternate_port > std::numeric_limits<uint16>::max()) { |
| 171 LOG(ERROR) << "Invalid QUIC hint alternate port: " | 185 LOG(ERROR) << "Invalid QUIC hint alternate port: " |
| 172 << quic_hint.alternate_port; | 186 << quic_hint.alternate_port; |
| 173 continue; | 187 continue; |
| 174 } | 188 } |
| 175 | 189 |
| 176 net::HostPortPair quic_hint_host_port_pair(quic_hint.host, | 190 net::HostPortPair quic_hint_host_port_pair(quic_hint.host, |
| 177 quic_hint.port); | 191 quic_hint.port); |
| 178 context_->http_server_properties()->SetAlternateProtocol( | 192 context_->http_server_properties()->SetAlternateProtocol( |
| 179 quic_hint_host_port_pair, | 193 quic_hint_host_port_pair, |
| 180 static_cast<uint16>(quic_hint.alternate_port), | 194 static_cast<uint16>(quic_hint.alternate_port), |
| 181 net::AlternateProtocol::QUIC, | 195 net::AlternateProtocol::QUIC, |
| 182 1.0f); | 196 1.0f); |
| 183 } | 197 } |
| 184 is_context_initialized_ = true; | |
| 185 while (!tasks_waiting_for_context_.empty()) { | |
| 186 tasks_waiting_for_context_.front().Run(); | |
| 187 tasks_waiting_for_context_.pop(); | |
| 188 } | |
| 189 } | 198 } |
| 199 config_.reset(NULL); | |
| 190 | 200 |
| 191 if (VLOG_IS_ON(2)) { | 201 if (VLOG_IS_ON(2)) { |
| 192 net_log_observer_.reset(new NetLogObserver()); | 202 net_log_observer_.reset(new NetLogObserver()); |
| 193 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(), | 203 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(), |
| 194 net::NetLog::LOG_ALL_BUT_BYTES); | 204 net::NetLog::LOG_ALL_BUT_BYTES); |
| 195 } | 205 } |
| 196 | 206 |
| 207 is_context_initialized_ = true; | |
| 208 while (!tasks_waiting_for_context_.empty()) { | |
| 209 tasks_waiting_for_context_.front().Run(); | |
| 210 tasks_waiting_for_context_.pop(); | |
| 211 } | |
| 212 | |
| 197 delegate_->OnContextInitialized(this); | 213 delegate_->OnContextInitialized(this); |
| 198 } | 214 } |
| 199 | 215 |
| 200 void URLRequestContextAdapter::PostTaskToNetworkThread( | 216 void URLRequestContextAdapter::PostTaskToNetworkThread( |
| 201 const tracked_objects::Location& posted_from, | 217 const tracked_objects::Location& posted_from, |
| 202 const RunAfterContextInitTask& callback) { | 218 const RunAfterContextInitTask& callback) { |
| 203 GetNetworkTaskRunner()->PostTask( | 219 GetNetworkTaskRunner()->PostTask( |
| 204 posted_from, | 220 posted_from, |
| 205 base::Bind( | 221 base::Bind( |
| 206 &URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread, | 222 &URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread, |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 net_log_logger_.reset(); | 298 net_log_logger_.reset(); |
| 283 } | 299 } |
| 284 } | 300 } |
| 285 | 301 |
| 286 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { | 302 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { |
| 287 VLOG(2) << "Net log entry: type=" << entry.type() | 303 VLOG(2) << "Net log entry: type=" << entry.type() |
| 288 << ", source=" << entry.source().type << ", phase=" << entry.phase(); | 304 << ", source=" << entry.source().type << ", phase=" << entry.phase(); |
| 289 } | 305 } |
| 290 | 306 |
| 291 } // namespace cronet | 307 } // namespace cronet |
| OLD | NEW |