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 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 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { |
226 const tracked_objects::Location& posted_from, | 186 StopNetLog(); |
227 const RunAfterContextInitTask& callback) { | |
228 GetNetworkTaskRunner()->PostTask( | |
229 posted_from, | |
230 base::Bind( | |
231 &URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread, | |
232 this, | |
233 callback)); | |
234 } | |
235 | |
236 void URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread( | |
237 const RunAfterContextInitTask& callback) { | |
238 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
239 if (is_context_initialized_) { | |
240 callback.Run(); | |
241 return; | |
242 } | |
243 tasks_waiting_for_context_.push(callback); | |
244 } | |
245 | |
246 URLRequestContextAdapter::~URLRequestContextAdapter() { | |
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. | 187 // TODO(mef): Ensure that |network_thread_| is destroyed properly. |
254 } | 188 } |
255 | 189 |
256 const std::string& URLRequestContextAdapter::GetUserAgent( | 190 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() { |
257 const GURL& url) const { | |
258 return user_agent_; | |
259 } | |
260 | |
261 net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() { | |
262 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
263 if (!context_) { | 191 if (!context_) { |
264 LOG(ERROR) << "URLRequestContext is not set up"; | 192 LOG(ERROR) << "URLRequestContext is not set up"; |
265 } | 193 } |
266 return context_.get(); | 194 return context_.get(); |
267 } | 195 } |
268 | 196 |
269 scoped_refptr<base::SingleThreadTaskRunner> | 197 scoped_refptr<base::SingleThreadTaskRunner> |
270 URLRequestContextAdapter::GetNetworkTaskRunner() const { | 198 CronetURLRequestContextAdapter::GetNetworkTaskRunner() const { |
271 return network_thread_->message_loop_proxy(); | 199 return network_thread_->message_loop_proxy(); |
272 } | 200 } |
273 | 201 |
274 void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) { | 202 void CronetURLRequestContextAdapter::StartNetLogToFile( |
275 PostTaskToNetworkThread( | 203 const std::string& file_name) { |
| 204 GetNetworkTaskRunner()->PostTask( |
276 FROM_HERE, | 205 FROM_HERE, |
277 base::Bind( | 206 base::Bind( |
278 &URLRequestContextAdapter::StartNetLogToFileHelper, this, file_name)); | 207 &CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread, |
| 208 this, |
| 209 file_name)); |
279 } | 210 } |
280 | 211 |
281 void URLRequestContextAdapter::StopNetLog() { | 212 void CronetURLRequestContextAdapter::StopNetLog() { |
282 PostTaskToNetworkThread( | 213 GetNetworkTaskRunner()->PostTask( |
283 FROM_HERE, base::Bind(&URLRequestContextAdapter::StopNetLogHelper, this)); | 214 FROM_HERE, |
| 215 base::Bind(&CronetURLRequestContextAdapter::StopNetLogOnNetworkThread, |
| 216 this)); |
284 } | 217 } |
285 | 218 |
286 void URLRequestContextAdapter::StartNetLogToFileHelper( | 219 void CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread( |
287 const std::string& file_name) { | 220 const std::string& file_name) { |
288 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 221 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
289 // Do nothing if already logging to a file. | 222 // Do nothing if already logging to a file. |
290 if (net_log_logger_) | 223 if (net_log_logger_) |
291 return; | 224 return; |
292 | 225 |
293 base::FilePath file_path(file_name); | 226 base::FilePath file_path(file_name); |
294 FILE* file = base::OpenFile(file_path, "w"); | 227 FILE* file = base::OpenFile(file_path, "w"); |
295 if (!file) | 228 if (!file) |
296 return; | 229 return; |
297 | 230 |
298 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants()); | 231 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants()); |
299 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); | 232 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); |
300 net_log_logger_->StartObserving(context_->net_log()); | 233 net_log_logger_->StartObserving(context_->net_log()); |
301 } | 234 } |
302 | 235 |
303 void URLRequestContextAdapter::StopNetLogHelper() { | 236 void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() { |
304 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 237 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
305 if (net_log_logger_) { | 238 if (net_log_logger_) { |
306 net_log_logger_->StopObserving(); | 239 net_log_logger_->StopObserving(); |
307 net_log_logger_.reset(); | 240 net_log_logger_.reset(); |
308 } | 241 } |
309 } | 242 } |
310 | 243 |
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 | 244 } // namespace cronet |
OLD | NEW |