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::RunTaskAfterContextInit( | |
199 const RunAfterContextInitTask& callback) { | |
200 GetNetworkTaskRunner()->PostTask( | |
201 FROM_HERE, | |
mef
2014/10/10 15:06:27
I'm a bit concerned that all posted tasks will now
xunjieli
2014/10/10 17:25:45
Done. Good idea!
| |
202 base::Bind( | |
203 &URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread, | |
204 this, | |
205 callback)); | |
206 } | |
207 | |
208 void URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread( | |
209 const RunAfterContextInitTask& callback) { | |
210 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
211 if (is_context_initialized_) { | |
212 callback.Run(); | |
213 return; | |
214 } | |
215 tasks_waiting_for_context_.push(callback); | |
216 } | |
217 | |
192 URLRequestContextAdapter::~URLRequestContextAdapter() { | 218 URLRequestContextAdapter::~URLRequestContextAdapter() { |
219 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
193 if (net_log_observer_) { | 220 if (net_log_observer_) { |
194 context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get()); | 221 context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get()); |
195 net_log_observer_.reset(); | 222 net_log_observer_.reset(); |
196 } | 223 } |
197 StopNetLog(); | 224 StopNetLogHelper(); |
198 // TODO(mef): Ensure that |network_thread_| is destroyed properly. | 225 // TODO(mef): Ensure that |network_thread_| is destroyed properly. |
199 } | 226 } |
200 | 227 |
201 const std::string& URLRequestContextAdapter::GetUserAgent( | 228 const std::string& URLRequestContextAdapter::GetUserAgent( |
202 const GURL& url) const { | 229 const GURL& url) const { |
203 return user_agent_; | 230 return user_agent_; |
204 } | 231 } |
205 | 232 |
206 net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() { | 233 net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() { |
234 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
207 if (!context_) { | 235 if (!context_) { |
208 LOG(ERROR) << "URLRequestContext is not set up"; | 236 LOG(ERROR) << "URLRequestContext is not set up"; |
209 } | 237 } |
210 return context_.get(); | 238 return context_.get(); |
211 } | 239 } |
212 | 240 |
213 scoped_refptr<base::SingleThreadTaskRunner> | 241 scoped_refptr<base::SingleThreadTaskRunner> |
214 URLRequestContextAdapter::GetNetworkTaskRunner() const { | 242 URLRequestContextAdapter::GetNetworkTaskRunner() const { |
215 return network_thread_->message_loop_proxy(); | 243 return network_thread_->message_loop_proxy(); |
216 } | 244 } |
217 | 245 |
218 void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) { | 246 void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) { |
247 RunTaskAfterContextInit(base::Bind( | |
248 &URLRequestContextAdapter::StartNetLogToFileHelper, this, file_name)); | |
249 } | |
250 | |
251 void URLRequestContextAdapter::StopNetLog() { | |
252 RunTaskAfterContextInit( | |
253 base::Bind(&URLRequestContextAdapter::StopNetLogHelper, this)); | |
254 } | |
255 | |
256 void URLRequestContextAdapter::StartNetLogToFileHelper( | |
257 const std::string& file_name) { | |
258 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
219 // Do nothing if already logging to a file. | 259 // Do nothing if already logging to a file. |
220 if (net_log_logger_) | 260 if (net_log_logger_) |
221 return; | 261 return; |
222 | 262 |
223 base::FilePath file_path(file_name); | 263 base::FilePath file_path(file_name); |
224 FILE* file = base::OpenFile(file_path, "w"); | 264 FILE* file = base::OpenFile(file_path, "w"); |
225 if (!file) | 265 if (!file) |
226 return; | 266 return; |
227 | 267 |
228 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants()); | 268 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants()); |
229 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); | 269 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); |
230 net_log_logger_->StartObserving(context_->net_log()); | 270 net_log_logger_->StartObserving(context_->net_log()); |
231 } | 271 } |
232 | 272 |
233 void URLRequestContextAdapter::StopNetLog() { | 273 void URLRequestContextAdapter::StopNetLogHelper() { |
274 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
234 if (net_log_logger_) { | 275 if (net_log_logger_) { |
235 net_log_logger_->StopObserving(); | 276 net_log_logger_->StopObserving(); |
236 net_log_logger_.reset(); | 277 net_log_logger_.reset(); |
237 } | 278 } |
238 } | 279 } |
239 | 280 |
240 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { | 281 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { |
241 VLOG(2) << "Net log entry: type=" << entry.type() | 282 VLOG(2) << "Net log entry: type=" << entry.type() |
242 << ", source=" << entry.source().type << ", phase=" << entry.phase(); | 283 << ", source=" << entry.source().type << ", phase=" << entry.phase(); |
243 } | 284 } |
244 | 285 |
245 } // namespace cronet | 286 } // namespace cronet |
OLD | NEW |