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