io_thread.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (c) 2017 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/io_thread.h"
  5. #include "atom/common/options_switches.h"
  6. #include "components/net_log/chrome_net_log.h"
  7. #include "content/public/browser/browser_thread.h"
  8. #include "content/public/browser/network_service_instance.h"
  9. #include "net/proxy_resolution/proxy_resolution_service.h"
  10. #include "net/url_request/url_request_context.h"
  11. #include "net/url_request/url_request_context_builder.h"
  12. #include "net/url_request/url_request_context_getter.h"
  13. #include "services/network/network_service.h"
  14. #if defined(USE_NSS_CERTS)
  15. #include "net/cert_net/nss_ocsp.h"
  16. #endif
  17. #if defined(OS_LINUX) || defined(OS_MACOSX)
  18. #include "net/cert/cert_net_fetcher.h"
  19. #include "net/cert_net/cert_net_fetcher_impl.h"
  20. #endif
  21. using content::BrowserThread;
  22. namespace atom {
  23. namespace {
  24. network::mojom::HttpAuthStaticParamsPtr CreateHttpAuthStaticParams() {
  25. network::mojom::HttpAuthStaticParamsPtr auth_static_params =
  26. network::mojom::HttpAuthStaticParams::New();
  27. auth_static_params->supported_schemes = {"basic", "digest", "ntlm",
  28. "negotiate"};
  29. return auth_static_params;
  30. }
  31. network::mojom::HttpAuthDynamicParamsPtr CreateHttpAuthDynamicParams(
  32. const base::CommandLine& command_line) {
  33. network::mojom::HttpAuthDynamicParamsPtr auth_dynamic_params =
  34. network::mojom::HttpAuthDynamicParams::New();
  35. auth_dynamic_params->server_whitelist =
  36. command_line.GetSwitchValueASCII(switches::kAuthServerWhitelist);
  37. auth_dynamic_params->delegate_whitelist = command_line.GetSwitchValueASCII(
  38. switches::kAuthNegotiateDelegateWhitelist);
  39. return auth_dynamic_params;
  40. }
  41. } // namespace
  42. IOThread::IOThread(net_log::ChromeNetLog* net_log) : net_log_(net_log) {
  43. BrowserThread::SetIOThreadDelegate(this);
  44. }
  45. IOThread::~IOThread() {
  46. BrowserThread::SetIOThreadDelegate(nullptr);
  47. }
  48. void IOThread::Init() {
  49. // Create the network service, so that shared host resolver
  50. // gets created which is required to set the auth preferences below.
  51. auto& command_line = *base::CommandLine::ForCurrentProcess();
  52. auto* network_service = content::GetNetworkServiceImpl();
  53. network_service->SetUpHttpAuth(CreateHttpAuthStaticParams());
  54. network_service->ConfigureHttpAuthPrefs(
  55. CreateHttpAuthDynamicParams(command_line));
  56. net::URLRequestContextBuilder builder;
  57. // TODO(deepak1556): We need to respoect user proxy configurations,
  58. // the following initialization has to happen before any request
  59. // contexts are utilized by the io thread, so that proper cert validation
  60. // take place, solutions:
  61. // 1) Use the request context from default partition, but since
  62. // an app can completely run on a custom session without ever creating
  63. // the default session, we will have to force create the default session
  64. // in those scenarios.
  65. // 2) Add a new api on app module that sets the proxy configuration
  66. // for the global requests, like the cert fetchers below and
  67. // geolocation requests.
  68. // 3) There is also ongoing work in upstream which will eventually allow
  69. // localizing these global fetchers to their own URLRequestContexts.
  70. builder.set_proxy_resolution_service(
  71. net::ProxyResolutionService::CreateDirect());
  72. url_request_context_ = builder.Build();
  73. url_request_context_getter_ = new net::TrivialURLRequestContextGetter(
  74. url_request_context_.get(), base::ThreadTaskRunnerHandle::Get());
  75. #if defined(USE_NSS_CERTS)
  76. net::SetURLRequestContextForNSSHttpIO(url_request_context_.get());
  77. #endif
  78. #if defined(OS_LINUX) || defined(OS_MACOSX)
  79. net::SetGlobalCertNetFetcher(
  80. net::CreateCertNetFetcher(url_request_context_.get()));
  81. #endif
  82. }
  83. void IOThread::CleanUp() {
  84. #if defined(USE_NSS_CERTS)
  85. net::SetURLRequestContextForNSSHttpIO(nullptr);
  86. #endif
  87. #if defined(OS_LINUX) || defined(OS_MACOSX)
  88. net::ShutdownGlobalCertNetFetcher();
  89. #endif
  90. // Explicitly release before the IO thread gets destroyed.
  91. url_request_context_.reset();
  92. url_request_context_getter_ = nullptr;
  93. if (net_log_)
  94. net_log_->ShutDownBeforeTaskScheduler();
  95. }
  96. } // namespace atom