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