switch the lld bot to the new COFF linker, 2nd try
[chromium-blink-merge.git] / components / cronet / android / url_request_context_adapter.cc
blob10be235aa42b1be35a24affcd9ff250ac3d955d6
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/files/scoped_file.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/single_thread_task_runner.h"
14 #include "components/cronet/url_request_context_config.h"
15 #include "net/android/network_change_notifier_factory_android.h"
16 #include "net/base/net_errors.h"
17 #include "net/base/net_util.h"
18 #include "net/base/network_change_notifier.h"
19 #include "net/base/network_delegate_impl.h"
20 #include "net/cert/cert_verifier.h"
21 #include "net/http/http_auth_handler_factory.h"
22 #include "net/http/http_network_layer.h"
23 #include "net/http/http_server_properties.h"
24 #include "net/log/write_to_file_net_log_observer.h"
25 #include "net/proxy/proxy_service.h"
26 #include "net/sdch/sdch_owner.h"
27 #include "net/ssl/ssl_config_service_defaults.h"
28 #include "net/url_request/static_http_user_agent_settings.h"
29 #include "net/url_request/url_request_context_builder.h"
30 #include "net/url_request/url_request_context_storage.h"
31 #include "net/url_request/url_request_job_factory_impl.h"
33 namespace {
35 class BasicNetworkDelegate : public net::NetworkDelegateImpl {
36 public:
37 BasicNetworkDelegate() {}
38 ~BasicNetworkDelegate() override {}
40 private:
41 // net::NetworkDelegate implementation.
42 int OnBeforeURLRequest(net::URLRequest* request,
43 const net::CompletionCallback& callback,
44 GURL* new_url) override {
45 return net::OK;
48 int OnBeforeSendHeaders(net::URLRequest* request,
49 const net::CompletionCallback& callback,
50 net::HttpRequestHeaders* headers) override {
51 return net::OK;
54 void OnSendHeaders(net::URLRequest* request,
55 const net::HttpRequestHeaders& headers) override {}
57 int OnHeadersReceived(
58 net::URLRequest* request,
59 const net::CompletionCallback& callback,
60 const net::HttpResponseHeaders* original_response_headers,
61 scoped_refptr<net::HttpResponseHeaders>* _response_headers,
62 GURL* allowed_unsafe_redirect_url) override {
63 return net::OK;
66 void OnBeforeRedirect(net::URLRequest* request,
67 const GURL& new_location) override {}
69 void OnResponseStarted(net::URLRequest* request) override {}
71 void OnRawBytesRead(const net::URLRequest& request,
72 int bytes_read) override {}
74 void OnCompleted(net::URLRequest* request, bool started) override {}
76 void OnURLRequestDestroyed(net::URLRequest* request) override {}
78 void OnPACScriptError(int line_number,
79 const base::string16& error) override {}
81 NetworkDelegate::AuthRequiredResponse OnAuthRequired(
82 net::URLRequest* request,
83 const net::AuthChallengeInfo& auth_info,
84 const AuthCallback& callback,
85 net::AuthCredentials* credentials) override {
86 return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
89 bool OnCanGetCookies(const net::URLRequest& request,
90 const net::CookieList& cookie_list) override {
91 return false;
94 bool OnCanSetCookie(const net::URLRequest& request,
95 const std::string& cookie_line,
96 net::CookieOptions* options) override {
97 return false;
100 bool OnCanAccessFile(const net::URLRequest& request,
101 const base::FilePath& path) const override {
102 return false;
105 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate);
108 } // namespace
110 namespace cronet {
112 URLRequestContextAdapter::URLRequestContextAdapter(
113 URLRequestContextAdapterDelegate* delegate,
114 std::string user_agent)
115 : load_disable_cache_(true),
116 is_context_initialized_(false) {
117 delegate_ = delegate;
118 user_agent_ = user_agent;
121 void URLRequestContextAdapter::Initialize(
122 scoped_ptr<URLRequestContextConfig> config) {
123 network_thread_ = new base::Thread("network");
124 base::Thread::Options options;
125 options.message_loop_type = base::MessageLoop::TYPE_IO;
126 network_thread_->StartWithOptions(options);
127 config_ = config.Pass();
130 void URLRequestContextAdapter::InitRequestContextOnMainThread() {
131 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService(
132 GetNetworkTaskRunner(), NULL));
133 GetNetworkTaskRunner()->PostTask(
134 FROM_HERE,
135 base::Bind(&URLRequestContextAdapter::InitRequestContextOnNetworkThread,
136 this));
139 void URLRequestContextAdapter::InitRequestContextOnNetworkThread() {
140 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
141 DCHECK(config_);
142 // TODO(mmenke): Add method to have the builder enable SPDY.
143 net::URLRequestContextBuilder context_builder;
144 context_builder.set_network_delegate(new BasicNetworkDelegate());
145 context_builder.set_proxy_config_service(proxy_config_service_.get());
146 config_->ConfigureURLRequestContextBuilder(&context_builder);
148 context_.reset(context_builder.Build());
150 if (config_->enable_sdch) {
151 DCHECK(context_->sdch_manager());
152 sdch_owner_.reset(
153 new net::SdchOwner(context_->sdch_manager(), context_.get()));
156 // Currently (circa M39) enabling QUIC requires setting probability threshold.
157 if (config_->enable_quic) {
158 context_->http_server_properties()
159 ->SetAlternativeServiceProbabilityThreshold(0.0f);
160 for (size_t hint = 0; hint < config_->quic_hints.size(); ++hint) {
161 const URLRequestContextConfig::QuicHint& quic_hint =
162 *config_->quic_hints[hint];
163 if (quic_hint.host.empty()) {
164 LOG(ERROR) << "Empty QUIC hint host: " << quic_hint.host;
165 continue;
168 url::CanonHostInfo host_info;
169 std::string canon_host(net::CanonicalizeHost(quic_hint.host, &host_info));
170 if (!host_info.IsIPAddress() &&
171 !net::IsCanonicalizedHostCompliant(canon_host)) {
172 LOG(ERROR) << "Invalid QUIC hint host: " << quic_hint.host;
173 continue;
176 if (quic_hint.port <= std::numeric_limits<uint16>::min() ||
177 quic_hint.port > std::numeric_limits<uint16>::max()) {
178 LOG(ERROR) << "Invalid QUIC hint port: "
179 << quic_hint.port;
180 continue;
183 if (quic_hint.alternate_port <= std::numeric_limits<uint16>::min() ||
184 quic_hint.alternate_port > std::numeric_limits<uint16>::max()) {
185 LOG(ERROR) << "Invalid QUIC hint alternate port: "
186 << quic_hint.alternate_port;
187 continue;
190 net::HostPortPair quic_hint_host_port_pair(canon_host,
191 quic_hint.port);
192 net::AlternativeService alternative_service(
193 net::AlternateProtocol::QUIC, "",
194 static_cast<uint16>(quic_hint.alternate_port));
195 context_->http_server_properties()->SetAlternativeService(
196 quic_hint_host_port_pair, alternative_service, 1.0f);
199 load_disable_cache_ = config_->load_disable_cache;
200 config_.reset(NULL);
202 if (VLOG_IS_ON(2)) {
203 net_log_observer_.reset(new NetLogObserver());
204 context_->net_log()->DeprecatedAddObserver(
205 net_log_observer_.get(),
206 net::NetLogCaptureMode::IncludeCookiesAndCredentials());
209 is_context_initialized_ = true;
210 while (!tasks_waiting_for_context_.empty()) {
211 tasks_waiting_for_context_.front().Run();
212 tasks_waiting_for_context_.pop();
215 delegate_->OnContextInitialized(this);
218 void URLRequestContextAdapter::PostTaskToNetworkThread(
219 const tracked_objects::Location& posted_from,
220 const RunAfterContextInitTask& callback) {
221 GetNetworkTaskRunner()->PostTask(
222 posted_from,
223 base::Bind(
224 &URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread,
225 this,
226 callback));
229 void URLRequestContextAdapter::RunTaskAfterContextInitOnNetworkThread(
230 const RunAfterContextInitTask& callback) {
231 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
232 if (is_context_initialized_) {
233 callback.Run();
234 return;
236 tasks_waiting_for_context_.push(callback);
239 URLRequestContextAdapter::~URLRequestContextAdapter() {
240 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
241 if (net_log_observer_) {
242 context_->net_log()->DeprecatedRemoveObserver(net_log_observer_.get());
243 net_log_observer_.reset();
245 StopNetLogHelper();
246 // TODO(mef): Ensure that |network_thread_| is destroyed properly.
249 const std::string& URLRequestContextAdapter::GetUserAgent(
250 const GURL& url) const {
251 return user_agent_;
254 net::URLRequestContext* URLRequestContextAdapter::GetURLRequestContext() {
255 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
256 if (!context_) {
257 LOG(ERROR) << "URLRequestContext is not set up";
259 return context_.get();
262 scoped_refptr<base::SingleThreadTaskRunner>
263 URLRequestContextAdapter::GetNetworkTaskRunner() const {
264 return network_thread_->task_runner();
267 void URLRequestContextAdapter::StartNetLogToFile(const std::string& file_name,
268 bool log_all) {
269 PostTaskToNetworkThread(
270 FROM_HERE,
271 base::Bind(&URLRequestContextAdapter::StartNetLogToFileHelper, this,
272 file_name, log_all));
275 void URLRequestContextAdapter::StopNetLog() {
276 PostTaskToNetworkThread(
277 FROM_HERE, base::Bind(&URLRequestContextAdapter::StopNetLogHelper, this));
280 void URLRequestContextAdapter::StartNetLogToFileHelper(
281 const std::string& file_name, bool log_all) {
282 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
283 // Do nothing if already logging to a file.
284 if (write_to_file_observer_)
285 return;
287 base::FilePath file_path(file_name);
288 base::ScopedFILE file(base::OpenFile(file_path, "w"));
289 if (!file)
290 return;
292 write_to_file_observer_.reset(new net::WriteToFileNetLogObserver());
293 if (log_all) {
294 write_to_file_observer_->set_capture_mode(
295 net::NetLogCaptureMode::IncludeSocketBytes());
297 write_to_file_observer_->StartObserving(context_->net_log(), file.Pass(),
298 nullptr, context_.get());
301 void URLRequestContextAdapter::StopNetLogHelper() {
302 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread());
303 if (write_to_file_observer_) {
304 write_to_file_observer_->StopObserving(context_.get());
305 write_to_file_observer_.reset();
309 void NetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) {
310 VLOG(2) << "Net log entry: type=" << entry.type()
311 << ", source=" << entry.source().type << ", phase=" << entry.phase();
314 } // namespace cronet