OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/service/net/service_url_request_context.h" | 5 #include "chrome/service/net/service_url_request_context_getter.h" |
6 | 6 |
7 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 7 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
8 #include <sys/utsname.h> | 8 #include <sys/utsname.h> |
9 #endif | 9 #endif |
10 | 10 |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/message_loop/message_loop_proxy.h" | 12 #include "base/message_loop/message_loop_proxy.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "base/sys_info.h" | 14 #include "base/sys_info.h" |
15 #include "chrome/common/chrome_version_info.h" | 15 #include "chrome/common/chrome_version_info.h" |
16 #include "chrome/service/service_process.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" | 17 #include "net/proxy/proxy_config_service.h" |
25 #include "net/proxy/proxy_service.h" | 18 #include "net/proxy/proxy_service.h" |
26 #include "net/ssl/ssl_config_service_defaults.h" | 19 #include "net/url_request/url_request_context_builder.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 | 20 |
31 namespace { | 21 namespace { |
32 // Copied from webkit/glue/user_agent.cc. We don't want to pull in a dependency | 22 // 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 | 23 // 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 | 24 // totally different from the user-agent used by the browser, just the |
35 // OS-specific parts are common. | 25 // OS-specific parts are common. |
36 std::string BuildOSCpuInfo() { | 26 std::string BuildOSCpuInfo() { |
37 std::string os_cpu; | 27 std::string os_cpu; |
38 | 28 |
39 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) | 29 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 #else | 69 #else |
80 "%s %s", | 70 "%s %s", |
81 unixinfo.sysname, // e.g. Linux | 71 unixinfo.sysname, // e.g. Linux |
82 cputype.c_str() // e.g. i686 | 72 cputype.c_str() // e.g. i686 |
83 #endif | 73 #endif |
84 ); // NOLINT | 74 ); // NOLINT |
85 | 75 |
86 return os_cpu; | 76 return os_cpu; |
87 } | 77 } |
88 | 78 |
| 79 // Returns the default user agent. |
89 std::string MakeUserAgentForServiceProcess() { | 80 std::string MakeUserAgentForServiceProcess() { |
90 std::string user_agent; | 81 std::string user_agent; |
91 chrome::VersionInfo version_info; | 82 chrome::VersionInfo version_info; |
92 if (!version_info.is_valid()) { | 83 if (!version_info.is_valid()) { |
93 DLOG(ERROR) << "Unable to create chrome::VersionInfo object"; | 84 DLOG(ERROR) << "Unable to create chrome::VersionInfo object"; |
94 } | 85 } |
95 std::string extra_version_info; | 86 std::string extra_version_info; |
96 if (!version_info.IsOfficialBuild()) | 87 if (!version_info.IsOfficialBuild()) |
97 extra_version_info = "-devel"; | 88 extra_version_info = "-devel"; |
98 base::StringAppendF(&user_agent, | 89 base::StringAppendF(&user_agent, |
99 "Chrome Service %s(%s)%s %s ", | 90 "Chrome Service %s(%s)%s %s ", |
100 version_info.Version().c_str(), | 91 version_info.Version().c_str(), |
101 version_info.LastChange().c_str(), | 92 version_info.LastChange().c_str(), |
102 extra_version_info.c_str(), | 93 extra_version_info.c_str(), |
103 BuildOSCpuInfo().c_str()); | 94 BuildOSCpuInfo().c_str()); |
104 return user_agent; | 95 return user_agent; |
105 } | 96 } |
106 | 97 |
107 } // namespace | 98 } // namespace |
108 | 99 |
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 | |
145 ServiceURLRequestContext::~ServiceURLRequestContext() { | |
146 } | |
147 | |
148 ServiceURLRequestContextGetter::ServiceURLRequestContextGetter() | 100 ServiceURLRequestContextGetter::ServiceURLRequestContextGetter() |
149 : network_task_runner_( | 101 : user_agent_(MakeUserAgentForServiceProcess()), |
| 102 network_task_runner_( |
150 g_service_process->io_thread()->message_loop_proxy()) { | 103 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 | 104 // TODO(sanjeevr): Change CreateSystemProxyConfigService to accept a |
155 // MessageLoopProxy* instead of MessageLoop*. | 105 // MessageLoopProxy* instead of MessageLoop*. |
156 DCHECK(g_service_process); | 106 DCHECK(g_service_process); |
157 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( | 107 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( |
158 g_service_process->io_thread()->message_loop_proxy().get(), | 108 g_service_process->io_thread()->message_loop_proxy().get(), |
159 g_service_process->file_thread()->message_loop())); | 109 g_service_process->file_thread()->message_loop())); |
160 } | 110 } |
161 | 111 |
162 net::URLRequestContext* | 112 net::URLRequestContext* |
163 ServiceURLRequestContextGetter::GetURLRequestContext() { | 113 ServiceURLRequestContextGetter::GetURLRequestContext() { |
164 if (!url_request_context_.get()) | 114 if (!url_request_context_.get()) { |
165 url_request_context_.reset( | 115 net::URLRequestContextBuilder builder; |
166 new ServiceURLRequestContext(user_agent_, | 116 builder.set_user_agent(user_agent_); |
167 proxy_config_service_.release())); | 117 builder.set_accept_language("en-us,fr"); |
| 118 builder.set_proxy_config_service(proxy_config_service_.release()); |
| 119 builder.set_throttling_enabled(true); |
| 120 url_request_context_.reset(builder.Build()); |
| 121 } |
168 return url_request_context_.get(); | 122 return url_request_context_.get(); |
169 } | 123 } |
170 | 124 |
171 scoped_refptr<base::SingleThreadTaskRunner> | 125 scoped_refptr<base::SingleThreadTaskRunner> |
172 ServiceURLRequestContextGetter::GetNetworkTaskRunner() const { | 126 ServiceURLRequestContextGetter::GetNetworkTaskRunner() const { |
173 return network_task_runner_; | 127 return network_task_runner_; |
174 } | 128 } |
175 | 129 |
176 ServiceURLRequestContextGetter::~ServiceURLRequestContextGetter() {} | 130 ServiceURLRequestContextGetter::~ServiceURLRequestContextGetter() {} |
OLD | NEW |