1 // Copyright (c) 2012 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 "chrome/service/net/service_url_request_context.h"
7 #if defined(OS_POSIX) && !defined(OS_MACOSX)
8 #include <sys/utsname.h>
11 #include "base/compiler_specific.h"
12 #include "base/message_loop/message_loop_proxy.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/sys_info.h"
15 #include "chrome/common/chrome_version_info.h"
16 #include "chrome/service/service_process.h"
17 #include "net/cert/cert_verifier.h"
18 #include "net/cookies/cookie_monster.h"
19 #include "net/dns/host_resolver.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/proxy/proxy_config_service.h"
25 #include "net/proxy/proxy_service.h"
26 #include "net/ssl/ssl_config_service_defaults.h"
27 #include "net/url_request/static_http_user_agent_settings.h"
28 #include "net/url_request/url_request_throttler_manager.h"
31 // Copied from webkit/glue/user_agent.cc. We don't want to pull in a dependency
32 // on webkit/glue which also pulls in the renderer. Also our user-agent is
33 // totally different from the user-agent used by the browser, just the
34 // OS-specific parts are common.
35 std::string
BuildOSCpuInfo() {
38 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS)
39 int32 os_major_version
= 0;
40 int32 os_minor_version
= 0;
41 int32 os_bugfix_version
= 0;
42 base::SysInfo::OperatingSystemVersionNumbers(&os_major_version
,
46 #if defined(OS_POSIX) && !defined(OS_MACOSX)
47 // Should work on any Posix system.
48 struct utsname unixinfo
;
52 // special case for biarch systems
53 if (strcmp(unixinfo
.machine
, "x86_64") == 0 &&
54 sizeof(void*) == sizeof(int32
)) { // NOLINT
55 cputype
.assign("i686 (x86_64)");
57 cputype
.assign(unixinfo
.machine
);
67 #elif defined(OS_MACOSX)
68 "Intel Mac OS X %d_%d_%d",
72 #elif defined(OS_CHROMEOS)
74 cputype
.c_str(), // e.g. i686
80 unixinfo
.sysname
, // e.g. Linux
81 cputype
.c_str() // e.g. i686
88 std::string
MakeUserAgentForServiceProcess() {
89 std::string user_agent
;
90 chrome::VersionInfo version_info
;
91 if (!version_info
.is_valid()) {
92 DLOG(ERROR
) << "Unable to create chrome::VersionInfo object";
94 std::string extra_version_info
;
95 if (!version_info
.IsOfficialBuild())
96 extra_version_info
= "-devel";
97 base::StringAppendF(&user_agent
,
98 "Chrome Service %s(%s)%s %s ",
99 version_info
.Version().c_str(),
100 version_info
.LastChange().c_str(),
101 extra_version_info
.c_str(),
102 BuildOSCpuInfo().c_str());
108 ServiceURLRequestContext::ServiceURLRequestContext(
109 const std::string
& user_agent
,
110 net::ProxyConfigService
* net_proxy_config_service
)
112 storage_
.set_host_resolver(net::HostResolver::CreateDefaultResolver(NULL
));
113 storage_
.set_proxy_service(net::ProxyService::CreateUsingSystemProxyResolver(
114 net_proxy_config_service
, 0u, NULL
));
115 storage_
.set_cert_verifier(net::CertVerifier::CreateDefault());
116 storage_
.set_ssl_config_service(new net::SSLConfigServiceDefaults
);
117 storage_
.set_http_auth_handler_factory(
118 net::HttpAuthHandlerFactory::CreateDefault(host_resolver()));
119 storage_
.set_http_server_properties(
120 scoped_ptr
<net::HttpServerProperties
>(
121 new net::HttpServerPropertiesImpl()));
122 storage_
.set_transport_security_state(new net::TransportSecurityState
);
123 storage_
.set_throttler_manager(new net::URLRequestThrottlerManager
);
125 net::HttpNetworkSession::Params session_params
;
126 session_params
.host_resolver
= host_resolver();
127 session_params
.cert_verifier
= cert_verifier();
128 session_params
.transport_security_state
= transport_security_state();
129 session_params
.proxy_service
= proxy_service();
130 session_params
.ssl_config_service
= ssl_config_service();
131 session_params
.http_auth_handler_factory
= http_auth_handler_factory();
132 session_params
.http_server_properties
= http_server_properties();
133 scoped_refptr
<net::HttpNetworkSession
> network_session(
134 new net::HttpNetworkSession(session_params
));
135 storage_
.set_http_transaction_factory(new net::HttpCache(
136 network_session
.get(), net::HttpCache::DefaultBackend::InMemory(0)));
137 // In-memory cookie store.
138 storage_
.set_cookie_store(new net::CookieMonster(NULL
, NULL
));
139 storage_
.set_http_user_agent_settings(new net::StaticHttpUserAgentSettings(
140 "en-us,fr", user_agent
));
143 ServiceURLRequestContext::~ServiceURLRequestContext() {
146 ServiceURLRequestContextGetter::ServiceURLRequestContextGetter()
147 : network_task_runner_(
148 g_service_process
->io_thread()->message_loop_proxy()) {
149 // Build the default user agent.
150 user_agent_
= MakeUserAgentForServiceProcess();
152 // TODO(sanjeevr): Change CreateSystemProxyConfigService to accept a
153 // MessageLoopProxy* instead of MessageLoop*.
154 DCHECK(g_service_process
);
155 proxy_config_service_
.reset(net::ProxyService::CreateSystemProxyConfigService(
156 g_service_process
->io_thread()->message_loop_proxy().get(),
157 g_service_process
->file_thread()->message_loop()));
160 net::URLRequestContext
*
161 ServiceURLRequestContextGetter::GetURLRequestContext() {
162 if (!url_request_context_
.get())
163 url_request_context_
.reset(
164 new ServiceURLRequestContext(user_agent_
,
165 proxy_config_service_
.release()));
166 return url_request_context_
.get();
169 scoped_refptr
<base::SingleThreadTaskRunner
>
170 ServiceURLRequestContextGetter::GetNetworkTaskRunner() const {
171 return network_task_runner_
;
174 ServiceURLRequestContextGetter::~ServiceURLRequestContextGetter() {}