windows_system_proxy_resolver_impl.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2020 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. #ifndef SERVICES_PROXY_RESOLVER_WIN_WINDOWS_SYSTEM_PROXY_RESOLVER_IMPL_H_
  5. #define SERVICES_PROXY_RESOLVER_WIN_WINDOWS_SYSTEM_PROXY_RESOLVER_IMPL_H_
  6. #include <windows.h>
  7. #include <winhttp.h>
  8. #include <memory>
  9. #include <set>
  10. #include <string>
  11. #include "base/component_export.h"
  12. #include "base/containers/unique_ptr_adapters.h"
  13. #include "base/sequence_checker.h"
  14. #include "base/win/windows_types.h"
  15. #include "mojo/public/cpp/bindings/pending_receiver.h"
  16. #include "mojo/public/cpp/bindings/receiver.h"
  17. #include "net/proxy_resolution/win/winhttp_status.h"
  18. #include "services/proxy_resolver_win/public/mojom/proxy_resolver_win.mojom.h"
  19. class GURL;
  20. namespace proxy_resolver_win {
  21. class WinHttpAPIWrapper;
  22. // This implements a mojo service to resolve proxies on Windows using only
  23. // WinHttp APIs. It enables support for features like the Name Resolution Proxy
  24. // Table (NRPT), DirectAccess, and per-interface proxy configurations. The
  25. // WindowsSystemProxyResolverImpl attempts to create a HINTERNET session handle
  26. // for all proxy resolutions, which should last for the lifetime of the process.
  27. // Once created, incoming proxy resolution requests can be forwarded to WinHttp,
  28. // which is guaranteed to return exactly once per request. The
  29. // WindowsSystemProxyResolverImpl is also in charge of managing the lifetime of
  30. // each of these requests, which will reply via callback once they have
  31. // received a response from WinHttp.
  32. class COMPONENT_EXPORT(PROXY_RESOLVER_WIN) WindowsSystemProxyResolverImpl
  33. : public mojom::WindowsSystemProxyResolver {
  34. public:
  35. explicit WindowsSystemProxyResolverImpl(
  36. mojo::PendingReceiver<mojom::WindowsSystemProxyResolver> receiver);
  37. WindowsSystemProxyResolverImpl(const WindowsSystemProxyResolverImpl&) =
  38. delete;
  39. WindowsSystemProxyResolverImpl& operator=(
  40. const WindowsSystemProxyResolverImpl&) = delete;
  41. ~WindowsSystemProxyResolverImpl() override;
  42. void SetCreateWinHttpAPIWrapperForTesting(
  43. std::unique_ptr<WinHttpAPIWrapper> winhttp_api_wrapper_for_testing);
  44. // mojom::WindowsSystemProxyResolver implementation
  45. void GetProxyForUrl(const GURL& url,
  46. GetProxyForUrlCallback callback) override;
  47. private:
  48. friend class WindowsSystemProxyResolverImplTest;
  49. class Request;
  50. // Sets up the WinHttp session that will be used throughout the lifetime of
  51. // this object.
  52. net::WinHttpStatus EnsureInitialized();
  53. // This is the callback provided to WinHttp. Once a call to resolve a proxy
  54. // succeeds or errors out, it'll call into here with `context` being a pointer
  55. // to a Request that has been kept alive. This callback can hit in any thread
  56. // and will immediately post a task to the right sequence.
  57. static void CALLBACK WinHttpStatusCallback(HINTERNET resolver_handle,
  58. DWORD_PTR context,
  59. DWORD status,
  60. void* info,
  61. DWORD info_len);
  62. // Set to true once a WinHttp session has been successfully created and
  63. // configured.
  64. bool initialized_ = false;
  65. // This is a thin wrapper over WinHttp APIs.
  66. std::unique_ptr<WinHttpAPIWrapper> winhttp_api_wrapper_;
  67. // Tests may set their own wrapper that will get filled into
  68. // `winhttp_api_wrapper_` when needed instead of creating a new
  69. // WinHttpAPIWrapper.
  70. std::unique_ptr<WinHttpAPIWrapper> winhttp_api_wrapper_for_testing_;
  71. // This is the set of in-flight WinHttp proxy resolution calls. Each Request
  72. // will be kept alive for the duration of that call. After that, the Request
  73. // will attempt to respond to the callback and then get deleted.
  74. std::set<std::unique_ptr<Request>, base::UniquePtrComparator> requests_;
  75. mojo::Receiver<mojom::WindowsSystemProxyResolver> receiver_;
  76. SEQUENCE_CHECKER(sequence_checker_);
  77. };
  78. } // namespace proxy_resolver_win
  79. #endif // SERVICES_PROXY_RESOLVER_WIN_WINDOWS_SYSTEM_PROXY_RESOLVER_IMPL_H_