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.
4
5 #include "chrome/service/net/service_url_request_context.h"
6
7 #if defined(OS_POSIX) && !defined(OS_MACOSX)
8 #include <sys/utsname.h>
9 #endif
10
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_job_factory_impl.h"
29 #include "net/url_request/url_request_throttler_manager.h"
30
31 namespace {
32 // Copied from webkit/glue/user_agent.cc. We don't want to pull in a dependency
33 // on webkit/glue which also pulls in the renderer. Also our user-agent is
34 // totally different from the user-agent used by the browser, just the
35 // OS-specific parts are common.
BuildOSCpuInfo()36 std::string BuildOSCpuInfo() {
37 std::string os_cpu;
38
39 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS)
40 int32 os_major_version = 0;
41 int32 os_minor_version = 0;
42 int32 os_bugfix_version = 0;
43 base::SysInfo::OperatingSystemVersionNumbers(&os_major_version,
44 &os_minor_version,
45 &os_bugfix_version);
46 #endif
47 #if defined(OS_POSIX) && !defined(OS_MACOSX)
48 // Should work on any Posix system.
49 struct utsname unixinfo;
50 uname(&unixinfo);
51
52 std::string cputype;
53 // special case for biarch systems
54 if (strcmp(unixinfo.machine, "x86_64") == 0 &&
55 sizeof(void*) == sizeof(int32)) { // NOLINT
56 cputype.assign("i686 (x86_64)");
57 } else {
58 cputype.assign(unixinfo.machine);
59 }
60 #endif
61
62 base::StringAppendF(
63 &os_cpu,
64 #if defined(OS_WIN)
65 "Windows NT %d.%d",
66 os_major_version,
67 os_minor_version
68 #elif defined(OS_MACOSX)
69 "Intel Mac OS X %d_%d_%d",
70 os_major_version,
71 os_minor_version,
72 os_bugfix_version
73 #elif defined(OS_CHROMEOS)
74 "CrOS %s %d.%d.%d",
75 cputype.c_str(), // e.g. i686
76 os_major_version,
77 os_minor_version,
78 os_bugfix_version
79 #else
80 "%s %s",
81 unixinfo.sysname, // e.g. Linux
82 cputype.c_str() // e.g. i686
83 #endif
84 ); // NOLINT
85
86 return os_cpu;
87 }
88
MakeUserAgentForServiceProcess()89 std::string MakeUserAgentForServiceProcess() {
90 std::string user_agent;
91 chrome::VersionInfo version_info;
92 if (!version_info.is_valid()) {
93 DLOG(ERROR) << "Unable to create chrome::VersionInfo object";
94 }
95 std::string extra_version_info;
96 if (!version_info.IsOfficialBuild())
97 extra_version_info = "-devel";
98 base::StringAppendF(&user_agent,
99 "Chrome Service %s(%s)%s %s ",
100 version_info.Version().c_str(),
101 version_info.LastChange().c_str(),
102 extra_version_info.c_str(),
103 BuildOSCpuInfo().c_str());
104 return user_agent;
105 }
106
107 } // namespace
108
ServiceURLRequestContext(const std::string & user_agent,net::ProxyConfigService * net_proxy_config_service)109 ServiceURLRequestContext::ServiceURLRequestContext(
110 const std::string& user_agent,
111 net::ProxyConfigService* net_proxy_config_service)
112 : storage_(this) {
113 storage_.set_host_resolver(net::HostResolver::CreateDefaultResolver(NULL));
114 storage_.set_proxy_service(net::ProxyService::CreateUsingSystemProxyResolver(
115 net_proxy_config_service, 0u, NULL));
116 storage_.set_cert_verifier(net::CertVerifier::CreateDefault());
117 storage_.set_ssl_config_service(new net::SSLConfigServiceDefaults);
118 storage_.set_http_auth_handler_factory(
119 net::HttpAuthHandlerFactory::CreateDefault(host_resolver()));
120 storage_.set_http_server_properties(
121 scoped_ptr<net::HttpServerProperties>(
122 new net::HttpServerPropertiesImpl()));
123 storage_.set_transport_security_state(new net::TransportSecurityState);
124 storage_.set_throttler_manager(new net::URLRequestThrottlerManager);
125
126 net::HttpNetworkSession::Params session_params;
127 session_params.host_resolver = host_resolver();
128 session_params.cert_verifier = cert_verifier();
129 session_params.transport_security_state = transport_security_state();
130 session_params.proxy_service = proxy_service();
131 session_params.ssl_config_service = ssl_config_service();
132 session_params.http_auth_handler_factory = http_auth_handler_factory();
133 session_params.http_server_properties = http_server_properties();
134 scoped_refptr<net::HttpNetworkSession> network_session(
135 new net::HttpNetworkSession(session_params));
136 storage_.set_http_transaction_factory(new net::HttpCache(
137 network_session.get(), net::HttpCache::DefaultBackend::InMemory(0)));
138 // In-memory cookie store.
139 storage_.set_cookie_store(new net::CookieMonster(NULL, NULL));
140 storage_.set_job_factory(new net::URLRequestJobFactoryImpl());
141 storage_.set_http_user_agent_settings(new net::StaticHttpUserAgentSettings(
142 "en-us,fr", user_agent));
143 }
144
~ServiceURLRequestContext()145 ServiceURLRequestContext::~ServiceURLRequestContext() {
146 }
147
ServiceURLRequestContextGetter()148 ServiceURLRequestContextGetter::ServiceURLRequestContextGetter()
149 : network_task_runner_(
150 g_service_process->io_thread()->message_loop_proxy()) {
151 // Build the default user agent.
152 user_agent_ = MakeUserAgentForServiceProcess();
153
154 // TODO(sanjeevr): Change CreateSystemProxyConfigService to accept a
155 // MessageLoopProxy* instead of MessageLoop*.
156 DCHECK(g_service_process);
157 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService(
158 g_service_process->io_thread()->message_loop_proxy().get(),
159 g_service_process->file_thread()->message_loop()));
160 }
161
162 net::URLRequestContext*
GetURLRequestContext()163 ServiceURLRequestContextGetter::GetURLRequestContext() {
164 if (!url_request_context_.get())
165 url_request_context_.reset(
166 new ServiceURLRequestContext(user_agent_,
167 proxy_config_service_.release()));
168 return url_request_context_.get();
169 }
170
171 scoped_refptr<base::SingleThreadTaskRunner>
GetNetworkTaskRunner() const172 ServiceURLRequestContextGetter::GetNetworkTaskRunner() const {
173 return network_task_runner_;
174 }
175
~ServiceURLRequestContextGetter()176 ServiceURLRequestContextGetter::~ServiceURLRequestContextGetter() {}
177