dhcp_pac_file_fetcher.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (c) 2012 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 NET_PROXY_RESOLUTION_DHCP_PAC_FILE_FETCHER_H_
  5. #define NET_PROXY_RESOLUTION_DHCP_PAC_FILE_FETCHER_H_
  6. #include <string>
  7. #include "base/compiler_specific.h"
  8. #include "net/base/completion_once_callback.h"
  9. #include "net/base/net_export.h"
  10. #include "net/proxy_resolution/pac_file_fetcher.h"
  11. #include "net/traffic_annotation/network_traffic_annotation.h"
  12. #include "url/gurl.h"
  13. namespace net {
  14. class NetLogWithSource;
  15. // Interface for classes that can fetch a PAC file as configured via DHCP.
  16. //
  17. // The Fetch method on this interface tries to retrieve the most appropriate
  18. // PAC script configured via DHCP.
  19. //
  20. // Normally there are zero or one DHCP scripts configured, but in the
  21. // presence of multiple adapters with DHCP enabled, the fetcher resolves
  22. // which PAC script to use if one or more are available.
  23. class NET_EXPORT_PRIVATE DhcpPacFileFetcher {
  24. public:
  25. DhcpPacFileFetcher(const DhcpPacFileFetcher&) = delete;
  26. DhcpPacFileFetcher& operator=(const DhcpPacFileFetcher&) = delete;
  27. // Destruction should cancel any outstanding requests.
  28. virtual ~DhcpPacFileFetcher();
  29. // Attempts to retrieve the most appropriate PAC script configured via DHCP,
  30. // and invokes |callback| on completion.
  31. //
  32. // Returns OK on success, otherwise the error code. If the return code is
  33. // ERR_IO_PENDING, then the request completes asynchronously, and |callback|
  34. // will be invoked later with the final error code.
  35. //
  36. // After synchronous or asynchronous completion with a result code of OK,
  37. // |*utf16_text| is filled with the response. On failure, the result text is
  38. // an empty string, and the result code is a network error. Some special
  39. // network errors that may occur are:
  40. //
  41. // ERR_PAC_NOT_IN_DHCP -- no script configured in DHCP.
  42. //
  43. // The following all indicate there was one or more script configured
  44. // in DHCP but all failed to download, and the error for the most
  45. // preferred adapter that had a script configured was what the error
  46. // code says:
  47. //
  48. // ERR_TIMED_OUT -- fetch took too long to complete.
  49. // ERR_FILE_TOO_BIG -- response body was too large.
  50. // ERR_HTTP_RESPONSE_CODE_FAILURE -- script downloaded but returned a
  51. // non-200 HTTP response.
  52. // ERR_NOT_IMPLEMENTED -- script required authentication.
  53. //
  54. // If the request is cancelled (either using the "Cancel()" method or by
  55. // deleting |this|), then no callback is invoked.
  56. //
  57. // Only one fetch is allowed to be outstanding at a time.
  58. virtual int Fetch(std::u16string* utf16_text,
  59. CompletionOnceCallback callback,
  60. const NetLogWithSource& net_log,
  61. const NetworkTrafficAnnotationTag traffic_annotation) = 0;
  62. // Aborts the in-progress fetch (if any).
  63. virtual void Cancel() = 0;
  64. // Cancels the in-progress fetch (if any), without invoking its callback.
  65. // Future requests will fail immediately. Must be called before the
  66. // URLRequestContext the fetcher was created with is torn down.
  67. virtual void OnShutdown() = 0;
  68. // After successful completion of |Fetch()|, this will return the URL
  69. // retrieved from DHCP. It is reset if/when |Fetch()| is called again.
  70. virtual const GURL& GetPacURL() const = 0;
  71. // Intended for unit tests only, so they can test that factories return
  72. // the right types under given circumstances.
  73. virtual std::string GetFetcherName() const;
  74. protected:
  75. DhcpPacFileFetcher();
  76. };
  77. // A do-nothing retriever, always returns synchronously with
  78. // ERR_NOT_IMPLEMENTED result and empty text.
  79. class NET_EXPORT_PRIVATE DoNothingDhcpPacFileFetcher
  80. : public DhcpPacFileFetcher {
  81. public:
  82. DoNothingDhcpPacFileFetcher();
  83. DoNothingDhcpPacFileFetcher(const DoNothingDhcpPacFileFetcher&) = delete;
  84. DoNothingDhcpPacFileFetcher& operator=(const DoNothingDhcpPacFileFetcher&) =
  85. delete;
  86. ~DoNothingDhcpPacFileFetcher() override;
  87. int Fetch(std::u16string* utf16_text,
  88. CompletionOnceCallback callback,
  89. const NetLogWithSource& net_log,
  90. const NetworkTrafficAnnotationTag traffic_annotation) override;
  91. void Cancel() override;
  92. void OnShutdown() override;
  93. const GURL& GetPacURL() const override;
  94. std::string GetFetcherName() const override;
  95. private:
  96. GURL gurl_;
  97. };
  98. } // namespace net
  99. #endif // NET_PROXY_RESOLUTION_DHCP_PAC_FILE_FETCHER_H_