pac_file_fetcher_impl.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_PAC_FILE_FETCHER_IMPL_H_
  5. #define NET_PROXY_RESOLUTION_PAC_FILE_FETCHER_IMPL_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include <string>
  9. #include "base/compiler_specific.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/time/time.h"
  14. #include "net/base/completion_once_callback.h"
  15. #include "net/base/net_export.h"
  16. #include "net/proxy_resolution/pac_file_fetcher.h"
  17. #include "net/traffic_annotation/network_traffic_annotation.h"
  18. #include "net/url_request/url_request.h"
  19. class GURL;
  20. namespace net {
  21. class URLRequestContext;
  22. // Implementation of PacFileFetcher that downloads scripts using the
  23. // specified request context.
  24. class NET_EXPORT PacFileFetcherImpl : public PacFileFetcher,
  25. public URLRequest::Delegate {
  26. public:
  27. // Creates a PacFileFetcher that issues requests through
  28. // |url_request_context|. |url_request_context| must remain valid for the
  29. // lifetime of PacFileFetcherImpl.
  30. // Note that while a request is in progress, we will be holding a reference
  31. // to |url_request_context|. Be careful not to create cycles between the
  32. // fetcher and the context; you can break such cycles by calling Cancel().
  33. //
  34. // Fetch() supports the following URL schemes, provided the underlying
  35. // |url_request_context| also supports them:
  36. //
  37. // * http://
  38. // * https://
  39. // * ftp://
  40. // * data:
  41. static std::unique_ptr<PacFileFetcherImpl> Create(
  42. URLRequestContext* url_request_context);
  43. PacFileFetcherImpl(const PacFileFetcherImpl&) = delete;
  44. PacFileFetcherImpl& operator=(const PacFileFetcherImpl&) = delete;
  45. ~PacFileFetcherImpl() override;
  46. // Used by unit-tests to modify the default limits.
  47. base::TimeDelta SetTimeoutConstraint(base::TimeDelta timeout);
  48. size_t SetSizeConstraint(size_t size_bytes);
  49. void OnResponseCompleted(URLRequest* request, int net_error);
  50. // PacFileFetcher methods:
  51. int Fetch(const GURL& url,
  52. std::u16string* text,
  53. CompletionOnceCallback callback,
  54. const NetworkTrafficAnnotationTag traffic_annotation) override;
  55. void Cancel() override;
  56. URLRequestContext* GetRequestContext() const override;
  57. void OnShutdown() override;
  58. // URLRequest::Delegate methods:
  59. void OnReceivedRedirect(URLRequest* request,
  60. const RedirectInfo& redirect_info,
  61. bool* defer_redirect) override;
  62. void OnAuthRequired(URLRequest* request,
  63. const AuthChallengeInfo& auth_info) override;
  64. void OnSSLCertificateError(URLRequest* request,
  65. int net_error,
  66. const SSLInfo& ssl_info,
  67. bool is_hsts_ok) override;
  68. void OnResponseStarted(URLRequest* request, int net_error) override;
  69. void OnReadCompleted(URLRequest* request, int num_bytes) override;
  70. private:
  71. enum { kBufSize = 4096 };
  72. explicit PacFileFetcherImpl(URLRequestContext* url_request_context);
  73. // Returns true if |url| has an acceptable URL scheme (i.e. http://, https://,
  74. // etc).
  75. bool IsUrlSchemeAllowed(const GURL& url) const;
  76. // Read more bytes from the response.
  77. void ReadBody(URLRequest* request);
  78. // Handles a response from Read(). Returns true if we should continue trying
  79. // to read. |num_bytes| is 0 for EOF, and < 0 on errors.
  80. bool ConsumeBytesRead(URLRequest* request, int num_bytes);
  81. // Called once the request has completed to notify the caller of
  82. // |response_code_| and |response_text_|.
  83. void FetchCompleted();
  84. // Clear out the state for the current request.
  85. void ResetCurRequestState();
  86. // Callback for time-out task of request with id |id|.
  87. void OnTimeout(int id);
  88. // The context used for making network requests. Set to nullptr by
  89. // OnShutdown.
  90. raw_ptr<URLRequestContext> url_request_context_;
  91. // Buffer that URLRequest writes into.
  92. scoped_refptr<IOBuffer> buf_;
  93. // The next ID to use for |cur_request_| (monotonically increasing).
  94. int next_id_ = 0;
  95. // The current (in progress) request, or NULL.
  96. std::unique_ptr<URLRequest> cur_request_;
  97. // State for current request (only valid when |cur_request_| is not NULL):
  98. // Unique ID for the current request.
  99. int cur_request_id_ = 0;
  100. // Callback to invoke on completion of the fetch.
  101. CompletionOnceCallback callback_;
  102. // Holds the error condition that was hit on the current request, or OK.
  103. int result_code_ = OK;
  104. // Holds the bytes read so far. Will not exceed |max_response_bytes|.
  105. std::string bytes_read_so_far_;
  106. // This buffer is owned by the owner of |callback|, and will be filled with
  107. // UTF16 response on completion.
  108. raw_ptr<std::u16string> result_text_ = nullptr;
  109. // The maximum number of bytes to allow in responses.
  110. size_t max_response_bytes_;
  111. // The maximum amount of time to wait for download to complete.
  112. base::TimeDelta max_duration_;
  113. // The time that the fetch started.
  114. base::TimeTicks fetch_start_time_;
  115. // The time that the first byte was received.
  116. base::TimeTicks fetch_time_to_first_byte_;
  117. // Factory for creating the time-out task. This takes care of revoking
  118. // outstanding tasks when |this| is deleted.
  119. base::WeakPtrFactory<PacFileFetcherImpl> weak_factory_{this};
  120. };
  121. } // namespace net
  122. #endif // NET_PROXY_RESOLUTION_PAC_FILE_FETCHER_IMPL_H_