windows_system_proxy_resolver_mojo.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2021 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. #include "services/network/windows_system_proxy_resolver_mojo.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/sequence_checker.h"
  10. #include "net/proxy_resolution/proxy_list.h"
  11. #include "net/proxy_resolution/win/windows_system_proxy_resolution_request.h"
  12. #include "net/proxy_resolution/win/winhttp_status.h"
  13. namespace network {
  14. class WindowsSystemProxyResolverMojo::RequestImpl final
  15. : public net::WindowsSystemProxyResolver::Request {
  16. public:
  17. RequestImpl(WindowsSystemProxyResolverMojo* resolver,
  18. const GURL& url,
  19. net::WindowsSystemProxyResolutionRequest* callback_target);
  20. RequestImpl(const RequestImpl&) = delete;
  21. RequestImpl& operator=(const RequestImpl&) = delete;
  22. ~RequestImpl() override;
  23. private:
  24. // Implements the callback for GetProxyForUrl()
  25. void ReportResult(const net::ProxyList& proxy_list,
  26. net::WinHttpStatus winhttp_status,
  27. int windows_error);
  28. // As described at WindowsSystemProxyResolutionRequest::GetProxyForUrl,
  29. // `callback_target_` must outlive `this`.
  30. raw_ptr<net::WindowsSystemProxyResolutionRequest> callback_target_;
  31. SEQUENCE_CHECKER(sequence_checker_);
  32. base::WeakPtrFactory<WindowsSystemProxyResolverMojo::RequestImpl>
  33. weak_ptr_factory_{this};
  34. };
  35. WindowsSystemProxyResolverMojo::RequestImpl::RequestImpl(
  36. WindowsSystemProxyResolverMojo* resolver,
  37. const GURL& url,
  38. net::WindowsSystemProxyResolutionRequest* callback_target)
  39. : callback_target_(callback_target) {
  40. DCHECK(callback_target_);
  41. resolver->mojo_windows_system_proxy_resolver_->GetProxyForUrl(
  42. url,
  43. base::BindOnce(&WindowsSystemProxyResolverMojo::RequestImpl::ReportResult,
  44. weak_ptr_factory_.GetWeakPtr()));
  45. }
  46. WindowsSystemProxyResolverMojo::RequestImpl::~RequestImpl() {
  47. // This does not need to check if there is an ongoing proxy resolution.
  48. // Destroying the RequestImpl is the intended way of "canceling" a proxy
  49. // resolution.
  50. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  51. }
  52. void WindowsSystemProxyResolverMojo::RequestImpl::ReportResult(
  53. const net::ProxyList& proxy_list,
  54. net::WinHttpStatus winhttp_status,
  55. int windows_error) {
  56. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  57. callback_target_->ProxyResolutionComplete(proxy_list, winhttp_status,
  58. windows_error);
  59. }
  60. WindowsSystemProxyResolverMojo::WindowsSystemProxyResolverMojo(
  61. mojo::PendingRemote<proxy_resolver_win::mojom::WindowsSystemProxyResolver>
  62. mojo_windows_system_proxy_resolver)
  63. : mojo_windows_system_proxy_resolver_(
  64. std::move(mojo_windows_system_proxy_resolver)) {}
  65. WindowsSystemProxyResolverMojo::~WindowsSystemProxyResolverMojo() = default;
  66. std::unique_ptr<net::WindowsSystemProxyResolver::Request>
  67. WindowsSystemProxyResolverMojo::GetProxyForUrl(
  68. const GURL& url,
  69. net::WindowsSystemProxyResolutionRequest* callback_target) {
  70. return std::make_unique<RequestImpl>(this, url, callback_target);
  71. }
  72. } // namespace network