| 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/metrics/statistics_recorder.h" | 20 #include "base/metrics/statistics_recorder.h" |
| 21 #include "base/path_service.h" | 21 #include "base/path_service.h" |
| 22 #include "base/synchronization/waitable_event.h" | 22 #include "base/synchronization/waitable_event.h" |
| 23 #include "base/threading/worker_pool.h" | 23 #include "base/threading/worker_pool.h" |
| 24 #include "components/cronet/histogram_manager.h" |
| 24 #include "components/cronet/ios/version.h" | 25 #include "components/cronet/ios/version.h" |
| 25 #include "components/prefs/json_pref_store.h" | 26 #include "components/prefs/json_pref_store.h" |
| 26 #include "components/prefs/pref_filter.h" | 27 #include "components/prefs/pref_filter.h" |
| 27 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
| 28 #include "net/base/network_change_notifier.h" | 29 #include "net/base/network_change_notifier.h" |
| 29 #include "net/cert/cert_verify_result.h" | 30 #include "net/cert/cert_verify_result.h" |
| 30 #include "net/cert/ct_policy_enforcer.h" | 31 #include "net/cert/ct_policy_enforcer.h" |
| 31 #include "net/cert/multi_log_ct_verifier.h" | 32 #include "net/cert/multi_log_ct_verifier.h" |
| 32 #include "net/dns/host_resolver.h" | 33 #include "net/dns/host_resolver.h" |
| 33 #include "net/dns/mapped_host_resolver.h" | 34 #include "net/dns/mapped_host_resolver.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 50 #include "url/scheme_host_port.h" | 51 #include "url/scheme_host_port.h" |
| 51 #include "url/url_util.h" | 52 #include "url/url_util.h" |
| 52 | 53 |
| 53 namespace { | 54 namespace { |
| 54 | 55 |
| 55 base::AtExitManager* g_at_exit_ = nullptr; | 56 base::AtExitManager* g_at_exit_ = nullptr; |
| 56 net::NetworkChangeNotifier* g_network_change_notifier = nullptr; | 57 net::NetworkChangeNotifier* g_network_change_notifier = nullptr; |
| 57 // MessageLoop on the main thread. | 58 // MessageLoop on the main thread. |
| 58 base::MessageLoop* g_main_message_loop = nullptr; | 59 base::MessageLoop* g_main_message_loop = nullptr; |
| 59 | 60 |
| 61 // Request context getter for Cronet. |
| 62 class CronetURLRequestContextGetter : public net::URLRequestContextGetter { |
| 63 public: |
| 64 CronetURLRequestContextGetter( |
| 65 cronet::CronetEnvironment* environment, |
| 66 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) |
| 67 : environment_(environment), task_runner_(task_runner) {} |
| 68 |
| 69 net::URLRequestContext* GetURLRequestContext() override { |
| 70 DCHECK(environment_); |
| 71 return environment_->GetURLRequestContext(); |
| 72 } |
| 73 |
| 74 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() |
| 75 const override { |
| 76 return task_runner_; |
| 77 } |
| 78 |
| 79 private: |
| 80 // Must be called on the IO thread. |
| 81 ~CronetURLRequestContextGetter() override {} |
| 82 |
| 83 cronet::CronetEnvironment* environment_; |
| 84 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 85 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestContextGetter); |
| 86 }; |
| 87 |
| 60 } // namespace | 88 } // namespace |
| 61 | 89 |
| 62 namespace cronet { | 90 namespace cronet { |
| 63 | 91 |
| 64 bool CronetEnvironment::IsOnNetworkThread() { | 92 bool CronetEnvironment::IsOnNetworkThread() { |
| 65 return network_io_thread_->task_runner()->BelongsToCurrentThread(); | 93 return network_io_thread_->task_runner()->BelongsToCurrentThread(); |
| 66 } | 94 } |
| 67 | 95 |
| 68 void CronetEnvironment::PostToNetworkThread( | 96 void CronetEnvironment::PostToNetworkThread( |
| 69 const tracked_objects::Location& from_here, | 97 const tracked_objects::Location& from_here, |
| 70 const base::Closure& task) { | 98 const base::Closure& task) { |
| 71 network_io_thread_->task_runner()->PostTask(from_here, task); | 99 network_io_thread_->task_runner()->PostTask(from_here, task); |
| 72 } | 100 } |
| 73 | 101 |
| 74 void CronetEnvironment::PostToFileUserBlockingThread( | 102 void CronetEnvironment::PostToFileUserBlockingThread( |
| 75 const tracked_objects::Location& from_here, | 103 const tracked_objects::Location& from_here, |
| 76 const base::Closure& task) { | 104 const base::Closure& task) { |
| 77 file_user_blocking_thread_->message_loop()->PostTask(from_here, task); | 105 file_user_blocking_thread_->message_loop()->PostTask(from_here, task); |
| 78 } | 106 } |
| 79 | 107 |
| 80 net::URLRequestContext* CronetEnvironment::GetURLRequestContext() const { | 108 net::URLRequestContext* CronetEnvironment::GetURLRequestContext() const { |
| 81 return main_context_.get(); | 109 return main_context_.get(); |
| 82 } | 110 } |
| 83 | 111 |
| 112 net::URLRequestContextGetter* CronetEnvironment::GetURLRequestContextGetter() |
| 113 const { |
| 114 return main_context_getter_.get(); |
| 115 } |
| 116 |
| 84 // static | 117 // static |
| 85 void CronetEnvironment::Initialize() { | 118 void CronetEnvironment::Initialize() { |
| 86 // DCHECK_EQ([NSThread currentThread], [NSThread mainThread]); | 119 // DCHECK_EQ([NSThread currentThread], [NSThread mainThread]); |
| 87 // This method must be called once from the main thread. | 120 // This method must be called once from the main thread. |
| 88 if (!g_at_exit_) | 121 if (!g_at_exit_) |
| 89 g_at_exit_ = new base::AtExitManager; | 122 g_at_exit_ = new base::AtExitManager; |
| 90 | 123 |
| 91 url::Initialize(); | 124 url::Initialize(); |
| 92 base::CommandLine::Init(0, nullptr); | 125 base::CommandLine::Init(0, nullptr); |
| 93 | 126 |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 base::FilePath ssl_key_log_file; | 242 base::FilePath ssl_key_log_file; |
| 210 if (!PathService::Get(base::DIR_HOME, &ssl_key_log_file)) | 243 if (!PathService::Get(base::DIR_HOME, &ssl_key_log_file)) |
| 211 return; | 244 return; |
| 212 net::SSLClientSocket::SetSSLKeyLogFile( | 245 net::SSLClientSocket::SetSSLKeyLogFile( |
| 213 ssl_key_log_file.Append(ssl_key_log_file_name_), | 246 ssl_key_log_file.Append(ssl_key_log_file_name_), |
| 214 file_thread_->task_runner()); | 247 file_thread_->task_runner()); |
| 215 } | 248 } |
| 216 | 249 |
| 217 proxy_config_service_ = net::ProxyService::CreateSystemProxyConfigService( | 250 proxy_config_service_ = net::ProxyService::CreateSystemProxyConfigService( |
| 218 network_io_thread_->task_runner(), nullptr); | 251 network_io_thread_->task_runner(), nullptr); |
| 219 | 252 main_context_getter_ = new CronetURLRequestContextGetter( |
| 220 #if defined(USE_NSS_CERTS) | 253 this, network_io_thread_->task_runner()); |
| 221 net::SetURLRequestContextForNSSHttpIO(main_context_.get()); | |
| 222 #endif | |
| 223 base::subtle::MemoryBarrier(); | 254 base::subtle::MemoryBarrier(); |
| 224 PostToNetworkThread(FROM_HERE, | 255 PostToNetworkThread(FROM_HERE, |
| 225 base::Bind(&CronetEnvironment::InitializeOnNetworkThread, | 256 base::Bind(&CronetEnvironment::InitializeOnNetworkThread, |
| 226 base::Unretained(this))); | 257 base::Unretained(this))); |
| 227 } | 258 } |
| 228 | 259 |
| 229 CronetEnvironment::~CronetEnvironment() { | 260 CronetEnvironment::~CronetEnvironment() { |
| 230 // net::HTTPProtocolHandlerDelegate::SetInstance(nullptr); | 261 // net::HTTPProtocolHandlerDelegate::SetInstance(nullptr); |
| 231 #if defined(USE_NSS_CERTS) | |
| 232 net::SetURLRequestContextForNSSHttpIO(nullptr); | |
| 233 #endif | |
| 234 } | 262 } |
| 235 | 263 |
| 236 void CronetEnvironment::InitializeOnNetworkThread() { | 264 void CronetEnvironment::InitializeOnNetworkThread() { |
| 237 DCHECK(network_io_thread_->task_runner()->BelongsToCurrentThread()); | 265 DCHECK(network_io_thread_->task_runner()->BelongsToCurrentThread()); |
| 238 base::FeatureList::InitializeInstance(std::string(), std::string()); | 266 base::FeatureList::InitializeInstance(std::string(), std::string()); |
| 239 // TODO(mef): Use net:UrlRequestContextBuilder instead of manual build. | 267 // TODO(mef): Use net:UrlRequestContextBuilder instead of manual build. |
| 240 main_context_.reset(new net::URLRequestContext); | 268 main_context_.reset(new net::URLRequestContext); |
| 241 main_context_->set_net_log(net_log_.get()); | 269 main_context_->set_net_log(net_log_.get()); |
| 242 std::string user_agent(user_agent_product_name_ + | 270 std::string user_agent(user_agent_product_name_ + |
| 243 " (iOS); Cronet/" CRONET_VERSION); | 271 " (iOS); Cronet/" CRONET_VERSION); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 std::string CronetEnvironment::user_agent() { | 371 std::string CronetEnvironment::user_agent() { |
| 344 const net::HttpUserAgentSettings* user_agent_settings = | 372 const net::HttpUserAgentSettings* user_agent_settings = |
| 345 main_context_->http_user_agent_settings(); | 373 main_context_->http_user_agent_settings(); |
| 346 if (!user_agent_settings) { | 374 if (!user_agent_settings) { |
| 347 return nullptr; | 375 return nullptr; |
| 348 } | 376 } |
| 349 | 377 |
| 350 return user_agent_settings->GetUserAgent(); | 378 return user_agent_settings->GetUserAgent(); |
| 351 } | 379 } |
| 352 | 380 |
| 381 std::vector<uint8_t> CronetEnvironment::GetHistogramDeltas() { |
| 382 base::StatisticsRecorder::Initialize(); |
| 383 std::vector<uint8_t> data; |
| 384 if (!HistogramManager::GetInstance()->GetDeltas(&data)) |
| 385 return std::vector<uint8_t>(); |
| 386 return data; |
| 387 } |
| 388 |
| 353 } // namespace cronet | 389 } // namespace cronet |
| OLD | NEW |