• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_throttler_manager.h"
29 
30 namespace {
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.
BuildOSCpuInfo()35 std::string BuildOSCpuInfo() {
36   std::string os_cpu;
37 
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,
43                                                &os_minor_version,
44                                                &os_bugfix_version);
45 #endif
46 #if defined(OS_POSIX) && !defined(OS_MACOSX)
47   // Should work on any Posix system.
48   struct utsname unixinfo;
49   uname(&unixinfo);
50 
51   std::string cputype;
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)");
56   } else {
57     cputype.assign(unixinfo.machine);
58   }
59 #endif
60 
61   base::StringAppendF(
62       &os_cpu,
63 #if defined(OS_WIN)
64       "Windows NT %d.%d",
65       os_major_version,
66       os_minor_version
67 #elif defined(OS_MACOSX)
68       "Intel Mac OS X %d_%d_%d",
69       os_major_version,
70       os_minor_version,
71       os_bugfix_version
72 #elif defined(OS_CHROMEOS)
73       "CrOS %s %d.%d.%d",
74       cputype.c_str(),  // e.g. i686
75       os_major_version,
76       os_minor_version,
77       os_bugfix_version
78 #else
79       "%s %s",
80       unixinfo.sysname,  // e.g. Linux
81       cputype.c_str()    // e.g. i686
82 #endif
83   );  // NOLINT
84 
85   return os_cpu;
86 }
87 
MakeUserAgentForServiceProcess()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";
93   }
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());
103   return user_agent;
104 }
105 
106 }  // namespace
107 
ServiceURLRequestContext(const std::string & user_agent,net::ProxyConfigService * net_proxy_config_service)108 ServiceURLRequestContext::ServiceURLRequestContext(
109     const std::string& user_agent,
110     net::ProxyConfigService* net_proxy_config_service)
111     : storage_(this) {
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);
124 
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));
141 }
142 
~ServiceURLRequestContext()143 ServiceURLRequestContext::~ServiceURLRequestContext() {
144 }
145 
ServiceURLRequestContextGetter()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();
151 
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()));
158 }
159 
160 net::URLRequestContext*
GetURLRequestContext()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();
167 }
168 
169 scoped_refptr<base::SingleThreadTaskRunner>
GetNetworkTaskRunner() const170 ServiceURLRequestContextGetter::GetNetworkTaskRunner() const {
171   return network_task_runner_;
172 }
173 
~ServiceURLRequestContextGetter()174 ServiceURLRequestContextGetter::~ServiceURLRequestContextGetter() {}
175