Chromium Code Reviews| 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 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 171 } | 171 } |
| 172 | 172 |
| 173 net::HostPortPair quic_hint_host_port_pair(quic_hint.host, | 173 net::HostPortPair quic_hint_host_port_pair(quic_hint.host, |
| 174 quic_hint.port); | 174 quic_hint.port); |
| 175 context_->http_server_properties()->SetAlternateProtocol( | 175 context_->http_server_properties()->SetAlternateProtocol( |
| 176 quic_hint_host_port_pair, | 176 quic_hint_host_port_pair, |
| 177 static_cast<uint16>(quic_hint.alternate_port), | 177 static_cast<uint16>(quic_hint.alternate_port), |
| 178 net::AlternateProtocol::QUIC, | 178 net::AlternateProtocol::QUIC, |
| 179 1.0f); | 179 1.0f); |
| 180 } | 180 } |
| 181 is_context_initialized_ = true; | |
| 182 while (!tasks_waiting_for_context_.empty()) { | |
| 183 tasks_waiting_for_context_.front().Run(); | |
| 184 tasks_waiting_for_context_.pop(); | |
| 185 } | |
| 181 } | 186 } |
| 182 | 187 |
| 183 if (VLOG_IS_ON(2)) { | 188 if (VLOG_IS_ON(2)) { |
| 184 net_log_observer_.reset(new NetLogObserver()); | 189 net_log_observer_.reset(new NetLogObserver()); |
| 185 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(), | 190 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(), |
| 186 net::NetLog::LOG_ALL_BUT_BYTES); | 191 net::NetLog::LOG_ALL_BUT_BYTES); |
| 187 } | 192 } |
| 188 | 193 |
| 189 delegate_->OnContextInitialized(this); | 194 delegate_->OnContextInitialized(this); |
| 190 } | 195 } |
| 191 | 196 |
| 197 // Runs a task that might depend on the context being initialized. | |
| 198 // This method can be called on any thread. | |
|
mmenke
2014/10/02 22:11:52
nit: Function level comments should go with the d
xunjieli
2014/10/03 14:32:55
Done.
| |
| 199 void URLRequestContextAdapter::RunTask( | |
| 200 const RunAfterContextInitTask& callback) { | |
| 201 GetNetworkTaskRunner()->PostTask( | |
| 202 FROM_HERE, | |
| 203 base::Bind( | |
| 204 &URLRequestContextAdapter::RunTaskOnNetworkThread, this, callback)); | |
| 205 } | |
| 206 | |
| 207 // This method should only be run on the network thread. | |
| 208 void URLRequestContextAdapter::RunTaskOnNetworkThread( | |
| 209 const RunAfterContextInitTask& callback) { | |
| 210 if (is_context_initialized_) { | |
| 211 callback.Run(); | |
| 212 return; | |
| 213 } | |
| 214 tasks_waiting_for_context_.push(callback); | |
| 215 } | |
| 216 | |
| 192 URLRequestContextAdapter::~URLRequestContextAdapter() { | 217 URLRequestContextAdapter::~URLRequestContextAdapter() { |
| 193 if (net_log_observer_) { | 218 if (net_log_observer_) { |
| 194 context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get()); | 219 context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get()); |
| 195 net_log_observer_.reset(); | 220 net_log_observer_.reset(); |
| 196 } | 221 } |
| 197 StopNetLog(); | 222 StopNetLog(); |
| 198 // TODO(mef): Ensure that |network_thread_| is destroyed properly. | 223 // TODO(mef): Ensure that |network_thread_| is destroyed properly. |
| 199 } | 224 } |
| 200 | 225 |
| 201 const std::string& URLRequestContextAdapter::GetUserAgent( | 226 const std::string& URLRequestContextAdapter::GetUserAgent( |
| 202 const GURL& url) const { | 227 const GURL& url) const { |
| 203 return user_agent_; | 228 return user_agent_; |
| 204 } | 229 } |
| 205 | 230 |
| 206 net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() { | 231 net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() { |
| 207 if (!context_) { | 232 if (!context_) { |
| 208 LOG(ERROR) << "URLRequestContext is not set up"; | 233 LOG(ERROR) << "URLRequestContext is not set up"; |
| 209 } | 234 } |
| 210 return context_.get(); | 235 return context_.get(); |
| 211 } | 236 } |
| 212 | 237 |
| 213 scoped_refptr<base::SingleThreadTaskRunner> | 238 scoped_refptr<base::SingleThreadTaskRunner> |
| 214 URLRequestContextAdapter::GetNetworkTaskRunner() const { | 239 URLRequestContextAdapter::GetNetworkTaskRunner() const { |
| 215 return network_thread_->message_loop_proxy(); | 240 return network_thread_->message_loop_proxy(); |
| 216 } | 241 } |
| 217 | 242 |
| 218 void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) { | 243 void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) { |
|
mmenke
2014/10/02 22:11:52
This also has to use RunTask now (Same with StopNe
xunjieli
2014/10/03 14:32:55
Done.
| |
| 219 // Do nothing if already logging to a file. | 244 // Do nothing if already logging to a file. |
| 220 if (net_log_logger_) | 245 if (net_log_logger_) |
| 221 return; | 246 return; |
| 222 | 247 |
| 223 base::FilePath file_path(file_name); | 248 base::FilePath file_path(file_name); |
| 224 FILE* file = base::OpenFile(file_path, "w"); | 249 FILE* file = base::OpenFile(file_path, "w"); |
| 225 if (!file) | 250 if (!file) |
| 226 return; | 251 return; |
| 227 | 252 |
| 228 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants()); | 253 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants()); |
| 229 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); | 254 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); |
| 230 net_log_logger_->StartObserving(context_->net_log()); | 255 net_log_logger_->StartObserving(context_->net_log()); |
| 231 } | 256 } |
| 232 | 257 |
| 233 void URLRequestContextAdapter::StopNetLog() { | 258 void URLRequestContextAdapter::StopNetLog() { |
| 234 if (net_log_logger_) { | 259 if (net_log_logger_) { |
| 235 net_log_logger_->StopObserving(); | 260 net_log_logger_->StopObserving(); |
| 236 net_log_logger_.reset(); | 261 net_log_logger_.reset(); |
| 237 } | 262 } |
| 238 } | 263 } |
| 239 | 264 |
| 240 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { | 265 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { |
| 241 VLOG(2) << "Net log entry: type=" << entry.type() | 266 VLOG(2) << "Net log entry: type=" << entry.type() |
| 242 << ", source=" << entry.source().type << ", phase=" << entry.phase(); | 267 << ", source=" << entry.source().type << ", phase=" << entry.phase(); |
| 243 } | 268 } |
| 244 | 269 |
| 245 } // namespace cronet | 270 } // namespace cronet |
| OLD | NEW |