service_url_request_context.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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 30namespace { 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. 35std::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 88std::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 108ServiceURLRequestContext::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(new net::HttpServerPropertiesImpl); 120 storage_.set_transport_security_state(new net::TransportSecurityState); 121 storage_.set_throttler_manager(new net::URLRequestThrottlerManager); 122 123 net::HttpNetworkSession::Params session_params; 124 session_params.host_resolver = host_resolver(); 125 session_params.cert_verifier = cert_verifier(); 126 session_params.transport_security_state = transport_security_state(); 127 session_params.proxy_service = proxy_service(); 128 session_params.ssl_config_service = ssl_config_service(); 129 session_params.http_auth_handler_factory = http_auth_handler_factory(); 130 session_params.http_server_properties = http_server_properties(); 131 scoped_refptr<net::HttpNetworkSession> network_session( 132 new net::HttpNetworkSession(session_params)); 133 storage_.set_http_transaction_factory(new net::HttpCache( 134 network_session.get(), net::HttpCache::DefaultBackend::InMemory(0))); 135 // In-memory cookie store. 136 storage_.set_cookie_store(new net::CookieMonster(NULL, NULL)); 137 storage_.set_http_user_agent_settings(new net::StaticHttpUserAgentSettings( 138 "en-us,fr", user_agent)); 139} 140 141ServiceURLRequestContext::~ServiceURLRequestContext() { 142} 143 144ServiceURLRequestContextGetter::ServiceURLRequestContextGetter() 145 : network_task_runner_( 146 g_service_process->io_thread()->message_loop_proxy()) { 147 // Build the default user agent. 148 user_agent_ = MakeUserAgentForServiceProcess(); 149 150 // TODO(sanjeevr): Change CreateSystemProxyConfigService to accept a 151 // MessageLoopProxy* instead of MessageLoop*. 152 DCHECK(g_service_process); 153 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( 154 g_service_process->io_thread()->message_loop_proxy().get(), 155 g_service_process->file_thread()->message_loop())); 156} 157 158net::URLRequestContext* 159ServiceURLRequestContextGetter::GetURLRequestContext() { 160 if (!url_request_context_.get()) 161 url_request_context_.reset( 162 new ServiceURLRequestContext(user_agent_, 163 proxy_config_service_.release())); 164 return url_request_context_.get(); 165} 166 167scoped_refptr<base::SingleThreadTaskRunner> 168ServiceURLRequestContextGetter::GetNetworkTaskRunner() const { 169 return network_task_runner_; 170} 171 172ServiceURLRequestContextGetter::~ServiceURLRequestContextGetter() {} 173