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 "ios/web/shell/shell_url_request_context_getter.h"
7 #include "base/base_paths.h"
8 #include "base/logging.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/path_service.h"
12 #include "base/threading/worker_pool.h"
13 #include "ios/net/cookies/cookie_store_ios.h"
14 #include "ios/web/public/web_client.h"
15 #include "ios/web/public/web_thread.h"
16 #include "ios/web/shell/shell_network_delegate.h"
17 #include "net/base/cache_type.h"
18 #include "net/cert/cert_verifier.h"
19 #include "net/dns/host_resolver.h"
20 #include "net/extras/sqlite/sqlite_persistent_cookie_store.h"
21 #include "net/http/http_auth_handler_factory.h"
22 #include "net/http/http_cache.h"
23 #include "net/http/http_network_session.h"
24 #include "net/http/http_server_properties_impl.h"
25 #include "net/http/transport_security_persister.h"
26 #include "net/http/transport_security_state.h"
27 #include "net/proxy/proxy_config_service_ios.h"
28 #include "net/proxy/proxy_service.h"
29 #include "net/ssl/channel_id_service.h"
30 #include "net/ssl/default_channel_id_store.h"
31 #include "net/ssl/ssl_config_service_defaults.h"
32 #include "net/url_request/data_protocol_handler.h"
33 #include "net/url_request/static_http_user_agent_settings.h"
34 #include "net/url_request/url_request_context.h"
35 #include "net/url_request/url_request_context_storage.h"
36 #include "net/url_request/url_request_job_factory_impl.h"
40 ShellURLRequestContextGetter::ShellURLRequestContextGetter(
41 const base::FilePath
& base_path
,
42 const scoped_refptr
<base::SingleThreadTaskRunner
>& network_task_runner
,
43 const scoped_refptr
<base::SingleThreadTaskRunner
>& file_task_runner
,
44 const scoped_refptr
<base::SingleThreadTaskRunner
>& cache_task_runner
)
45 : base_path_(base_path
),
46 file_task_runner_(file_task_runner
),
47 network_task_runner_(network_task_runner
),
48 cache_task_runner_(cache_task_runner
),
49 proxy_config_service_(new net::ProxyConfigServiceIOS
),
50 net_log_(new net::NetLog()) {
53 ShellURLRequestContextGetter::~ShellURLRequestContextGetter() {
56 net::URLRequestContext
* ShellURLRequestContextGetter::GetURLRequestContext() {
57 DCHECK(network_task_runner_
->BelongsToCurrentThread());
59 if (!url_request_context_
) {
60 url_request_context_
.reset(new net::URLRequestContext());
61 url_request_context_
->set_net_log(net_log_
.get());
62 DCHECK(!network_delegate_
.get());
63 network_delegate_
.reset(new ShellNetworkDelegate
);
64 url_request_context_
->set_network_delegate(network_delegate_
.get());
67 new net::URLRequestContextStorage(url_request_context_
.get()));
69 // Setup the cookie store.
70 base::FilePath cookie_path
;
71 bool cookie_path_found
= PathService::Get(base::DIR_APP_DATA
, &cookie_path
);
72 DCHECK(cookie_path_found
);
73 cookie_path
= cookie_path
.Append("WebShell").Append("Cookies");
74 scoped_refptr
<net::CookieMonster::PersistentCookieStore
> persistent_store
=
75 new net::SQLitePersistentCookieStore(
76 cookie_path
, network_task_runner_
,
77 web::WebThread::GetBlockingPool()->GetSequencedTaskRunner(
78 web::WebThread::GetBlockingPool()->GetSequenceToken()),
80 scoped_refptr
<net::CookieStoreIOS
> cookie_store
=
81 new net::CookieStoreIOS(persistent_store
.get());
82 storage_
->set_cookie_store(cookie_store
.get());
83 net::CookieStoreIOS::SwitchSynchronizedStore(nullptr, cookie_store
.get());
85 std::string user_agent
= web::GetWebClient()->GetUserAgent(false);
86 storage_
->set_http_user_agent_settings(
88 new net::StaticHttpUserAgentSettings("en-us,en", user_agent
))
90 storage_
->set_proxy_service(
91 net::ProxyService::CreateUsingSystemProxyResolver(
92 proxy_config_service_
.release(), 0,
93 url_request_context_
->net_log()));
94 storage_
->set_ssl_config_service(new net::SSLConfigServiceDefaults
);
95 storage_
->set_cert_verifier(net::CertVerifier::CreateDefault());
97 storage_
->set_transport_security_state(
98 make_scoped_ptr(new net::TransportSecurityState()));
99 transport_security_persister_
.reset(new net::TransportSecurityPersister(
100 url_request_context_
->transport_security_state(), base_path_
,
101 file_task_runner_
, false));
102 storage_
->set_channel_id_service(make_scoped_ptr(
103 new net::ChannelIDService(new net::DefaultChannelIDStore(nullptr),
104 base::WorkerPool::GetTaskRunner(true))));
105 storage_
->set_http_server_properties(scoped_ptr
<net::HttpServerProperties
>(
106 new net::HttpServerPropertiesImpl()));
108 scoped_ptr
<net::HostResolver
> host_resolver(
109 net::HostResolver::CreateDefaultResolver(
110 url_request_context_
->net_log()));
111 storage_
->set_http_auth_handler_factory(
112 net::HttpAuthHandlerFactory::CreateDefault(host_resolver
.get()));
113 storage_
->set_host_resolver(host_resolver
.Pass());
115 net::HttpNetworkSession::Params network_session_params
;
116 network_session_params
.cert_verifier
=
117 url_request_context_
->cert_verifier();
118 network_session_params
.transport_security_state
=
119 url_request_context_
->transport_security_state();
120 network_session_params
.channel_id_service
=
121 url_request_context_
->channel_id_service();
122 network_session_params
.net_log
= url_request_context_
->net_log();
123 network_session_params
.proxy_service
=
124 url_request_context_
->proxy_service();
125 network_session_params
.ssl_config_service
=
126 url_request_context_
->ssl_config_service();
127 network_session_params
.http_auth_handler_factory
=
128 url_request_context_
->http_auth_handler_factory();
129 network_session_params
.network_delegate
= network_delegate_
.get();
130 network_session_params
.http_server_properties
=
131 url_request_context_
->http_server_properties();
132 network_session_params
.host_resolver
=
133 url_request_context_
->host_resolver();
135 base::FilePath cache_path
= base_path_
.Append(FILE_PATH_LITERAL("Cache"));
136 net::HttpCache::DefaultBackend
* main_backend
=
137 new net::HttpCache::DefaultBackend(net::DISK_CACHE
,
138 net::CACHE_BACKEND_DEFAULT
,
139 cache_path
, 0, cache_task_runner_
);
141 storage_
->set_http_transaction_factory(
143 new net::HttpCache(network_session_params
, main_backend
))
146 scoped_ptr
<net::URLRequestJobFactoryImpl
> job_factory(
147 new net::URLRequestJobFactoryImpl());
148 bool set_protocol
= job_factory
->SetProtocolHandler(
149 "data", make_scoped_ptr(new net::DataProtocolHandler
));
150 DCHECK(set_protocol
);
152 storage_
->set_job_factory(job_factory
.Pass());
155 return url_request_context_
.get();
158 scoped_refptr
<base::SingleThreadTaskRunner
>
159 ShellURLRequestContextGetter::GetNetworkTaskRunner() const {
160 return network_task_runner_
;