OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/cronet/ios/cronet_environment.h" | 5 #include "components/cronet/ios/cronet_environment.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
10 #include "base/atomicops.h" | 10 #include "base/atomicops.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/feature_list.h" | 12 #include "base/feature_list.h" |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
15 #include "base/files/scoped_file.h" | 15 #include "base/files/scoped_file.h" |
16 #include "base/json/json_writer.h" | 16 #include "base/json/json_writer.h" |
17 #include "base/mac/bind_objc_block.h" | 17 #include "base/mac/bind_objc_block.h" |
18 #include "base/mac/foundation_util.h" | 18 #include "base/mac/foundation_util.h" |
19 #include "base/macros.h" | 19 #include "base/macros.h" |
20 #include "base/memory/ptr_util.h" | 20 #include "base/memory/ptr_util.h" |
21 #include "base/metrics/statistics_recorder.h" | 21 #include "base/metrics/statistics_recorder.h" |
22 #include "base/path_service.h" | 22 #include "base/path_service.h" |
23 #include "base/single_thread_task_runner.h" | 23 #include "base/single_thread_task_runner.h" |
24 #include "base/synchronization/waitable_event.h" | 24 #include "base/synchronization/waitable_event.h" |
25 #include "base/threading/worker_pool.h" | 25 #include "base/threading/worker_pool.h" |
26 #include "components/cronet/histogram_manager.h" | |
26 #include "components/cronet/ios/version.h" | 27 #include "components/cronet/ios/version.h" |
27 #include "components/prefs/json_pref_store.h" | 28 #include "components/prefs/json_pref_store.h" |
28 #include "components/prefs/pref_filter.h" | 29 #include "components/prefs/pref_filter.h" |
30 #include "ios/web/public/user_agent.h" | |
29 #include "net/base/net_errors.h" | 31 #include "net/base/net_errors.h" |
30 #include "net/base/network_change_notifier.h" | 32 #include "net/base/network_change_notifier.h" |
31 #include "net/cert/cert_verifier.h" | 33 #include "net/cert/cert_verifier.h" |
32 #include "net/cert/ct_known_logs.h" | 34 #include "net/cert/ct_known_logs.h" |
33 #include "net/cert/ct_log_verifier.h" | 35 #include "net/cert/ct_log_verifier.h" |
34 #include "net/cert/ct_policy_enforcer.h" | 36 #include "net/cert/ct_policy_enforcer.h" |
35 #include "net/cert/ct_verifier.h" | 37 #include "net/cert/ct_verifier.h" |
36 #include "net/cert/multi_log_ct_verifier.h" | 38 #include "net/cert/multi_log_ct_verifier.h" |
37 #include "net/dns/host_resolver.h" | 39 #include "net/dns/host_resolver.h" |
38 #include "net/dns/mapped_host_resolver.h" | 40 #include "net/dns/mapped_host_resolver.h" |
(...skipping 17 matching lines...) Expand all Loading... | |
56 #include "url/scheme_host_port.h" | 58 #include "url/scheme_host_port.h" |
57 #include "url/url_util.h" | 59 #include "url/url_util.h" |
58 | 60 |
59 namespace { | 61 namespace { |
60 | 62 |
61 base::AtExitManager* g_at_exit_ = nullptr; | 63 base::AtExitManager* g_at_exit_ = nullptr; |
62 net::NetworkChangeNotifier* g_network_change_notifier = nullptr; | 64 net::NetworkChangeNotifier* g_network_change_notifier = nullptr; |
63 // MessageLoop on the main thread. | 65 // MessageLoop on the main thread. |
64 base::MessageLoop* g_main_message_loop = nullptr; | 66 base::MessageLoop* g_main_message_loop = nullptr; |
65 | 67 |
68 // Request context getter for Cronet. | |
69 class CronetURLRequestContextGetter : public net::URLRequestContextGetter { | |
70 public: | |
71 CronetURLRequestContextGetter( | |
72 cronet::CronetEnvironment* environment, | |
73 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) | |
74 : environment_(environment), task_runner_(task_runner) {} | |
75 | |
76 net::URLRequestContext* GetURLRequestContext() override { | |
77 DCHECK(environment_); | |
78 return environment_->GetURLRequestContext(); | |
79 } | |
80 | |
81 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() | |
82 const override { | |
83 return task_runner_; | |
84 } | |
85 | |
86 private: | |
87 // Must be called on the IO thread. | |
88 ~CronetURLRequestContextGetter() override {} | |
89 | |
90 cronet::CronetEnvironment* environment_; | |
91 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
92 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestContextGetter); | |
93 }; | |
94 | |
66 } // namespace | 95 } // namespace |
67 | 96 |
68 namespace cronet { | 97 namespace cronet { |
69 | 98 |
70 bool CronetEnvironment::IsOnNetworkThread() { | 99 bool CronetEnvironment::IsOnNetworkThread() { |
71 return network_io_thread_->task_runner()->BelongsToCurrentThread(); | 100 return network_io_thread_->task_runner()->BelongsToCurrentThread(); |
72 } | 101 } |
73 | 102 |
74 void CronetEnvironment::PostToNetworkThread( | 103 void CronetEnvironment::PostToNetworkThread( |
75 const tracked_objects::Location& from_here, | 104 const tracked_objects::Location& from_here, |
76 const base::Closure& task) { | 105 const base::Closure& task) { |
77 network_io_thread_->task_runner()->PostTask(from_here, task); | 106 network_io_thread_->task_runner()->PostTask(from_here, task); |
78 } | 107 } |
79 | 108 |
80 void CronetEnvironment::PostToFileUserBlockingThread( | 109 void CronetEnvironment::PostToFileUserBlockingThread( |
81 const tracked_objects::Location& from_here, | 110 const tracked_objects::Location& from_here, |
82 const base::Closure& task) { | 111 const base::Closure& task) { |
83 file_user_blocking_thread_->task_runner()->PostTask(from_here, task); | 112 file_user_blocking_thread_->task_runner()->PostTask(from_here, task); |
84 } | 113 } |
85 | 114 |
86 net::URLRequestContext* CronetEnvironment::GetURLRequestContext() const { | 115 net::URLRequestContext* CronetEnvironment::GetURLRequestContext() const { |
87 return main_context_.get(); | 116 return main_context_.get(); |
88 } | 117 } |
89 | 118 |
119 net::URLRequestContextGetter* CronetEnvironment::GetURLRequestContextGetter() | |
120 const { | |
121 return main_context_getter_.get(); | |
122 } | |
123 | |
90 // static | 124 // static |
91 void CronetEnvironment::Initialize() { | 125 void CronetEnvironment::Initialize() { |
92 // DCHECK_EQ([NSThread currentThread], [NSThread mainThread]); | 126 // DCHECK_EQ([NSThread currentThread], [NSThread mainThread]); |
93 // This method must be called once from the main thread. | 127 // This method must be called once from the main thread. |
94 if (!g_at_exit_) | 128 if (!g_at_exit_) |
95 g_at_exit_ = new base::AtExitManager; | 129 g_at_exit_ = new base::AtExitManager; |
96 | 130 |
97 url::Initialize(); | 131 url::Initialize(); |
98 base::CommandLine::Init(0, nullptr); | 132 base::CommandLine::Init(0, nullptr); |
99 | 133 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
180 return context->http_transaction_factory()->GetSession(); | 214 return context->http_transaction_factory()->GetSession(); |
181 } | 215 } |
182 | 216 |
183 void CronetEnvironment::AddQuicHint(const std::string& host, | 217 void CronetEnvironment::AddQuicHint(const std::string& host, |
184 int port, | 218 int port, |
185 int alternate_port) { | 219 int alternate_port) { |
186 DCHECK(port == alternate_port); | 220 DCHECK(port == alternate_port); |
187 quic_hints_.push_back(net::HostPortPair(host, port)); | 221 quic_hints_.push_back(net::HostPortPair(host, port)); |
188 } | 222 } |
189 | 223 |
190 CronetEnvironment::CronetEnvironment(const std::string& user_agent_product_name) | 224 CronetEnvironment::CronetEnvironment(const std::string& user_agent, |
225 bool user_agent_partial) | |
191 : http2_enabled_(false), | 226 : http2_enabled_(false), |
192 quic_enabled_(false), | 227 quic_enabled_(false), |
193 user_agent_product_name_(user_agent_product_name), | 228 user_agent_(user_agent), |
229 user_agent_partial_(user_agent_partial), | |
194 net_log_(new net::NetLog) {} | 230 net_log_(new net::NetLog) {} |
195 | 231 |
196 void CronetEnvironment::Start() { | 232 void CronetEnvironment::Start() { |
197 // Threads setup. | 233 // Threads setup. |
198 network_cache_thread_.reset(new base::Thread("Chrome Network Cache Thread")); | 234 network_cache_thread_.reset(new base::Thread("Chrome Network Cache Thread")); |
199 network_cache_thread_->StartWithOptions( | 235 network_cache_thread_->StartWithOptions( |
200 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | 236 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
201 network_io_thread_.reset(new base::Thread("Chrome Network IO Thread")); | 237 network_io_thread_.reset(new base::Thread("Chrome Network IO Thread")); |
202 network_io_thread_->StartWithOptions( | 238 network_io_thread_->StartWithOptions( |
203 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | 239 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
(...skipping 11 matching lines...) Expand all Loading... | |
215 base::FilePath ssl_key_log_file; | 251 base::FilePath ssl_key_log_file; |
216 if (!PathService::Get(base::DIR_HOME, &ssl_key_log_file)) | 252 if (!PathService::Get(base::DIR_HOME, &ssl_key_log_file)) |
217 return; | 253 return; |
218 net::SSLClientSocket::SetSSLKeyLogFile( | 254 net::SSLClientSocket::SetSSLKeyLogFile( |
219 ssl_key_log_file.Append(ssl_key_log_file_name_), | 255 ssl_key_log_file.Append(ssl_key_log_file_name_), |
220 file_thread_->task_runner()); | 256 file_thread_->task_runner()); |
221 } | 257 } |
222 | 258 |
223 proxy_config_service_ = net::ProxyService::CreateSystemProxyConfigService( | 259 proxy_config_service_ = net::ProxyService::CreateSystemProxyConfigService( |
224 network_io_thread_->task_runner(), nullptr); | 260 network_io_thread_->task_runner(), nullptr); |
225 | 261 main_context_getter_ = new CronetURLRequestContextGetter( |
226 #if defined(USE_NSS_CERTS) | 262 this, network_io_thread_->task_runner()); |
227 net::SetURLRequestContextForNSSHttpIO(main_context_.get()); | |
228 #endif | |
229 base::subtle::MemoryBarrier(); | 263 base::subtle::MemoryBarrier(); |
230 PostToNetworkThread(FROM_HERE, | 264 PostToNetworkThread(FROM_HERE, |
231 base::Bind(&CronetEnvironment::InitializeOnNetworkThread, | 265 base::Bind(&CronetEnvironment::InitializeOnNetworkThread, |
232 base::Unretained(this))); | 266 base::Unretained(this))); |
233 } | 267 } |
234 | 268 |
235 CronetEnvironment::~CronetEnvironment() { | 269 CronetEnvironment::~CronetEnvironment() { |
236 // net::HTTPProtocolHandlerDelegate::SetInstance(nullptr); | 270 // net::HTTPProtocolHandlerDelegate::SetInstance(nullptr); |
237 #if defined(USE_NSS_CERTS) | |
238 net::SetURLRequestContextForNSSHttpIO(nullptr); | |
239 #endif | |
240 } | 271 } |
241 | 272 |
242 void CronetEnvironment::InitializeOnNetworkThread() { | 273 void CronetEnvironment::InitializeOnNetworkThread() { |
243 DCHECK(network_io_thread_->task_runner()->BelongsToCurrentThread()); | 274 DCHECK(network_io_thread_->task_runner()->BelongsToCurrentThread()); |
244 base::FeatureList::InitializeInstance(std::string(), std::string()); | 275 base::FeatureList::InitializeInstance(std::string(), std::string()); |
245 // TODO(mef): Use net:UrlRequestContextBuilder instead of manual build. | 276 // TODO(mef): Use net:UrlRequestContextBuilder instead of manual build. |
246 main_context_.reset(new net::URLRequestContext); | 277 main_context_.reset(new net::URLRequestContext); |
247 main_context_->set_net_log(net_log_.get()); | 278 main_context_->set_net_log(net_log_.get()); |
248 std::string user_agent(user_agent_product_name_ + | 279 |
249 " (iOS); Cronet/" CRONET_VERSION); | 280 if (user_agent_partial_) { |
281 user_agent_ = web::BuildUserAgentFromProduct(user_agent_); | |
282 user_agent_partial_ = false; | |
kapishnikov
2016/10/19 18:28:53
Why do we set it here to 'false'? I assume Initial
mef
2016/10/19 20:12:59
Good point. Done. I thought about moving it into c
| |
283 } | |
250 main_context_->set_http_user_agent_settings( | 284 main_context_->set_http_user_agent_settings( |
251 new net::StaticHttpUserAgentSettings("en", user_agent)); | 285 new net::StaticHttpUserAgentSettings(accept_language_, user_agent_)); |
252 | 286 |
253 main_context_->set_ssl_config_service(new net::SSLConfigServiceDefaults); | 287 main_context_->set_ssl_config_service(new net::SSLConfigServiceDefaults); |
254 main_context_->set_transport_security_state( | 288 main_context_->set_transport_security_state( |
255 new net::TransportSecurityState()); | 289 new net::TransportSecurityState()); |
256 http_server_properties_.reset(new net::HttpServerPropertiesImpl()); | 290 http_server_properties_.reset(new net::HttpServerPropertiesImpl()); |
257 main_context_->set_http_server_properties(http_server_properties_.get()); | 291 main_context_->set_http_server_properties(http_server_properties_.get()); |
258 | 292 |
259 // TODO(rdsmith): Note that the ".release()" calls below are leaking | 293 // TODO(rdsmith): Note that the ".release()" calls below are leaking |
260 // the objects in question; this should be fixed by having an object | 294 // the objects in question; this should be fixed by having an object |
261 // corresponding to URLRequestContextStorage that actually owns those | 295 // corresponding to URLRequestContextStorage that actually owns those |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
352 std::string CronetEnvironment::user_agent() { | 386 std::string CronetEnvironment::user_agent() { |
353 const net::HttpUserAgentSettings* user_agent_settings = | 387 const net::HttpUserAgentSettings* user_agent_settings = |
354 main_context_->http_user_agent_settings(); | 388 main_context_->http_user_agent_settings(); |
355 if (!user_agent_settings) { | 389 if (!user_agent_settings) { |
356 return nullptr; | 390 return nullptr; |
357 } | 391 } |
358 | 392 |
359 return user_agent_settings->GetUserAgent(); | 393 return user_agent_settings->GetUserAgent(); |
360 } | 394 } |
361 | 395 |
396 std::vector<uint8_t> CronetEnvironment::GetHistogramDeltas() { | |
397 base::StatisticsRecorder::Initialize(); | |
398 std::vector<uint8_t> data; | |
399 if (!HistogramManager::GetInstance()->GetDeltas(&data)) | |
400 return std::vector<uint8_t>(); | |
401 return data; | |
402 } | |
403 | |
362 } // namespace cronet | 404 } // namespace cronet |
OLD | NEW |