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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "components/cronet/url_request_context_config.h" | 10 #include "components/cronet/url_request_context_config.h" |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 | 128 |
129 GetNetworkTaskRunner()->PostTask( | 129 GetNetworkTaskRunner()->PostTask( |
130 FROM_HERE, | 130 FROM_HERE, |
131 base::Bind(&URLRequestContextAdapter::InitializeURLRequestContext, | 131 base::Bind(&URLRequestContextAdapter::InitializeURLRequestContext, |
132 this, | 132 this, |
133 Passed(&config))); | 133 Passed(&config))); |
134 } | 134 } |
135 | 135 |
136 void URLRequestContextAdapter::InitializeURLRequestContext( | 136 void URLRequestContextAdapter::InitializeURLRequestContext( |
137 scoped_ptr<URLRequestContextConfig> config) { | 137 scoped_ptr<URLRequestContextConfig> config) { |
| 138 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
138 // TODO(mmenke): Add method to have the builder enable SPDY. | 139 // TODO(mmenke): Add method to have the builder enable SPDY. |
139 net::URLRequestContextBuilder context_builder; | 140 net::URLRequestContextBuilder context_builder; |
140 context_builder.set_network_delegate(new BasicNetworkDelegate()); | 141 context_builder.set_network_delegate(new BasicNetworkDelegate()); |
141 context_builder.set_proxy_config_service( | 142 context_builder.set_proxy_config_service( |
142 new net::ProxyConfigServiceFixed(net::ProxyConfig())); | 143 new net::ProxyConfigServiceFixed(net::ProxyConfig())); |
143 config->ConfigureURLRequestContextBuilder(&context_builder); | 144 config->ConfigureURLRequestContextBuilder(&context_builder); |
144 | 145 |
145 context_.reset(context_builder.Build()); | 146 context_.reset(context_builder.Build()); |
146 | 147 |
147 // Currently (circa M39) enabling QUIC requires setting probability threshold. | 148 // Currently (circa M39) enabling QUIC requires setting probability threshold. |
(...skipping 23 matching lines...) Expand all Loading... |
171 } | 172 } |
172 | 173 |
173 net::HostPortPair quic_hint_host_port_pair(quic_hint.host, | 174 net::HostPortPair quic_hint_host_port_pair(quic_hint.host, |
174 quic_hint.port); | 175 quic_hint.port); |
175 context_->http_server_properties()->SetAlternateProtocol( | 176 context_->http_server_properties()->SetAlternateProtocol( |
176 quic_hint_host_port_pair, | 177 quic_hint_host_port_pair, |
177 static_cast<uint16>(quic_hint.alternate_port), | 178 static_cast<uint16>(quic_hint.alternate_port), |
178 net::AlternateProtocol::QUIC, | 179 net::AlternateProtocol::QUIC, |
179 1.0f); | 180 1.0f); |
180 } | 181 } |
| 182 is_context_initialized_ = true; |
| 183 while (!tasks_waiting_for_context_.empty()) { |
| 184 tasks_waiting_for_context_.front().Run(); |
| 185 tasks_waiting_for_context_.pop(); |
| 186 } |
181 } | 187 } |
182 | 188 |
183 if (VLOG_IS_ON(2)) { | 189 if (VLOG_IS_ON(2)) { |
184 net_log_observer_.reset(new NetLogObserver()); | 190 net_log_observer_.reset(new NetLogObserver()); |
185 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(), | 191 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(), |
186 net::NetLog::LOG_ALL_BUT_BYTES); | 192 net::NetLog::LOG_ALL_BUT_BYTES); |
187 } | 193 } |
188 | 194 |
189 delegate_->OnContextInitialized(this); | 195 delegate_->OnContextInitialized(this); |
190 } | 196 } |
191 | 197 |
| 198 void URLRequestContextAdapter::PostTaskToNetworkThread( |
| 199 const tracked_objects::Location& posted_from, |
| 200 const RunAfterContextInitTask& callback) { |
| 201 GetNetworkTaskRunner()->PostTask( |
| 202 posted_from, |
| 203 base::Bind( |
| 204 &URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread, |
| 205 this, |
| 206 callback)); |
| 207 } |
| 208 |
| 209 void URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread( |
| 210 const RunAfterContextInitTask& callback) { |
| 211 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 212 if (is_context_initialized_) { |
| 213 callback.Run(); |
| 214 return; |
| 215 } |
| 216 tasks_waiting_for_context_.push(callback); |
| 217 } |
| 218 |
192 URLRequestContextAdapter::~URLRequestContextAdapter() { | 219 URLRequestContextAdapter::~URLRequestContextAdapter() { |
| 220 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
193 if (net_log_observer_) { | 221 if (net_log_observer_) { |
194 context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get()); | 222 context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get()); |
195 net_log_observer_.reset(); | 223 net_log_observer_.reset(); |
196 } | 224 } |
197 StopNetLog(); | 225 StopNetLogHelper(); |
198 // TODO(mef): Ensure that |network_thread_| is destroyed properly. | 226 // TODO(mef): Ensure that |network_thread_| is destroyed properly. |
199 } | 227 } |
200 | 228 |
201 const std::string& URLRequestContextAdapter::GetUserAgent( | 229 const std::string& URLRequestContextAdapter::GetUserAgent( |
202 const GURL& url) const { | 230 const GURL& url) const { |
203 return user_agent_; | 231 return user_agent_; |
204 } | 232 } |
205 | 233 |
206 net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() { | 234 net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() { |
| 235 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
207 if (!context_) { | 236 if (!context_) { |
208 LOG(ERROR) << "URLRequestContext is not set up"; | 237 LOG(ERROR) << "URLRequestContext is not set up"; |
209 } | 238 } |
210 return context_.get(); | 239 return context_.get(); |
211 } | 240 } |
212 | 241 |
213 scoped_refptr<base::SingleThreadTaskRunner> | 242 scoped_refptr<base::SingleThreadTaskRunner> |
214 URLRequestContextAdapter::GetNetworkTaskRunner() const { | 243 URLRequestContextAdapter::GetNetworkTaskRunner() const { |
215 return network_thread_->message_loop_proxy(); | 244 return network_thread_->message_loop_proxy(); |
216 } | 245 } |
217 | 246 |
218 void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) { | 247 void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) { |
| 248 PostTaskToNetworkThread( |
| 249 FROM_HERE, |
| 250 base::Bind( |
| 251 &URLRequestContextAdapter::StartNetLogToFileHelper, this, file_name)); |
| 252 } |
| 253 |
| 254 void URLRequestContextAdapter::StopNetLog() { |
| 255 PostTaskToNetworkThread( |
| 256 FROM_HERE, base::Bind(&URLRequestContextAdapter::StopNetLogHelper, this)); |
| 257 } |
| 258 |
| 259 void URLRequestContextAdapter::StartNetLogToFileHelper( |
| 260 const std::string& file_name) { |
| 261 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
219 // Do nothing if already logging to a file. | 262 // Do nothing if already logging to a file. |
220 if (net_log_logger_) | 263 if (net_log_logger_) |
221 return; | 264 return; |
222 | 265 |
223 base::FilePath file_path(file_name); | 266 base::FilePath file_path(file_name); |
224 FILE* file = base::OpenFile(file_path, "w"); | 267 FILE* file = base::OpenFile(file_path, "w"); |
225 if (!file) | 268 if (!file) |
226 return; | 269 return; |
227 | 270 |
228 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants()); | 271 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants()); |
229 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); | 272 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); |
230 net_log_logger_->StartObserving(context_->net_log()); | 273 net_log_logger_->StartObserving(context_->net_log()); |
231 } | 274 } |
232 | 275 |
233 void URLRequestContextAdapter::StopNetLog() { | 276 void URLRequestContextAdapter::StopNetLogHelper() { |
| 277 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
234 if (net_log_logger_) { | 278 if (net_log_logger_) { |
235 net_log_logger_->StopObserving(); | 279 net_log_logger_->StopObserving(); |
236 net_log_logger_.reset(); | 280 net_log_logger_.reset(); |
237 } | 281 } |
238 } | 282 } |
239 | 283 |
240 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { | 284 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { |
241 VLOG(2) << "Net log entry: type=" << entry.type() | 285 VLOG(2) << "Net log entry: type=" << entry.type() |
242 << ", source=" << entry.source().type << ", phase=" << entry.phase(); | 286 << ", source=" << entry.source().type << ", phase=" << entry.phase(); |
243 } | 287 } |
244 | 288 |
245 } // namespace cronet | 289 } // namespace cronet |
OLD | NEW |