Fix some lint errors in url_request_context_adapter
[chromium-blink-merge.git] / components / cronet / android / url_request_context_adapter.cc
blobffd934459d400c98f35a6299fbefd5653e5b50fd
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
3 // found in the LICENSE file.
5 #include "components/cronet/android/url_request_context_adapter.h"
7 #include <limits>
9 #include "base/bind.h"
10 #include "base/files/file_util.h"
11 #include "base/single_thread_task_runner.h"
12 #include "components/cronet/url_request_context_config.h"
13 #include "net/base/net_errors.h"
14 #include "net/base/net_log_logger.h"
15 #include "net/cert/cert_verifier.h"
16 #include "net/http/http_auth_handler_factory.h"
17 #include "net/http/http_network_layer.h"
18 #include "net/http/http_server_properties.h"
19 #include "net/proxy/proxy_config_service_fixed.h"
20 #include "net/proxy/proxy_service.h"
21 #include "net/ssl/ssl_config_service_defaults.h"
22 #include "net/url_request/static_http_user_agent_settings.h"
23 #include "net/url_request/url_request_context_builder.h"
24 #include "net/url_request/url_request_context_storage.h"
25 #include "net/url_request/url_request_job_factory_impl.h"
27 namespace {
29 class BasicNetworkDelegate : public net::NetworkDelegate {
30 public:
31 BasicNetworkDelegate() {}
32 virtual ~BasicNetworkDelegate() {}
34 private:
35 // net::NetworkDelegate implementation.
36 int OnBeforeURLRequest(net::URLRequest* request,
37 const net::CompletionCallback& callback,
38 GURL* new_url) override {
39 return net::OK;
42 int OnBeforeSendHeaders(net::URLRequest* request,
43 const net::CompletionCallback& callback,
44 net::HttpRequestHeaders* headers) override {
45 return net::OK;
48 void OnSendHeaders(net::URLRequest* request,
49 const net::HttpRequestHeaders& headers) override {}
51 int OnHeadersReceived(
52 net::URLRequest* request,
53 const net::CompletionCallback& callback,
54 const net::HttpResponseHeaders* original_response_headers,
55 scoped_refptr<net::HttpResponseHeaders>* _response_headers,
56 GURL* allowed_unsafe_redirect_url) override {
57 return net::OK;
60 void OnBeforeRedirect(net::URLRequest* request,
61 const GURL& new_location) override {}
63 void OnResponseStarted(net::URLRequest* request) override {}
65 void OnRawBytesRead(const net::URLRequest& request,
66 int bytes_read) override {}
68 void OnCompleted(net::URLRequest* request, bool started) override {}
70 void OnURLRequestDestroyed(net::URLRequest* request) override {}
72 void OnPACScriptError(int line_number,
73 const base::string16& error) override {}
75 NetworkDelegate::AuthRequiredResponse OnAuthRequired(
76 net::URLRequest* request,
77 const net::AuthChallengeInfo& auth_info,
78 const AuthCallback& callback,
79 net::AuthCredentials* credentials) override {
80 return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
83 bool OnCanGetCookies(const net::URLRequest& request,
84 const net::CookieList& cookie_list) override {
85 return false;
88 bool OnCanSetCookie(const net::URLRequest& request,
89 const std::string& cookie_line,
90 net::CookieOptions* options) override {
91 return false;
94 bool OnCanAccessFile(const net::URLRequest& request,
95 const base::FilePath& path) const override {
96 return false;
99 bool OnCanThrottleRequest(
100 const net::URLRequest& request) const override {
101 return false;
104 int OnBeforeSocketStreamConnect(
105 net::SocketStream* stream,
106 const net::CompletionCallback& callback) override {
107 return net::OK;
110 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate);
113 } // namespace
115 namespace cronet {
117 URLRequestContextAdapter::URLRequestContextAdapter(
118 URLRequestContextAdapterDelegate* delegate,
119 std::string user_agent) {
120 delegate_ = delegate;
121 user_agent_ = user_agent;
124 void URLRequestContextAdapter::Initialize(
125 scoped_ptr<URLRequestContextConfig> config) {
126 network_thread_ = new base::Thread("network");
127 base::Thread::Options options;
128 options.message_loop_type = base::MessageLoop::TYPE_IO;
129 network_thread_->StartWithOptions(options);
131 GetNetworkTaskRunner()->PostTask(
132 FROM_HERE,
133 base::Bind(&URLRequestContextAdapter::InitializeURLRequestContext,
134 this,
135 Passed(&config)));
138 void URLRequestContextAdapter::InitializeURLRequestContext(
139 scoped_ptr<URLRequestContextConfig> config) {
140 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
141 // TODO(mmenke): Add method to have the builder enable SPDY.
142 net::URLRequestContextBuilder context_builder;
143 context_builder.set_network_delegate(new BasicNetworkDelegate());
144 context_builder.set_proxy_config_service(
145 new net::ProxyConfigServiceFixed(net::ProxyConfig()));
146 config->ConfigureURLRequestContextBuilder(&context_builder);
148 context_.reset(context_builder.Build());
150 // Currently (circa M39) enabling QUIC requires setting probability threshold.
151 if (config->enable_quic) {
152 context_->http_server_properties()
153 ->SetAlternateProtocolProbabilityThreshold(0.0f);
154 for (size_t hint = 0; hint < config->quic_hints.size(); ++hint) {
155 const URLRequestContextConfig::QuicHint& quic_hint =
156 *config->quic_hints[hint];
157 if (quic_hint.host.empty()) {
158 LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host;
159 continue;
162 if (quic_hint.port <= std::numeric_limits<uint16>::min() ||
163 quic_hint.port > std::numeric_limits<uint16>::max()) {
164 LOG(ERROR) << "Invalid QUIC hint port: "
165 << quic_hint.port;
166 continue;
169 if (quic_hint.alternate_port <= std::numeric_limits<uint16>::min() ||
170 quic_hint.alternate_port > std::numeric_limits<uint16>::max()) {
171 LOG(ERROR) << "Invalid QUIC hint alternate port: "
172 << quic_hint.alternate_port;
173 continue;
176 net::HostPortPair quic_hint_host_port_pair(quic_hint.host,
177 quic_hint.port);
178 context_->http_server_properties()->SetAlternateProtocol(
179 quic_hint_host_port_pair,
180 static_cast<uint16>(quic_hint.alternate_port),
181 net::AlternateProtocol::QUIC,
182 1.0f);
184 is_context_initialized_ = true;
185 while (!tasks_waiting_for_context_.empty()) {
186 tasks_waiting_for_context_.front().Run();
187 tasks_waiting_for_context_.pop();
191 if (VLOG_IS_ON(2)) {
192 net_log_observer_.reset(new NetLogObserver());
193 context_->net_log()->AddThreadSafeObserver(net_log_observer_.get(),
194 net::NetLog::LOG_ALL_BUT_BYTES);
197 delegate_->OnContextInitialized(this);
200 void URLRequestContextAdapter::PostTaskToNetworkThread(
201 const tracked_objects::Location& posted_from,
202 const RunAfterContextInitTask& callback) {
203 GetNetworkTaskRunner()->PostTask(
204 posted_from,
205 base::Bind(
206 &URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread,
207 this,
208 callback));
211 void URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread(
212 const RunAfterContextInitTask& callback) {
213 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
214 if (is_context_initialized_) {
215 callback.Run();
216 return;
218 tasks_waiting_for_context_.push(callback);
221 URLRequestContextAdapter::~URLRequestContextAdapter() {
222 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
223 if (net_log_observer_) {
224 context_->net_log()->RemoveThreadSafeObserver(net_log_observer_.get());
225 net_log_observer_.reset();
227 StopNetLogHelper();
228 // TODO(mef): Ensure that |network_thread_| is destroyed properly.
231 const std::string& URLRequestContextAdapter::GetUserAgent(
232 const GURL& url) const {
233 return user_agent_;
236 net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() {
237 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
238 if (!context_) {
239 LOG(ERROR) << "URLRequestContext is not set up";
241 return context_.get();
244 scoped_refptr<base::SingleThreadTaskRunner>
245 URLRequestContextAdapter::GetNetworkTaskRunner() const {
246 return network_thread_->message_loop_proxy();
249 void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name) {
250 PostTaskToNetworkThread(
251 FROM_HERE,
252 base::Bind(
253 &URLRequestContextAdapter::StartNetLogToFileHelper, this, file_name));
256 void URLRequestContextAdapter::StopNetLog() {
257 PostTaskToNetworkThread(
258 FROM_HERE, base::Bind(&URLRequestContextAdapter::StopNetLogHelper, this));
261 void URLRequestContextAdapter::StartNetLogToFileHelper(
262 const std::string& file_name) {
263 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
264 // Do nothing if already logging to a file.
265 if (net_log_logger_)
266 return;
268 base::FilePath file_path(file_name);
269 FILE* file = base::OpenFile(file_path, "w");
270 if (!file)
271 return;
273 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants());
274 net_log_logger_.reset(new net::NetLogLogger(file, *constants));
275 net_log_logger_->StartObserving(context_->net_log());
278 void URLRequestContextAdapter::StopNetLogHelper() {
279 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
280 if (net_log_logger_) {
281 net_log_logger_->StopObserving();
282 net_log_logger_.reset();
286 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) {
287 VLOG(2) << "Net log entry: type=" << entry.type()
288 << ", source=" << entry.source().type << ", phase=" << entry.phase();
291 } // namespace cronet