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/cronet_url_request_context_adapter.h" |
| 6 | |
| 7 #include <limits> | |
| 8 | 6 |
| 9 #include "base/bind.h" | 7 #include "base/bind.h" |
| 10 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 11 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 12 #include "components/cronet/url_request_context_config.h" | 10 #include "components/cronet/url_request_context_config.h" |
| 13 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 14 #include "net/base/net_log_logger.h" | 12 #include "net/base/net_log_logger.h" |
| 15 #include "net/cert/cert_verifier.h" | 13 #include "net/cert/cert_verifier.h" |
| 16 #include "net/http/http_auth_handler_factory.h" | 14 #include "net/http/http_auth_handler_factory.h" |
| 17 #include "net/http/http_network_layer.h" | 15 #include "net/http/http_network_layer.h" |
| 18 #include "net/http/http_server_properties.h" | 16 #include "net/http/http_server_properties.h" |
| 19 #include "net/proxy/proxy_config_service_fixed.h" | 17 #include "net/proxy/proxy_config_service_fixed.h" |
| 20 #include "net/proxy/proxy_service.h" | 18 #include "net/proxy/proxy_service.h" |
| 21 #include "net/ssl/ssl_config_service_defaults.h" | 19 #include "net/ssl/ssl_config_service_defaults.h" |
| 22 #include "net/url_request/static_http_user_agent_settings.h" | 20 #include "net/url_request/static_http_user_agent_settings.h" |
| 21 #include "net/url_request/url_request_context.h" | |
| 23 #include "net/url_request/url_request_context_builder.h" | 22 #include "net/url_request/url_request_context_builder.h" |
| 24 #include "net/url_request/url_request_context_storage.h" | 23 #include "net/url_request/url_request_context_storage.h" |
| 25 #include "net/url_request/url_request_job_factory_impl.h" | 24 #include "net/url_request/url_request_job_factory_impl.h" |
| 26 | 25 |
| 27 namespace { | 26 namespace { |
| 28 | 27 |
| 29 class BasicNetworkDelegate : public net::NetworkDelegate { | 28 class BasicNetworkDelegate : public net::NetworkDelegate { |
| 30 public: | 29 public: |
| 31 BasicNetworkDelegate() {} | 30 BasicNetworkDelegate() {} |
| 32 virtual ~BasicNetworkDelegate() {} | 31 virtual ~BasicNetworkDelegate() {} |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 107 return net::OK; | 106 return net::OK; |
| 108 } | 107 } |
| 109 | 108 |
| 110 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); | 109 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); |
| 111 }; | 110 }; |
| 112 | 111 |
| 113 } // namespace | 112 } // namespace |
| 114 | 113 |
| 115 namespace cronet { | 114 namespace cronet { |
| 116 | 115 |
| 117 URLRequestContextAdapter::URLRequestContextAdapter( | 116 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter( |
| 118 URLRequestContextAdapterDelegate* delegate, | 117 CronetURLRequestContextAdapterDelegate* delegate) { |
| 119 std::string user_agent) { | |
| 120 delegate_ = delegate; | 118 delegate_ = delegate; |
| 121 user_agent_ = user_agent; | |
| 122 } | 119 } |
| 123 | 120 |
| 124 void URLRequestContextAdapter::Initialize( | 121 void CronetURLRequestContextAdapter::Initialize( |
| 125 scoped_ptr<URLRequestContextConfig> config) { | 122 scoped_ptr<URLRequestContextConfig> config) { |
| 126 network_thread_ = new base::Thread("network"); | 123 network_thread_ = new base::Thread("network"); |
| 127 base::Thread::Options options; | 124 base::Thread::Options options; |
| 128 options.message_loop_type = base::MessageLoop::TYPE_IO; | 125 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 129 network_thread_->StartWithOptions(options); | 126 network_thread_->StartWithOptions(options); |
| 130 | 127 |
| 131 GetNetworkTaskRunner()->PostTask( | 128 GetNetworkTaskRunner()->PostTask( |
| 132 FROM_HERE, | 129 FROM_HERE, |
| 133 base::Bind(&URLRequestContextAdapter::InitializeURLRequestContext, | 130 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, |
| 134 this, | 131 this, |
| 135 Passed(&config))); | 132 Passed(&config))); |
| 136 } | 133 } |
| 137 | 134 |
| 138 void URLRequestContextAdapter::InitializeURLRequestContext( | 135 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( |
| 139 scoped_ptr<URLRequestContextConfig> config) { | 136 scoped_ptr<URLRequestContextConfig> config) { |
| 140 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
|
xunjieli
2014/10/17 00:31:35
nit: suggest adding DCHECK(GetNetworkTaskRunner()-
mef
2014/10/17 20:19:42
Done.
| |
| 141 // TODO(mmenke): Add method to have the builder enable SPDY. | 137 // TODO(mmenke): Add method to have the builder enable SPDY. |
| 142 net::URLRequestContextBuilder context_builder; | 138 net::URLRequestContextBuilder context_builder; |
| 143 context_builder.set_network_delegate(new BasicNetworkDelegate()); | 139 context_builder.set_network_delegate(new BasicNetworkDelegate()); |
| 144 context_builder.set_proxy_config_service( | 140 context_builder.set_proxy_config_service( |
| 145 new net::ProxyConfigServiceFixed(net::ProxyConfig())); | 141 new net::ProxyConfigServiceFixed(net::ProxyConfig())); |
| 146 config->ConfigureURLRequestContextBuilder(&context_builder); | 142 config->ConfigureURLRequestContextBuilder(&context_builder); |
| 147 | 143 |
| 148 context_.reset(context_builder.Build()); | 144 context_.reset(context_builder.Build()); |
| 149 | 145 |
| 150 // Currently (circa M39) enabling QUIC requires setting probability threshold. | 146 // Currently (circa M39) enabling QUIC requires setting probability threshold. |
| 151 if (config->enable_quic) { | 147 if (config->enable_quic) { |
| 152 context_->http_server_properties() | 148 context_->http_server_properties() |
| 153 ->SetAlternateProtocolProbabilityThreshold(0.0f); | 149 ->SetAlternateProtocolProbabilityThreshold(0.0f); |
| 154 for (size_t hint = 0; hint < config->quic_hints.size(); ++hint) { | 150 for (size_t hint = 0; hint < config->quic_hints.size(); ++hint) { |
| 155 const URLRequestContextConfig::QuicHint& quic_hint = | 151 const URLRequestContextConfig::QuicHint& quic_hint = |
| 156 *config->quic_hints[hint]; | 152 *config->quic_hints[hint]; |
| 157 if (quic_hint.host.empty()) { | 153 if (quic_hint.host.empty()) { |
| 158 LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host; | 154 LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host; |
| 159 continue; | 155 continue; |
| 160 } | 156 } |
| 161 | 157 |
| 162 if (quic_hint.port <= std::numeric_limits<uint16>::min() || | 158 if (quic_hint.port <= std::numeric_limits<uint16>::min() || |
| 163 quic_hint.port > std::numeric_limits<uint16>::max()) { | 159 quic_hint.port > std::numeric_limits<uint16>::max()) { |
| 164 LOG(ERROR) << "Invalid QUIC hint port: " | 160 LOG(ERROR) << "Invalid QUIC hint port: " << quic_hint.port; |
| 165 << quic_hint.port; | |
| 166 continue; | 161 continue; |
| 167 } | 162 } |
| 168 | 163 |
| 169 if (quic_hint.alternate_port <= std::numeric_limits<uint16>::min() || | 164 if (quic_hint.alternate_port <= std::numeric_limits<uint16>::min() || |
| 170 quic_hint.alternate_port > std::numeric_limits<uint16>::max()) { | 165 quic_hint.alternate_port > std::numeric_limits<uint16>::max()) { |
| 171 LOG(ERROR) << "Invalid QUIC hint alternate port: " | 166 LOG(ERROR) << "Invalid QUIC hint alternate port: " |
| 172 << quic_hint.alternate_port; | 167 << quic_hint.alternate_port; |
| 173 continue; | 168 continue; |
| 174 } | 169 } |
| 175 | 170 |
| 176 net::HostPortPair quic_hint_host_port_pair(quic_hint.host, | 171 net::HostPortPair quic_hint_host_port_pair(quic_hint.host, |
| 177 quic_hint.port); | 172 quic_hint.port); |
| 178 context_->http_server_properties()->SetAlternateProtocol( | 173 context_->http_server_properties()->SetAlternateProtocol( |
| 179 quic_hint_host_port_pair, | 174 quic_hint_host_port_pair, |
| 180 static_cast<uint16>(quic_hint.alternate_port), | 175 static_cast<uint16>(quic_hint.alternate_port), |
| 181 net::AlternateProtocol::QUIC, | 176 net::AlternateProtocol::QUIC, |
| 182 1.0f); | 177 1.0f); |
| 183 } | 178 } |
| 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 } | 179 } |
| 190 | 180 user_agent_ = config->user_agent; |
| 191 if (VLOG_IS_ON(2)) { | |
| 192 net_log_observer_.reset(new NetLogObserver()); | |
| 193 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(), | |
| 194 net::NetLog::LOG_ALL_BUT_BYTES); | |
| 195 } | |
| 196 | 181 |
| 197 delegate_->OnContextInitialized(this); | 182 delegate_->OnContextInitialized(this); |
| 198 } | 183 } |
| 199 | 184 |
| 200 void URLRequestContextAdapter::PostTaskToNetworkThread( | 185 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { |
| 201 const tracked_objects::Location& posted_from, | 186 StopNetLog(); |
| 202 const RunAfterContextInitTask& callback) { | |
| 203 GetNetworkTaskRunner()->PostTask( | |
| 204 posted_from, | |
| 205 base::Bind( | |
| 206 &URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread, | |
| 207 this, | |
| 208 callback)); | |
| 209 } | |
| 210 | |
| 211 void URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread( | |
| 212 const RunAfterContextInitTask& callback) { | |
| 213 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
| 214 if (is_context_initialized_) { | |
| 215 callback.Run(); | |
| 216 return; | |
| 217 } | |
| 218 tasks_waiting_for_context_.push(callback); | |
| 219 } | |
| 220 | |
| 221 URLRequestContextAdapter::~URLRequestContextAdapter() { | |
| 222 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
| 223 if (net_log_observer_) { | |
| 224 context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get()); | |
| 225 net_log_observer_.reset(); | |
| 226 } | |
| 227 StopNetLogHelper(); | |
| 228 // TODO(mef): Ensure that |network_thread_| is destroyed properly. | 187 // TODO(mef): Ensure that |network_thread_| is destroyed properly. |
| 229 } | 188 } |
| 230 | 189 |
| 231 const std::string& URLRequestContextAdapter::GetUserAgent( | 190 const std::string& CronetURLRequestContextAdapter::GetUserAgent( |
| 232 const GURL& url) const { | 191 const GURL& url) const { |
| 233 return user_agent_; | 192 return user_agent_; |
| 234 } | 193 } |
| 235 | 194 |
| 236 net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() { | 195 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() { |
| 237 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
| 238 if (!context_) { | 196 if (!context_) { |
| 239 LOG(ERROR) << "URLRequestContext is not set up"; | 197 LOG(ERROR) << "URLRequestContext is not set up"; |
| 240 } | 198 } |
| 241 return context_.get(); | 199 return context_.get(); |
| 242 } | 200 } |
| 243 | 201 |
| 244 scoped_refptr<base::SingleThreadTaskRunner> | 202 scoped_refptr<base::SingleThreadTaskRunner> |
| 245 URLRequestContextAdapter::GetNetworkTaskRunner() const { | 203 CronetURLRequestContextAdapter::GetNetworkTaskRunner() const { |
| 246 return network_thread_->message_loop_proxy(); | 204 return network_thread_->message_loop_proxy(); |
| 247 } | 205 } |
| 248 | 206 |
| 249 void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) { | 207 void CronetURLRequestContextAdapter::StartNetLogToFile( |
| 250 PostTaskToNetworkThread( | 208 const std::string& file_name) { |
| 209 GetNetworkTaskRunner()->PostTask( | |
| 251 FROM_HERE, | 210 FROM_HERE, |
| 252 base::Bind( | 211 base::Bind( |
| 253 &URLRequestContextAdapter::StartNetLogToFileHelper, this, file_name)); | 212 &CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread, |
| 213 this, | |
| 214 file_name)); | |
| 254 } | 215 } |
| 255 | 216 |
| 256 void URLRequestContextAdapter::StopNetLog() { | 217 void CronetURLRequestContextAdapter::StopNetLog() { |
| 257 PostTaskToNetworkThread( | 218 GetNetworkTaskRunner()->PostTask( |
| 258 FROM_HERE, base::Bind(&URLRequestContextAdapter::StopNetLogHelper, this)); | 219 FROM_HERE, |
| 220 base::Bind(&CronetURLRequestContextAdapter::StopNetLogOnNetworkThread, | |
| 221 this)); | |
| 259 } | 222 } |
| 260 | 223 |
| 261 void URLRequestContextAdapter::StartNetLogToFileHelper( | 224 void CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread( |
| 262 const std::string& file_name) { | 225 const std::string& file_name) { |
| 263 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
| 264 // Do nothing if already logging to a file. | 226 // Do nothing if already logging to a file. |
| 265 if (net_log_logger_) | 227 if (net_log_logger_) |
| 266 return; | 228 return; |
| 267 | 229 |
| 268 base::FilePath file_path(file_name); | 230 base::FilePath file_path(file_name); |
| 269 FILE* file = base::OpenFile(file_path, "w"); | 231 FILE* file = base::OpenFile(file_path, "w"); |
| 270 if (!file) | 232 if (!file) |
| 271 return; | 233 return; |
| 272 | 234 |
| 273 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants()); | 235 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants()); |
| 274 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); | 236 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); |
| 275 net_log_logger_->StartObserving(context_->net_log()); | 237 net_log_logger_->StartObserving(context_->net_log()); |
| 276 } | 238 } |
| 277 | 239 |
| 278 void URLRequestContextAdapter::StopNetLogHelper() { | 240 void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() { |
| 279 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
| 280 if (net_log_logger_) { | 241 if (net_log_logger_) { |
| 281 net_log_logger_->StopObserving(); | 242 net_log_logger_->StopObserving(); |
| 282 net_log_logger_.reset(); | 243 net_log_logger_.reset(); |
| 283 } | 244 } |
| 284 } | 245 } |
| 285 | 246 |
| 286 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { | |
| 287 VLOG(2) << "Net log entry: type=" << entry.type() | |
| 288 << ", source=" << entry.source().type << ", phase=" << entry.phase(); | |
| 289 } | |
| 290 | |
| 291 } // namespace cronet | 247 } // namespace cronet |
| OLD | NEW |