| 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/message_loop/message_loop.h" | |
| 12 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 13 #include "components/cronet/url_request_context_config.h" | 10 #include "components/cronet/url_request_context_config.h" |
| 14 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 15 #include "net/base/net_log_logger.h" | 12 #include "net/base/net_log_logger.h" |
| 16 #include "net/cert/cert_verifier.h" | 13 #include "net/cert/cert_verifier.h" |
| 17 #include "net/http/http_auth_handler_factory.h" | 14 #include "net/http/http_auth_handler_factory.h" |
| 18 #include "net/http/http_network_layer.h" | 15 #include "net/http/http_network_layer.h" |
| 19 #include "net/http/http_server_properties.h" | 16 #include "net/http/http_server_properties.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 // 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 | |
| 33 class BasicNetworkDelegate : public net::NetworkDelegate { | 28 class BasicNetworkDelegate : public net::NetworkDelegate { |
| 34 public: | 29 public: |
| 35 BasicNetworkDelegate() {} | 30 BasicNetworkDelegate() {} |
| 36 virtual ~BasicNetworkDelegate() {} | 31 virtual ~BasicNetworkDelegate() {} |
| 37 | 32 |
| 38 private: | 33 private: |
| 39 // net::NetworkDelegate implementation. | 34 // net::NetworkDelegate implementation. |
| 40 int OnBeforeURLRequest(net::URLRequest* request, | 35 int OnBeforeURLRequest(net::URLRequest* request, |
| 41 const net::CompletionCallback& callback, | 36 const net::CompletionCallback& callback, |
| 42 GURL* new_url) override { | 37 GURL* new_url) override { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 59 scoped_refptr<net::HttpResponseHeaders>* _response_headers, | 54 scoped_refptr<net::HttpResponseHeaders>* _response_headers, |
| 60 GURL* allowed_unsafe_redirect_url) override { | 55 GURL* allowed_unsafe_redirect_url) override { |
| 61 return net::OK; | 56 return net::OK; |
| 62 } | 57 } |
| 63 | 58 |
| 64 void OnBeforeRedirect(net::URLRequest* request, | 59 void OnBeforeRedirect(net::URLRequest* request, |
| 65 const GURL& new_location) override {} | 60 const GURL& new_location) override {} |
| 66 | 61 |
| 67 void OnResponseStarted(net::URLRequest* request) override {} | 62 void OnResponseStarted(net::URLRequest* request) override {} |
| 68 | 63 |
| 69 void OnRawBytesRead(const net::URLRequest& request, | 64 void OnRawBytesRead(const net::URLRequest& request, int bytes_read) override { |
| 70 int bytes_read) override {} | 65 } |
| 71 | 66 |
| 72 void OnCompleted(net::URLRequest* request, bool started) override {} | 67 void OnCompleted(net::URLRequest* request, bool started) override {} |
| 73 | 68 |
| 74 void OnURLRequestDestroyed(net::URLRequest* request) override {} | 69 void OnURLRequestDestroyed(net::URLRequest* request) override {} |
| 75 | 70 |
| 76 void OnPACScriptError(int line_number, | 71 void OnPACScriptError(int line_number, const base::string16& error) override { |
| 77 const base::string16& error) override {} | 72 } |
| 78 | 73 |
| 79 NetworkDelegate::AuthRequiredResponse OnAuthRequired( | 74 NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
| 80 net::URLRequest* request, | 75 net::URLRequest* request, |
| 81 const net::AuthChallengeInfo& auth_info, | 76 const net::AuthChallengeInfo& auth_info, |
| 82 const AuthCallback& callback, | 77 const AuthCallback& callback, |
| 83 net::AuthCredentials* credentials) override { | 78 net::AuthCredentials* credentials) override { |
| 84 return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; | 79 return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 85 } | 80 } |
| 86 | 81 |
| 87 bool OnCanGetCookies(const net::URLRequest& request, | 82 bool OnCanGetCookies(const net::URLRequest& request, |
| 88 const net::CookieList& cookie_list) override { | 83 const net::CookieList& cookie_list) override { |
| 89 return false; | 84 return false; |
| 90 } | 85 } |
| 91 | 86 |
| 92 bool OnCanSetCookie(const net::URLRequest& request, | 87 bool OnCanSetCookie(const net::URLRequest& request, |
| 93 const std::string& cookie_line, | 88 const std::string& cookie_line, |
| 94 net::CookieOptions* options) override { | 89 net::CookieOptions* options) override { |
| 95 return false; | 90 return false; |
| 96 } | 91 } |
| 97 | 92 |
| 98 bool OnCanAccessFile(const net::URLRequest& request, | 93 bool OnCanAccessFile(const net::URLRequest& request, |
| 99 const base::FilePath& path) const override { | 94 const base::FilePath& path) const override { |
| 100 return false; | 95 return false; |
| 101 } | 96 } |
| 102 | 97 |
| 103 bool OnCanThrottleRequest( | 98 bool OnCanThrottleRequest(const net::URLRequest& request) const override { |
| 104 const net::URLRequest& request) const override { | |
| 105 return false; | 99 return false; |
| 106 } | 100 } |
| 107 | 101 |
| 108 int OnBeforeSocketStreamConnect( | 102 int OnBeforeSocketStreamConnect( |
| 109 net::SocketStream* stream, | 103 net::SocketStream* stream, |
| 110 const net::CompletionCallback& callback) override { | 104 const net::CompletionCallback& callback) override { |
| 111 return net::OK; | 105 return net::OK; |
| 112 } | 106 } |
| 113 | 107 |
| 114 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); | 108 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); |
| 115 }; | 109 }; |
| 116 | 110 |
| 117 } // namespace | 111 } // namespace |
| 118 | 112 |
| 119 namespace cronet { | 113 namespace cronet { |
| 120 | 114 |
| 121 URLRequestContextAdapter::URLRequestContextAdapter( | 115 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter( |
| 122 URLRequestContextAdapterDelegate* delegate, | 116 CronetURLRequestContextAdapterDelegate* delegate) { |
| 123 std::string user_agent) { | |
| 124 delegate_ = delegate; | 117 delegate_ = delegate; |
| 125 user_agent_ = user_agent; | |
| 126 } | 118 } |
| 127 | 119 |
| 128 void URLRequestContextAdapter::Initialize( | 120 void CronetURLRequestContextAdapter::Initialize( |
| 129 scoped_ptr<URLRequestContextConfig> config) { | 121 scoped_ptr<URLRequestContextConfig> config) { |
| 130 network_thread_ = new base::Thread("network"); | 122 network_thread_ = new base::Thread("network"); |
| 131 base::Thread::Options options; | 123 base::Thread::Options options; |
| 132 options.message_loop_type = base::MessageLoop::TYPE_IO; | 124 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 133 network_thread_->StartWithOptions(options); | 125 network_thread_->StartWithOptions(options); |
| 134 config_ = config.Pass(); | 126 |
| 127 GetNetworkTaskRunner()->PostTask( |
| 128 FROM_HERE, |
| 129 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, |
| 130 this, |
| 131 Passed(&config))); |
| 135 } | 132 } |
| 136 | 133 |
| 137 void URLRequestContextAdapter::InitRequestContextOnMainThread() { | 134 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( |
| 138 if (!base::MessageLoop::current()) { | 135 scoped_ptr<URLRequestContextConfig> config) { |
| 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)); | |
| 147 GetNetworkTaskRunner()->PostTask( | |
| 148 FROM_HERE, | |
| 149 base::Bind(&URLRequestContextAdapter::InitRequestContextOnNetworkThread, | |
| 150 this)); | |
| 151 } | |
| 152 | |
| 153 void URLRequestContextAdapter::InitRequestContextOnNetworkThread() { | |
| 154 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 136 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 155 DCHECK(config_); | |
| 156 // TODO(mmenke): Add method to have the builder enable SPDY. | 137 // TODO(mmenke): Add method to have the builder enable SPDY. |
| 157 net::URLRequestContextBuilder context_builder; | 138 net::URLRequestContextBuilder context_builder; |
| 158 context_builder.set_network_delegate(new BasicNetworkDelegate()); | 139 context_builder.set_network_delegate(new BasicNetworkDelegate()); |
| 159 context_builder.set_proxy_config_service(proxy_config_service_.get()); | 140 context_builder.set_proxy_config_service( |
| 160 config_->ConfigureURLRequestContextBuilder(&context_builder); | 141 new net::ProxyConfigServiceFixed(net::ProxyConfig())); |
| 142 config->ConfigureURLRequestContextBuilder(&context_builder); |
| 161 | 143 |
| 162 context_.reset(context_builder.Build()); | 144 context_.reset(context_builder.Build()); |
| 163 | 145 |
| 164 // Currently (circa M39) enabling QUIC requires setting probability threshold. | 146 // Currently (circa M39) enabling QUIC requires setting probability threshold. |
| 165 if (config_->enable_quic) { | 147 if (config->enable_quic) { |
| 166 context_->http_server_properties() | 148 context_->http_server_properties() |
| 167 ->SetAlternateProtocolProbabilityThreshold(0.0f); | 149 ->SetAlternateProtocolProbabilityThreshold(0.0f); |
| 168 for (size_t hint = 0; hint < config_->quic_hints.size(); ++hint) { | 150 for (size_t hint = 0; hint < config->quic_hints.size(); ++hint) { |
| 169 const URLRequestContextConfig::QuicHint& quic_hint = | 151 const URLRequestContextConfig::QuicHint& quic_hint = |
| 170 *config_->quic_hints[hint]; | 152 *config->quic_hints[hint]; |
| 171 if (quic_hint.host.empty()) { | 153 if (quic_hint.host.empty()) { |
| 172 LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host; | 154 LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host; |
| 173 continue; | 155 continue; |
| 174 } | 156 } |
| 175 | 157 |
| 176 if (quic_hint.port <= std::numeric_limits<uint16>::min() || | 158 if (quic_hint.port <= std::numeric_limits<uint16>::min() || |
| 177 quic_hint.port > std::numeric_limits<uint16>::max()) { | 159 quic_hint.port > std::numeric_limits<uint16>::max()) { |
| 178 LOG(ERROR) << "Invalid QUIC hint port: " | 160 LOG(ERROR) << "Invalid QUIC hint port: " << quic_hint.port; |
| 179 << quic_hint.port; | |
| 180 continue; | 161 continue; |
| 181 } | 162 } |
| 182 | 163 |
| 183 if (quic_hint.alternate_port <= std::numeric_limits<uint16>::min() || | 164 if (quic_hint.alternate_port <= std::numeric_limits<uint16>::min() || |
| 184 quic_hint.alternate_port > std::numeric_limits<uint16>::max()) { | 165 quic_hint.alternate_port > std::numeric_limits<uint16>::max()) { |
| 185 LOG(ERROR) << "Invalid QUIC hint alternate port: " | 166 LOG(ERROR) << "Invalid QUIC hint alternate port: " |
| 186 << quic_hint.alternate_port; | 167 << quic_hint.alternate_port; |
| 187 continue; | 168 continue; |
| 188 } | 169 } |
| 189 | 170 |
| 190 net::HostPortPair quic_hint_host_port_pair(quic_hint.host, | 171 net::HostPortPair quic_hint_host_port_pair(quic_hint.host, |
| 191 quic_hint.port); | 172 quic_hint.port); |
| 192 context_->http_server_properties()->SetAlternateProtocol( | 173 context_->http_server_properties()->SetAlternateProtocol( |
| 193 quic_hint_host_port_pair, | 174 quic_hint_host_port_pair, |
| 194 static_cast<uint16>(quic_hint.alternate_port), | 175 static_cast<uint16>(quic_hint.alternate_port), |
| 195 net::AlternateProtocol::QUIC, | 176 net::AlternateProtocol::QUIC, |
| 196 1.0f); | 177 1.0f); |
| 197 } | 178 } |
| 198 } | 179 } |
| 199 config_.reset(NULL); | 180 user_agent_ = config->user_agent; |
| 200 | |
| 201 if (VLOG_IS_ON(2)) { | |
| 202 net_log_observer_.reset(new NetLogObserver()); | |
| 203 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(), | |
| 204 net::NetLog::LOG_ALL_BUT_BYTES); | |
| 205 } | |
| 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 | 181 |
| 213 delegate_->OnContextInitialized(this); | 182 delegate_->OnContextInitialized(this); |
| 214 } | 183 } |
| 215 | 184 |
| 216 void URLRequestContextAdapter::PostTaskToNetworkThread( | 185 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { |
| 217 const tracked_objects::Location& posted_from, | 186 StopNetLog(); |
| 218 const RunAfterContextInitTask& callback) { | |
| 219 GetNetworkTaskRunner()->PostTask( | |
| 220 posted_from, | |
| 221 base::Bind( | |
| 222 &URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread, | |
| 223 this, | |
| 224 callback)); | |
| 225 } | |
| 226 | |
| 227 void URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread( | |
| 228 const RunAfterContextInitTask& callback) { | |
| 229 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
| 230 if (is_context_initialized_) { | |
| 231 callback.Run(); | |
| 232 return; | |
| 233 } | |
| 234 tasks_waiting_for_context_.push(callback); | |
| 235 } | |
| 236 | |
| 237 URLRequestContextAdapter::~URLRequestContextAdapter() { | |
| 238 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
| 239 if (net_log_observer_) { | |
| 240 context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get()); | |
| 241 net_log_observer_.reset(); | |
| 242 } | |
| 243 StopNetLogHelper(); | |
| 244 // TODO(mef): Ensure that |network_thread_| is destroyed properly. | 187 // TODO(mef): Ensure that |network_thread_| is destroyed properly. |
| 245 } | 188 } |
| 246 | 189 |
| 247 const std::string& URLRequestContextAdapter::GetUserAgent( | 190 const std::string& CronetURLRequestContextAdapter::GetUserAgent( |
| 248 const GURL& url) const { | 191 const GURL& url) const { |
| 249 return user_agent_; | 192 return user_agent_; |
| 250 } | 193 } |
| 251 | 194 |
| 252 net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() { | 195 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() { |
| 253 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
| 254 if (!context_) { | 196 if (!context_) { |
| 255 LOG(ERROR) << "URLRequestContext is not set up"; | 197 LOG(ERROR) << "URLRequestContext is not set up"; |
| 256 } | 198 } |
| 257 return context_.get(); | 199 return context_.get(); |
| 258 } | 200 } |
| 259 | 201 |
| 260 scoped_refptr<base::SingleThreadTaskRunner> | 202 scoped_refptr<base::SingleThreadTaskRunner> |
| 261 URLRequestContextAdapter::GetNetworkTaskRunner() const { | 203 CronetURLRequestContextAdapter::GetNetworkTaskRunner() const { |
| 262 return network_thread_->message_loop_proxy(); | 204 return network_thread_->message_loop_proxy(); |
| 263 } | 205 } |
| 264 | 206 |
| 265 void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) { | 207 void CronetURLRequestContextAdapter::StartNetLogToFile( |
| 266 PostTaskToNetworkThread( | 208 const std::string& file_name) { |
| 209 GetNetworkTaskRunner()->PostTask( |
| 267 FROM_HERE, | 210 FROM_HERE, |
| 268 base::Bind( | 211 base::Bind( |
| 269 &URLRequestContextAdapter::StartNetLogToFileHelper, this, file_name)); | 212 &CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread, |
| 213 this, |
| 214 file_name)); |
| 270 } | 215 } |
| 271 | 216 |
| 272 void URLRequestContextAdapter::StopNetLog() { | 217 void CronetURLRequestContextAdapter::StopNetLog() { |
| 273 PostTaskToNetworkThread( | 218 GetNetworkTaskRunner()->PostTask( |
| 274 FROM_HERE, base::Bind(&URLRequestContextAdapter::StopNetLogHelper, this)); | 219 FROM_HERE, |
| 220 base::Bind(&CronetURLRequestContextAdapter::StopNetLogOnNetworkThread, |
| 221 this)); |
| 275 } | 222 } |
| 276 | 223 |
| 277 void URLRequestContextAdapter::StartNetLogToFileHelper( | 224 void CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread( |
| 278 const std::string& file_name) { | 225 const std::string& file_name) { |
| 279 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 226 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 280 // Do nothing if already logging to a file. | 227 // Do nothing if already logging to a file. |
| 281 if (net_log_logger_) | 228 if (net_log_logger_) |
| 282 return; | 229 return; |
| 283 | 230 |
| 284 base::FilePath file_path(file_name); | 231 base::FilePath file_path(file_name); |
| 285 FILE* file = base::OpenFile(file_path, "w"); | 232 FILE* file = base::OpenFile(file_path, "w"); |
| 286 if (!file) | 233 if (!file) |
| 287 return; | 234 return; |
| 288 | 235 |
| 289 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants()); | 236 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants()); |
| 290 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); | 237 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); |
| 291 net_log_logger_->StartObserving(context_->net_log()); | 238 net_log_logger_->StartObserving(context_->net_log()); |
| 292 } | 239 } |
| 293 | 240 |
| 294 void URLRequestContextAdapter::StopNetLogHelper() { | 241 void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() { |
| 295 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 242 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 296 if (net_log_logger_) { | 243 if (net_log_logger_) { |
| 297 net_log_logger_->StopObserving(); | 244 net_log_logger_->StopObserving(); |
| 298 net_log_logger_.reset(); | 245 net_log_logger_.reset(); |
| 299 } | 246 } |
| 300 } | 247 } |
| 301 | 248 |
| 302 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { | |
| 303 VLOG(2) << "Net log entry: type=" << entry.type() | |
| 304 << ", source=" << entry.source().type << ", phase=" << entry.phase(); | |
| 305 } | |
| 306 | |
| 307 } // namespace cronet | 249 } // namespace cronet |
| OLD | NEW |